decompiled_code:
__int64 screen_init()
{
__int64 v0; // rdi
int v1; // esi
__int64 v2; // rsi
__int64 v3; // rdi
__int64 v4; // rax
unsigned int v5; // edx
const char *v7; // rax

TTF_Init();
message_font = TTF_OpenFont("VeraMono.ttf", 11LL);
if ( screen_extended_mode )
{
v0 = (unsigned int)(scale_factor << 7);
screen_width = scale_factor << 7;
v1 = 64;
}
else
{
v0 = (unsigned int)(scale_factor << 6);
screen_width = scale_factor << 6;
v1 = 32;
}
v2 = (unsigned int)(scale_factor * v1);
screen_height = v2;
v3 = SDL_SetVideoMode(v0, v2, 32LL, 0LL);
screen = v3;
if ( !v3 )
{
v7 = (const char *)SDL_GetError();
printf("Error: Unable to set video mode: %s\n", v7);
v5 = 0;
if ( virtscreen )
return v5;
return 0;
}
SDL_SetAlpha(v3, 0x10000LL, 255LL);
SDL_WM_SetCaption("YAC8 Emulator", 0LL);
init_colors(screen);
virtscreen = screen_create_surface((unsigned int)screen_width, (unsigned int)screen_height, 0xFFu, 0xFFFFFFFF);
v4 = screen_create_surface((unsigned int)screen_width, (unsigned int)screen_height, 0xC8u, COLOR_BLACK);
overlay = v4;
if ( !virtscreen )
return 0;
v5 = 1;
if ( !v4 )
return 0;
return v5;
}

canonical_decompiled_code:
__int64 func10()
{
__int64 v0; // rdi
int v1; // esi
__int64 v2; // rsi
__int64 v3; // rdi
__int64 v4; // rax
unsigned int v5; // edx
const char *v7; // rax

TTF_Init();
message_font = TTF_OpenFont("VeraMono.ttf", 11LL);
if ( screen_extended_mode )
{
v0 = (unsigned int)(scale_factor << 7);
screen_width = scale_factor << 7;
v1 = 64;
}
else
{
v0 = (unsigned int)(scale_factor << 6);
screen_width = scale_factor << 6;
v1 = 32;
}
v2 = (unsigned int)(scale_factor * v1);
screen_height = v2;
v3 = SDL_SetVideoMode(v0, v2, 32LL, 0LL);
screen = v3;
if ( !v3 )
{
v7 = (const char *)SDL_GetError();
printf("Error: Unable to set video mode: %s\n", v7);
v5 = 0;
if ( virtscreen )
return v5;
return 0;
}
SDL_SetAlpha(v3, 0x10000LL, 255LL);
SDL_WM_SetCaption("YAC8 Emulator", 0LL);
func0(screen);
virtscreen = func9((unsigned int)screen_width, (unsigned int)screen_height, 0xFFu, 0xFFFFFFFF);
v4 = func9((unsigned int)screen_width, (unsigned int)screen_height, 0xC8u, COLOR_BLACK);
overlay = v4;
if ( !virtscreen )
return 0;
v5 = 1;
if ( !v4 )
return 0;
return v5;
}

original_code:
int
screen_init(void)
{
    int result = 0;
    TTF_Init();
    message_font = TTF_OpenFont("VeraMono.ttf", 11);
    screen_width = (screen_extended_mode ? 128 : 64) * scale_factor;
    screen_height = (screen_extended_mode ? 64 : 32) * scale_factor;
    screen = SDL_SetVideoMode(screen_width,
                              screen_height,
                              32,
                              0x00000000);
    if (screen == ((void *)0)) {
        printf("Error: Unable to set video mode: %s\n", SDL_GetError());
    }
    else {
        SDL_SetAlpha(screen, 0x00010000, 255);
        SDL_WM_SetCaption("YAC8 Emulator", ((void *)0));
        init_colors(screen);
        virtscreen = screen_create_surface(screen_width, screen_height,
                                            255, -1);
        overlay = screen_create_surface(screen_width, screen_height,
                                         200, COLOR_BLACK);
        result = 1;
    }
    return ((virtscreen != ((void *)0)) && (overlay != ((void *)0)) && result);
}

canonical_original_code:
int screen_init()
{
    int result = 0;
    TTF_Init();
    message_font = TTF_OpenFont("VeraMono.ttf", 11);
    screen_width = (screen_extended_mode ? 128 : 64) * scale_factor;
    screen_height = (screen_extended_mode ? 64 : 32) * scale_factor;
    screen = SDL_SetVideoMode(screen_width,
                              screen_height,
                              32,
                              0x00000000);
    if (screen == ((void *)0)) {
        printf("Error: Unable to set video mode: %s\n", SDL_GetError());
    }
    else {
        SDL_SetAlpha(screen, 0x00010000, 255);
        SDL_WM_SetCaption("YAC8 Emulator", ((void *)0));
        init_colors(screen);
        virtscreen = screen_create_surface(screen_width, screen_height,
                                            255, -1);
        overlay = screen_create_surface(screen_width, screen_height,
                                         200, COLOR_BLACK);
        result = 1;
    }
    return ((virtscreen != ((void *)0)) && (overlay != ((void *)0)) && result);
}

variable_types: result int


decompiled_code:
__int64 screen_destroy()
{
SDL_FreeSurface(virtscreen);
SDL_FreeSurface(overlay);
return SDL_FreeSurface(screen);
}

canonical_decompiled_code:
__int64 func11()
{
SDL_FreeSurface(virtscreen);
SDL_FreeSurface(overlay);
return SDL_FreeSurface(screen);
}

original_code:
void
screen_destroy(void)
{
    SDL_FreeSurface(virtscreen);
    SDL_FreeSurface(overlay);
    SDL_FreeSurface(screen);
}

canonical_original_code:
void screen_destroy()
{
    SDL_FreeSurface(virtscreen);
    SDL_FreeSurface(overlay);
    SDL_FreeSurface(screen);
}

return_type: void


decompiled_code:
__int64 screen_set_extended()
{
screen_destroy();
screen_extended_mode = 1;
return screen_init();
}

canonical_decompiled_code:
__int64 func12()
{
func11();
screen_extended_mode = 1;
return func10();
}

original_code:
void
screen_set_extended(void)
{
    screen_destroy();
    screen_extended_mode = 1;
    screen_init();
}

canonical_original_code:
void screen_set_extended()
{
    screen_destroy();
    screen_extended_mode = 1;
    screen_init();
}


decompiled_code:
__int64 screen_disable_extended()
{
screen_destroy();
screen_extended_mode = 0;
return screen_init();
}

canonical_decompiled_code:
__int64 func13()
{
func11();
screen_extended_mode = 0;
return func10();
}

original_code:
void
screen_disable_extended(void)
{
    screen_destroy();
    screen_extended_mode = 0;
    screen_init();
}

canonical_original_code:
void screen_disable_extended()
{
    screen_destroy();
    screen_extended_mode = 0;
    screen_init();
}


decompiled_code:
__int64 screen_get_height()
{
return screen_extended_mode == 0 ? 32 : 64;
}

canonical_decompiled_code:
__int64 func14()
{
return screen_extended_mode == 0 ? 32 : 64;
}

original_code:
int
screen_get_height(void)
{
    return screen_extended_mode ? 64 : 32;
}

canonical_original_code:
int screen_get_height()
{
    return screen_extended_mode ? 64 : 32;
}

return_type: int


decompiled_code:
__int64 screen_get_width()
{
return screen_extended_mode == 0 ? 64 : 128;
}

canonical_decompiled_code:
__int64 func15()
{
return screen_extended_mode == 0 ? 64 : 128;
}

original_code:
int
screen_get_width(void)
{
    return screen_extended_mode ? 128 : 64;
}

canonical_original_code:
int screen_get_width()
{
    return screen_extended_mode ? 128 : 64;
}


decompiled_code:
__int64 __fastcall keyboard_keycodetosymbol(int a1)
{
__int64 v1; // rax

v1 = 0LL;
while ( keyboard_def[2 * v1] != a1 )
{
if ( ++v1 == 15 )
return 279LL;
}
return (unsigned int)keyboard_def[2 * (int)v1 + 1];
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1)
{
__int64 v1; // rax

v1 = 0LL;
while ( keyboard_def[2 * v1] != a1 )
{
if ( ++v1 == 15 )
return 279LL;
}
return (unsigned int)keyboard_def[2 * (int)v1 + 1];
}

canonical_original_code:
enum SDLKey keyboard_keycodetosymbol(int keycode)
{
    int i;
    for (i = 0; i < 15; i++) {
        if (keyboard_def[i].keycode == keycode) {
            return keyboard_def[i].symbol;
        }
    }
    return SDLK_END;
}

variable_types: keycode int

variable_types: i int

return_type: enum SDLKey { SDLK_UNKNOWN=0, SDLK_FIRST=0, SDLK_BACKSPACE=8, SDLK_TAB=9, SDLK_CLEAR=12, SDLK_RETURN=13, SDLK_PAUSE=19, SDLK_ESCAPE=27, SDLK_SPACE=32, SDLK_EXCLAIM=33, SDLK_QUOTEDBL=34, SDLK_HASH=35, SDLK_DOLLAR=36, SDLK_AMPERSAND=38, SDLK_QUOTE=39, SDLK_LEFTPAREN=40, SDLK_RIGHTPAREN=41, SDLK_ASTERISK=42, SDLK_PLUS=43, SDLK_COMMA=44, SDLK_MINUS=45, SDLK_PERIOD=46, SDLK_SLASH=47, SDLK_0=48, SDLK_1=49, SDLK_2=50, SDLK_3=51, SDLK_4=52, SDLK_5=53, SDLK_6=54, SDLK_7=55, SDLK_8=56, SDLK_9=57, SDLK_COLON=58, SDLK_SEMICOLON=59, SDLK_LESS=60, SDLK_EQUALS=61, SDLK_GREATER=62, SDLK_QUESTION=63, SDLK_AT=64, SDLK_LEFTBRACKET=91, SDLK_BACKSLASH=92, SDLK_RIGHTBRACKET=93, SDLK_CARET=94, SDLK_UNDERSCORE=95, SDLK_BACKQUOTE=96, SDLK_a=97, SDLK_b=98, SDLK_c=99, SDLK_d=100, SDLK_e=101, SDLK_f=102, SDLK_g=103, SDLK_h=104, SDLK_i=105, SDLK_j=106, SDLK_k=107, SDLK_l=108, SDLK_m=109, SDLK_n=110, SDLK_o=111, SDLK_p=112, SDLK_q=113, SDLK_r=114, SDLK_s=115, SDLK_t=116, SDLK_u=117, SDLK_v=118, SDLK_w=119, SDLK_x=120, SDLK_y=121, SDLK_z=122, SDLK_DELETE=127, SDLK_WORLD_0=160, SDLK_WORLD_1=161, SDLK_WORLD_2=162, SDLK_WORLD_3=163, SDLK_WORLD_4=164, SDLK_WORLD_5=165, SDLK_WORLD_6=166, SDLK_WORLD_7=167, SDLK_WORLD_8=168, SDLK_WORLD_9=169, SDLK_WORLD_10=170, SDLK_WORLD_11=171, SDLK_WORLD_12=172, SDLK_WORLD_13=173, SDLK_WORLD_14=174, SDLK_WORLD_15=175, SDLK_WORLD_16=176, SDLK_WORLD_17=177, SDLK_WORLD_18=178, SDLK_WORLD_19=179, SDLK_WORLD_20=180, SDLK_WORLD_21=181, SDLK_WORLD_22=182, SDLK_WORLD_23=183, SDLK_WORLD_24=184, SDLK_WORLD_25=185, SDLK_WORLD_26=186, SDLK_WORLD_27=187, SDLK_WORLD_28=188, SDLK_WORLD_29=189, SDLK_WORLD_30=190, SDLK_WORLD_31=191, SDLK_WORLD_32=192, SDLK_WORLD_33=193, SDLK_WORLD_34=194, SDLK_WORLD_35=195, SDLK_WORLD_36=196, SDLK_WORLD_37=197, SDLK_WORLD_38=198, SDLK_WORLD_39=199, SDLK_WORLD_40=200, SDLK_WORLD_41=201, SDLK_WORLD_42=202, SDLK_WORLD_43=203, SDLK_WORLD_44=204, SDLK_WORLD_45=205, SDLK_WORLD_46=206, SDLK_WORLD_47=207, SDLK_WORLD_48=208, SDLK_WORLD_49=209, SDLK_WORLD_50=210, SDLK_WORLD_51=211, SDLK_WORLD_52=212, SDLK_WORLD_53=213, SDLK_WORLD_54=214, SDLK_WORLD_55=215, SDLK_WORLD_56=216, SDLK_WORLD_57=217, SDLK_WORLD_58=218, SDLK_WORLD_59=219, SDLK_WORLD_60=220, SDLK_WORLD_61=221, SDLK_WORLD_62=222, SDLK_WORLD_63=223, SDLK_WORLD_64=224, SDLK_WORLD_65=225, SDLK_WORLD_66=226, SDLK_WORLD_67=227, SDLK_WORLD_68=228, SDLK_WORLD_69=229, SDLK_WORLD_70=230, SDLK_WORLD_71=231, SDLK_WORLD_72=232, SDLK_WORLD_73=233, SDLK_WORLD_74=234, SDLK_WORLD_75=235, SDLK_WORLD_76=236, SDLK_WORLD_77=237, SDLK_WORLD_78=238, SDLK_WORLD_79=239, SDLK_WORLD_80=240, SDLK_WORLD_81=241, SDLK_WORLD_82=242, SDLK_WORLD_83=243, SDLK_WORLD_84=244, SDLK_WORLD_85=245, SDLK_WORLD_86=246, SDLK_WORLD_87=247, SDLK_WORLD_88=248, SDLK_WORLD_89=249, SDLK_WORLD_90=250, SDLK_WORLD_91=251, SDLK_WORLD_92=252, SDLK_WORLD_93=253, SDLK_WORLD_94=254, SDLK_WORLD_95=255, SDLK_KP0=256, SDLK_KP1=257, SDLK_KP2=258, SDLK_KP3=259, SDLK_KP4=260, SDLK_KP5=261, SDLK_KP6=262, SDLK_KP7=263, SDLK_KP8=264, SDLK_KP9=265, SDLK_KP_PERIOD=266, SDLK_KP_DIVIDE=267, SDLK_KP_MULTIPLY=268, SDLK_KP_MINUS=269, SDLK_KP_PLUS=270, SDLK_KP_ENTER=271, SDLK_KP_EQUALS=272, SDLK_UP=273, SDLK_DOWN=274, SDLK_RIGHT=275, SDLK_LEFT=276, SDLK_INSERT=277, SDLK_HOME=278, SDLK_END=279, SDLK_PAGEUP=280, SDLK_PAGEDOWN=281, SDLK_F1=282, SDLK_F2=283, SDLK_F3=284, SDLK_F4=285, SDLK_F5=286, SDLK_F6=287, SDLK_F7=288, SDLK_F8=289, SDLK_F9=290, SDLK_F10=291, SDLK_F11=292, SDLK_F12=293, SDLK_F13=294, SDLK_F14=295, SDLK_F15=296, SDLK_NUMLOCK=300, SDLK_CAPSLOCK=301, SDLK_SCROLLOCK=302, SDLK_RSHIFT=303, SDLK_LSHIFT=304, SDLK_RCTRL=305, SDLK_LCTRL=306, SDLK_RALT=307, SDLK_LALT=308, SDLK_RMETA=309, SDLK_LMETA=310, SDLK_LSUPER=311, SDLK_RSUPER=312, SDLK_MODE=313, SDLK_COMPOSE=314, SDLK_HELP=315, SDLK_PRINT=316, SDLK_SYSREQ=317, SDLK_BREAK=318, SDLK_MENU=319, SDLK_POWER=320, SDLK_EURO=321, SDLK_UNDO=322, SDLK_LAST=323 }


decompiled_code:
__int64 __fastcall keyboard_symboltokeycode(int a1)
{
__int64 v1; // rax

v1 = 0LL;
while ( keyboard_def[2 * v1 + 1] != a1 )
{
if ( ++v1 == 15 )
return 4294966297LL;
}
return (unsigned int)keyboard_def[2 * (int)v1];
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1)
{
__int64 v1; // rax

v1 = 0LL;
while ( keyboard_def[2 * v1 + 1] != a1 )
{
if ( ++v1 == 15 )
return 4294966297LL;
}
return (unsigned int)keyboard_def[2 * (int)v1];
}

