// File: /workspace/github_c_makefile_star2_O3/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
pushq %rax
movq %rsi, %r14
movl %edi, (%rsp)
movq (%rsi), %rbx
movq %rbx, %rdi
movl $0x2f, %esi
callq 0x2110
leaq 0x1(%rax), %r15
testq %rax, %rax
cmoveq %rbx, %r15
xorl %r12d, %r12d
leaq 0x6112(%rip), %r13      # 0xcdb0
xorl %ebx, %ebx
movq (%r12,%r13), %rbp
movq (%rbp), %rsi
movq %r15, %rdi
callq 0x2170
testl %eax, %eax
jne 0x6cc3
callq *0x8(%rbp)
movq %rax, %rbx
testq %rax, %rax
je 0x6ea3
addq $0x8, %r12
cmpq $0x28, %r12
jne 0x6ca0
testq %rbx, %rbx
je 0x6e9c
movl $0x1, 0x10(%rbx)
leaq 0x28d(%rip), %rax       # 0x6f71
movq %rax, 0x18(%rbx)
leaq 0x6831(%rip), %rdi      # 0xd520
leaq 0x361(%rip), %rdx       # 0x7057
movl $0x6, %esi
movq %rbx, %rcx
callq 0x72f0
movl (%rsp), %edi
movq %r14, %rsi
callq 0x738d
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpl $-0x1, %eax
je 0x6f60
movl %eax, %r15d
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x28(%rax)
testl %eax, %eax
jne 0x6f60
movq 0x6291(%rip), %rax      # 0xcfc8
movq (%rax), %rdi
xorl %esi, %esi
callq 0x20e0
movq 0x6898(%rip), %r14      # 0xd5e0
leaq 0x6891(%rip), %r13      # 0xd5e0
cmpq %r13, %r14
je 0x6e94
leaq 0x4(%rsp), %r15
movl 0x1c(%r14), %eax
cmpl $0x2, %eax
je 0x6dd2
movl $0x1, %ebp
cmpl $0x1, %eax
jne 0x6e83
movq (%rbx), %rax
movl 0x18(%r14), %esi
movq %rbx, %rdi
callq *0x20(%rax)
movl $0x1, %ebp
testl %eax, %eax
je 0x6f21
movl %eax, %edi
callq 0x4718
testq %rax, %rax
je 0x6f21
movq %rax, %r12
movq (%rbx), %rax
movl 0x18(%r14), %edx
movq %rbx, %rdi
movq %r12, %rsi
callq *0x38(%rax)
testl %eax, %eax
jne 0x6ed2
movq 0x10(%r14), %rdi
movq %r12, %rsi
callq 0x49a7
testl %eax, %eax
je 0x6e79
jmp 0x6edc
movq 0x10(%r14), %rdi
movq %r15, %rsi
callq 0x476a
testl %eax, %eax
jne 0x6f1c
movl 0x4(%rsp), %edi
callq 0x4718
testq %rax, %rax
je 0x6f1c
movq %rax, %r12
movq %r13, %rbp
leaq 0x10(%r14), %r13
movq (%r13), %rdi
movq %rax, %rsi
callq 0x4860
testl %eax, %eax
jne 0x6ec8
movq (%rbx), %rax
movl 0x18(%r14), %esi
movq %rbx, %rdi
callq *0x20(%rax)
movl 0x4(%r12), %edx
testl %eax, %eax
je 0x6ee9
cmpl %edx, %eax
jb 0x6ee9
movq (%rbx), %rax
movl 0x18(%r14), %edx
movq %rbx, %rdi
movq %r12, %rsi
callq *0x48(%rax)
testl %eax, %eax
movq %rbp, %r13
jne 0x6f08
cmpl $0x0, 0x10(%rbx)
je 0x6e79
movq (%rbx), %rax
movq 0x40(%rax), %rax
testq %rax, %rax
je 0x6e79
movl 0x18(%r14), %edx
movq %rbx, %rdi
movq %r12, %rsi
callq *%rax
testl %eax, %eax
jne 0x6f0f
movq %r12, %rdi
callq 0x4765
xorl %ebp, %ebp
movq (%r14), %r14
cmpq %r13, %r14
jne 0x6d5d
jmp 0x6f21
movl %r15d, %ebp
jmp 0x6f49
callq 0x2c5a
jmp 0x6ebe
movq 0x614e(%rip), %rax      # 0xcff8
movq (%rax), %rdi
leaq 0x2594(%rip), %rsi      # 0x9448
movq %r15, %rdx
xorl %eax, %eax
callq 0x2180
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0x6f60
movq %r13, %rdi
callq 0x2b9b
jmp 0x6f03
movq %r12, %rdi
callq 0x2c03
jmp 0x6f21
movq %r14, %rdi
movq %r12, %rsi
callq 0x2c2d
jmp 0x6f21
movq 0x6108(%rip), %rcx      # 0xcff8
movq (%rcx), %rdi
leaq 0x25ca(%rip), %rsi      # 0x94c4
movl %eax, %ecx
xorl %eax, %eax
callq 0x2180
movq %rbp, %r13
jmp 0x6f14
callq 0x2bbf
jmp 0x6f14
callq 0x2be1
movq %r12, %rdi
callq 0x4765
movl $0x1, %ebp
movq 0x66b8(%rip), %r14      # 0xd5e0
cmpq %r13, %r14
je 0x6f49
movq (%r14), %r15
movq 0x10(%r14), %rdi
callq 0x2030
movq %r14, %rdi
callq 0x2030
movq %r15, %r14
cmpq %r13, %r15
jne 0x6f2d
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x30(%rax)
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x10(%rax)
callq 0x7353
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq