// File: /workspace/github_c_makefile_star2_O1/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
movq %rbx, %rdi
movl $0x2f, %esi
callq 0x2110
leaq 0x1(%rax), %r12
testq %rax, %rax
cmoveq %rbx, %r12
xorl %r13d, %r13d
leaq 0x60ec(%rip), %r15      # 0xcdb0
movl $0x0, 0x4(%rsp)
xorl %ebx, %ebx
movq (%r13,%r15), %r14
movq (%r14), %rsi
movq %r12, %rdi
callq 0x2160
movb $0x1, %bpl
testl %eax, %eax
jne 0x6cf1
callq *0x8(%r14)
movq %rax, %rbx
testq %rax, %rax
je 0x6d02
testb %bpl, %bpl
je 0x6d2b
addq $0x8, %r13
cmpq $0x28, %r13
jne 0x6cce
jmp 0x6d34
movq 0x62ef(%rip), %rax      # 0xcff8
movq (%rax), %rdi
xorl %ebp, %ebp
leaq 0x26b7(%rip), %rsi      # 0x93cc
movq %r12, %rdx
xorl %eax, %eax
callq 0x2170
movl $0xffffffff, 0x4(%rsp)  # imm = 0xFFFFFFFF
xorl %ebx, %ebx
jmp 0x6cf1
movl 0x4(%rsp), %ebp
jmp 0x6fc5
testq %rbx, %rbx
je 0x6fbb
movl $0x1, 0x10(%rbx)
leaq 0x28b(%rip), %rax       # 0x6fd6
movq %rax, 0x18(%rbx)
leaq 0x67ba(%rip), %rdi      # 0xd510
leaq 0x2e8(%rip), %rdx       # 0x7045
movl $0x6, %esi
movq %rbx, %rcx
callq 0x72e0
movl 0x8(%rsp), %edi
movq 0x10(%rsp), %rsi
callq 0x7383
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpl $-0x1, %eax
je 0x6fc5
movl %eax, %r14d
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x28(%rax)
testl %eax, %eax
jne 0x6fc5
movq 0x6227(%rip), %rax      # 0xcfc8
movq (%rax), %rdi
xorl %esi, %esi
callq 0x20e0
movq 0x681e(%rip), %r15      # 0xd5d0
leaq 0x6817(%rip), %r13      # 0xd5d0
cmpq %r13, %r15
je 0x6f77
movl 0x1c(%r15), %eax
cmpl $0x2, %eax
je 0x6e35
movl $0x1, %ebp
cmpl $0x1, %eax
jne 0x6f69
movq (%rbx), %rax
movl 0x18(%r15), %esi
movq %rbx, %rdi
callq *0x20(%rax)
testl %eax, %eax
je 0x6e54
movl %eax, %edi
callq 0x47ac
testq %rax, %rax
je 0x6e54
movq %rax, %r12
movq (%rbx), %rax
movl 0x18(%r15), %edx
movq %rbx, %rdi
movq %r12, %rsi
callq *0x38(%rax)
testl %eax, %eax
jne 0x6f21
movq 0x10(%r15), %rdi
movq %r12, %rsi
callq 0x4cef
testl %eax, %eax
jne 0x6f28
movb $0x1, %bpl
xorl %r14d, %r14d
movq %r12, %rdi
callq 0x47fb
jmp 0x6e5c
movq 0x10(%r15), %rdi
leaq 0xc(%rsp), %rsi
callq 0x4800
movl $0x1, %ebp
testl %eax, %eax
je 0x6e6d
xorl %r13d, %r13d
jmp 0x6f5d
movl $0x1, %r14d
xorl %ebp, %ebp
testb %bpl, %bpl
movl %r14d, %ebp
jne 0x6f69
jmp 0x6f7a
movl 0xc(%rsp), %edi
callq 0x47ac
testq %rax, %rax
je 0x6e4c
movq %rax, %r12
leaq 0x10(%r15), %r13
movq (%r13), %rdi
movq %rax, %rsi
callq 0x48f6
testl %eax, %eax
jne 0x6f3e
movq (%rbx), %rax
movl 0x18(%r15), %esi
movq %rbx, %rdi
callq *0x20(%rax)
testl %eax, %eax
je 0x6ef8
cmpl 0x4(%r12), %eax
jb 0x6ef8
movq (%rbx), %rax
movl 0x18(%r15), %edx
movq %rbx, %rdi
movq %r12, %rsi
callq *0x48(%rax)
testl %eax, %eax
jne 0x6f48
movb $0x1, %r13b
xorl %ebp, %ebp
cmpl $0x0, 0x10(%rbx)
je 0x6f55
movq (%rbx), %rax
movq 0x40(%rax), %rax
testq %rax, %rax
je 0x6f55
movl 0x18(%r15), %edx
movq %rbx, %rdi
movq %r12, %rsi
callq *%rax
testl %eax, %eax
je 0x6f55
callq 0x2c39
jmp 0x6f4d
movq 0x60f9(%rip), %rcx      # 0xcff8
movq (%rcx), %rdi
movl 0x4(%r12), %edx
xorl %r13d, %r13d
leaq 0x2537(%rip), %rsi      # 0x9448
movl %eax, %ecx
xorl %eax, %eax
callq 0x2170
movl $0x1, %ebp
jmp 0x6f55
callq 0x2c5b
jmp 0x6f31
leaq 0x10(%r15), %rdi
callq 0x2c7d
movl $0x1, %r14d
xorl %ebp, %ebp
jmp 0x6e2b
movq %r13, %rdi
callq 0x2bf3
jmp 0x6f4d
callq 0x2c17
movl $0x1, %ebp
xorl %r13d, %r13d
movq %r12, %rdi
callq 0x47fb
testb %r13b, %r13b
leaq 0x6669(%rip), %r13      # 0xd5d0
je 0x6f7a
movq (%r15), %r15
cmpq %r13, %r15
jne 0x6dc2
jmp 0x6f7a
movl %r14d, %ebp
movq 0x664f(%rip), %r14      # 0xd5d0
cmpq %r13, %r14
je 0x6fa2
movq (%r14), %r15
movq 0x10(%r14), %rdi
callq 0x2030
movq %r14, %rdi
callq 0x2030
movq %r15, %r14
cmpq %r13, %r15
jne 0x6f86
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x30(%rax)
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x10(%rax)
callq 0x7343
jmp 0x6fc5
callq 0x2ca1
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movl %ebp, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq