// File: /workspace/github_c_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %edi, 0x8(%rsp)
movq %rsi, 0x10(%rsp)
movq (%rsi), %rbx
pushq $0x2f
popq %rsi
movq %rbx, %rdi
callq 0x2110
leaq 0x1(%rax), %r12
testq %rax, %rax
cmoveq %rbx, %r12
xorl %r13d, %r13d
leaq 0x6e66(%rip), %r15      # 0xcdb0
xorl %ebx, %ebx
pushq $-0x1
popq %rbp
cmpq $0x28, %r13
je 0x5f7f
movq (%r13,%r15), %r14
movq (%r14), %rsi
movq %r12, %rdi
callq 0x2170
testl %eax, %eax
jne 0x5f79
callq *0x8(%r14)
movq %rax, %rbx
testq %rax, %rax
je 0x6145
addq $0x8, %r13
jmp 0x5f4f
testq %rbx, %rbx
je 0x6124
movl $0x1, 0x10(%rbx)
leaq 0x2c5(%rip), %rax       # 0x625b
movq %rax, 0x18(%rbx)
leaq 0x757f(%rip), %rdi      # 0xd520
leaq 0x322(%rip), %rdx       # 0x62ca
pushq $0x6
popq %rsi
movq %rbx, %rcx
callq 0x6544
movl 0x8(%rsp), %edi
movq 0x10(%rsp), %rsi
callq 0x65da
cmpl $-0x1, %eax
je 0x624a
movl %eax, %r15d
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x28(%rax)
testl %eax, %eax
jne 0x624a
movq 0x6fe3(%rip), %rax      # 0xcfc8
movq (%rax), %rdi
xorl %esi, %esi
callq 0x20e0
leaq 0x75ea(%rip), %r14      # 0xd5e0
pushq $0x1
popq %r12
movq %r14, %r13
movl %r15d, %ebp
movq (%r13), %r13
cmpq %r14, %r13
je 0x6207
movl 0x1c(%r13), %eax
cmpl $0x2, %eax
je 0x6077
movl %r12d, %ebp
cmpl $0x1, %eax
jne 0x6000
movq (%rbx), %rax
movl 0x18(%r13), %esi
movq %rbx, %rdi
callq *0x20(%rax)
testl %eax, %eax
je 0x6204
movl %eax, %edi
callq 0x3b14
testq %rax, %rax
je 0x6204
movq %rax, %r15
movq (%rbx), %rax
movl 0x18(%r13), %edx
movq %rbx, %rdi
movq %r15, %rsi
callq *0x38(%rax)
testl %eax, %eax
jne 0x617c
movq 0x10(%r13), %rdi
movq %r15, %rsi
callq 0x3f41
testl %eax, %eax
je 0x6115
jmp 0x6191
movq 0x10(%r13), %rdi
leaq 0xc(%rsp), %rsi
callq 0x3b63
testl %eax, %eax
jne 0x6204
movl 0xc(%rsp), %edi
callq 0x3b14
testq %rax, %rax
je 0x6204
movq %rax, %r15
movq 0x10(%r13), %rdi
movq %rax, %rsi
callq 0x3c4f
testl %eax, %eax
jne 0x6165
movq (%rbx), %rax
movl 0x18(%r13), %esi
movq %rbx, %rdi
callq *0x20(%rax)
movl 0x4(%r15), %edx
testl %eax, %eax
je 0x61af
cmpl %edx, %eax
jb 0x61af
movq (%rbx), %rax
movl 0x18(%r13), %edx
movq %rbx, %rdi
movq %r15, %rsi
callq *0x48(%rax)
testl %eax, %eax
jne 0x61cb
cmpl $0x0, 0x10(%rbx)
je 0x6115
movq (%rbx), %rax
movq 0x40(%rax), %rax
testq %rax, %rax
je 0x6115
movl 0x18(%r13), %edx
movq %rbx, %rdi
movq %r15, %rsi
callq *%rax
testl %eax, %eax
jne 0x61e0
movq %r15, %rdi
callq 0x3b5e
xorl %ebp, %ebp
jmp 0x6000
movq 0x6ecd(%rip), %rax      # 0xcff8
movq (%rax), %rcx
leaq 0x32b0(%rip), %rdi      # 0x93e5
pushq $0x11
popq %rsi
pushq $0x1
popq %rdx
callq 0x2280
jmp 0x624a
movq 0x6eac(%rip), %rax      # 0xcff8
movq (%rax), %rdi
leaq 0x3276(%rip), %rsi      # 0x93cc
movq %r12, %rdx
xorl %eax, %eax
callq 0x2180
jmp 0x624a
movq 0x6e8c(%rip), %rax      # 0xcff8
movq (%rax), %rdi
movq 0x10(%r13), %rdx
leaq 0x32b4(%rip), %rsi      # 0x942e
jmp 0x61a6
movq 0x6e75(%rip), %rax      # 0xcff8
movq (%rax), %rcx
leaq 0x326a(%rip), %rdi      # 0x93f7
pushq $0x1b
jmp 0x61f3
movq 0x6e60(%rip), %rax      # 0xcff8
movq (%rax), %rdi
movq 0x10(%r13), %rdx
leaq 0x326d(%rip), %rsi      # 0x9413
xorl %eax, %eax
callq 0x2180
jmp 0x61fc
movq 0x6e42(%rip), %rcx      # 0xcff8
movq (%rcx), %rdi
leaq 0x3288(%rip), %rsi      # 0x9448
movl %eax, %ecx
xorl %eax, %eax
callq 0x2180
jmp 0x61fc
movq 0x6e26(%rip), %rax      # 0xcff8
movq (%rax), %rcx
leaq 0x328e(%rip), %rdi      # 0x946a
pushq $0x1a
jmp 0x61f3
movq 0x6e11(%rip), %rax      # 0xcff8
movq (%rax), %rcx
leaq 0x3294(%rip), %rdi      # 0x9485
pushq $0x11
popq %rsi
pushq $0x1
popq %rdx
callq 0x2280
movq %r15, %rdi
callq 0x3b5e
movl %r12d, %ebp
movq 0x73d2(%rip), %r14      # 0xd5e0
leaq 0x73cb(%rip), %r15      # 0xd5e0
cmpq %r15, %r14
je 0x6233
movq (%r14), %r12
movq 0x10(%r14), %rdi
callq 0x2030
movq %r14, %rdi
callq 0x2030
movq %r12, %r14
jmp 0x6215
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x30(%rax)
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x10(%rax)
callq 0x65a3
movl %ebp, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq