// File: /workspace/github_c_makefile_star2_O0/orempel[P]multiboot_tool/multiboot.c
// Function: main
int main(int argc, char *argv[])
{
    struct multiboot *mboot = NULL;

    char *progname = strrchr(argv[0], '/');
    progname = (progname != NULL) ? (progname +1) : argv[0];

    unsigned int i;
    for (i = 0; i < ARRAY_SIZE(prog_ops); i++)
    {
        struct multiboot_ops * ops = prog_ops[i];

        if (strcmp(progname, ops->exec_name) == 0)
        {
            mboot = ops->alloc();
            if (mboot == NULL)
            {
                fprintf(stderr, "failed to allocate '%s'\n",
                        progname);

                return -1;
            }
        }
    }

    if (mboot == NULL)
    {
        fprintf(stderr, "invalid progname\n");
        return -1;
    }

    mboot->verify      = 1;
    mboot->progress_cb = progress_mode1_cb;

    optarg_register(main_optargs, ARRAY_SIZE(main_optargs),
                    main_optarg_cb, (void *)mboot);

    int abort = optarg_parse(argc, argv);

    if ((abort == -1) ||
        (mboot->ops->open(mboot) != 0)
       )
    {
        return -1;
    }

    setbuf(stdout, NULL);

    struct mboot_action *action, *tmp;
    list_for_each_entry(action, &action_list, list)
    {
        abort = 1;
        if (action->mode == ACTION_READ)
        {
            int memsize = mboot->ops->get_memsize(mboot, action->memtype);
            if (memsize == 0)
            {
                break;
            }

            struct databuf *dbuf = dbuf_alloc(memsize);
            if (dbuf == NULL)
            {
                break;
            }

            int result = mboot->ops->read(mboot, dbuf, action->memtype);
            if (result != 0)
            {
                fprintf(stderr, "failed to read from device\n");
                dbuf_free(dbuf);
                break;
            }

            result = file_write(action->filename, dbuf);
            if (result != 0)
            {
                fprintf(stderr, "failed to write file '%s'\n", action->filename);
                dbuf_free(dbuf);
                break;
            }

            dbuf_free(dbuf);
            abort = 0;

        }
        else if (action->mode == ACTION_WRITE)
        {
            unsigned int size;
            int result;

            result = file_getsize(action->filename, &size);
            if (result != 0)
            {
                break;
            }

            struct databuf *dbuf = dbuf_alloc(size);
            if (dbuf == NULL)
            {
                break;
            }

            result = file_read(action->filename, dbuf);
            if (result != 0)
            {
                fprintf(stderr, "failed to read file '%s'\n", action->filename);
                dbuf_free(dbuf);
                break;
            }

            uint32_t memsize = mboot->ops->get_memsize(mboot, action->memtype);
            if ((memsize == 0) || (memsize < dbuf->length))
            {
                fprintf(stderr, "invalid memsize: 0x%04x > 0x%04x\n", dbuf->length, memsize);
                dbuf_free(dbuf);
                break;
            }

            result = mboot->ops->write(mboot, dbuf, action->memtype);
            if (result != 0)
            {
                fprintf(stderr, "failed to write to device\n");
                dbuf_free(dbuf);
                break;
            }

            if (mboot->verify &&
                (mboot->ops->verify != NULL)
               )
            {
                result = mboot->ops->verify(mboot, dbuf, action->memtype);
                if (result != 0)
                {
                    fprintf(stderr, "failed to verify\n");
                    dbuf_free(dbuf);
                    break;
                }
            }

            dbuf_free(dbuf);
            abort = 0;
        }
    }

    list_for_each_entry_safe(action, tmp, &action_list, list)
    {
        free(action->filename);
        free(action);
    }

    mboot->ops->close(mboot);
    mboot->ops->free(mboot);

    optarg_free();
    return abort;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xd0, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x0, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movl $0x2f, %esi
callq 0x20f0
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
je 0x8c0e
movq -0x20(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xd0(%rbp)
jmp 0x8c1c
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0xd0(%rbp)
movq -0xd0(%rbp), %rax
movq %rax, -0x20(%rbp)
movl $0x0, -0x24(%rbp)
movl -0x24(%rbp), %eax
cmpq $0x5, %rax
jae 0x8ca9
movl -0x24(%rbp), %eax
movl %eax, %ecx
leaq 0x78bd(%rip), %rax      # 0x10500
movq (%rax,%rcx,8), %rax
movq %rax, -0x30(%rbp)
movq -0x20(%rbp), %rdi
movq -0x30(%rbp), %rax
movq (%rax), %rsi
callq 0x2150
cmpl $0x0, %eax
jne 0x8c9c
movq -0x30(%rbp), %rax
callq *0x8(%rax)
movq %rax, -0x18(%rbp)
cmpq $0x0, -0x18(%rbp)
jne 0x8c9a
movq 0x737f(%rip), %rax      # 0xfff8
movq (%rax), %rdi
movq -0x20(%rbp), %rdx
leaq 0x3805(%rip), %rsi      # 0xc48c
movb $0x0, %al
callq 0x2160
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x9133
jmp 0x8c9c
jmp 0x8c9e
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x8c2e
cmpq $0x0, -0x18(%rbp)
jne 0x8cd4
movq 0x7341(%rip), %rax      # 0xfff8
movq (%rax), %rdi
leaq 0x37e4(%rip), %rsi      # 0xc4a5
movb $0x0, %al
callq 0x2160
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x9133
movq -0x18(%rbp), %rax
movl $0x1, 0x10(%rax)
movq -0x18(%rbp), %rax
leaq 0x456(%rip), %rcx       # 0x9140
movq %rcx, 0x18(%rax)
movq -0x18(%rbp), %rcx
leaq 0x7837(%rip), %rdi      # 0x10530
movl $0x6, %esi
leaq 0x4db(%rip), %rdx       # 0x91e0
callq 0x9670
movl -0x8(%rbp), %edi
movq -0x10(%rbp), %rsi
callq 0x9800
movl %eax, -0x34(%rbp)
cmpl $-0x1, -0x34(%rbp)
je 0x8d35
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq 0x28(%rax), %rax
movq -0x18(%rbp), %rdi
callq *%rax
cmpl $0x0, %eax
je 0x8d41
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x9133
movq 0x7280(%rip), %rax      # 0xffc8
movq (%rax), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x20c0
movq 0x7895(%rip), %rax      # 0x105f0
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
leaq 0x7876(%rip), %rcx      # 0x105f0
cmpq %rcx, %rax
je 0x9061
movl $0x1, -0x34(%rbp)
movq -0x40(%rbp), %rax
cmpl $0x1, 0x1c(%rax)
jne 0x8e7e
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq 0x20(%rax), %rax
movq -0x18(%rbp), %rdi
movq -0x40(%rbp), %rcx
movl 0x18(%rcx), %esi
callq *%rax
movl %eax, -0x5c(%rbp)
cmpl $0x0, -0x5c(%rbp)
jne 0x8dbe
jmp 0x9061
movl -0x5c(%rbp), %edi
callq 0x4d20
movq %rax, -0x68(%rbp)
cmpq $0x0, -0x68(%rbp)
jne 0x8dd6
jmp 0x9061
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq 0x38(%rax), %rax
movq -0x18(%rbp), %rdi
movq -0x68(%rbp), %rsi
movq -0x40(%rbp), %rcx
movl 0x18(%rcx), %edx
callq *%rax
movl %eax, -0x6c(%rbp)
cmpl $0x0, -0x6c(%rbp)
je 0x8e21
movq 0x71f6(%rip), %rax      # 0xfff8
movq (%rax), %rdi
leaq 0x36ab(%rip), %rsi      # 0xc4b7
movb $0x0, %al
callq 0x2160
movq -0x68(%rbp), %rdi
callq 0x4da0
jmp 0x9061
movq -0x40(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x68(%rbp), %rsi
callq 0x51a0
movl %eax, -0x6c(%rbp)
cmpl $0x0, -0x6c(%rbp)
je 0x8e69
movq 0x71b6(%rip), %rax      # 0xfff8
movq (%rax), %rdi
movq -0x40(%rbp), %rax
movq 0x10(%rax), %rdx
leaq 0x367f(%rip), %rsi      # 0xc4d3
movb $0x0, %al
callq 0x2160
movq -0x68(%rbp), %rdi
callq 0x4da0
jmp 0x9061
movq -0x68(%rbp), %rdi
callq 0x4da0
movl $0x0, -0x34(%rbp)
jmp 0x9033
movq -0x40(%rbp), %rax
cmpl $0x2, 0x1c(%rax)
jne 0x9031
movq -0x40(%rbp), %rax
movq 0x10(%rax), %rdi
leaq -0x70(%rbp), %rsi
callq 0x4dc0
movl %eax, -0x74(%rbp)
cmpl $0x0, -0x74(%rbp)
je 0x8eab
jmp 0x9061
movl -0x70(%rbp), %edi
callq 0x4d20
movq %rax, -0x80(%rbp)
cmpq $0x0, -0x80(%rbp)
jne 0x8ec3
jmp 0x9061
movq -0x40(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x80(%rbp), %rsi
callq 0x4f90
movl %eax, -0x74(%rbp)
cmpl $0x0, -0x74(%rbp)
je 0x8f0b
movq 0x7114(%rip), %rax      # 0xfff8
movq (%rax), %rdi
movq -0x40(%rbp), %rax
movq 0x10(%rax), %rdx
leaq 0x35f8(%rip), %rsi      # 0xc4ee
movb $0x0, %al
callq 0x2160
movq -0x80(%rbp), %rdi
callq 0x4da0
jmp 0x9061
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq 0x20(%rax), %rax
movq -0x18(%rbp), %rdi
movq -0x40(%rbp), %rcx
movl 0x18(%rcx), %esi
callq *%rax
movl %eax, -0x84(%rbp)
cmpl $0x0, -0x84(%rbp)
je 0x8f41
movl -0x84(%rbp), %eax
movq -0x80(%rbp), %rcx
cmpl 0x4(%rcx), %eax
jae 0x8f74
movq 0x70b0(%rip), %rax      # 0xfff8
movq (%rax), %rdi
movq -0x80(%rbp), %rax
movl 0x4(%rax), %edx
movl -0x84(%rbp), %ecx
leaq 0x35a9(%rip), %rsi      # 0xc508
movb $0x0, %al
callq 0x2160
movq -0x80(%rbp), %rdi
callq 0x4da0
jmp 0x9061
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq 0x48(%rax), %rax
movq -0x18(%rbp), %rdi
movq -0x80(%rbp), %rsi
movq -0x40(%rbp), %rcx
movl 0x18(%rcx), %edx
callq *%rax
movl %eax, -0x74(%rbp)
cmpl $0x0, -0x74(%rbp)
je 0x8fbf
movq 0x7058(%rip), %rax      # 0xfff8
movq (%rax), %rdi
leaq 0x3580(%rip), %rsi      # 0xc52a
movb $0x0, %al
callq 0x2160
movq -0x80(%rbp), %rdi
callq 0x4da0
jmp 0x9061
movq -0x18(%rbp), %rax
cmpl $0x0, 0x10(%rax)
je 0x9021
movq -0x18(%rbp), %rax
movq (%rax), %rax
cmpq $0x0, 0x40(%rax)
je 0x9021
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq 0x40(%rax), %rax
movq -0x18(%rbp), %rdi
movq -0x80(%rbp), %rsi
movq -0x40(%rbp), %rcx
movl 0x18(%rcx), %edx
callq *%rax
movl %eax, -0x74(%rbp)
cmpl $0x0, -0x74(%rbp)
je 0x901f
movq 0x6ff5(%rip), %rax      # 0xfff8
movq (%rax), %rdi
leaq 0x3538(%rip), %rsi      # 0xc545
movb $0x0, %al
callq 0x2160
movq -0x80(%rbp), %rdi
callq 0x4da0
jmp 0x9061
jmp 0x9021
movq -0x80(%rbp), %rdi
callq 0x4da0
movl $0x0, -0x34(%rbp)
jmp 0x9033
jmp 0x9035
movq -0x40(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x90(%rbp)
movq -0x90(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x98(%rbp), %rax
movq %rax, -0x40(%rbp)
jmp 0x8d6f
movq 0x7588(%rip), %rax      # 0x105f0
movq %rax, -0xa0(%rbp)
movq -0xa0(%rbp), %rax
movq %rax, -0xa8(%rbp)
movq -0xa8(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
movq (%rax), %rax
movq %rax, -0xb0(%rbp)
movq -0xb0(%rbp), %rax
movq %rax, -0xb8(%rbp)
movq -0xb8(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x40(%rbp), %rax
leaq 0x7536(%rip), %rcx      # 0x105f0
cmpq %rcx, %rax
je 0x9106
movq -0x40(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0x2030
movq -0x40(%rbp), %rdi
callq 0x2030
movq -0x48(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x48(%rbp), %rax
movq (%rax), %rax
movq %rax, -0xc0(%rbp)
movq -0xc0(%rbp), %rax
movq %rax, -0xc8(%rbp)
movq -0xc8(%rbp), %rax
movq %rax, -0x48(%rbp)
jmp 0x90af
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq 0x30(%rax), %rax
movq -0x18(%rbp), %rdi
callq *%rax
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq 0x10(%rax), %rax
movq -0x18(%rbp), %rdi
callq *%rax
callq 0x9730
movl -0x34(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xd0, %rsp
popq %rbp
retq
nop