original_code:
int
keyboard_symboltokeycode(SDLKey symbol)
{
    int i;
    for (i = 0; i < 15; i++) {
        if (keyboard_def[i].symbol == symbol) {
            return keyboard_def[i].keycode;
        }
    }
    return -999;
}

canonical_original_code:
int keyboard_symboltokeycode(enum SDLKey symbol)
{
    int i;
    for (i = 0; i < 15; i++) {
        if (keyboard_def[i].symbol == symbol) {
            return keyboard_def[i].keycode;
        }
    }
    return -999;
}

variable_types: enum SDLKey { SDLK_UNKNOWN=0, SDLK_FIRST=0, SDLK_BACKSPACE=8, SDLK_TAB=9, SDLK_CLEAR=12, SDLK_RETURN=13, SDLK_PAUSE=19, SDLK_ESCAPE=27, SDLK_SPACE=32, SDLK_EXCLAIM=33, SDLK_QUOTEDBL=34, SDLK_HASH=35, SDLK_DOLLAR=36, SDLK_AMPERSAND=38, SDLK_QUOTE=39, SDLK_LEFTPAREN=40, SDLK_RIGHTPAREN=41, SDLK_ASTERISK=42, SDLK_PLUS=43, SDLK_COMMA=44, SDLK_MINUS=45, SDLK_PERIOD=46, SDLK_SLASH=47, SDLK_0=48, SDLK_1=49, SDLK_2=50, SDLK_3=51, SDLK_4=52, SDLK_5=53, SDLK_6=54, SDLK_7=55, SDLK_8=56, SDLK_9=57, SDLK_COLON=58, SDLK_SEMICOLON=59, SDLK_LESS=60, SDLK_EQUALS=61, SDLK_GREATER=62, SDLK_QUESTION=63, SDLK_AT=64, SDLK_LEFTBRACKET=91, SDLK_BACKSLASH=92, SDLK_RIGHTBRACKET=93, SDLK_CARET=94, SDLK_UNDERSCORE=95, SDLK_BACKQUOTE=96, SDLK_a=97, SDLK_b=98, SDLK_c=99, SDLK_d=100, SDLK_e=101, SDLK_f=102, SDLK_g=103, SDLK_h=104, SDLK_i=105, SDLK_j=106, SDLK_k=107, SDLK_l=108, SDLK_m=109, SDLK_n=110, SDLK_o=111, SDLK_p=112, SDLK_q=113, SDLK_r=114, SDLK_s=115, SDLK_t=116, SDLK_u=117, SDLK_v=118, SDLK_w=119, SDLK_x=120, SDLK_y=121, SDLK_z=122, SDLK_DELETE=127, SDLK_WORLD_0=160, SDLK_WORLD_1=161, SDLK_WORLD_2=162, SDLK_WORLD_3=163, SDLK_WORLD_4=164, SDLK_WORLD_5=165, SDLK_WORLD_6=166, SDLK_WORLD_7=167, SDLK_WORLD_8=168, SDLK_WORLD_9=169, SDLK_WORLD_10=170, SDLK_WORLD_11=171, SDLK_WORLD_12=172, SDLK_WORLD_13=173, SDLK_WORLD_14=174, SDLK_WORLD_15=175, SDLK_WORLD_16=176, SDLK_WORLD_17=177, SDLK_WORLD_18=178, SDLK_WORLD_19=179, SDLK_WORLD_20=180, SDLK_WORLD_21=181, SDLK_WORLD_22=182, SDLK_WORLD_23=183, SDLK_WORLD_24=184, SDLK_WORLD_25=185, SDLK_WORLD_26=186, SDLK_WORLD_27=187, SDLK_WORLD_28=188, SDLK_WORLD_29=189, SDLK_WORLD_30=190, SDLK_WORLD_31=191, SDLK_WORLD_32=192, SDLK_WORLD_33=193, SDLK_WORLD_34=194, SDLK_WORLD_35=195, SDLK_WORLD_36=196, SDLK_WORLD_37=197, SDLK_WORLD_38=198, SDLK_WORLD_39=199, SDLK_WORLD_40=200, SDLK_WORLD_41=201, SDLK_WORLD_42=202, SDLK_WORLD_43=203, SDLK_WORLD_44=204, SDLK_WORLD_45=205, SDLK_WORLD_46=206, SDLK_WORLD_47=207, SDLK_WORLD_48=208, SDLK_WORLD_49=209, SDLK_WORLD_50=210, SDLK_WORLD_51=211, SDLK_WORLD_52=212, SDLK_WORLD_53=213, SDLK_WORLD_54=214, SDLK_WORLD_55=215, SDLK_WORLD_56=216, SDLK_WORLD_57=217, SDLK_WORLD_58=218, SDLK_WORLD_59=219, SDLK_WORLD_60=220, SDLK_WORLD_61=221, SDLK_WORLD_62=222, SDLK_WORLD_63=223, SDLK_WORLD_64=224, SDLK_WORLD_65=225, SDLK_WORLD_66=226, SDLK_WORLD_67=227, SDLK_WORLD_68=228, SDLK_WORLD_69=229, SDLK_WORLD_70=230, SDLK_WORLD_71=231, SDLK_WORLD_72=232, SDLK_WORLD_73=233, SDLK_WORLD_74=234, SDLK_WORLD_75=235, SDLK_WORLD_76=236, SDLK_WORLD_77=237, SDLK_WORLD_78=238, SDLK_WORLD_79=239, SDLK_WORLD_80=240, SDLK_WORLD_81=241, SDLK_WORLD_82=242, SDLK_WORLD_83=243, SDLK_WORLD_84=244, SDLK_WORLD_85=245, SDLK_WORLD_86=246, SDLK_WORLD_87=247, SDLK_WORLD_88=248, SDLK_WORLD_89=249, SDLK_WORLD_90=250, SDLK_WORLD_91=251, SDLK_WORLD_92=252, SDLK_WORLD_93=253, SDLK_WORLD_94=254, SDLK_WORLD_95=255, SDLK_KP0=256, SDLK_KP1=257, SDLK_KP2=258, SDLK_KP3=259, SDLK_KP4=260, SDLK_KP5=261, SDLK_KP6=262, SDLK_KP7=263, SDLK_KP8=264, SDLK_KP9=265, SDLK_KP_PERIOD=266, SDLK_KP_DIVIDE=267, SDLK_KP_MULTIPLY=268, SDLK_KP_MINUS=269, SDLK_KP_PLUS=270, SDLK_KP_ENTER=271, SDLK_KP_EQUALS=272, SDLK_UP=273, SDLK_DOWN=274, SDLK_RIGHT=275, SDLK_LEFT=276, SDLK_INSERT=277, SDLK_HOME=278, SDLK_END=279, SDLK_PAGEUP=280, SDLK_PAGEDOWN=281, SDLK_F1=282, SDLK_F2=283, SDLK_F3=284, SDLK_F4=285, SDLK_F5=286, SDLK_F6=287, SDLK_F7=288, SDLK_F8=289, SDLK_F9=290, SDLK_F10=291, SDLK_F11=292, SDLK_F12=293, SDLK_F13=294, SDLK_F14=295, SDLK_F15=296, SDLK_NUMLOCK=300, SDLK_CAPSLOCK=301, SDLK_SCROLLOCK=302, SDLK_RSHIFT=303, SDLK_LSHIFT=304, SDLK_RCTRL=305, SDLK_LCTRL=306, SDLK_RALT=307, SDLK_LALT=308, SDLK_RMETA=309, SDLK_LMETA=310, SDLK_LSUPER=311, SDLK_RSUPER=312, SDLK_MODE=313, SDLK_COMPOSE=314, SDLK_HELP=315, SDLK_PRINT=316, SDLK_SYSREQ=317, SDLK_BREAK=318, SDLK_MENU=319, SDLK_POWER=320, SDLK_EURO=321, SDLK_UNDO=322, SDLK_LAST=323 }


decompiled_code:
__int64 __fastcall keyboard_checkforkeypress(int a1)
{
__int64 v1; // rbx

v1 = SDL_GetKeyState(0LL);
return *(unsigned __int8 *)(v1 + (unsigned int)keyboard_keycodetosymbol(a1));
}

canonical_decompiled_code:
__int64 __fastcall func2(int a1)
{
__int64 v1; // rbx

v1 = SDL_GetKeyState(0LL);
return *(unsigned __int8 *)(v1 + (unsigned int)func0(a1));
}

original_code:
int
keyboard_checkforkeypress(int keycode)
{
    Uint8 *keystates = SDL_GetKeyState(((void *)0));
    return keystates[keyboard_keycodetosymbol(keycode)];
}

canonical_original_code:
int keyboard_checkforkeypress(int keycode)
{
    unsigned char *keystates = SDL_GetKeyState(((void *)0));
    return keystates[keyboard_keycodetosymbol(keycode)];
}

variable_types:
keystates
unsigned char *


decompiled_code:
__int64 keyboard_waitgetkeypress()
{
while ( !(unsigned int)SDL_PollEvent(&event) || event != 2 )
SDL_Delay(20LL);
return keyboard_symboltokeycode(*((_DWORD *)&event + 2));
}

canonical_decompiled_code:
__int64 func3()
{
while ( !(unsigned int)SDL_PollEvent(&event) || event != 2 )
SDL_Delay(20LL);
return func1(*((_DWORD *)&event + 2));
}

original_code:
int
keyboard_waitgetkeypress(void)
{
    while (1) {
        if (SDL_PollEvent(&event)) {
            switch (event.type) {
                case SDL_KEYDOWN:
                    return keyboard_symboltokeycode(event.key.keysym.sym);
                    break;
                default:
                    break;
            }
        }
        SDL_Delay(20);
    }
}

canonical_original_code:
int keyboard_waitgetkeypress()
{
    while (1) {
        if (SDL_PollEvent(&event)) {
            switch (event.type) {
                case SDL_KEYDOWN:
                    return keyboard_symboltokeycode(event.key.keysym.sym);
                    break;
                default:
                    break;
            }
        }
        SDL_Delay(20);
    }
}


decompiled_code:
_BOOL8 __fastcall memory_init(int a1)
{
memory = malloc(a1);
return memory != 0LL;
}

canonical_decompiled_code:
_BOOL8 __fastcall func0(int a1)
{
memory = malloc(a1);
return memory != 0LL;
}

original_code:
int
memory_init(int memorysize)
{
   memory = (byte *)malloc(sizeof (byte) * memorysize);
   return memory != ((void *)0);
}

canonical_original_code:
int memory_init(int memorysize)
{
   memory = (unsigned char *)malloc(sizeof (byte) * memorysize);
   return memory != ((void *)0);
}

variable_types: memorysize int


decompiled_code:
void memory_destroy()
{
free(memory);
}

canonical_decompiled_code:
void func1()
{
free(memory);
}

original_code:
void
memory_destroy(void)
{
   free(memory);
}

canonical_original_code:
void memory_destroy()
{
   free(memory);
}


bflush

repo: crashkopf/libAPL

