// File: /workspace/github_c_makefile_star2_O1/Ziddykins[P]Dawn/src/main.c
// Function: main
int main (int argc, char **argv) {
    //For getops();
    int opt;

    //Default settings for the bot
    //These will be overwritten if a configuration file is found
    //or values are specified on the command line
    char *monsters = "monsters.raw";
    char *port     = "6667";
    char *server   = "irc.dal.net";
    //char *password = "none";
    int mflag, sflag, pflag;
    mflag = sflag = pflag = 0;

    //The bot structure it self
    CALLEXIT(!(dawn = calloc(1, sizeof *dawn)))

    //Initial settings
    strncpy(dawn->nickname, "Dawn-19", MAX_NICK_LENGTH);
    strcpy(dawn->realname, "dongs");
    strcpy(dawn->ident,    "hehe");
    strcpy(dawn->password, "temp");
    strncpy(dawn->active_room, "#stacked", MAX_CHANNEL_LENGTH);

    //Load players
    persistent_load();

    while ((opt = getopt(argc, argv, "hm:n:p:r:s:")) != -1) {
        switch (opt) {
            case 'h': //print help
                print_usage(argv);
                return 0;
            case 'm': //raw monsters file
                CALLEXIT(!(monsters = malloc(strlen(optarg) + 1)))
                strcpy(monsters, optarg);
                mflag = 1;
                break;
            case 'n': //bot's nickname
                strncpy(dawn->nickname, optarg, MAX_NICK_LENGTH-1);
                break;
            case 'p': //server port
                CALLEXIT(!(port = malloc(strlen(optarg) + 1)))
                strcpy(port, optarg);
                pflag = 1;
                break;
            case 'r': //room to join
                dawn->active_room[0] = '#';
                strncpy(dawn->active_room+1, optarg, MAX_CHANNEL_LENGTH-2);
                break;
            case 's': //server to connect to
                CALLEXIT(!(server = malloc(16))) //do we need IPV6 support?
                hostname_to_ip(optarg, server);  //network.c
                sflag = 1;
                break;
            case 'w': //nickserv password
                strncpy(dawn->password, optarg, 64-1);
                break;
            default:
                print_usage(argv);
                return 1;
        }
    }

    FILE *urandom;
    CALLEXIT(!(urandom = fopen("/dev/urandom", "r")))

    unsigned int seed;
    CALLEXIT(!(fread(&seed, sizeof (seed), 1, urandom)))
    srand(seed);
    fclose(urandom);

    FILE *auth_key_file;
    CALLEXIT(!(auth_key_file = fopen("auth_key.txt", "w")))
    CALLEXIT(!(auth_key = calloc(AUTH_KEY_LEN+1, 1)))

    for(size_t i = 0; i < AUTH_KEY_LEN; i++) {
        do {
            auth_key[i] = (char)(rand() % 32);
        } while(auth_key[i] >= 26);

        if (rand()%2) {
            auth_key[i] += 'a';
        } else {
            auth_key[i] += 'A';
        }
    }

    CALLEXIT(!(fwrite(auth_key, AUTH_KEY_LEN, sizeof *auth_key, auth_key_file)))

    fclose(auth_key_file);

    printf(INFO "auth_key: %s\n", auth_key);
    auth_key_valid = 1;

    int match;
    ssize_t len;

    if (access(monsters, F_OK) != -1) {
        FILE *file = fopen(monsters, "r");
        char line[MAX_MESSAGE_BUFFER];
        char name[MAX_MONSTER_LENGTH];
        int count = 0;
        int hp, str, def, intel, mdef, gold, exp, mhp, drop_level;

        if (!file) {
            fprintf(stderr, ERR "Error opening raw monsters\n");
            return 1;
        }
        while (fgets(line, sizeof(line), file)) {
            //TODO: Add ranges to raw files
            if (sscanf(line, "%[^:]:%d:%d:%d:%d:%d:%d:%d:%d:%d",
                    name, &hp, &str, &def, &intel, &mdef, &gold, &exp, &mhp, &drop_level) != 10) {
                fprintf(stderr, ERR "main: Malformed monster file at line %d\n", count);
                return 1;
            }
            strncpy(dawn->monsters[count].name, name, 64);
            dawn->monsters[count].hp         = hp;
            dawn->monsters[count].str        = str;
            dawn->monsters[count].def        = def;
            dawn->monsters[count].intel      = intel;
            dawn->monsters[count].mdef       = mdef;
            dawn->monsters[count].gold       = gold;
            dawn->monsters[count].exp        = exp;
            dawn->monsters[count].mhp        = mhp;
            dawn->monsters[count].drop_level = drop_level;
            dawn->monsters[count].slay_cost  = gold;
            dawn->monsters[count].active     = 0;
            dawn->monsters[count].type       = DEFMON;
            dawn->monsters[count].alignment  = NEUTRAL;
            count++;
        }
        fclose(file);
    }

    dawn->login_sent = 0;
    dawn->in_rooms   = 0;

    init_send_queue();
    init_cmds();

    if (init_connect_server(server, port) == 0) {
        while ((len = recv(con_socket, buffer, MAX_RECV_BUFFER, 0)) != -1) {
            char out[MAX_MESSAGE_BUFFER];
            buffer[len] = '\0';

            //Handle keepalive pings from the server
            if (matches_regex(buffer, "PING :(.*)")) {
                printf("PONG: %s\n", regex_group[1]);
                sprintf(out, "PONG :%s\r\n", regex_group[1]);
                add_msg(out, strlen(out));
            }

            //Connected to the server, send nick/user details
            if (dawn->login_sent == 0) {
                match = matches_regex(buffer, "AUTH") || matches_regex(buffer, "Looking up your hostname...");
                if (match) {
                    printf(INFO "Server checking for AUTH, sending nick/user\n");
                    handle_login(dawn->nickname, dawn->password, dawn->realname, dawn->ident);
                    dawn->login_sent = 1;
                }
            }

            //Nickname is in use, add a random suffix
            if (matches_regex(buffer, ":.*?\\s433\\s*\\s.*")) {
                int rand_suffix = rand() % 10;
                size_t nicklen = strlen(dawn->nickname);

                fprintf(stderr, WARN "Username %s in use\n", dawn->nickname);
                snprintf(dawn->nickname+nicklen, MAX_NICK_LENGTH-nicklen, "%d", rand_suffix);
                handle_login(dawn->nickname, dawn->password, dawn->realname, dawn->ident);
            }


            //If we're logged in and we're received a
            //welcome message, join the rooms
            if (!dawn->in_rooms && dawn->login_sent == 1) {
                match = matches_regex(buffer, ":(.*?)\\s001(.*)");

                if (match) {
                    printf(INFO "Got welcome message from server, joining rooms\n");
                    sprintf(out, "JOIN %s\r\n", dawn->active_room);
                    fluctuate_market();
                    add_msg(out, strlen(out));
                    dawn->in_rooms = 1;
                }
            }

            //Upon joining rooms the bot will search for status 353, which is names, and will
            //iterate through the online people, setting their status to available if they
            //have an account on the bot
            if (dawn->in_rooms) {
                struct Message message;

                //Set lengths based on server rather than assuming
                if (matches_regex(buffer, ":.*?005.*?CHANNELLEN=(\\d+)\\s.*?NICKLEN=(\\d+)")) {
                    MAX_CHANNEL_LENGTH = (unsigned int)strtoul(regex_group[1], 0, 10) + 1;
                    MAX_NICK_LENGTH    = (unsigned int)strtoul(regex_group[2], 0, 10) + 1;
                    dawn->nickname[MAX_NICK_LENGTH] = '\0';
                    dawn->active_room[MAX_CHANNEL_LENGTH] = '\0';
                }

                //Check if user is identified
                if (matches_regex(buffer, ":(.*?)\\s307\\s(.*?)\\s(.*?)\\s:")) {
                    int pindex = get_pindex(regex_group[3]);

                    if (pindex != -1 && (dawn->players[pindex].max_auth > AL_USER || dawn->players[pindex].auth_level > AL_NOAUTH)) {
                        if (dawn->players[pindex].max_auth < AL_REG) { //upgrade
                            dawn->players[pindex].max_auth = AL_REG;
                        }

                        if (dawn->players[pindex].auth_level < dawn->players[pindex].max_auth) {
                            dawn->players[pindex].auth_level = dawn->players[pindex].max_auth;
                            snprintf(out, MAX_MESSAGE_BUFFER, "PRIVMSG %s :%s has been verified. (%s)\r\n",
                                dawn->active_room,
                                dawn->players[pindex].username,
                                auth_level_to_str((enum auth_level)(dawn->players[pindex].auth_level)));
                            add_msg(out, strlen(out));
                        } else {
                            snprintf(out, MAX_MESSAGE_BUFFER, "PRIVMSG %s :%s is already verified. (%s)\r\n",
                                dawn->active_room,
                                dawn->players[pindex].username,
                                auth_level_to_str((enum auth_level)(dawn->players[pindex].auth_level)));
                            add_msg(out, strlen(out));
                        }
                    }
                }

                //NAMES (Status 353)
                //This will split up the received users in the room and set
                //their status availability to 1, stripping their name of status symbols
                //and converting their username to lowercase
                if (matches_regex(buffer, ":(.*?)\\s353\\s(.*?)\\s@\\s(.*?)\\s:(.*)")) {
                    char *ch_ptr;
                    ch_ptr = strtok(regex_group[4], " @&+");
                    while (ch_ptr != NULL) {
                        int index = get_pindex(to_lower(ch_ptr));
                        if (index != -1){
                            dawn->players[index].available = 1;
                            dawn->players[index].auth_level = AL_NOAUTH;
                            sprintf(out, "WHOIS %s\r\n", dawn->players[index].username);
                            add_msg(out, strlen(out));
                        }
                        ch_ptr = strtok(NULL, " @&+\r\n");
                    }
                }

                //Kicks
                if (matches_regex(buffer, ":(.*?)!~?(.*?)@(.*?)\\s(\\w+)\\s(.*?)\\s(.*?)\\s:(.*)")) {
                    if (strcmp(regex_group[4], "KICK") == 0) {
                        int index = get_pindex(to_lower(regex_group[6]));
                        if (index != -1) {
                            dawn->players[index].available = 0;
                            dawn->players[index].auth_level = AL_NOAUTH;
                        }
                    }
                }

                //Nick changes
                if (matches_regex(buffer, ":(.*?)!~?.*?@.*?\\sNICK\\s:(.*?)\r\n")) {
                    int oldindex = get_pindex(to_lower(regex_group[1]));
                    int newindex = get_pindex(to_lower(regex_group[2]));
                    if (oldindex != -1) {
                        dawn->players[oldindex].available = 0;
                        dawn->players[oldindex].auth_level = AL_NOAUTH;
                    }
                    if (newindex != -1) {
                        dawn->players[newindex].available = 1;
                        dawn->players[newindex].auth_level = AL_NOAUTH;
                    }
                }

                //Parting and joining
                if (matches_regex(buffer, ":(.*?)!~?(.*?)@(.*?)\\s(.*?)\\s(.*?)")) {
                    int pindex = get_pindex(to_lower(regex_group[1]));
                    if (pindex != -1) {
                        if (strcmp(regex_group[4], "PART") == 0) {
                            dawn->players[pindex].available = 0;
                            dawn->players[pindex].auth_level = AL_NOAUTH;
                        } else if (strcmp(regex_group[4], "JOIN") == 0) {
                            dawn->players[pindex].available = 1;
                            dawn->players[pindex].auth_level = AL_NOAUTH;
                            sprintf(out, "WHOIS %s\r\n", regex_group[1]);
                            add_msg(out, strlen(out));
                        } else if (strcmp(regex_group[4], "QUIT") == 0) {
                            dawn->players[pindex].available = 0;
                        }
                    }
                    if (dawn->player_count == 0) {
                        struct Message temp;
                        strncpy(temp.sender_nick, dawn->nickname, MAX_NICK_LENGTH);
                        strncpy(temp.sender_hostmask, regex_group[3], MAX_HOSTNAME_LENGTH);
                        init_new_character(&temp);
                    }
                }

                //Regular messages and notices
                if (matches_regex(buffer, ":(.*?)!~?(.*?)@(.*?)\\s(.*?)\\s(.*?)\\s:(.*)\r\n")) {
                    strncpy(message.sender_nick,     to_lower(regex_group[1]), MAX_NICK_LENGTH);
                    strncpy(message.sender_ident,    regex_group[2], MAX_IDENT_LENGTH);
                    strncpy(message.sender_hostmask, regex_group[3], MAX_HOSTNAME_LENGTH);
                    strncpy(message.receiver,        regex_group[5], MAX_NICK_LENGTH);
                    strncpy(message.message,         regex_group[6], MAX_MESSAGE_BUFFER);

                    if (strcmp(regex_group[4], "PRIVMSG") == 0) {
                        if (matches_regex(message.message, CMD_LIT)) {
                            if (message.receiver[0] == '#') {
                                invoke_cmd(0, get_pindex(message.sender_nick), regex_group[0], &message, CMD_EXEC);
                            } else {
                                parse_private_message(&message);
                            }
                        }
                    } else if (strcmp(regex_group[4], "NOTICE") == 0) {
                    //    parse_notice_message(&message);
                    }
                }
            }
            bzero(buffer, MAX_MESSAGE_BUFFER);
        }
    } else {
        perror(ERR "main: Failed to connect");
        close(con_socket);
        return 1;
    }

    if (len != -1) {
        close(con_socket);
    }

    if (sflag) {
         free(server);
    }

    if (pflag) {
        free(port);
    }

    if (mflag) {
        free(monsters);
    }

    free_msg_hist_list();
    free_msg_list();
    free_event_list();
    free_cmds();
    free_map();
    free(dawn);

    if (auth_key_valid || auth_key) {
        free(auth_key);
    }

    printf(INFO "Program exiting normally [%s]\n", timestamp());

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1408, %rsp           # imm = 0x1408
movq %rsi, %rbx
movl %edi, %ebp
movl $0x1, %edi
movl $0xf2eb8, %esi          # imm = 0xF2EB8
callq 0x2110
leaq 0x19040(%rip), %r15     # 0x21a80
movq %rax, (%r15)
testq %rax, %rax
je 0x976d
movq %rax, %r14
movl 0x1680f(%rip), %edx     # 0x1f264
leaq 0xbd7e(%rip), %rsi      # 0x147da
movq %rax, %rdi
callq 0x20e0
movl $0x676e6f64, 0x40(%r14) # imm = 0x676E6F64
movw $0x73, 0x44(%r14)
xorl %eax, %eax
movb %al, 0x84(%r14)
movl $0x65686568, 0x80(%r14) # imm = 0x65686568
movl $0x0, (%rsp)
movb %al, 0xc4(%r14)
movl $0x706d6574, 0xc0(%r14) # imm = 0x706D6574
addq $0x100, %r14            # imm = 0x100
movl 0x167b3(%rip), %edx     # 0x1f260
leaq 0xbd3e(%rip), %rsi      # 0x147f2
movq %r14, %rdi
callq 0x20e0
callq 0xd873
leaq 0xbcb0(%rip), %rax      # 0x14778
movq %rax, 0x10(%rsp)
leaq 0xbcb1(%rip), %rax      # 0x14785
movq %rax, 0x18(%rsp)
leaq 0xbcaa(%rip), %rax      # 0x1478a
movq %rax, 0x20(%rsp)
movb $0x1, %al
movl %eax, 0x4(%rsp)
leaq 0xbd09(%rip), %r14      # 0x147fb
leaq 0xbc3f(%rip), %r13      # 0x14738
movq 0x164c0(%rip), %r12     # 0x1efc0
movl $0x0, 0x8(%rsp)
movl %ebp, %edi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x2060
leal -0x68(%rax), %ecx
cmpl $0xf, %ecx
ja 0x8c35
movslq (%r13,%rcx,4), %rax
addq %r13, %rax
jmpq *%rax
movq (%r15), %rdi
movq (%r12), %rsi
movl 0x1672c(%rip), %edx     # 0x1f264
decl %edx
callq 0x20e0
jmp 0x8b08
movq (%r15), %rdi
movb $0x23, 0x100(%rdi)
addq $0x101, %rdi            # imm = 0x101
movq (%r12), %rsi
movl 0x16704(%rip), %edx     # 0x1f260
addl $-0x2, %edx
callq 0x20e0
jmp 0x8b08
movq (%r15), %rdi
movl $0xc0, %eax
addq %rax, %rdi
movq (%r12), %rsi
movl $0x3f, %edx
callq 0x20e0
jmp 0x8b08
movq (%r12), %r15
movq %r15, %rdi
callq 0x20c0
leaq 0x1(%rax), %rdi
callq 0x22b0
testq %rax, %rax
je 0x9777
movq %rax, 0x18(%rsp)
movq %rax, %rdi
movq %r15, %rsi
callq 0x21c0
movl $0x0, 0x4(%rsp)
leaq 0x18ec2(%rip), %r15     # 0x21a80
jmp 0x8b08
movq (%r12), %r15
movq %r15, %rdi
callq 0x20c0
leaq 0x1(%rax), %rdi
callq 0x22b0
testq %rax, %rax
je 0x977c
movq %rax, 0x10(%rsp)
movq %rax, %rdi
movq %r15, %rsi
callq 0x21c0
movl $0x1, 0x8(%rsp)
leaq 0x18e80(%rip), %r15     # 0x21a80
jmp 0x8b08
movl $0x10, %edi
callq 0x22b0
testq %rax, %rax
je 0x9772
movq (%r12), %rdi
movq %rax, 0x20(%rsp)
movq %rax, %rsi
callq 0xcfc3
movl $0x1, (%rsp)
jmp 0x8b08
cmpl $-0x1, %eax
jne 0x9700
leaq 0xbc8d(%rip), %rdi      # 0x148d2
leaq 0xb777(%rip), %rsi      # 0x143c3
callq 0x2230
testq %rax, %rax
je 0x9781
movq %rax, %rbx
leaq 0x44(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %rax, %rcx
callq 0x2320
testq %rax, %rax
je 0x9786
movl 0x44(%rsp), %edi
callq 0x21d0
movq %rbx, %rdi
callq 0x21f0
leaq 0xbce1(%rip), %rdi      # 0x14976
leaq 0xbae7(%rip), %rsi      # 0x14783
callq 0x2230
testq %rax, %rax
je 0x978b
movq %rax, %rbx
movl $0x19, %edi
movl $0x1, %esi
callq 0x2110
movq %rax, 0x165c5(%rip)     # 0x1f288
testq %rax, %rax
je 0x9790
xorl %r14d, %r14d
callq 0x2080
leal 0x1f(%rax), %ecx
testl %eax, %eax
cmovnsl %eax, %ecx
andl $0xe0, %ecx
subl %ecx, %eax
movq 0x1659d(%rip), %rcx     # 0x1f288
movb %al, (%rcx,%r14)
movq 0x16592(%rip), %rax     # 0x1f288
cmpb $0x19, (%rax,%r14)
jg 0x8ccf
callq 0x2080
movq 0x1657f(%rip), %rcx     # 0x1f288
andb $0x1, %al
shlb $0x5, %al
addb (%rcx,%r14), %al
addb $0x41, %al
movb %al, (%rcx,%r14)
incq %r14
cmpq $0x18, %r14
jne 0x8ccf
movq 0x16560(%rip), %rdi     # 0x1f288
movl $0x18, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x2480
testq %rax, %rax
je 0x9795
movq %rbx, %rdi
callq 0x21f0
movq 0x16536(%rip), %rsi     # 0x1f288
leaq 0xbd2a(%rip), %rdi      # 0x14a83
xorl %eax, %eax
callq 0x2040
leaq 0x16519(%rip), %rax     # 0x1f280
movl $0x1, (%rax)
movq 0x10(%rsp), %rdi
xorl %esi, %esi
callq 0x23d0
cmpl $-0x1, %eax
je 0x8efc
leaq 0xb63a(%rip), %rsi      # 0x143c3
movq 0x10(%rsp), %rdi
callq 0x2230
testq %rax, %rax
je 0x9734
movq %rax, %rbx
leaq 0x200(%rsp), %rdi
movl $0x190, %esi            # imm = 0x190
movq %rax, %rdx
callq 0x2380
testq %rax, %rax
je 0x8edf
movl $0xf1b40, %r12d         # imm = 0xF1B40
leaq 0x200(%rsp), %r15
leaq 0xb00(%rsp), %r14
xorl %ebp, %ebp
movq %r15, %rdi
leaq 0xbcf4(%rip), %rsi      # 0x14ad3
movq %r14, %rdx
leaq 0x70(%rsp), %rcx
leaq 0x40(%rsp), %r8
leaq 0x3c(%rsp), %r9
xorl %eax, %eax
leaq 0x28(%rsp), %r10
pushq %r10
leaq 0x34(%rsp), %r10
pushq %r10
leaq 0x40(%rsp), %r10
pushq %r10
leaq 0x24(%rsp), %r10
pushq %r10
leaq 0x54(%rsp), %r10
pushq %r10
leaq 0x60(%rsp), %r10
pushq %r10
callq 0x21e0
addq $0x30, %rsp
cmpl $0xa, %eax
jne 0x9746
leaq 0x18c4a(%rip), %r13     # 0x21a80
movq (%r13), %rdi
addq %r12, %rdi
movl $0x40, %edx
movq %r14, %rsi
callq 0x20e0
movl 0x70(%rsp), %eax
movq (%r13), %rcx
movl %eax, 0x40(%rcx,%r12)
movl 0x40(%rsp), %eax
movl %eax, 0x4c(%rcx,%r12)
movl 0x3c(%rsp), %eax
movl %eax, 0x48(%rcx,%r12)
movl 0x38(%rsp), %eax
movl %eax, 0x50(%rcx,%r12)
movl 0x34(%rsp), %eax
movl %eax, 0x54(%rcx,%r12)
movl 0xc(%rsp), %eax
movl %eax, 0x58(%rcx,%r12)
movl 0x30(%rsp), %eax
movl %eax, 0x5c(%rcx,%r12)
movl 0x2c(%rsp), %eax
movl %eax, 0x44(%rcx,%r12)
movl 0x28(%rsp), %eax
movl %eax, 0x60(%rcx,%r12)
movl 0xc(%rsp), %eax
movl %eax, 0x68(%rcx,%r12)
movl $0x0, 0x64(%rcx,%r12)
movabsq $0x200000005, %rax      # imm = 0x200000005
movq %rax, 0x70(%rcx,%r12)
movq %r15, %rdi
movl $0x190, %esi            # imm = 0x190
movq %rbx, %rdx
callq 0x2380
incl %ebp
addq $0x78, %r12
testq %rax, %rax
jne 0x8dd5
movq %rbx, %rdi
callq 0x21f0
xorl %ebx, %ebx
movb $0x1, %r14b
leaq 0x18b8d(%rip), %r15     # 0x21a80
testb %r14b, %r14b
je 0x9719
movq (%r15), %rax
movq $0x0, 0x140(%rax)
callq 0xca74
callq 0x4d24
movq 0x20(%rsp), %rdi
movq 0x18(%rsp), %rsi
callq 0xcaef
testl %eax, %eax
jne 0x972d
leaq 0x1638e(%rip), %rax     # 0x1f2c0
movl (%rax), %edi
leaq 0x16395(%rip), %rbx     # 0x1f2d0
movl $0x1000, %edx           # imm = 0x1000
movq %rbx, %rsi
xorl %ecx, %ecx
callq 0x20a0
cmpq $-0x1, %rax
je 0x9669
movl $0x80, %ecx
leaq 0xb00(%rsp), %rdx
addq %rcx, %rdx
movq %rdx, 0x58(%rsp)
leaq 0x200(%rsp), %rdx
addq %rdx, %rcx
movq %rcx, 0x68(%rsp)
movl $0x4b0, %ecx            # imm = 0x4B0
addq 0x1605b(%rip), %rcx     # 0x1efe0
movq %rcx, 0x48(%rsp)
movl $0x960, %ecx            # imm = 0x960
addq 0x1604a(%rip), %rcx     # 0x1efe0
movq %rcx, 0x50(%rsp)
movl $0x640, %ecx            # imm = 0x640
addq 0x16039(%rip), %rcx     # 0x1efe0
movq %rcx, 0x60(%rsp)
leaq 0x70(%rsp), %r14
leaq 0x17328(%rip), %rbp     # 0x202e0
movb $0x0, (%rbx,%rax)
movq %rbx, %rdi
leaq 0xbb6a(%rip), %rsi      # 0x14b30
callq 0xd1ba
testl %eax, %eax
je 0x9017
movl $0x190, %eax            # imm = 0x190
leaq (%rax,%rbp), %r15
leaq 0xbb5c(%rip), %rdi      # 0x14b3b
movq %r15, %rsi
xorl %eax, %eax
callq 0x2040
movq %r14, %rdi
leaq 0xbb52(%rip), %rsi      # 0x14b45
movq %r15, %rdx
leaq 0x18a83(%rip), %r15     # 0x21a80
xorl %eax, %eax
callq 0x2050
movq %r14, %rdi
callq 0x20c0
movq %r14, %rdi
movq %rax, %rsi
callq 0xcd1c
movq (%r15), %rax
cmpl $0x0, 0x140(%rax)
jne 0x907c
movq %rbx, %rdi
leaq 0xeb8f(%rip), %rsi      # 0x17bbc
callq 0xd1ba
testl %eax, %eax
jne 0x9049
movq %rbx, %rdi
leaq 0xbb10(%rip), %rsi      # 0x14b50
callq 0xd1ba
testl %eax, %eax
je 0x907c
leaq 0xbd65(%rip), %rdi      # 0x14db5
callq 0x2330
movq (%r15), %rdi
leaq 0xc0(%rdi), %rsi
leaq 0x40(%rdi), %rdx
leaq 0x80(%rdi), %rcx
callq 0xd2e2
movq (%r15), %rax
movl $0x1, 0x140(%rax)
movq %rbx, %rdi
leaq 0xbae6(%rip), %rsi      # 0x14b6c
callq 0xd1ba
testl %eax, %eax
jne 0x94a1
movq (%r15), %rax
cmpl $0x0, 0x144(%rax)
jne 0x9108
cmpl $0x1, 0x140(%rax)
jne 0x9108
movq %rbx, %rdi
leaq 0xbaf2(%rip), %rsi      # 0x14ba4
callq 0xd1ba
testl %eax, %eax
je 0x9108
leaq 0xbd32(%rip), %rdi      # 0x14df4
callq 0x2330
movq (%r15), %rdx
movl $0x100, %eax            # imm = 0x100
addq %rax, %rdx
movq %r14, %rdi
leaq 0xbad8(%rip), %rsi      # 0x14bb4
xorl %eax, %eax
callq 0x2050
callq 0xb498
movq %r14, %rdi
callq 0x20c0
movq %r14, %rdi
movq %rax, %rsi
callq 0xcd1c
movq (%r15), %rax
movl $0x1, 0x144(%rax)
movq (%r15), %rax
cmpl $0x0, 0x144(%rax)
je 0x9638
movq %rbx, %rdi
leaq 0xba9c(%rip), %rsi      # 0x14bbe
callq 0xd1ba
testl %eax, %eax
je 0x9179
leaq 0x190(%rbp), %rdi
xorl %esi, %esi
movl $0xa, %edx
callq 0x2090
incl %eax
movl %eax, 0x1611a(%rip)     # 0x1f260
leaq 0x320(%rbp), %rdi
xorl %esi, %esi
movl $0xa, %edx
callq 0x2090
incl %eax
movl %eax, 0x16103(%rip)     # 0x1f264
movq (%r15), %rcx
xorl %edx, %edx
movb %dl, (%rcx,%rax)
movq (%r15), %rax
movl 0x160ee(%rip), %ecx     # 0x1f260
movb %dl, 0x100(%rax,%rcx)
movq %rbp, %r12
movq %rbx, %rdi
leaq 0xba65(%rip), %rsi      # 0x14beb
callq 0xd1ba
testl %eax, %eax
je 0x925a
movq 0x48(%rsp), %rdi
callq 0xd89c
cmpl $-0x1, %eax
je 0x925a
movq (%r15), %r13
cltq
imulq $0x26a8, %rax, %rax     # imm = 0x26A8
leaq (%rax,%r13), %rbp
addq $0x1a0, %rbp            # imm = 0x1A0
cmpb $0x1, 0x26a1(%rbp)
ja 0x91d3
cmpb $0x0, 0x26a0(%rbp)
je 0x925a
leaq 0x1a0(%r13), %rcx
cmpb $0x1, 0x26a1(%rcx,%rax)
ja 0x91f0
addq %rcx, %rax
addq $0x26a1, %rax           # imm = 0x26A1
movb $0x2, (%rax)
movb 0x26a0(%rbp), %al
movb 0x26a1(%rbp), %cl
leaq 0xba05(%rip), %r15      # 0x14c08
cmpb %cl, %al
jae 0x9216
movb %cl, 0x26a0(%rbp)
movl %ecx, %eax
leaq 0x97aa(%rip), %r15      # 0x129c0
addq $0x100, %r13            # imm = 0x100
movzbl %al, %edi
callq 0xe457
movl $0x190, %esi            # imm = 0x190
movq %r14, %rdi
movq %r15, %rdx
movq %r13, %rcx
movq %rbp, %r8
movq %rax, %r9
xorl %eax, %eax
callq 0x2390
movq %r14, %rdi
callq 0x20c0
movq %r14, %rdi
movq %rax, %rsi
callq 0xcd1c
leaq 0x18826(%rip), %r15     # 0x21a80
movq %rbx, %rdi
leaq 0xb9cf(%rip), %rsi      # 0x14c33
callq 0xd1ba
testl %eax, %eax
movq %r12, %rbp
leaq 0x96b9(%rip), %r13      # 0x1292e
je 0x92f5
movq 0x60(%rsp), %rdi
leaq 0xb9d4(%rip), %rsi      # 0x14c57
callq 0x2370
testq %rax, %rax
je 0x92f5
movq %rax, %rdi
callq 0xd008
movq %rax, %rdi
callq 0xd89c
cmpl $-0x1, %eax
je 0x92ea
movq (%r15), %rcx
cltq
imulq $0x26a8, %rax, %rax     # imm = 0x26A8
leaq (%rcx,%rax), %rdx
addq $0x1a0, %rdx            # imm = 0x1A0
movl $0x1, 0x2664(%rdx)
movb $0x0, 0x26a0(%rdx)
movq %r14, %rdi
movq %r13, %rsi
xorl %eax, %eax
callq 0x2050
movq %r14, %rdi
callq 0x20c0
movq %r14, %rdi
movq %rax, %rsi
callq 0xcd1c
xorl %edi, %edi
leaq 0xb969(%rip), %rsi      # 0x14c5c
jmp 0x9283
movq %rbx, %rdi
leaq 0xb964(%rip), %rsi      # 0x14c63
callq 0xd1ba
testl %eax, %eax
je 0x9356
movl 0x640(%rbp), %eax
movl $0x4b43494b, %ecx       # imm = 0x4B43494B
xorl %ecx, %eax
movzbl 0x644(%rbp), %ecx
orl %eax, %ecx
jne 0x9356
movq 0x50(%rsp), %rdi
callq 0xd008
movq %rax, %rdi
callq 0xd89c
cmpl $-0x1, %eax
je 0x9356
movq (%r15), %rcx
cltq
imulq $0x26a8, %rax, %rax     # imm = 0x26A8
movl $0x0, 0x2804(%rcx,%rax)
movb $0x0, 0x2840(%rcx,%rax)
movq %rbx, %rdi
leaq 0xb939(%rip), %rsi      # 0x14c99
callq 0xd1ba
testl %eax, %eax
je 0x93e2
leaq 0x190(%rbp), %rdi
callq 0xd008
movq %rax, %rdi
callq 0xd89c
movq %rbp, %rcx
movl %eax, %ebp
leaq 0x320(%rcx), %rdi
callq 0xd008
movq %rax, %rdi
callq 0xd89c
cmpl $-0x1, %ebp
je 0x93bb
movq (%r15), %rcx
movslq %ebp, %rdx
imulq $0x26a8, %rdx, %rdx     # imm = 0x26A8
movl $0x0, 0x2804(%rcx,%rdx)
movb $0x0, 0x2840(%rcx,%rdx)
cmpl $-0x1, %eax
movq %r12, %rbp
je 0x93e2
movq (%r15), %rcx
cltq
imulq $0x26a8, %rax, %rax     # imm = 0x26A8
movl $0x1, 0x2804(%rcx,%rax)
movb $0x0, 0x2840(%rcx,%rax)
movq %rbx, %rdi
leaq 0xb8ce(%rip), %rsi      # 0x14cba
callq 0xd1ba
testl %eax, %eax
je 0x952f
movl $0x190, %eax            # imm = 0x190
leaq (%rax,%rbp), %r13
movq %r13, %rdi
callq 0xd008
movq %rax, %rdi
callq 0xd89c
cmpl $-0x1, %eax
je 0x94f1
movl 0x640(%rbp), %ecx
movl $0x54524150, %edx       # imm = 0x54524150
xorl %edx, %ecx
movzbl 0x644(%rbp), %edx
orl %ecx, %edx
je 0x9480
movl 0x640(%rbp), %ecx
movl $0x4e494f4a, %edx       # imm = 0x4E494F4A
xorl %edx, %ecx
movzbl 0x644(%rbp), %edx
orl %ecx, %edx
je 0x94ab
movl 0x640(%rbp), %ecx
movl $0x54495551, %edx       # imm = 0x54495551
xorl %edx, %ecx
movzbl 0x644(%rbp), %edx
orl %ecx, %edx
jne 0x94f1
movq (%r15), %rcx
cltq
imulq $0x26a8, %rax, %rax     # imm = 0x26A8
movl $0x0, 0x2804(%rcx,%rax)
jmp 0x94f1
movq (%r15), %rcx
cltq
imulq $0x26a8, %rax, %rax     # imm = 0x26A8
movl $0x0, 0x2804(%rcx,%rax)
movb $0x0, 0x2840(%rcx,%rax)
jmp 0x94f1
callq 0x28dc
jmp 0x9093
movq (%r15), %rcx
cltq
imulq $0x26a8, %rax, %rax     # imm = 0x26A8
movl $0x1, 0x2804(%rcx,%rax)
movb $0x0, 0x2840(%rcx,%rax)
movq %r14, %rdi
leaq 0x945a(%rip), %rsi      # 0x1292e
movq %r13, %rdx
xorl %eax, %eax
callq 0x2050
movq %r14, %rdi
callq 0x20c0
movq %r14, %rdi
movq %rax, %rsi
callq 0xcd1c
movq (%r15), %rsi
cmpl $0x0, 0x148(%rsi)
jne 0x952f
movl 0x15d61(%rip), %edx     # 0x1f264
leaq 0xb00(%rsp), %r13
movq %r13, %rdi
callq 0x20e0
movl $0x3f, %edx
movq 0x58(%rsp), %rdi
movq 0x48(%rsp), %rsi
callq 0x20e0
movq %r13, %rdi
callq 0xd958
movq %rbx, %rdi
leaq 0xb7b3(%rip), %rsi      # 0x14cec
callq 0xd1ba
testl %eax, %eax
je 0x9638
leaq 0x190(%rbp), %rdi
callq 0xd008
movl 0x15d0b(%rip), %r15d    # 0x1f264
leaq 0x200(%rsp), %rdi
movq %rax, %rsi
movq %r15, %rdx
callq 0x20e0
leaq 0x320(%rbp), %rsi
movl $0xa, %edx
leaq 0x240(%rsp), %rdi
callq 0x20e0
leaq 0x4b0(%rbp), %rsi
movl $0x3f, %edx
movq 0x68(%rsp), %rdi
callq 0x20e0
leaq 0x7d0(%rbp), %rsi
leaq 0x2c0(%rsp), %rdi
movq %r15, %rdx
leaq 0x184cc(%rip), %r15     # 0x21a80
callq 0x20e0
leaq 0x960(%rbp), %rsi
movl $0x190, %edx            # imm = 0x190
leaq 0x300(%rsp), %rdi
callq 0x20e0
movabsq $0x47534d56495250, %rax # imm = 0x47534D56495250
cmpq %rax, 0x640(%rbp)
jne 0x9638
leaq 0x300(%rsp), %rdi
leaq 0x9415(%rip), %rsi      # 0x12a09
callq 0xd1ba
testl %eax, %eax
je 0x9638
cmpb $0x23, 0x2c0(%rsp)
jne 0x962b
leaq 0x200(%rsp), %r13
movq %r13, %rdi
callq 0xd89c
xorl %edi, %edi
movl %eax, %esi
movq %rbp, %rdx
movq %r13, %rcx
xorl %r8d, %r8d
callq 0x4259
jmp 0x9638
leaq 0x200(%rsp), %rdi
callq 0xd355
movl $0x190, %edx            # imm = 0x190
movq %rbx, %rdi
xorl %esi, %esi
callq 0x20f0
leaq 0x15c72(%rip), %rax     # 0x1f2c0
movl (%rax), %edi
movl $0x1000, %edx           # imm = 0x1000
movq %rbx, %rsi
xorl %ecx, %ecx
callq 0x20a0
cmpq $-0x1, %rax
jne 0x8fb8
cmpl $0x0, (%rsp)
je 0x9679
movq 0x20(%rsp), %rdi
callq 0x2240
testb $0x1, 0x4(%rsp)
leaq 0x15bfb(%rip), %rbx     # 0x1f280
jne 0x9691
movq 0x18(%rsp), %rdi
callq 0x2240
cmpl $0x0, 0x8(%rsp)
je 0x96a2
movq 0x10(%rsp), %rdi
callq 0x2240
callq 0xcbe9
callq 0xcccb
callq 0xf7a7
callq 0x701c
callq 0xaeb8
movq (%r15), %rdi
callq 0x2240
cmpl $0x0, (%rbx)
setne %al
movq 0x15bb8(%rip), %rdi     # 0x1f288
testq %rdi, %rdi
setne %cl
orb %al, %cl
cmpb $0x1, %cl
jne 0x96e2
callq 0x2240
callq 0x9801
leaq 0xb655(%rip), %rdi      # 0x14d43
leaq 0x15b9b(%rip), %rsi     # 0x1f290
xorl %ebx, %ebx
xorl %eax, %eax
callq 0x2040
jmp 0x9719
movq %rbx, %rdi
callq 0x979a
movl $0x1, %ebx
jmp 0x9719
movq %rbx, %rdi
callq 0x979a
xorl %ebx, %ebx
movl %ebx, %eax
addq $0x1408, %rsp           # imm = 0x1408
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x28bc
jmp 0x9708
callq 0x28a0
movl $0x1, %ebx
xorl %r14d, %r14d
jmp 0x8ef3
movq 0x1588b(%rip), %rax     # 0x1efd8
movq (%rax), %rdi
leaq 0xb39d(%rip), %rsi      # 0x14af4
xorl %r14d, %r14d
movl %ebp, %edx
xorl %eax, %eax
callq 0x2310
movl $0x1, %ebx
jmp 0x8eec
callq 0x29d2
callq 0x2861
callq 0x2876
callq 0x288b
callq 0x29bd
callq 0x29a8
callq 0x2993
callq 0x297e
callq 0x2969