decompiled_code:
__int64 __fastcall bflush(_DWORD *a1)
{
a1[1] = 0;
*a1 = 0;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func1(_DWORD *a1)
{
a1[1] = 0;
*a1 = 0;
return 0LL;
}

original_code:
int bflush(buffer * b) {
 b->len = 0;
 b->pos = 0;
 return 0;
}

canonical_original_code:
int bflush(struct buffer *b) {
 b->len = 0;
 b->pos = 0;
 return 0;
}

variable_types: struct buffer { unsigned int pos; unsigned int len; unsigned int size; char *data; } *


readb

decompiled_code:
__int64 __fastcall readb(unsigned int *a1, _BYTE *a2, int a3)
{
unsigned int i; // ecx
unsigned int v4; // eax
unsigned int v5; // er8

if ( !a1 || !a2 )
return 0LL;
if ( !a3 )
return 0;
for ( i = 0; i != a3; ++i )
{
if ( !a1[1] )
break;
*a2 = *(_BYTE *)(*((_QWORD *)a1 + 2) + *a1);
v4 = *a1 + 1;
v5 = a1[2];
if ( v4 >= v5 )
v4 = *a1 + 1 - v5;
*a1 = v4;
--a1[1];
++a2;
}
return i;
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned int *a1, _BYTE *a2, int a3)
{
unsigned int i; // ecx
unsigned int v4; // eax
unsigned int v5; // er8

if ( !a1 || !a2 )
return 0LL;
if ( !a3 )
return 0;
for ( i = 0; i != a3; ++i )
{
if ( !a1[1] )
break;
*a2 = *(_BYTE *)(*((_QWORD *)a1 + 2) + *a1);
v4 = *a1 + 1;
v5 = a1[2];
if ( v4 >= v5 )
v4 = *a1 + 1 - v5;
*a1 = v4;
--a1[1];
++a2;
}
return i;
}

original_code:
int readb(buffer * b, char * d, unsigned int m) {
 unsigned int c;
 if (!b || !d)
  return 0;
 c = 0;
 while ((c < m) && (b->len)) {
  d[c] = b->data[b->pos];
  b->pos++;
  if(b->pos >= b->size)
   b->pos -= b->size;
  b->len--;
  c++;
 }
 return c;
}

canonical_original_code:
int readb(struct buffer *b, char *d, unsigned int m) {
 unsigned int c;
 if (!b || !d)
  return 0;
 c = 0;
 while ((c < m) && (b->len)) {
  d[c] = b->data[b->pos];
  b->pos++;
  if(b->pos >= b->size)
   b->pos -= b->size;
  b->len--;
  c++;
 }
 return c;
}

variable_types:
d
char *

variable_types:
m
unsigned int

variable_types:
c
unsigned int

user_defined_types: struct buffer { unsigned int pos; unsigned int len; unsigned int size; char *data; }


writeb

decompiled_code:
__int64 __fastcall writeb(__int64 a1, _BYTE *a2, int a3)
{
__int64 result; // rax
unsigned int v5; // eax
unsigned int v6; // edx
unsigned int v7; // ecx

if ( !a1 )
return 0LL;
if ( !a2 )
return 0LL;
result = 0LL;
if ( *(_DWORD *)(a1 + 4) != *(_DWORD *)(a1 + 8) )
{
v5 = btail((_DWORD *)a1);
if ( a3 )
{
v6 = 0;
do
{
if ( *(_DWORD *)(a1 + 4) >= *(_DWORD *)(a1 + 8) )
break;
*(_BYTE *)(*(_QWORD *)(a1 + 16) + v5++) = *a2;
v7 = *(_DWORD *)(a1 + 8);
if ( v7 <= v5 )
v5 -= v7;
++*(_DWORD *)(a1 + 4);
++v6;
++a2;
}
while ( a3 != v6 );
}
else
{
v6 = 0;
}
result = v6;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, _BYTE *a2, int a3)
{
__int64 result; // rax
unsigned int v5; // eax
unsigned int v6; // edx
unsigned int v7; // ecx

if ( !a1 )
return 0LL;
if ( !a2 )
return 0LL;
result = 0LL;
if ( *(_DWORD *)(a1 + 4) != *(_DWORD *)(a1 + 8) )
{
v5 = func0((_DWORD *)a1);
if ( a3 )
{
v6 = 0;
do
{
if ( *(_DWORD *)(a1 + 4) >= *(_DWORD *)(a1 + 8) )
break;
*(_BYTE *)(*(_QWORD *)(a1 + 16) + v5++) = *a2;
v7 = *(_DWORD *)(a1 + 8);
if ( v7 <= v5 )
v5 -= v7;
++*(_DWORD *)(a1 + 4);
++v6;
++a2;
}
while ( a3 != v6 );
}
else
{
v6 = 0;
}
result = v6;
}
return result;
}

original_code:
int writeb(buffer * b, char * s, unsigned int m) {
 unsigned int c;
 unsigned int p;
 if (!b || !s)
  return 0;
 if (b->len == b->size)
  return 0;
 c = 0;
 p = btail(b);
 while ((c < m) && (b->len < b->size)) {
  b->data[p] = s[c];
  p++;
  if (p >= b->size)
   p -= b->size;
  b->len++;
  c++;
 }
 return c;
}

canonical_original_code:
int writeb(struct buffer *b, char *s, unsigned int m) {
 unsigned int c;
 unsigned int p;
 if (!b || !s)
  return 0;
 if (b->len == b->size)
  return 0;
 c = 0;
 p = btail(b);
 while ((c < m) && (b->len < b->size)) {
  b->data[p] = s[c];
  p++;
  if (p >= b->size)
   p -= b->size;
  b->len++;
  c++;
 }
 return c;
}

variable_types:
s
char *

variable_types:
p
unsigned int


repo: crass/mhddfs

decompiled_code:
int mhdd_debug_init()
{
return pthread_mutex_init(&debug_lock, 0LL);
}

canonical_decompiled_code:
int func0()
{
return pthread_mutex_init(&debug_lock, 0LL);
}

original_code:
void mhdd_debug_init(void)
{
 pthread_mutex_init(&debug_lock, 0);
}

canonical_original_code:
void mhdd_debug_init()
{
 pthread_mutex_init(&debug_lock, 0);
}


decompiled_code:
__int64 mhdd_debug(int a1, const char *a2, ...)
{
unsigned int v2; // ebp
struct tm *v3; // rax
pthread_t v4; // rax
gcc_va_list va; // [rsp+0h] [rbp-128h] BYREF
time_t timer; // [rsp+18h] [rbp-110h] BYREF
char s[64]; // [rsp+20h] [rbp-108h] BYREF

v2 = 0;
if ( *((_DWORD *)&mhdd + 14) <= a1 && *((_QWORD *)&mhdd + 4) )
{
timer = time(0LL);
v3 = localtime(&timer);
strftime(s, 0x40uLL, "%Y-%m-%d %H:%M:%S", v3);
pthread_mutex_lock(&debug_lock);
fprintf(*((FILE **)&mhdd + 4), "mhddfs [%s]", s);
if ( a1 )
{
if ( a1 == 1 )
fwrite(" (info): ", 1uLL, 9uLL, *((FILE **)&mhdd + 4));
else
fwrite(": ", 1uLL, 2uLL, *((FILE **)&mhdd + 4));
}
else
{
fwrite(" (debug): ", 1uLL, 0xAuLL, *((FILE **)&mhdd + 4));
}
v4 = pthread_self();
fprintf(*((FILE **)&mhdd + 4), "[%ld] ", v4);
va_start(va, a2);
v2 = vfprintf(*((FILE **)&mhdd + 4), a2, va);
pthread_mutex_unlock(&debug_lock);
}
return v2;
}

canonical_decompiled_code:
__int64 func1(int a1, const char *a2, ...)
{
unsigned int v2; // ebp
struct tm *v3; // rax
pthread_t v4; // rax
gcc_va_list va; // [rsp+0h] [rbp-128h] BYREF
time_t timer; // [rsp+18h] [rbp-110h] BYREF
char s[64]; // [rsp+20h] [rbp-108h] BYREF

v2 = 0;
if ( *((_DWORD *)&mhdd + 14) <= a1 && *((_QWORD *)&mhdd + 4) )
{
timer = time(0LL);
v3 = localtime(&timer);
strftime(s, 0x40uLL, "%Y-%m-%d %H:%M:%S", v3);
pthread_mutex_lock(&debug_lock);
fprintf(*((FILE **)&mhdd + 4), "mhddfs [%s]", s);
if ( a1 )
{
if ( a1 == 1 )
fwrite(" (info): ", 1uLL, 9uLL, *((FILE **)&mhdd + 4));
else
fwrite(": ", 1uLL, 2uLL, *((FILE **)&mhdd + 4));
}
else
{
fwrite(" (debug): ", 1uLL, 0xAuLL, *((FILE **)&mhdd + 4));
}
v4 = pthread_self();
fprintf(*((FILE **)&mhdd + 4), "[%ld] ", v4);
va_start(va, a2);
v2 = vfprintf(*((FILE **)&mhdd + 4), a2, va);
pthread_mutex_unlock(&debug_lock);
}
return v2;
}

original_code:
int mhdd_debug(int level, const char *fmt, ...)
{
 if (level<mhdd.loglevel) return 0;
 if (!mhdd.debug) return 0;
 char tstr[64];
 time_t t=time(0);
 struct tm *lt;
 lt=localtime(&t);
 strftime(tstr, 64, "%Y-%m-%d %H:%M:%S", lt);
 pthread_mutex_lock(&debug_lock);
 fprintf(mhdd.debug, "mhddfs [%s]", tstr);
 switch(level)
 {
  case 0: fprintf(mhdd.debug, " (debug): "); break;
  case 1: fprintf(mhdd.debug, " (info): "); break;
  default: fprintf(mhdd.debug, ": "); break;
 }
 fprintf(mhdd.debug, "[%ld] ", (long int)pthread_self());
 va_list ap;
 __builtin_va_start(ap,fmt);
 int res=vfprintf(mhdd.debug, fmt, ap);
 __builtin_va_end(ap);
 pthread_mutex_unlock(&debug_lock);
 return res;
}

canonical_original_code:
int mhdd_debug(int level, char *fmt, ...)
{
 if (level<mhdd.loglevel) return 0;
 if (!mhdd.debug) return 0;
 char tstr[64];
 long t = time(0);
 struct tm *lt;
 lt=localtime(&t);
 strftime(tstr, 64, "%Y-%m-%d %H:%M:%S", lt);
 pthread_mutex_lock(&debug_lock);
 fprintf(mhdd.debug, "mhddfs [%s]", tstr);
 switch(level)
 {
  case 0: fprintf(mhdd.debug, " (debug): "); break;
  case 1: fprintf(mhdd.debug, " (info): "); break;
  default: fprintf(mhdd.debug, ": "); break;
 }
 fprintf(mhdd.debug, "[%ld] ", (long)pthread_self());
 __builtin_va_list ap;
 __builtin_va_start(ap,fmt);
 int res = vfprintf(mhdd.debug, fmt, ap);
 __builtin_va_end(ap);
 pthread_mutex_unlock(&debug_lock);
 return res;
}

variable_types: level int

variable_types:
fmt
char *

variable_types:
tstr
char [64]

variable_types: t long

variable_types: struct tm { int tm_sec; int tm_min; int tm_hour; int tm_mday; int tm_mon; int tm_year; int tm_wday; int tm_yday; int tm_isdst; long tm_gmtoff; char *tm_zone; } *

variable_types: res int


decompiled_code:
int flist_init()
{
return pthread_rwlock_init(&files_lock, 0LL);
}

canonical_decompiled_code:
int func0()
{
return pthread_rwlock_init(&files_lock, 0LL);
}

original_code:
void flist_init(void)
{
 pthread_rwlock_init(&files_lock, 0);
}

canonical_original_code:
void flist_init()
{
 pthread_rwlock_init(&files_lock, 0);
}


decompiled_code:
__int64 flist_unlock()
{
if ( pthread_rwlock_unlock(&files_lock) )
mhdd_debug(0LL, "E error unlock flist\n");
return mhdd_debug(0LL, "< unlock flist\n");
}

canonical_decompiled_code:
__int64 func1()
{
if ( pthread_rwlock_unlock(&files_lock) )
mhdd_debug(0LL, "E error unlock flist\n");
return mhdd_debug(0LL, "< unlock flist\n");
}

original_code:
void flist_unlock(void)
{
 if (pthread_rwlock_unlock(&files_lock) != 0) {
  mhdd_debug(0, "E error unlock flist\n");
 }
 mhdd_debug(0, "< unlock flist\n");
}

canonical_original_code:
void flist_unlock()
{
 if (pthread_rwlock_unlock(&files_lock) != 0) {
  mhdd_debug(0, "E error unlock flist\n");
 }
 mhdd_debug(0, "< unlock flist\n");
}


decompiled_code:
int flist_rdlock()
{
return pthread_rwlock_rdlock(&files_lock);
}

canonical_decompiled_code:
int func2()
{
return pthread_rwlock_rdlock(&files_lock);
}

original_code:
void flist_rdlock(void)
{
 pthread_rwlock_rdlock(&files_lock);
}

canonical_original_code:
void flist_rdlock()
{
 pthread_rwlock_rdlock(&files_lock);
}


decompiled_code:
int flist_wrlock()
{
return pthread_rwlock_wrlock(&files_lock);
}

canonical_decompiled_code:
int func3()
{
return pthread_rwlock_wrlock(&files_lock);
}

original_code:
void flist_wrlock(void)
{
 pthread_rwlock_wrlock(&files_lock);
}

canonical_original_code:
void flist_wrlock()
{
 pthread_rwlock_wrlock(&files_lock);
}


decompiled_code:
__int64 __fastcall item_by_id(__int64 a1, int a2)
{
__int64 result; // rax

if ( a2 )
flist_wrlock();
else
flist_rdlock();
result = files;
if ( files )
{
while ( *(_QWORD *)(result + 24) != a1 )
{
result = *(_QWORD *)(result + 32);
if ( !result )
goto LABEL_6;
}
}
else
{
LABEL_6:
flist_unlock();
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, int a2)
{
__int64 result; // rax

if ( a2 )
func3();
else
func2();
result = files;
if ( files )
{
while ( *(_QWORD *)(result + 24) != a1 )
{
result = *(_QWORD *)(result + 32);
if ( !result )
goto LABEL_6;
}
}
else
{
LABEL_6:
func1();
result = 0LL;
}
return result;
}

original_code:
static struct flist * item_by_id(uint64_t id, int wrlock)
{
 struct flist * next;
 if (wrlock)
  flist_wrlock();
 else
  flist_rdlock();
 for(next = files; next; next = next->next) {
  if (next->id == id)
   return next;
 }
 flist_unlock();
 return 0;
}

canonical_original_code:
static struct flist *item_by_id(unsigned long id, int wrlock)
{
 struct flist *next;
 if (wrlock)
  flist_wrlock();
 else
  flist_rdlock();
 for(next = files; next; next = next->next) {
  if (next->id == id)
   return next;
 }
 flist_unlock();
 return 0;
}

variable_types:
id
unsigned long

variable_types: wrlock int

variable_types: struct flist { char *name; char *real_name; int flags; int fh; union { unsigned long id; struct flist *aid; }; struct flist *next; } *


decompiled_code:
int flist_wrlock_locked()
{
pthread_rwlock_unlock(&files_lock);
return pthread_rwlock_wrlock(&files_lock);
}

canonical_decompiled_code:
int func5()
{
pthread_rwlock_unlock(&files_lock);
return pthread_rwlock_wrlock(&files_lock);
}

original_code:
void flist_wrlock_locked(void)
{
 pthread_rwlock_unlock(&files_lock);
 pthread_rwlock_wrlock(&files_lock);
}

canonical_original_code:
void flist_wrlock_locked()
{
 pthread_rwlock_unlock(&files_lock);
 pthread_rwlock_wrlock(&files_lock);
}


decompiled_code:
__int64 __fastcall delete_item(void *ptr, int a2)
{
void *v2; // rdx
_QWORD *v3; // rax
__int64 v4; // rax
__int64 v5; // rax

if ( a2 )
{
if ( a2 != 1 )
goto LABEL_4;
}
else
{
flist_wrlock();
}
flist_wrlock_locked();
LABEL_4:
v2 = (void *)files;
if ( files )
{
if ( (void *)files == ptr )
{
LABEL_9:
v4 = *((_QWORD *)ptr + 4);
if ( v4 )
*(_QWORD *)(v4 + 40) = *((_QWORD *)ptr + 5);
v5 = *((_QWORD *)ptr + 5);
if ( v5 )
*(_QWORD *)(v5 + 32) = *((_QWORD *)ptr + 4);
if ( v2 == ptr )
files = *((_QWORD *)ptr + 4);
mhdd_debug(0LL, "delete_item: %s (%s)\n", *(const char **)ptr, *((const char **)ptr + 1));
free(*(void **)ptr);
free(*((void **)ptr + 1));
free(ptr);
}
else
{
v3 = (_QWORD *)files;
while ( 1 )
{
v3 = (_QWORD *)v3[4];
if ( !v3 )
break;
if ( ptr == v3 )
goto LABEL_9;
}
}
}
return flist_unlock();
}

canonical_decompiled_code:
__int64 __fastcall func6(void *ptr, int a2)
{
void *v2; // rdx
_QWORD *v3; // rax
__int64 v4; // rax
__int64 v5; // rax

if ( a2 )
{
if ( a2 != 1 )
goto LABEL_4;
}
else
{
func3();
}
func5();
LABEL_4:
v2 = (void *)files;
if ( files )
{
if ( (void *)files == ptr )
{
LABEL_9:
v4 = *((_QWORD *)ptr + 4);
if ( v4 )
*(_QWORD *)(v4 + 40) = *((_QWORD *)ptr + 5);
v5 = *((_QWORD *)ptr + 5);
if ( v5 )
*(_QWORD *)(v5 + 32) = *((_QWORD *)ptr + 4);
if ( v2 == ptr )
files = *((_QWORD *)ptr + 4);
mhdd_debug(0LL, "delete_item: %s (%s)\n", *(const char **)ptr, *((const char **)ptr + 1));
free(*(void **)ptr);
free(*((void **)ptr + 1));
free(ptr);
}
else
{
v3 = (_QWORD *)files;
while ( 1 )
{
v3 = (_QWORD *)v3[4];
if ( !v3 )
break;
if ( ptr == v3 )
goto LABEL_9;
}
}
}
return func1();
}

original_code:
static void delete_item(struct flist *item, int locked)
{
 struct flist *next;
 switch(locked) {
  case UNLOCKED:
   flist_wrlock();
  case RDLOCKED:
   flist_wrlock_locked();
  case WRLOCKED:
   break;
 }
 for(next = files; next; next = next->next) {
  if (next == item) {
   if (item->next)
    item->next->prev = item->prev;
   if (item->prev)
    item->prev->next = item->next;
   if (files==item)
    files = item->next;
   mhdd_debug(0, "delete_item: %s (%s)\n",
     item->name, item->real_name);
   free(item->name);
   free(item->real_name);
   free(item);
   break;
  }
 }
 flist_unlock();
}

variable_types: locked int

user_defined_types: struct flist { char *name; char *real_name; int flags; int fh; union { unsigned long id; struct flist *aid; }; struct flist *next; }


decompiled_code:
__int64 __fastcall flist_item_by_id(__int64 a1)
{
return item_by_id(a1, 0);
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1)
{
return func4(a1, 0);
}

original_code:
struct flist * flist_item_by_id(uint64_t id) {
 return item_by_id(id, 0);
}

canonical_original_code:
struct flist *flist_item_by_id(unsigned long id) {
 return item_by_id(id, 0);
}

return_type: struct flist { char *name; char *real_name; int flags; int fh; union { unsigned long id; struct flist *aid; }; struct flist *next; } *


decompiled_code:
__int64 __fastcall flist_item_by_id_wrlock(__int64 a1)
{
return item_by_id(a1, 1);
}

canonical_decompiled_code:
__int64 __fastcall func9(__int64 a1)
{
return func4(a1, 1);
}

original_code:
struct flist * flist_item_by_id_wrlock(uint64_t id) {
 return item_by_id(id, 1);
}

canonical_original_code:
struct flist *flist_item_by_id_wrlock(unsigned long id) {
 return item_by_id(id, 1);
}


decompiled_code:
__int64 __fastcall flist_delete(void *a1)
{
return delete_item(a1, 0);
}

canonical_decompiled_code:
__int64 __fastcall func10(void *a1)
{
return func6(a1, 0);
}

original_code:
void flist_delete(struct flist *item)
{
 delete_item(item, UNLOCKED);
}


decompiled_code:
__int64 __fastcall flist_delete_locked(void *a1)
{
return delete_item(a1, 1);
}

canonical_decompiled_code:
__int64 __fastcall func11(void *a1)
{
return func6(a1, 1);
}

original_code:
void flist_delete_locked(struct flist *item)
{
 delete_item(item, RDLOCKED);
}


decompiled_code:
__int64 __fastcall flist_delete_wrlocked(void *a1)
{
return delete_item(a1, 2);
}

canonical_decompiled_code:
__int64 __fastcall func12(void *a1)
{
return func6(a1, 2);
}

original_code:
void flist_delete_wrlocked(struct flist * item)
{
 delete_item(item, WRLOCKED);
}

canonical_original_code:
void flist_delete_wrlocked(struct flist *item)
{
 delete_item(item, WRLOCKED);
}


original_code:
static void add_mhdd_dirs(const char * dir)
{
 int i;
 char ** newdirs;
 char *add_dir;
 if (*dir=='/')
 {
  add_dir=strdup(dir);
 }
 else
 {
  char cpwd[4096];
  getcwd(cpwd, 4096);
  add_dir = create_path(cpwd, dir);
 }
 if (!mhdd.dirs)
 {
  mhdd.dirs=calloc(2, sizeof(char *));
  mhdd.dirs[0]=add_dir;
  mhdd.cdirs=1;
  return;
 }
 newdirs=calloc(mhdd.cdirs+2, sizeof(char *));
 for (i=0; i<mhdd.cdirs; i++)
 {
  newdirs[i]=mhdd.dirs[i];
 }
 newdirs[mhdd.cdirs++]=add_dir;
 free(mhdd.dirs);
 mhdd.dirs=newdirs;
}

canonical_original_code:
static void add_mhdd_dirs(char *dir)
{
 int i;
 char **newdirs;
 char *add_dir;
 if (*dir=='/')
 {
  add_dir=strdup(dir);
 }
 else
 {
  char cpwd[4096];
  getcwd(cpwd, 4096);
  add_dir = create_path(cpwd, dir);
 }
 if (!mhdd.dirs)
 {
  mhdd.dirs=calloc(2, sizeof(char *));
  mhdd.dirs[0]=add_dir;
  mhdd.cdirs=1;
  return;
 }
 newdirs=calloc(mhdd.cdirs+2, sizeof(char *));
 for (i=0; i<mhdd.cdirs; i++)
 {
  newdirs[i]=mhdd.dirs[i];
 }
 newdirs[mhdd.cdirs++]=add_dir;
 free(mhdd.dirs);
 mhdd.dirs=newdirs;
}

variable_types:
dir
char *

variable_types: i int

variable_types:
newdirs
char **

variable_types:
cpwd
char [4096]


decompiled_code:
__int64 __fastcall mhddfs_opt_proc(__int64 a1, const char *a2, int a3)
{
char *v3; // rax
char *v4; // r12
char *v5; // rbx
const char *v6; // rbx
char *v7; // rbp
char *v8; // rax
__int64 result; // rax

if ( a3 == -2 )
{
v3 = strdup(a2);
v4 = v3;
if ( v3 )
{
v5 = v3;
do
{
v6 = &v5[*v5 == 44];
v7 = strdup(v6);
v8 = strchr(v7, 44);
if ( v8 )
*v8 = 0;
add_mhdd_dirs(v7);
free(v7);
v5 = strchr(v6 + 1, 44);
}
while ( v5 );
}
free(v4);
result = 0LL;
}
else
{
if ( a3 == 15121974 )
{
fprintf(stderr, "mhddfs version: %s\n", "0.1.39");
exit(0);
}
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, const char *a2, int a3)
{
char *v3; // rax
char *v4; // r12
char *v5; // rbx
const char *v6; // rbx
char *v7; // rbp
char *v8; // rax
__int64 result; // rax

if ( a3 == -2 )
{
v3 = strdup(a2);
v4 = v3;
if ( v3 )
{
v5 = v3;
do
{
v6 = &v5[*v5 == 44];
v7 = strdup(v6);
v8 = strchr(v7, 44);
if ( v8 )
*v8 = 0;
func0(v7);
free(v7);
v5 = strchr(v6 + 1, 44);
}
while ( v5 );
}
free(v4);
result = 0LL;
}
else
{
if ( a3 == 15121974 )
{
fprintf(stderr, "mhddfs version: %s\n", "0.1.39");
exit(0);
}
result = 1LL;
}
return result;
}

original_code:
static int mhddfs_opt_proc(void *data,
  const char *arg, int key, struct fuse_args *outargs)
{
 switch(key)
 {
  case 15121974:
   fprintf(stderr, "mhddfs version: %s\n", "0.1.39");
   exit(0);
  case -2:
   {
    char *dir = strdup(arg);
    char *tmp;
    for (tmp=dir; tmp; tmp=strchr(tmp+1, ','))
    {
     if (*tmp==',') tmp++;
     char *add=strdup(tmp);
     char *end=strchr(add, ',');
     if (end) *end=0;
     add_mhdd_dirs(add);
     free(add);
    }
    free(dir);
    return 0;
   }
 }
 return 1;
}

canonical_original_code:
static int mhddfs_opt_proc(void *data, char *arg, int key, struct fuse_args *outargs)
{
 switch(key)
 {
  case 15121974:
   fprintf(stderr, "mhddfs version: %s\n", "0.1.39");
   exit(0);
  case -2:
   {
    char *dir = strdup(arg);
    char *tmp;
    for (tmp=dir; tmp; tmp=strchr(tmp+1, ','))
    {
     if (*tmp==',') tmp++;
     char *add = strdup(tmp);
     char *end = strchr(add, ',');
     if (end) *end=0;
     add_mhdd_dirs(add);
     free(add);
    }
    free(dir);
    return 0;
   }
 }
 return 1;
}

variable_types:
data
void *

variable_types:
arg
char *

variable_types: key int

variable_types: struct fuse_args { int argc; char **argv; int allocated; } *

variable_types:
tmp
char *

variable_types:
add
char *

variable_types:
end
char *


decompiled_code:
void check_if_unique_mountpoints()
{
int v0; // ebx
int v1; // er15
const char **v2; // rbp
_QWORD *v3; // rbx
const char *v4; // r13
__ino64_t v5; // rdx
int v6; // eax
__int64 v7; // rdx
int v8; // ecx
struct stat64 *v9; // r12
const char **v10; // rax
const char *v11; // r14
__int64 v12; // [rsp+0h] [rbp-58h]
_QWORD *ptr; // [rsp+10h] [rbp-48h]

v0 = *((_DWORD *)&mhdd + 4);
ptr = calloc(v0, 0x90uLL);
if ( v0 > 0 )
{
v1 = 0;
do
{
v9 = (struct stat64 *)&ptr[18 * v1];
if ( stat64(*(const char **)(*(&mhdd + 1) + 8LL * v1), v9) )
memset(v9, 0, sizeof(struct stat64));
if ( v1 > 0 )
{
v10 = (const char **)*(&mhdd + 1);
v11 = v10[v1];
v2 = v10;
v3 = ptr + 1;
v12 = (__int64)&v10[(unsigned int)(v1 - 1) + 1];
while ( 1 )
{
v4 = *v2;
if ( !strcmp(v11, *v2) )
break;
if ( *(v3 - 1) == v9->st_dev )
{
v5 = v9->st_ino;
if ( v5 != 0 && v9->st_dev != 0 && *v3 == v5 )
break;
}
++v2;
v3 += 18;
if ( v2 == (const char **)v12 )
goto LABEL_12;
}
fprintf(stderr, "mhddfs: Duplicate directories: %s %s\n\t%s was excluded from dirlist\n", v11, v4, v11);
free(*(void **)(*(&mhdd + 1) + 8LL * v1));
*(_QWORD *)(*(&mhdd + 1) + 8LL * v1) = 0LL;
v6 = *((_DWORD *)&mhdd + 4) - 1;
if ( v6 > v1 )
{
v7 = 8LL * v1 + 8;
v8 = v1;
do
{
*(_QWORD *)(*(&mhdd + 1) + v7 - 8) = *(_QWORD *)(*(&mhdd + 1) + v7);
++v8;
v6 = *((_DWORD *)&mhdd + 4) - 1;
v7 += 8LL;
}
while ( v6 > v8 );
}
*((_DWORD *)&mhdd + 4) = v6;
--v1;
}
LABEL_12:
++v1;
}
while ( *((_DWORD *)&mhdd + 4) > v1 );
}
free(ptr);
}

original_code:
static void check_if_unique_mountpoints(void)
{
 int i, j;
 struct stat * stats = calloc(mhdd.cdirs, sizeof(struct stat));
 for (i = 0; i < mhdd.cdirs; i++) {
  if (stat(mhdd.dirs[i], stats + i) != 0)
   memset(stats + i, 0, sizeof(struct stat));
  for (j = 0; j < i; j++) {
   if (strcmp(mhdd.dirs[i], mhdd.dirs[j]) != 0) {
    if (stats[j].st_dev != stats[i].st_dev)
     continue;
    if (stats[j].st_ino != stats[i].st_ino)
     continue;
    if (!stats[i].st_dev)
     continue;
    if (!stats[i].st_ino)
     continue;
   }
   fprintf(stderr,
    "mhddfs: Duplicate directories: %s %s\n"
    "\t%s was excluded from dirlist\n",
    mhdd.dirs[i],
    mhdd.dirs[j],
    mhdd.dirs[i]
   );
   free(mhdd.dirs[i]);
   mhdd.dirs[i] = 0;
   for (j = i; j < mhdd.cdirs - 1; j++)
    mhdd.dirs[j] = mhdd.dirs[j+1];
   mhdd.cdirs--;
   i--;
   break;
  }
 }
 free(stats);
}

canonical_original_code:
static void check_if_unique_mountpoints()
{
 int i; int j;
 struct stat *stats = calloc(mhdd.cdirs, sizeof(struct stat));
 for (i = 0; i < mhdd.cdirs; i++) {
  if (stat(mhdd.dirs[i], stats + i) != 0)
   memset(stats + i, 0, sizeof(struct stat));
  for (j = 0; j < i; j++) {
   if (strcmp(mhdd.dirs[i], mhdd.dirs[j]) != 0) {
    if (stats[j].st_dev != stats[i].st_dev)
     continue;
    if (stats[j].st_ino != stats[i].st_ino)
     continue;
    if (!stats[i].st_dev)
     continue;
    if (!stats[i].st_ino)
     continue;
   }
   fprintf(stderr,
    "mhddfs: Duplicate directories: %s %s\n"
    "\t%s was excluded from dirlist\n",
    mhdd.dirs[i],
    mhdd.dirs[j],
    mhdd.dirs[i]
   );
   free(mhdd.dirs[i]);
   mhdd.dirs[i] = 0;
   for (j = i; j < mhdd.cdirs - 1; j++)
    mhdd.dirs[j] = mhdd.dirs[j+1];
   mhdd.cdirs--;
   i--;
   break;
  }
 }
 free(stats);
}

variable_types: j int

variable_types: struct stat { unsigned long st_dev; unsigned long st_ino; unsigned long st_nlink; unsigned int st_mode; unsigned int st_uid; unsigned int st_gid; int __pad0; unsigned long st_rdev; long st_size; long st_blksize; long st_blocks; struct timespec { long tv_sec; long tv_nsec; } st_atim; struct timespec st_mtim; struct timespec st_ctim; long __glibc_reserved[3]; } *

user_defined_types: struct stat { unsigned long st_dev; unsigned long st_ino; unsigned long st_nlink; unsigned int st_mode; unsigned int st_uid; unsigned int st_gid; int __pad0; unsigned long st_rdev; long st_size; long st_blksize; long st_blocks; struct timespec st_atim; struct timespec st_mtim; struct timespec st_ctim; long __glibc_reserved[3]; }


repo: crass/uefivars

decompiled_code:
char *__fastcall find_write_victim(__int64 a1, char *a2)
{
char *v2; // r12
int v3; // eax
int v4; // er14
char *v5; // rax
struct dirent64 **v6; // rbp
struct dirent64 **v7; // r13
struct dirent64 *v8; // rax
const char *v9; // rbx
int v10; // er13
int v11; // er14
__int64 i; // rbx
struct dirent64 *v13; // rdi
char v15[4104]; // [rsp+0h] [rbp-1038h] BYREF
struct dirent64 **namelist; // [rsp+1008h] [rbp-30h] BYREF

v2 = a2;
namelist = 0LL;
memset(v15, 0, 0x1000uLL);
v3 = scandir64(
"/proc/efi/vars",
&namelist,
select_variable_names,
(int (*)(const struct dirent64 **, const struct dirent64 **))&alphasort64);
if ( v3 < 0 )
return 0LL;
v4 = v3;
efichar_to_char(v15, a1, 4096LL);
v5 = &v15[strlen(v15)];
*(_WORD *)v5 = 45;
efi_guid_unparse(a1 + 1024, v5 + 1);
if ( v4 <= 0 )
{
free(namelist);
v2 = 0LL;
}
else
{
v6 = namelist;
v7 = &namelist[(unsigned int)(v4 - 1) + 1];
do
{
v8 = *v6;
if ( *v6 )
{
v9 = v8->d_name;
if ( strncmp(v15, v8->d_name, 0x1000uLL) )
{
sprintf(a2, "%s/%s", "/proc/efi/vars", v9);
v10 = 1;
goto LABEL_9;
}
}
++v6;
}
while ( v6 != v7 );
v10 = 0;
LABEL_9:
v11 = v4 - 1;
for ( i = v11; ; --i )
{
v13 = namelist[i];
if ( v13 )
{
free(v13);
namelist[i] = 0LL;
}
if ( i == v11 - (unsigned __int64)(unsigned int)v11 )
break;
}
free(namelist);
if ( !v10 )
v2 = 0LL;
}
return v2;
}

canonical_decompiled_code:
char *__fastcall func0(__int64 a1, char *a2)
{
char *v2; // r12
int v3; // eax
int v4; // er14
char *v5; // rax
struct dirent64 **v6; // rbp
struct dirent64 **v7; // r13
struct dirent64 *v8; // rax
const char *v9; // rbx
int v10; // er13
int v11; // er14
__int64 i; // rbx
struct dirent64 *v13; // rdi
char v15[4104]; // [rsp+0h] [rbp-1038h] BYREF
struct dirent64 **namelist; // [rsp+1008h] [rbp-30h] BYREF

v2 = a2;
namelist = 0LL;
memset(v15, 0, 0x1000uLL);
v3 = scandir64(
"/proc/efi/vars",
&namelist,
select_variable_names,
(int (*)(const struct dirent64 **, const struct dirent64 **))&alphasort64);
if ( v3 < 0 )
return 0LL;
v4 = v3;
efichar_to_char(v15, a1, 4096LL);
v5 = &v15[strlen(v15)];
*(_WORD *)v5 = 45;
efi_guid_unparse(a1 + 1024, v5 + 1);
if ( v4 <= 0 )
{
free(namelist);
v2 = 0LL;
}
else
{
v6 = namelist;
v7 = &namelist[(unsigned int)(v4 - 1) + 1];
do
{
v8 = *v6;
if ( *v6 )
{
v9 = v8->d_name;
if ( strncmp(v15, v8->d_name, 0x1000uLL) )
{
sprintf(a2, "%s/%s", "/proc/efi/vars", v9);
v10 = 1;
goto LABEL_9;
}
}
++v6;
}
while ( v6 != v7 );
v10 = 0;
LABEL_9:
v11 = v4 - 1;
for ( i = v11; ; --i )
{
v13 = namelist[i];
if ( v13 )
{
free(v13);
namelist[i] = 0LL;
}
if ( i == v11 - (unsigned __int64)(unsigned int)v11 )
break;
}
free(namelist);
if ( !v10 )
v2 = 0LL;
}
return v2;
}

original_code:
static char *
find_write_victim(efi_variable_t *var, char file[4096])
{
 struct dirent **namelist = ((void *)0);
 int i, n, found=0;
 char testname[4096], *p;
 memset(testname, 0, sizeof(testname));
 n = scandir("/proc/efi/vars", &namelist,
      select_variable_names, alphasort);
 if (n < 0)
  return ((void *)0);
 p = testname;
 efichar_to_char(p, var->VariableName, 4096);
 p += strlen(p);
 p += sprintf(p, "-");
 efi_guid_unparse(&var->VendorGuid, p);
 for (i=0; i<n; i++) {
  if (namelist[i] &&
      strncmp(testname, namelist[i]->d_name, sizeof(testname))) {
   found++;
   sprintf(file, "%s/%s", "/proc/efi/vars",
    namelist[i]->d_name);
   break;
  }
 }
 while (n--) {
  if (namelist[n]) {
   free(namelist[n]);
   namelist[n] = ((void *)0);
  }
 }
 free(namelist);
 if (!found) return ((void *)0);
 return file;
}

canonical_original_code:
static char *find_write_victim(struct _efi_variable_t *var, char file[4096])
{
 struct dirent **namelist = ((void *)0);
 int i; int n; int found = 0;
 char testname[4096]; char *p;
 memset(testname, 0, sizeof(testname));
 n = scandir("/proc/efi/vars", &namelist,
      select_variable_names, alphasort);
 if (n < 0)
  return ((void *)0);
 p = testname;
 efichar_to_char(p, var->VariableName, 4096);
 p += strlen(p);
 p += sprintf(p, "-");
 efi_guid_unparse(&var->VendorGuid, p);
 for (i=0; i<n; i++) {
  if (namelist[i] &&
      strncmp(testname, namelist[i]->d_name, sizeof(testname))) {
   found++;
   sprintf(file, "%s/%s", "/proc/efi/vars",
    namelist[i]->d_name);
   break;
  }
 }
 while (n--) {
  if (namelist[n]) {
   free(namelist[n]);
   namelist[n] = ((void *)0);
  }
 }
 free(namelist);
 if (!found) return ((void *)0);
 return file;
}

variable_types: struct _efi_variable_t { unsigned short VariableName[-1]; struct efi_guid_t { unsigned char b[16]; } VendorGuid; unsigned long DataSize; unsigned char Data[1024]; unsigned long Status; unsigned int Attributes; } *

variable_types:
file
char [4096]

variable_types: struct dirent { unsigned long d_ino; long d_off; unsigned short d_reclen; unsigned char d_type; char d_name[256]; } **

variable_types: n int

variable_types: found int

variable_types:
testname
char [4096]

variable_types:
p
char *

return_type: char *

user_defined_types: struct _efi_variable_t { unsigned short VariableName[-1]; struct efi_guid_t VendorGuid; unsigned long DataSize; unsigned char Data[1024]; unsigned long Status; unsigned int Attributes; }


decompiled_code:
__int64 __fastcall procfs_write_variable(void *buf)
{
__int64 result; // rax
char *v2; // rdx
int v3; // eax
int v4; // ebp
char v5[4096]; // [rsp-1000h] [rbp-2018h] BYREF
char s[4120]; // [rsp+0h] [rbp-1018h] BYREF

result = 0x8000000000000002LL;
if ( buf )
{
memset(s, 0, 0x1000uLL);
memset(v5, 0, sizeof(v5));
v2 = find_write_victim((__int64)buf, v5);
result = 0x8000000000000002LL;
if ( v2 )
{
v3 = open64(v5, 1);
v4 = v3;
if ( v3 == -1 )
{
sprintf(s, "write_variable():open(%s)", v5);
perror(s);
result = 0x8000000000000002LL;
}
else if ( write(v3, buf, 0x824uLL) == 2084 )
{
close(v4);
result = 0LL;
}
else
{
close(v4);
result = 0x8000000000000002LL;
}
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(void *buf)
{
__int64 result; // rax
char *v2; // rdx
int v3; // eax
int v4; // ebp
char v5[4096]; // [rsp-1000h] [rbp-2018h] BYREF
char s[4120]; // [rsp+0h] [rbp-1018h] BYREF

result = 0x8000000000000002LL;
if ( buf )
{
memset(s, 0, 0x1000uLL);
memset(v5, 0, sizeof(v5));
v2 = func0((__int64)buf, v5);
result = 0x8000000000000002LL;
if ( v2 )
{
v3 = open64(v5, 1);
v4 = v3;
if ( v3 == -1 )
{
sprintf(s, "write_variable():open(%s)", v5);
perror(s);
result = 0x8000000000000002LL;
}
else if ( write(v3, buf, 0x824uLL) == 2084 )
{
close(v4);
result = 0LL;
}
else
{
close(v4);
result = 0x8000000000000002LL;
}
}
}
return result;
}

original_code:
static efi_status_t
procfs_write_variable(efi_variable_t *var)
{
 int fd;
 size_t writesize;
 char buffer[4096], name[4096], *p = ((void *)0);
 if (!var) return ((2) | (1L << ((sizeof(unsigned long) * 8) - 1)));
 memset(buffer, 0, sizeof(buffer));
 memset(name, 0, sizeof(name));
 p = find_write_victim(var, name);
 if (!p) return ((2) | (1L << ((sizeof(unsigned long) * 8) - 1)));
 fd = open(name, 01);
 if (fd == -1) {
  sprintf(buffer, "write_variable():open(%s)", name);
  perror(buffer);
  return ((2) | (1L << ((sizeof(unsigned long) * 8) - 1)));
 }
 writesize = write(fd, var, sizeof(*var));
 if (writesize != sizeof(*var)) {
  close(fd);
  return ((2) | (1L << ((sizeof(unsigned long) * 8) - 1)));
 }
 close(fd);
 return 0;
}

canonical_original_code:
static unsigned long procfs_write_variable(struct _efi_variable_t *var)
{
 int fd;
 unsigned long writesize;
 char buffer[4096]; char name[4096]; char *p = ((void *)0);
 if (!var) return ((2) | (1L << ((sizeof(unsigned long) * 8) - 1)));
 memset(buffer, 0, sizeof(buffer));
 memset(name, 0, sizeof(name));
 p = find_write_victim(var, name);
 if (!p) return ((2) | (1L << ((sizeof(unsigned long) * 8) - 1)));
 fd = open(name, 01);
 if (fd == -1) {
  sprintf(buffer, "write_variable():open(%s)", name);
  perror(buffer);
  return ((2) | (1L << ((sizeof(unsigned long) * 8) - 1)));
 }
 writesize = write(fd, var, sizeof(*var));
 if (writesize != sizeof(*var)) {
  close(fd);
  return ((2) | (1L << ((sizeof(unsigned long) * 8) - 1)));
 }
 close(fd);
 return 0;
}

variable_types: fd int

variable_types:
writesize
unsigned long

variable_types:
buffer
char [4096]

variable_types:
name
char [4096]

return_type: unsigned long

user_defined_types: struct efi_guid_t { unsigned char b[16]; }


decompiled_code:
__int64 __fastcall procfs_delete_variable(__int64 a1)
{
__int64 result; // rax

result = 0x8000000000000002LL;
if ( a1 )
{
*(_QWORD *)(a1 + 1040) = 0LL;
*(_DWORD *)(a1 + 2080) = 0;
result = procfs_write_variable((void *)a1);
}
return result;
}

original_code:
static efi_status_t
procfs_delete_variable(efi_variable_t *var)
{
 if (!var) return ((2) | (1L << ((sizeof(unsigned long) * 8) - 1)));
 var->DataSize = 0;
 var->Attributes = 0;
 return procfs_write_variable(var);
}

canonical_original_code:
static unsigned long procfs_delete_variable(struct _efi_variable_t *var)
{
 if (!var) return ((2) | (1L << ((sizeof(unsigned long) * 8) - 1)));
 var->DataSize = 0;
 var->Attributes = 0;
 return procfs_write_variable(var);
}


decompiled_code:
__int64 __fastcall procfs_edit_variable(__int64 a1, void *a2)
{
__int64 result; // rax

result = 0x8000000000000002LL;
if ( a2 )
result = procfs_write_variable(a2);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, void *a2)
{
__int64 result; // rax

result = 0x8000000000000002LL;
if ( a2 )
result = func1(a2);
return result;
}

original_code:
static efi_status_t
procfs_edit_variable(const char *unused, efi_variable_t *var)
{
 if (!var) return ((2) | (1L << ((sizeof(unsigned long) * 8) - 1)));
 return procfs_write_variable(var);
}

canonical_original_code:
static unsigned long procfs_edit_variable(char *unused, struct _efi_variable_t *var)
{
 if (!var) return ((2) | (1L << ((sizeof(unsigned long) * 8) - 1)));
 return procfs_write_variable(var);
}

variable_types:
unused
char *


decompiled_code:
_BOOL8 __fastcall select_variable_names(const struct dirent64 *a1)
{
bool v1; // zf
_BOOL8 result; // rax

v1 = strcmp(a1->d_name, ".") == 0;
result = !v1;
if ( !v1 )
result = strcmp(a1->d_name, "..") != 0;
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func4(const struct dirent64 *a1)
{
bool v1; // zf
_BOOL8 result; // rax

v1 = strcmp(a1->d_name, ".") == 0;
result = !v1;
if ( !v1 )
result = strcmp(a1->d_name, "..") != 0;
return result;
}

original_code:
static int
select_variable_names(const struct dirent *d)
{
 if (!strcmp(d->d_name, ".") ||
     !strcmp(d->d_name, ".."))
  return 0;
 return 1;
}

canonical_original_code:
static int select_variable_names(struct dirent *d)
{
 if (!strcmp(d->d_name, ".") ||
     !strcmp(d->d_name, ".."))
  return 0;
 return 1;
}

variable_types: struct dirent { unsigned long d_ino; long d_off; unsigned short d_reclen; unsigned char d_type; char d_name[256]; } *


decompiled_code:
unsigned __int64 __fastcall procfs_read_variable(const char *a1, _QWORD *a2)
{
unsigned __int64 result; // rax
int v3; // ebp
char v4[4120]; // [rsp-1018h] [rbp-1018h] BYREF

if ( !a1 )
return 0x8000000000000002LL;
result = 0x8000000000000002LL;
if ( a2 )
{
snprintf(v4, 0xFFFuLL, "%s/%s", "/proc/efi/vars", a1);
v3 = open64(v4, 0);
result = 0x800000000000000ELL;
if ( v3 != -1 )
{
if ( read(v3, a2, 0x824uLL) == 2084 )
{
close(v3);
result = a2[259];
}
else
{
close(v3);
result = 0x8000000000000002LL;
}
}
}
return result;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func5(const char *a1, _QWORD *a2)
{
unsigned __int64 result; // rax
int v3; // ebp
char v4[4120]; // [rsp-1018h] [rbp-1018h] BYREF

if ( !a1 )
return 0x8000000000000002LL;
result = 0x8000000000000002LL;
if ( a2 )
{
snprintf(v4, 0xFFFuLL, "%s/%s", "/proc/efi/vars", a1);
v3 = open64(v4, 0);
result = 0x800000000000000ELL;
if ( v3 != -1 )
{
if ( read(v3, a2, 0x824uLL) == 2084 )
{
close(v3);
result = a2[259];
}
else
{
close(v3);
result = 0x8000000000000002LL;
}
}
}
return result;
}

original_code:
static efi_status_t
procfs_read_variable(const char *name, efi_variable_t *var)
{
 char filename[4096];
 int fd;
 size_t readsize;
 if (!name || !var) return ((2) | (1L << ((sizeof(unsigned long) * 8) - 1)));
 snprintf(filename, 4096 -1, "%s/%s", "/proc/efi/vars",name);
 fd = open(filename, 00);
 if (fd == -1) {
  return ((14) | (1L << ((sizeof(unsigned long) * 8) - 1)));
 }
 readsize = read(fd, var, sizeof(*var));
 if (readsize != sizeof(*var)) {
  close(fd);
  return ((2) | (1L << ((sizeof(unsigned long) * 8) - 1)));
 }
 close(fd);
 return var->Status;
}

canonical_original_code:
static unsigned long procfs_read_variable(char *name, struct _efi_variable_t *var)
{
 char filename[4096];
 int fd;
 unsigned long readsize;
 if (!name || !var) return ((2) | (1L << ((sizeof(unsigned long) * 8) - 1)));
 snprintf(filename, 4096 -1, "%s/%s", "/proc/efi/vars",name);
 fd = open(filename, 00);
 if (fd == -1) {
  return ((14) | (1L << ((sizeof(unsigned long) * 8) - 1)));
 }
 readsize = read(fd, var, sizeof(*var));
 if (readsize != sizeof(*var)) {
  close(fd);
  return ((2) | (1L << ((sizeof(unsigned long) * 8) - 1)));
 }
 close(fd);
 return var->Status;
}

variable_types:
name
char *

variable_types:
filename
char [4096]

variable_types:
readsize
unsigned long


decompiled_code:
_BOOL8 __fastcall is_mbr_valid(__int64 a1)
{
_BOOL8 result; // rax

if ( a1 )
result = *(_WORD *)(a1 + 510) == 0xAA55;
else
result = 0LL;
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func0(__int64 a1)
{
_BOOL8 result; // rax

if ( a1 )
result = *(_WORD *)(a1 + 510) == 0xAA55;
else
result = 0LL;
return result;
}

original_code:
static int
is_mbr_valid(legacy_mbr *mbr)
{
 if (!mbr)
  return 0;
 return (mbr->signature == 0xaa55);
}

canonical_original_code:
static int is_mbr_valid(struct _legacy_mbr *mbr)
{
 if (!mbr)
  return 0;
 return (mbr->signature == 0xaa55);
}

variable_types: struct _legacy_mbr { unsigned char bootcode[440]; unsigned int unique_mbr_signature; unsigned short unknown; struct _partition_record { unsigned char boot_indicator; unsigned char start_head; unsigned char start_sector; unsigned char start_track; unsigned char os_type; unsigned char end_head; unsigned char end_sector; unsigned char end_track; unsigned int starting_lba; unsigned int size_in_lba; } partition[4]; unsigned short signature; } *

user_defined_types: struct _legacy_mbr { unsigned char bootcode[440]; unsigned int unique_mbr_signature; unsigned short unknown; struct _partition_record partition[4]; unsigned short signature; }


decompiled_code:
__int64 __fastcall disk_get_scsi_pci(int fd, _BYTE *a2, _BYTE *a3, _BYTE *a4)
{
unsigned int v6; // ebx
int v8; // [rsp+Ch] [rbp-CCh] BYREF
int v9; // [rsp+10h] [rbp-C8h] BYREF
int v10; // [rsp+14h] [rbp-C4h] BYREF
char v11[8]; // [rsp+18h] [rbp-C0h] BYREF
struct stat64 buf; // [rsp+20h] [rbp-B8h] BYREF

v10 = 0;
v9 = 0;
v8 = 0;
memset(&buf, 0, sizeof(buf));
if ( fstat64(fd, &buf) == -1 )
{
perror("stat");
v6 = 1;
}
else if ( (buf.st_mode & 0xF000) == 0x8000 )
{
fwrite("You must call this program with a file name such as /dev/sda.\n", 1uLL, 0x3EuLL, stderr);
v6 = 1;
}
else
{
v6 = get_scsi_pci((unsigned int)fd, v11);
if ( v6 )
{
perror("get_scsi_pci");
}
else if ( (unsigned int)_isoc99_sscanf(v11, "%x:%x.%x", &v10, &v9, &v8) == 3 )
{
*a2 = v10;
*a3 = v9;
*a4 = v8;
}
else
{
puts("sscanf failed");
v6 = 1;
}
}
return v6;
}

canonical_decompiled_code:
__int64 __fastcall func2(int fd, _BYTE *a2, _BYTE *a3, _BYTE *a4)
{
unsigned int v6; // ebx
int v8; // [rsp+Ch] [rbp-CCh] BYREF
int v9; // [rsp+10h] [rbp-C8h] BYREF
int v10; // [rsp+14h] [rbp-C4h] BYREF
char v11[8]; // [rsp+18h] [rbp-C0h] BYREF
struct stat64 buf; // [rsp+20h] [rbp-B8h] BYREF

v10 = 0;
v9 = 0;
v8 = 0;
memset(&buf, 0, sizeof(buf));
if ( fstat64(fd, &buf) == -1 )
{
perror("stat");
v6 = 1;
}
else if ( (buf.st_mode & 0xF000) == 0x8000 )
{
fwrite("You must call this program with a file name such as /dev/sda.\n", 1uLL, 0x3EuLL, stderr);
v6 = 1;
}
else
{
v6 = get_scsi_pci((unsigned int)fd, v11);
if ( v6 )
{
perror("get_scsi_pci");
}
else if ( (unsigned int)_isoc99_sscanf(v11, "%x:%x.%x", &v10, &v9, &v8) == 3 )
{
*a2 = v10;
*a3 = v9;
*a4 = v8;
}
else
{
puts("sscanf failed");
v6 = 1;
}
}
return v6;
}

original_code:
static int
disk_get_scsi_pci(int fd,
      unsigned char *bus,
      unsigned char *device,
      unsigned char *function)
{
 int rc, usefd=fd;
 struct stat buf;
 char slot_name[8];
 unsigned int b=0,d=0,f=0;
 memset(&buf, 0, sizeof(buf));
 rc = fstat(fd, &buf);
 if (rc == -1) {
  perror("stat");
  return 1;
 }
 if (((((buf.st_mode)) & 0170000) == (0100000))) {
  fprintf(stderr, "You must call this program with "
   "a file name such as /dev/sda.\n");
  return 1;
 }
 rc = get_scsi_pci(usefd, slot_name);
 if (rc) {
  perror("get_scsi_pci");
  return rc;
 }
 rc = sscanf(slot_name, "%x:%x.%x", &b,&d,&f);
 if (rc != 3) {
  printf("sscanf failed\n");
  return 1;
 }
 *bus = b & 0xFF;
 *device = d & 0xFF;
 *function = f & 0xFF;
 return 0;
}

canonical_original_code:
static int disk_get_scsi_pci(int fd, unsigned char *bus, unsigned char *device, unsigned char *function)
{
 int rc; int usefd = fd;
 struct stat buf;
 char slot_name[8];
 unsigned int b = 0; unsigned int d = 0; unsigned int f = 0;
 memset(&buf, 0, sizeof(buf));
 rc = fstat(fd, &buf);
 if (rc == -1) {
  perror("stat");
  return 1;
 }
 if (((((buf.st_mode)) & 0170000) == (0100000))) {
  fprintf(stderr, "You must call this program with "
   "a file name such as /dev/sda.\n");
  return 1;
 }
 rc = get_scsi_pci(usefd, slot_name);
 if (rc) {
  perror("get_scsi_pci");
  return rc;
 }
 rc = sscanf(slot_name, "%x:%x.%x", &b,&d,&f);
 if (rc != 3) {
  printf("sscanf failed\n");
  return 1;
 }
 *bus = b & 0xFF;
 *device = d & 0xFF;
 *function = f & 0xFF;
 return 0;
}

variable_types:
bus
unsigned char *

variable_types:
device
unsigned char *

variable_types:
function
unsigned char *

variable_types: rc int

variable_types: usefd int

variable_types: struct stat { unsigned long st_dev; unsigned long st_ino; unsigned long st_nlink; unsigned int st_mode; unsigned int st_uid; unsigned int st_gid; int __pad0; unsigned long st_rdev; long st_size; long st_blksize; long st_blocks; struct timespec { long tv_sec; long tv_nsec; } st_atim; struct timespec st_mtim; struct timespec st_ctim; long __glibc_reserved[3]; }

variable_types:
b
unsigned int

variable_types:
d
unsigned int

variable_types:
f
unsigned int


original_code:
int
disk_info_from_fd(int fd,
    int *interface_type,
    unsigned int *controllernum,
    unsigned int *disknum,
    unsigned char *part)
{
 struct stat buf;
 int rc;
 uint64_t major;
 unsigned char minor;
 memset(&buf, 0, sizeof(struct stat));
 rc = fstat(fd, &buf);
 if (rc == -1) {
  perror("stat");
  return 1;
 }
 if (!(((((buf.st_mode)) & 0170000) == (0060000)) || ((((buf.st_mode)) & 0170000) == (0100000)))) {
  printf("Cannot stat non-block or non-regular file\n");
  return 1;
 }
 major = buf.st_dev >> 8;
 minor = buf.st_dev && 0xFF;
 if (major == 3) {
  *disknum = (minor >> 6) & 1;
  *controllernum = (major - 3 + 0) + *disknum;
  *interface_type = ata;
  *part = minor & 0x3F;
  return 0;
 }
 else if (major == 22) {
  *disknum = (minor >> 6) & 1;
  *controllernum = (major - 22 + 2) + *disknum;
  *interface_type = ata;
  *part = minor & 0x3F;
  return 0;
 }
 else if (major >= 33 && major <= 34) {
  *disknum = (minor >> 6) & 1;
  *controllernum = (major - 33 + 4) + *disknum;
  *interface_type = ata;
  *part = minor & 0x3F;
  return 0;
 }
 else if (major >= 56 && major <= 57) {
  *disknum = (minor >> 6) & 1;
  *controllernum = (major - 56 + 8) + *disknum;
  *interface_type = ata;
  *part = minor & 0x3F;
  return 0;
 }
 else if (major >= 88 && major <= 91) {
  *disknum = (minor >> 6) & 1;
  *controllernum = (major - 88 + 12) + *disknum;
  *interface_type = ata;
  *part = minor & 0x3F;
  return 0;
 }
 if (major >= 80 && major <= 87) {
  *interface_type = i2o;
  *disknum = 16*(major-80) + (minor >> 4);
  *part = (minor & 0xF);
  return 0;
 }
 if (major == 8) {
  *interface_type = scsi;
  *disknum = (minor >> 4);
  *part = (minor & 0xF);
  return 0;
 }
 else if ( major >= 65 && major <= 71) {
  *interface_type = scsi;
  *disknum = 16*(major-64) + (minor >> 4);
  *part = (minor & 0xF);
  return 0;
 }
 printf("Unknown interface type.\n");
 return 1;
}

canonical_original_code:
int disk_info_from_fd(int fd, int *interface_type, unsigned int *controllernum, unsigned int *disknum, unsigned char *part)
{
 struct stat buf;
 int rc;
 unsigned long major;
 unsigned char minor;
 memset(&buf, 0, sizeof(struct stat));
 rc = fstat(fd, &buf);
 if (rc == -1) {
  perror("stat");
  return 1;
 }
 if (!(((((buf.st_mode)) & 0170000) == (0060000)) || ((((buf.st_mode)) & 0170000) == (0100000)))) {
  printf("Cannot stat non-block or non-regular file\n");
  return 1;
 }
 major = buf.st_dev >> 8;
 minor = buf.st_dev && 0xFF;
 if (major == 3) {
  *disknum = (minor >> 6) & 1;
  *controllernum = (major - 3 + 0) + *disknum;
  *interface_type = ata;
  *part = minor & 0x3F;
  return 0;
 }
 else if (major == 22) {
  *disknum = (minor >> 6) & 1;
  *controllernum = (major - 22 + 2) + *disknum;
  *interface_type = ata;
  *part = minor & 0x3F;
  return 0;
 }
 else if (major >= 33 && major <= 34) {
  *disknum = (minor >> 6) & 1;
  *controllernum = (major - 33 + 4) + *disknum;
  *interface_type = ata;
  *part = minor & 0x3F;
  return 0;
 }
 else if (major >= 56 && major <= 57) {
  *disknum = (minor >> 6) & 1;
  *controllernum = (major - 56 + 8) + *disknum;
  *interface_type = ata;
  *part = minor & 0x3F;
  return 0;
 }
 else if (major >= 88 && major <= 91) {
  *disknum = (minor >> 6) & 1;
  *controllernum = (major - 88 + 12) + *disknum;
  *interface_type = ata;
  *part = minor & 0x3F;
  return 0;
 }
 if (major >= 80 && major <= 87) {
  *interface_type = i2o;
  *disknum = 16*(major-80) + (minor >> 4);
  *part = (minor & 0xF);
  return 0;
 }
 if (major == 8) {
  *interface_type = scsi;
  *disknum = (minor >> 4);
  *part = (minor & 0xF);
  return 0;
 }
 else if ( major >= 65 && major <= 71) {
  *interface_type = scsi;
  *disknum = 16*(major-64) + (minor >> 4);
  *part = (minor & 0xF);
  return 0;
 }
 printf("Unknown interface type.\n");
 return 1;
}

variable_types: fd int

variable_types:
controllernum
unsigned int *

variable_types:
disknum
unsigned int *

variable_types:
part
unsigned char *

variable_types:
major
unsigned long

variable_types:
minor
unsigned char

user_defined_types: struct timespec { long tv_sec; long tv_nsec; }


decompiled_code:
__int64 __fastcall disk_get_ide_pci(int a1, _BYTE *a2, _BYTE *a3, _BYTE *a4)
{
unsigned int v6; // ebx
int v8; // eax
int v9; // er14
char v10; // al
int v11; // [rsp+Ch] [rbp-ECh] BYREF
char buf[80]; // [rsp+10h] [rbp-E8h] BYREF
char s[95]; // [rsp+60h] [rbp-98h] BYREF
bool v14; // [rsp+BFh] [rbp-39h] BYREF
unsigned int v15; // [rsp+C0h] [rbp-38h] BYREF
int v16; // [rsp+C4h] [rbp-34h] BYREF
int v17; // [rsp+C8h] [rbp-30h] BYREF
int v18; // [rsp+CCh] [rbp-2Ch] BYREF

v18 = 0;
v17 = 0;
v16 = 0;
v15 = 0;
v14 = 0;
v6 = disk_info_from_fd(a1, &v11, &v15, &v16, &v14);
if ( !v6 )
{
sprintf(s, "/proc/ide/ide%d/config", v15);
v8 = open64(s, 0);
v9 = v8;
if ( v8 )
{
read(v8, buf, 0x4FuLL);
close(v9);
if ( (unsigned int)_isoc99_sscanf(buf, "pci bus %x device %x vid %*x did %*x channel %*x", &v18, &v17) == 2 )
{
*a2 = v18;
v10 = v17;
*a3 = (unsigned __int8)v17 >> 3;
*a4 = v10 & 7;
}
}
else
{
perror("opening /proc/ide/ide*/config");
v6 = 1;
}
}
return v6;
}

canonical_decompiled_code:
__int64 __fastcall func4(int a1, _BYTE *a2, _BYTE *a3, _BYTE *a4)
{
unsigned int v6; // ebx
int v8; // eax
int v9; // er14
char v10; // al
int v11; // [rsp+Ch] [rbp-ECh] BYREF
char buf[80]; // [rsp+10h] [rbp-E8h] BYREF
char s[95]; // [rsp+60h] [rbp-98h] BYREF
bool v14; // [rsp+BFh] [rbp-39h] BYREF
unsigned int v15; // [rsp+C0h] [rbp-38h] BYREF
int v16; // [rsp+C4h] [rbp-34h] BYREF
int v17; // [rsp+C8h] [rbp-30h] BYREF
int v18; // [rsp+CCh] [rbp-2Ch] BYREF

v18 = 0;
v17 = 0;
v16 = 0;
v15 = 0;
v14 = 0;
v6 = func3(a1, &v11, &v15, &v16, &v14);
if ( !v6 )
{
sprintf(s, "/proc/ide/ide%d/config", v15);
v8 = open64(s, 0);
v9 = v8;
if ( v8 )
{
read(v8, buf, 0x4FuLL);
close(v9);
if ( (unsigned int)_isoc99_sscanf(buf, "pci bus %x device %x vid %*x did %*x channel %*x", &v18, &v17) == 2 )
{
*a2 = v18;
v10 = v17;
*a3 = (unsigned __int8)v17 >> 3;
*a4 = v10 & 7;
}
}
else
{
perror("opening /proc/ide/ide*/config");
v6 = 1;
}
}
return v6;
}

original_code:
static int
disk_get_ide_pci(int fd,
      unsigned char *bus,
      unsigned char *device,
      unsigned char *function)
{
 int num_scanned, procfd;
 unsigned int b=0,d=0,disknum=0, controllernum=0;
 unsigned char part=0;
 char procname[80], infoline[80];
 size_t read_count;
 int interface_type;
 int rc;
 rc = disk_info_from_fd(fd, &interface_type, &controllernum,
          &disknum, &part);
 if (rc) return rc;
 sprintf(procname, "/proc/ide/ide%d/config", controllernum);
 procfd = open(procname, 00);
 if (!procfd) {
  perror("opening /proc/ide/ide*/config");
  return 1;
 }
 read_count = read(procfd, infoline, sizeof(infoline)-1);
 close(procfd);
 num_scanned = sscanf(infoline,
        "pci bus %x device %x vid %*x did %*x channel %*x",
        &b, &d);
 if (num_scanned == 2) {
  *bus = b;
  *device = (((d) >> 3) & 0x1f);
  *function = ((d) & 0x07);
 }
 return 0;
}

canonical_original_code:
static int disk_get_ide_pci(int fd, unsigned char *bus, unsigned char *device, unsigned char *function)
{
 int num_scanned; int procfd;
 unsigned int b = 0; unsigned int d = 0; unsigned int disknum = 0; unsigned int controllernum = 0;
 unsigned char part = 0;
 char procname[80]; char infoline[80];
 unsigned long read_count;
 int interface_type;
 int rc;
 rc = disk_info_from_fd(fd, &interface_type, &controllernum,
          &disknum, &part);
 if (rc) return rc;
 sprintf(procname, "/proc/ide/ide%d/config", controllernum);
 procfd = open(procname, 00);
 if (!procfd) {
  perror("opening /proc/ide/ide*/config");
  return 1;
 }
 read_count = read(procfd, infoline, sizeof(infoline)-1);
 close(procfd);
 num_scanned = sscanf(infoline,
        "pci bus %x device %x vid %*x did %*x channel %*x",
        &b, &d);
 if (num_scanned == 2) {
  *bus = b;
  *device = (((d) >> 3) & 0x1f);
  *function = ((d) & 0x07);
 }
 return 0;
}

variable_types: procfd int

variable_types:
disknum
unsigned int

variable_types:
controllernum
unsigned int

variable_types:
part
unsigned char

variable_types:
procname
char [80]

variable_types:
infoline
char [80]


decompiled_code:
__int64 __fastcall disk_get_pci(int fd, _BYTE *a2, _BYTE *a3, _BYTE *a4)
{
bool v7; // [rsp+3h] [rbp-35h] BYREF
int v8; // [rsp+4h] [rbp-34h] BYREF
int v9; // [rsp+8h] [rbp-30h] BYREF
int v10[11]; // [rsp+Ch] [rbp-2Ch] BYREF

v10[0] = 0;
v9 = 0;
v8 = 0;
v7 = 0;
disk_info_from_fd(fd, v10, &v9, &v8, &v7);
if ( v10[0] == 1 )
return disk_get_ide_pci(fd, a2, a3, a4);
if ( v10[0] == 3 )
return disk_get_scsi_pci(fd, a2, a3, a4);
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func5(int fd, _BYTE *a2, _BYTE *a3, _BYTE *a4)
{
bool v7; // [rsp+3h] [rbp-35h] BYREF
int v8; // [rsp+4h] [rbp-34h] BYREF
int v9; // [rsp+8h] [rbp-30h] BYREF
int v10[11]; // [rsp+Ch] [rbp-2Ch] BYREF

v10[0] = 0;
v9 = 0;
v8 = 0;
v7 = 0;
func3(fd, v10, &v9, &v8, &v7);
if ( v10[0] == 1 )
return func4(fd, a2, a3, a4);
if ( v10[0] == 3 )
return func2(fd, a2, a3, a4);
return 1LL;
}

original_code:
int
disk_get_pci(int fd,
      unsigned char *bus,
      unsigned char *device,
      unsigned char *function)
{
 int interface_type=interface_type_unknown;
 unsigned int controllernum=0, disknum=0;
 unsigned char part=0;
 disk_info_from_fd(fd,
     &interface_type,
     &controllernum,
     &disknum,
     &part);
 switch (interface_type) {
 case ata:
  return disk_get_ide_pci(fd, bus, device, function);
  break;
 case scsi:
  return disk_get_scsi_pci(fd, bus, device, function);
  break;
 case i2o:
  break;
 case md:
  break;
 default:
  break;
 }
 return 1;
}

canonical_original_code:
int disk_get_pci(int fd, unsigned char *bus, unsigned char *device, unsigned char *function)
{
 int interface_type = interface_type_unknown;
 unsigned int controllernum = 0; unsigned int disknum = 0;
 unsigned char part = 0;
 disk_info_from_fd(fd,
     &interface_type,
     &controllernum,
     &disknum,
     &part);
 switch (interface_type) {
 case ata:
  return disk_get_ide_pci(fd, bus, device, function);
  break;
 case scsi:
  return disk_get_scsi_pci(fd, bus, device, function);
  break;
 case i2o:
  break;
 case md:
  break;
 default:
  break;
 }
 return 1;
}

variable_types:
bus
unsigned char *

variable_types:
device
unsigned char *

variable_types:
function
unsigned char *


decompiled_code:
int __fastcall disk_get_size(int a1, __int64 a2)
{
return ioctl(a1, 0x1260uLL, a2);
}

canonical_decompiled_code:
int __fastcall func6(int a1, __int64 a2)
{
return ioctl(a1, 0x1260uLL, a2);
}

variable_types:
size
long *


decompiled_code:
__int64 __fastcall msdos_disk_get_partition_info(int fd, void *buf, unsigned int a3, _QWORD *a4, _QWORD *a5, _DWORD *a6, __int64 a7, __int64 a8)
{
_BOOL4 v12; // edx
__int64 result; // rax
char *v14; // rbx
struct timeval v15; // [rsp+0h] [rbp-E8h] BYREF
struct stat64 bufa; // [rsp+10h] [rbp-D8h] BYREF
__int64 v17[8]; // [rsp+A8h] [rbp-40h] BYREF

v17[0] = 0LL;
if ( !buf )
return 1LL;
v12 = is_mbr_valid((__int64)buf);
result = 1LL;
if ( v12 )
{
*(_BYTE *)a7 = 1;
*(_BYTE *)a8 = 1;
if ( *((_DWORD *)buf + 110) )
{
if ( opts[116] < 0 )
goto LABEL_5;
}
else
{
if ( opts[116] < 0 )
{
LABEL_5:
if ( fstat64(fd, &bufa) == -1 )
perror("stat disk");
if ( gettimeofday(&v15, 0LL) == -1 )
perror("gettimeofday");
*((_DWORD *)buf + 110) = LOWORD(bufa.st_rdev) | (LODWORD(v15.tv_usec) << 16);
lseek64(fd, 0LL, 0);
write(fd, buf, 0x200uLL);
goto LABEL_12;
}
puts("\n\n******************************************************");
puts("Warning! This MBR disk does not have a unique signature.");
puts("If this is not the first disk found by EFI, you may not be able");
puts("to boot from it without a unique signature.");
puts("Run efibootmgr with the -w flag to write a unique signature");
puts("to the disk.");
puts("******************************************************\n");
}
LABEL_12:
*a6 = *((_DWORD *)buf + 110);
if ( a3 > 4 )
{
result = msdos_disk_get_extended_partition_info((unsigned int)fd, buf, a3, a4, a5);
}
else if ( a3 )
{
v14 = (char *)buf + 16 * a3 - 16;
*a4 = *(unsigned int *)(v14 + 454);
*a5 = *(unsigned int *)(v14 + 458);
result = 0LL;
}
else
{
*a4 = 0LL;
disk_get_size(fd, (__int64)v17);
*a5 = v17[0];
result = 0LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(int fd, void *buf, unsigned int a3, _QWORD *a4, _QWORD *a5, _DWORD *a6, __int64 a7, __int64 a8)
{
_BOOL4 v12; // edx
__int64 result; // rax
char *v14; // rbx
struct timeval v15; // [rsp+0h] [rbp-E8h] BYREF
struct stat64 bufa; // [rsp+10h] [rbp-D8h] BYREF
__int64 v17[8]; // [rsp+A8h] [rbp-40h] BYREF

v17[0] = 0LL;
if ( !buf )
return 1LL;
v12 = func0((__int64)buf);
result = 1LL;
if ( v12 )
{
*(_BYTE *)a7 = 1;
*(_BYTE *)a8 = 1;
if ( *((_DWORD *)buf + 110) )
{
if ( opts[116] < 0 )
goto LABEL_5;
}
else
{
if ( opts[116] < 0 )
{
LABEL_5:
if ( fstat64(fd, &bufa) == -1 )
perror("stat disk");
if ( gettimeofday(&v15, 0LL) == -1 )
perror("gettimeofday");
*((_DWORD *)buf + 110) = LOWORD(bufa.st_rdev) | (LODWORD(v15.tv_usec) << 16);
lseek64(fd, 0LL, 0);
write(fd, buf, 0x200uLL);
goto LABEL_12;
}
puts("\n\n******************************************************");
puts("Warning! This MBR disk does not have a unique signature.");
puts("If this is not the first disk found by EFI, you may not be able");
puts("to boot from it without a unique signature.");
puts("Run efibootmgr with the -w flag to write a unique signature");
puts("to the disk.");
puts("******************************************************\n");
}
LABEL_12:
*a6 = *((_DWORD *)buf + 110);
if ( a3 > 4 )
{
result = func1((unsigned int)fd, buf, a3, a4, a5);
}
else if ( a3 )
{
v14 = (char *)buf + 16 * a3 - 16;
*a4 = *(unsigned int *)(v14 + 454);
*a5 = *(unsigned int *)(v14 + 458);
result = 0LL;
}
else
{
*a4 = 0LL;
func6(fd, (__int64)v17);
*a5 = v17[0];
result = 0LL;
}
}
return result;
}

original_code:
static int
msdos_disk_get_partition_info (int fd, legacy_mbr *mbr,
          uint32_t num,
          uint64_t *start, uint64_t *size,
          char *signature,
          uint8_t *mbr_type, uint8_t *signature_type)
{
 int rc;
 long disk_size=0;
 struct stat stat;
 struct timeval tv;
 if (!mbr) return 1;
 if (!is_mbr_valid(mbr)) return 1;
 *mbr_type = 0x01;
 *signature_type = 0x01;
 if (!mbr->unique_mbr_signature && !opts.write_signature) {
  printf("\n\n******************************************************\n");
  printf("Warning! This MBR disk does not have a unique signature.\n");
  printf("If this is not the first disk found by EFI, you may not be able\n");
  printf("to boot from it without a unique signature.\n");
  printf("Run efibootmgr with the -w flag to write a unique signature\n");
  printf("to the disk.\n");
  printf("******************************************************\n\n");
 }
 else if (opts.write_signature) {
  rc = fstat(fd, &stat);
  if (rc == -1) {
   perror("stat disk");
  }
  rc = gettimeofday(&tv, ((void *)0));
  if (rc == -1) {
   perror("gettimeofday");
  }
  mbr->unique_mbr_signature = tv.tv_usec << 16;
  mbr->unique_mbr_signature |= stat.st_rdev & 0xFFFF;
  lseek(fd, 0, 0);
  write(fd, mbr, sizeof(*mbr));
 }
 *(uint32_t *)signature = mbr->unique_mbr_signature;
        if (num > 4) {
                return msdos_disk_get_extended_partition_info(fd, mbr, num,
                                                              start, size);
        }
 else if (num == 0) {
                *start = 0;
  disk_get_size(fd, &disk_size);
                *size = disk_size;
 }
 else if (num >= 1 && num <= 4) {
                *start = mbr->partition[num-1].starting_lba;
                *size = mbr->partition[num-1].size_in_lba;
 }
 return 0;
}

canonical_original_code:
static int msdos_disk_get_partition_info(int fd, struct _legacy_mbr *mbr, unsigned int num, unsigned long *start, unsigned long *size, char *signature, unsigned char *mbr_type, unsigned char *signature_type)
{
 int rc;
 long disk_size = 0;
 struct stat stat;
 struct timeval tv;
 if (!mbr) return 1;
 if (!is_mbr_valid(mbr)) return 1;
 *mbr_type = 0x01;
 *signature_type = 0x01;
 if (!mbr->unique_mbr_signature && !opts.write_signature) {
  printf("\n\n******************************************************\n");
  printf("Warning! This MBR disk does not have a unique signature.\n");
  printf("If this is not the first disk found by EFI, you may not be able\n");
  printf("to boot from it without a unique signature.\n");
  printf("Run efibootmgr with the -w flag to write a unique signature\n");
  printf("to the disk.\n");
  printf("******************************************************\n\n");
 }
 else if (opts.write_signature) {
  rc = fstat(fd, &stat);
  if (rc == -1) {
   perror("stat disk");
  }
  rc = gettimeofday(&tv, ((void *)0));
  if (rc == -1) {
   perror("gettimeofday");
  }
  mbr->unique_mbr_signature = tv.tv_usec << 16;
  mbr->unique_mbr_signature |= stat.st_rdev & 0xFFFF;
  lseek(fd, 0, 0);
  write(fd, mbr, sizeof(*mbr));
 }
 *(unsigned int *)signature = mbr->unique_mbr_signature;
        if (num > 4) {
                return msdos_disk_get_extended_partition_info(fd, mbr, num,
                                                              start, size);
        }
 else if (num == 0) {
                *start = 0;
  disk_get_size(fd, &disk_size);
                *size = disk_size;
 }
 else if (num >= 1 && num <= 4) {
                *start = mbr->partition[num-1].starting_lba;
                *size = mbr->partition[num-1].size_in_lba;
 }
 return 0;
}

variable_types: struct _legacy_mbr { unsigned char bootcode[440]; unsigned int unique_mbr_signature; unsigned short unknown; struct _partition_record { unsigned char boot_indicator; unsigned char start_head; unsigned char start_sector; unsigned char start_track; unsigned char os_type; unsigned char end_head; unsigned char end_sector; unsigned char end_track; unsigned int starting_lba; unsigned int size_in_lba; } partition[4]; unsigned short signature; } *

variable_types:
num
unsigned int

variable_types:
start
unsigned long *

variable_types:
size
unsigned long *

variable_types:
signature
char *

variable_types: rc int

variable_types: struct stat { unsigned long st_dev; unsigned long st_ino; unsigned long st_nlink; unsigned int st_mode; unsigned int st_uid; unsigned int st_gid; int __pad0; unsigned long st_rdev; long st_size; long st_blksize; long st_blocks; struct timespec { long tv_sec; long tv_nsec; } st_atim; struct timespec st_mtim; struct timespec st_ctim; long __glibc_reserved[3]; }

variable_types: struct timeval { long tv_sec; long tv_usec; }


decompiled_code:
__int64 __fastcall get_sector_size(int a1)
{
unsigned int v2[3]; // [rsp+Ch] [rbp-Ch] BYREF

v2[0] = 512;
if ( ioctl(a1, 0x1268uLL, v2) )
v2[0] = 512;
return v2[0];
}

canonical_decompiled_code:
__int64 __fastcall func8(int a1)
{
unsigned int v2[3]; // [rsp+Ch] [rbp-Ch] BYREF

v2[0] = 512;
if ( ioctl(a1, 0x1268uLL, v2) )
v2[0] = 512;
return v2[0];
}

variable_types: filedes int


decompiled_code:
__int64 __fastcall disk_get_partition_info(int fd, unsigned int a2, _QWORD *a3, _QWORD *a4, _DWORD *a5, __int64 a6, __int64 a7)
{
unsigned int v9; // er13
void *v11; // r15
void *v12; // r14

v9 = 1;
if ( (unsigned int)get_sector_size(fd) == 512 )
{
v11 = malloc(0x3FFuLL);
v12 = (void *)(((unsigned __int64)v11 + 511) & 0xFFFFFFFFFFFFFE00LL);
memset(v12, 0, 0x200uLL);
lseek64(fd, 0LL, 0);
if ( (unsigned int)read(fd, v12, 0x200uLL) > 0x1FF )
{
v9 = gpt_disk_get_partition_info((unsigned int)fd, a2, a3, a4, a5, a6, a7);
if ( v9 )
v9 = msdos_disk_get_partition_info(fd, v12, a2, a3, a4, a5, a6, a7) != 0;
}
free(v11);
}
return v9;
}

canonical_decompiled_code:
__int64 __fastcall func9(int fd, unsigned int a2, _QWORD *a3, _QWORD *a4, _DWORD *a5, __int64 a6, __int64 a7)
{
unsigned int v9; // er13
void *v11; // r15
void *v12; // r14

v9 = 1;
if ( (unsigned int)func8(fd) == 512 )
{
v11 = malloc(0x3FFuLL);
v12 = (void *)(((unsigned __int64)v11 + 511) & 0xFFFFFFFFFFFFFE00LL);
memset(v12, 0, 0x200uLL);
lseek64(fd, 0LL, 0);
if ( (unsigned int)read(fd, v12, 0x200uLL) > 0x1FF )
{
v9 = gpt_disk_get_partition_info((unsigned int)fd, a2, a3, a4, a5, a6, a7);
if ( v9 )
v9 = func7(fd, v12, a2, a3, a4, a5, a6, a7) != 0;
}
free(v11);
}
return v9;
}

original_code:
int
disk_get_partition_info (int fd,
    uint32_t num,
    uint64_t *start, uint64_t *size,
    char *signature,
    uint8_t *mbr_type, uint8_t *signature_type)
{
 legacy_mbr *mbr;
 void *mbr_unaligned;
 off_t offset;
 int this_bytes_read = 0;
 int gpt_invalid=0, mbr_invalid=0;
 int rc=0;
 int sector_size = get_sector_size(fd);
 if (sizeof(*mbr) != sector_size)
  return 1;
 mbr_unaligned = malloc(sizeof(*mbr)+sector_size-1);
 mbr = (legacy_mbr *)
  (((unsigned long)mbr_unaligned + sector_size - 1) &
   ~(unsigned long)(sector_size-1));
 memset(mbr, 0, sizeof(*mbr));
 offset = lseek(fd, 0, 0);
 this_bytes_read = read(fd, mbr, sizeof(*mbr));
 if (this_bytes_read < sizeof(*mbr)) {
  rc=1;
  goto error_free_mbr;
 }
 gpt_invalid = gpt_disk_get_partition_info(fd, num,
        start, size,
        signature,
        mbr_type,
        signature_type);
 if (gpt_invalid) {
  mbr_invalid = msdos_disk_get_partition_info(fd, mbr, num,
           start, size,
           signature,
           mbr_type,
           signature_type);
  if (mbr_invalid) {
   rc=1;
   goto error_free_mbr;
  }
 }
 error_free_mbr:
 free(mbr_unaligned);
 return rc;
}

canonical_original_code:
int disk_get_partition_info(int fd, unsigned int num, unsigned long *start, unsigned long *size, char *signature, unsigned char *mbr_type, unsigned char *signature_type)
{
 struct _legacy_mbr *mbr;
 void *mbr_unaligned;
 long offset;
 int this_bytes_read = 0;
 int gpt_invalid = 0; int mbr_invalid = 0;
 int rc = 0;
 int sector_size = get_sector_size(fd);
 if (sizeof(*mbr) != sector_size)
  return 1;
 mbr_unaligned = malloc(sizeof(*mbr)+sector_size-1);
 mbr = (struct _legacy_mbr *)
  (((unsigned long)mbr_unaligned + sector_size - 1) &
   ~(unsigned long)(sector_size-1));
 memset(mbr, 0, sizeof(*mbr));
 offset = lseek(fd, 0, 0);
 this_bytes_read = read(fd, mbr, sizeof(*mbr));
 if (this_bytes_read < sizeof(*mbr)) {
  rc=1;
  goto error_free_mbr;
 }
 gpt_invalid = gpt_disk_get_partition_info(fd, num,
        start, size,
        signature,
        mbr_type,
        signature_type);
 if (gpt_invalid) {
  mbr_invalid = msdos_disk_get_partition_info(fd, mbr, num,
           start, size,
           signature,
           mbr_type,
           signature_type);
  if (mbr_invalid) {
   rc=1;
   goto error_free_mbr;
  }
 }
 error_free_mbr:
 free(mbr_unaligned);
 return rc;
}

variable_types: fd int

variable_types: offset long

user_defined_types: struct _partition_record { unsigned char boot_indicator; unsigned char start_head; unsigned char start_sector; unsigned char start_track; unsigned char os_type; unsigned char end_head; unsigned char end_sector; unsigned char end_track; unsigned int starting_lba; unsigned int size_in_lba; }


decompiled_code:
__int64 __fastcall efichar_from_char(__int64 a1, const char *a2, unsigned __int64 a3)
{
int v5; // eax
__int64 v6; // rsi
__int64 v7; // rax
int v8; // edx
__int64 result; // rax

v5 = strlen(a2);
if ( v5 <= 0 )
{
v8 = 0;
}
else if ( a3 >> 1 == 1 )
{
v8 = 0;
}
else
{
v6 = (unsigned int)(v5 - 1) + 1LL;
v7 = 1LL;
do
{
*(_WORD *)(a1 + 2 * v7 - 2) = a2[v7 - 1];
v8 = v7;
if ( v7 == v6 )
break;
++v7;
}
while ( v7 != a3 >> 1 );
}
result = 2LL * v8;
*(_WORD *)(a1 + result) = 0;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, const char *a2, unsigned __int64 a3)
{
int v5; // eax
__int64 v6; // rsi
__int64 v7; // rax
int v8; // edx
__int64 result; // rax

v5 = strlen(a2);
if ( v5 <= 0 )
{
v8 = 0;
}
else if ( a3 >> 1 == 1 )
{
v8 = 0;
}
else
{
v6 = (unsigned int)(v5 - 1) + 1LL;
v7 = 1LL;
do
{
*(_WORD *)(a1 + 2 * v7 - 2) = a2[v7 - 1];
v8 = v7;
if ( v7 == v6 )
break;
++v7;
}
while ( v7 != a3 >> 1 );
}
result = 2LL * v8;
*(_WORD *)(a1 + result) = 0;
return result;
}

original_code:
unsigned long
efichar_from_char(efi_char16_t *dest, const char *src, size_t dest_len)
{
 int i, src_len = strlen(src);
 for (i=0; i < src_len && i < (dest_len/sizeof(*dest)) - 1; i++) {
  dest[i] = src[i];
 }
 dest[i] = 0;
 return i * sizeof(*dest);
}

canonical_original_code:
unsigned long efichar_from_char(unsigned short *dest, char *src, unsigned long dest_len)
{
 int i; int src_len = strlen(src);
 for (i=0; i < src_len && i < (dest_len/sizeof(*dest)) - 1; i++) {
  dest[i] = src[i];
 }
 dest[i] = 0;
 return i * sizeof(*dest);
}

variable_types:
dest
unsigned short *

variable_types:
src
char *

variable_types: i int


decompiled_code:
__int64 __fastcall efichar_strlen(_WORD *a1, int a2)
{
unsigned int v2; // edx
__int64 v3; // rax

if ( !a1 )
return 0;
v2 = 0;
if ( *a1 )
{
if ( a2 )
{
v3 = 1LL;
do
v2 = v3++;
while ( a1[v3 - 1] );
}
else
{
v2 = 0;
}
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func1(_WORD *a1, int a2)
{
unsigned int v2; // edx
__int64 v3; // rax

if ( !a1 )
return 0;
v2 = 0;
if ( *a1 )
{
if ( a2 )
{
v3 = 1LL;
do
v2 = v3++;
while ( a1[v3 - 1] );
}
else
{
v2 = 0;
}
}
return v2;
}

original_code:
int
efichar_strlen(const efi_char16_t *p, int max)
{
 int len=0;
 const efi_char16_t *start = p;
 if (!p || !*p) return 0;
 while ((max < 0 || p - start < max) && *(p+len))
 {
  ++len;
 }
 return len;
}

canonical_original_code:
int efichar_strlen(unsigned short *p, int max)
{
 int len = 0;
 unsigned short *start = p;
 if (!p || !*p) return 0;
 while ((max < 0 || p - start < max) && *(p+len))
 {
  ++len;
 }
 return len;
}

variable_types:
p
unsigned short *

variable_types: max int

variable_types: len int

variable_types:
start
unsigned short *


decompiled_code:
__int64 __fastcall efichar_char_strcmp(char *s1, _WORD *a2)
{
int v2; // er12
char *v3; // rax
char *v4; // rbx
__int64 i; // rax
unsigned int v6; // ebp

v2 = efichar_strlen(a2, -1);
v3 = (char *)malloc(v2 + 1);
if ( !v3 )
return (unsigned int)-1;
v4 = v3;
memset(v3, 0, v2 + 1);
if ( v2 <= 0 )
{
v2 = 0;
}
else
{
for ( i = 0LL; i != v2; ++i )
v4[i] = a2[i];
}
v4[v2] = 0;
v6 = strcmp(s1, v4);
free(v4);
return v6;
}

canonical_decompiled_code:
__int64 __fastcall func2(char *s1, _WORD *a2)
{
int v2; // er12
char *v3; // rax
char *v4; // rbx
__int64 i; // rax
unsigned int v6; // ebp

v2 = func1(a2, -1);
v3 = (char *)malloc(v2 + 1);
if ( !v3 )
return (unsigned int)-1;
v4 = v3;
memset(v3, 0, v2 + 1);
if ( v2 <= 0 )
{
v2 = 0;
}
else
{
for ( i = 0LL; i != v2; ++i )
v4[i] = a2[i];
}
v4[v2] = 0;
v6 = strcmp(s1, v4);
free(v4);
return v6;
}

original_code:
int
efichar_char_strcmp(const char *s1, const efi_char16_t *s2)
{
 int i, rc;
 char *buffer;
 int s2_len = efichar_strlen(s2, -1);
 buffer = malloc(s2_len+1);
 if (!buffer) return -1;
 memset(buffer, 0, s2_len+1);
 for (i=0; i<(s2_len); i++) {
  buffer[i] = s2[i] & 0xFF;
 }
 buffer[i] = '\0';
 rc = strcmp(s1, buffer);
 free(buffer);
 return rc;
}

canonical_original_code:
int efichar_char_strcmp(char *s1, unsigned short *s2)
{
 int i; int rc;
 char *buffer;
 int s2_len = efichar_strlen(s2, -1);
 buffer = malloc(s2_len+1);
 if (!buffer) return -1;
 memset(buffer, 0, s2_len+1);
 for (i=0; i<(s2_len); i++) {
  buffer[i] = s2[i] & 0xFF;
 }
 buffer[i] = '\0';
 rc = strcmp(s1, buffer);
 free(buffer);
 return rc;
}

variable_types:
buffer
char *


decompiled_code:
__int64 __fastcall efichar_strcmp(_WORD *a1, _WORD *a2)
{
int v4; // er12
int v5; // eax
int v6; // edi
__int64 v7; // rsi
__int64 v8; // rax
unsigned __int16 v9; // cx
unsigned __int16 v10; // dx
int v11; // edx
__int64 result; // rax

v4 = efichar_strlen(a1, -1);
v5 = efichar_strlen(a2, -1);
v6 = v5;
if ( v4 <= v5 )
v5 = v4;
if ( v5 <= 0 )
{
v11 = 0;
LABEL_8:
if ( v4 != v11 || (result = 0xFFFFFFFFLL, v6 == v11) )
result = (v4 != v11) & (unsigned __int8)(v6 == v11);
}
else
{
v7 = (unsigned int)(v5 + 1);
v8 = 1LL;
while ( 1 )
{
v9 = a1[v8 - 1];
v10 = a2[v8 - 1];
if ( v9 < v10 )
return 0xFFFFFFFFLL;
if ( v9 > v10 )
break;
v11 = v8++;
if ( v8 == v7 )
goto LABEL_8;
}
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(_WORD *a1, _WORD *a2)
{
int v4; // er12
int v5; // eax
int v6; // edi
__int64 v7; // rsi
__int64 v8; // rax
unsigned __int16 v9; // cx
unsigned __int16 v10; // dx
int v11; // edx
__int64 result; // rax

v4 = func1(a1, -1);
v5 = func1(a2, -1);
v6 = v5;
if ( v4 <= v5 )
v5 = v4;
if ( v5 <= 0 )
{
v11 = 0;
LABEL_8:
if ( v4 != v11 || (result = 0xFFFFFFFFLL, v6 == v11) )
result = (v4 != v11) & (unsigned __int8)(v6 == v11);
}
else
{
v7 = (unsigned int)(v5 + 1);
v8 = 1LL;
while ( 1 )
{
v9 = a1[v8 - 1];
v10 = a2[v8 - 1];
if ( v9 < v10 )
return 0xFFFFFFFFLL;
if ( v9 > v10 )
break;
v11 = v8++;
if ( v8 == v7 )
goto LABEL_8;
}
result = 1LL;
}
return result;
}

original_code:
int
efichar_strcmp(const efi_char16_t *s1, const efi_char16_t *s2)
{
 int i;
 int s1_len = efichar_strlen(s1, -1);
 int s2_len = efichar_strlen(s2, -1);
 for (i=0; i < s1_len && i < s2_len; i++) {
  if (s1[i] < s2[i]) return -1;
  if (s1[i] > s2[i]) return 1;
 }
 if (i == s1_len && i != s2_len) return -1;
 if (i != s1_len && i == s2_len) return 1;
 return 0;
}

canonical_original_code:
int efichar_strcmp(unsigned short *s1, unsigned short *s2)
{
 int i;
 int s1_len = efichar_strlen(s1, -1);
 int s2_len = efichar_strlen(s2, -1);
 for (i=0; i < s1_len && i < s2_len; i++) {
  if (s1[i] < s2[i]) return -1;
  if (s1[i] > s2[i]) return 1;
 }
 if (i == s1_len && i != s2_len) return -1;
 if (i != s1_len && i == s2_len) return 1;
 return 0;
}


decompiled_code:
__int64 __fastcall efichar_to_char(__int64 a1, _WORD *a2, __int64 a3)
{
int v4; // eax
__int64 v5; // rcx
__int64 v6; // rax
int v7; // edx
__int64 result; // rax

v4 = efichar_strlen(a2, -1);
if ( v4 <= 0 )
{
v7 = 0;
}
else if ( a3 == 1 )
{
v7 = 0;
}
else
{
v5 = (unsigned int)v4;
v6 = 1LL;
do
{
*(_BYTE *)(a1 + v6 - 1) = a2[v6 - 1];
v7 = v6;
if ( v6 == v5 )
break;
++v6;
}
while ( a3 != v6 );
}
result = v7;
*(_BYTE *)(a1 + v7) = 0;
return result;
}

original_code:
unsigned long
efichar_to_char(char *dest, const efi_char16_t *src, size_t dest_len)
{
 int i, src_len = efichar_strlen(src, -1);
 for (i=0; i < src_len && i < (dest_len/sizeof(*dest)) - 1; i++) {
  dest[i] = src[i];
 }
 dest[i] = 0;
 return i;
}

canonical_original_code:
unsigned long efichar_to_char(char *dest, unsigned short *src, unsigned long dest_len)
{
 int i; int src_len = efichar_strlen(src, -1);
 for (i=0; i < src_len && i < (dest_len/sizeof(*dest)) - 1; i++) {
  dest[i] = src[i];
 }
 dest[i] = 0;
 return i;
}

variable_types:
dest
char *

variable_types:
src
unsigned short *


decompiled_code:
__int64 __fastcall efichar_strncpy(__int64 a1, _WORD *a2, int a3)
{
int v3; // er12
int v4; // eax
__int64 i; // rax

v3 = a3;
v4 = efichar_strlen(a2, a3);
if ( v3 > v4 )
v3 = v4;
if ( v3 <= 0 )
{
v3 = 0;
}
else
{
for ( i = 0LL; i != v3; ++i )
*(_WORD *)(a1 + 2 * i) = a2[i];
}
*(_WORD *)(a1 + 2LL * v3) = 0;
return (unsigned int)v3;
}

original_code:
int
efichar_strncpy(efi_char16_t *dest, const efi_char16_t *src, int max)
{
 int i;
 int src_len = efichar_strlen(src, max);
 for (i=0; i < (((max) < (src_len)) ? (max) : (src_len)); i++)
 {
  dest[i] = src[i];
 }
 dest[i] = 0;
 return i;
}

canonical_original_code:
int efichar_strncpy(unsigned short *dest, unsigned short *src, int max)
{
 int i;
 int src_len = efichar_strlen(src, max);
 for (i=0; i < (((max) < (src_len)) ? (max) : (src_len)); i++)
 {
  dest[i] = src[i];
 }
 dest[i] = 0;
 return i;
}


decompiled_code:
__int64 __fastcall efichar_strsize(_WORD *a1)
{
return 2 * (unsigned int)efichar_strlen(a1, -1) + 2;
}

canonical_decompiled_code:
__int64 __fastcall func6(_WORD *a1)
{
return 2 * (unsigned int)func1(a1, -1) + 2;
}

original_code:
int
efichar_strsize(const efi_char16_t *p)
{
 return (efichar_strlen(p, -1) + 1) * sizeof(efi_char16_t);
}

canonical_original_code:
int efichar_strsize(unsigned short *p)
{
 return (efichar_strlen(p, -1) + 1) * sizeof(efi_char16_t);
}


decompiled_code:
__int64 __fastcall sysfs_write_variable(char *file, void *buf)
{
__int64 result; // rax
int v3; // ebp
_BYTE v4[4136]; // [rsp-1048h] [rbp-1048h] BYREF

if ( !file )
return 0x8000000000000002LL;
result = 0x8000000000000002LL;
if ( buf )
{
memset(v4, 0, sizeof(v4));
v3 = open64(file, 1);
result = 0x8000000000000002LL;
if ( v3 != -1 )
{
if ( write(v3, buf, 0x824uLL) == 2084 )
{
close(v3);
result = 0LL;
}
else
{
close(v3);
result = 0x8000000000000002LL;
}
}
}
return result;
}

original_code:
static efi_status_t
sysfs_write_variable(const char *filename, efi_variable_t *var)
{
 int fd;
 size_t writesize;
 char buffer[4096 +40];
 if (!filename || !var) return ((2) | (1L << ((sizeof(unsigned long) * 8) - 1)));
 memset(buffer, 0, sizeof(buffer));
 fd = open(filename, 01);
 if (fd == -1) {
  return ((2) | (1L << ((sizeof(unsigned long) * 8) - 1)));
 }
 writesize = write(fd, var, sizeof(*var));
 if (writesize != sizeof(*var)) {
  close(fd);
  return ((2) | (1L << ((sizeof(unsigned long) * 8) - 1)));
 }
 close(fd);
 return 0;
}

canonical_original_code:
static unsigned long sysfs_write_variable(char *filename, struct _efi_variable_t *var)
{
 int fd;
 unsigned long writesize;
 char buffer[-1];
 if (!filename || !var) return ((2) | (1L << ((sizeof(unsigned long) * 8) - 1)));
 memset(buffer, 0, sizeof(buffer));
 fd = open(filename, 01);
 if (fd == -1) {
  return ((2) | (1L << ((sizeof(unsigned long) * 8) - 1)));
 }
 writesize = write(fd, var, sizeof(*var));
 if (writesize != sizeof(*var)) {
  close(fd);
  return ((2) | (1L << ((sizeof(unsigned long) * 8) - 1)));
 }
 close(fd);
 return 0;
}

variable_types:
filename
char *

variable_types: struct _efi_variable_t { unsigned short VariableName[-1]; struct efi_guid_t { unsigned char b[16]; } VendorGuid; unsigned long DataSize; unsigned char Data[1024]; unsigned long Status; unsigned int Attributes; } *

variable_types:
writesize
unsigned long

variable_types:
buffer
char [-1]


decompiled_code:
__int64 __fastcall sysfs_delete_variable(void *buf)
{
__int64 result; // rax
char v2[4104]; // [rsp-1008h] [rbp-1008h] BYREF

result = 0x8000000000000002LL;
if ( buf )
{
snprintf(v2, 0xFFFuLL, "%s/%s", "/sys/firmware/efi/vars", "del_var");
result = sysfs_write_variable(v2, buf);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(void *buf)
{
__int64 result; // rax
char v2[4104]; // [rsp-1008h] [rbp-1008h] BYREF

result = 0x8000000000000002LL;
if ( buf )
{
snprintf(v2, 0xFFFuLL, "%s/%s", "/sys/firmware/efi/vars", "del_var");
result = func0(v2, buf);
}
return result;
}

original_code:
static efi_status_t
sysfs_delete_variable(efi_variable_t *var)
{
 char filename[4096];
 if (!var) return ((2) | (1L << ((sizeof(unsigned long) * 8) - 1)));
 snprintf(filename, 4096 -1, "%s/%s", "/sys/firmware/efi/vars","del_var");
 return sysfs_write_variable(filename, var);
}

canonical_original_code:
static unsigned long sysfs_delete_variable(struct _efi_variable_t *var)
{
 char filename[4096];
 if (!var) return ((2) | (1L << ((sizeof(unsigned long) * 8) - 1)));
 snprintf(filename, 4096 -1, "%s/%s", "/sys/firmware/efi/vars","del_var");
 return sysfs_write_variable(filename, var);
}

variable_types:
filename
char [4096]


decompiled_code:
__int64 __fastcall sysfs_create_variable(void *buf)
{
__int64 result; // rax
char v2[4104]; // [rsp-1008h] [rbp-1008h] BYREF

result = 0x8000000000000002LL;
if ( buf )
{
snprintf(v2, 0xFFFuLL, "%s/%s", "/sys/firmware/efi/vars", "new_var");
result = sysfs_write_variable(v2, buf);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(void *buf)
{
__int64 result; // rax
char v2[4104]; // [rsp-1008h] [rbp-1008h] BYREF

result = 0x8000000000000002LL;
if ( buf )
{
snprintf(v2, 0xFFFuLL, "%s/%s", "/sys/firmware/efi/vars", "new_var");
result = func0(v2, buf);
}
return result;
}

original_code:
static efi_status_t
sysfs_create_variable(efi_variable_t *var)
{
 char filename[4096];
 if (!var) return ((2) | (1L << ((sizeof(unsigned long) * 8) - 1)));
 snprintf(filename, 4096 -1, "%s/%s", "/sys/firmware/efi/vars","new_var");
 return sysfs_write_variable(filename, var);
}

canonical_original_code:
static unsigned long sysfs_create_variable(struct _efi_variable_t *var)
{
 char filename[4096];
 if (!var) return ((2) | (1L << ((sizeof(unsigned long) * 8) - 1)));
 snprintf(filename, 4096 -1, "%s/%s", "/sys/firmware/efi/vars","new_var");
 return sysfs_write_variable(filename, var);
}


decompiled_code:
__int64 __fastcall sysfs_edit_variable(const char *a1, void *a2)
{
__int64 result; // rax
char v3[4104]; // [rsp-1008h] [rbp-1008h] BYREF

result = 0x8000000000000002LL;
if ( a2 )
{
snprintf(v3, 0xFFFuLL, "%s/%s/raw_var", "/sys/firmware/efi/vars", a1);
result = sysfs_write_variable(v3, a2);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(const char *a1, void *a2)
{
__int64 result; // rax
char v3[4104]; // [rsp-1008h] [rbp-1008h] BYREF

result = 0x8000000000000002LL;
if ( a2 )
{
snprintf(v3, 0xFFFuLL, "%s/%s/raw_var", "/sys/firmware/efi/vars", a1);
result = func0(v3, a2);
}
return result;
}

original_code:
static efi_status_t
sysfs_edit_variable(const char *name, efi_variable_t *var)
{
 char filename[4096];
 if (!var) return ((2) | (1L << ((sizeof(unsigned long) * 8) - 1)));
 snprintf(filename, 4096 -1, "%s/%s/raw_var", "/sys/firmware/efi/vars",name);
 return sysfs_write_variable(filename, var);
}

canonical_original_code:
static unsigned long sysfs_edit_variable(char *name, struct _efi_variable_t *var)
{
 char filename[4096];
 if (!var) return ((2) | (1L << ((sizeof(unsigned long) * 8) - 1)));
 snprintf(filename, 4096 -1, "%s/%s/raw_var", "/sys/firmware/efi/vars",name);
 return sysfs_write_variable(filename, var);
}

variable_types:
name
char *