// File: /workspace/github_c_makefile_star2_O0/orempel[P]multiboot_tool/eprom_prog.c
// Function: eprog_open
static int eprog_open(struct multiboot *mboot)
{
    struct eprog_privdata *p_prog = (struct eprog_privdata *)mboot->privdata;

    if (p_prog->device == NULL)
    {
        fprintf(stderr, "abort: no device given\n");
        return -1;
    }

    if (p_prog->eprom_type == 0)
    {
        fprintf(stderr, "abort: no EPROM type given\n");
        return -1;
    }

    if (eprog_open_device(p_prog) < 0)
    {
        return -1;
    }

    if (eprog_sync(p_prog) < 0)
    {
        fprintf(stderr, "failed to sync\n");
        eprog_close_device(p_prog);
        return -1;
    }

    if (eprog_get_version(p_prog, p_prog->version, sizeof(p_prog->version)) < 0)
    {
        fprintf(stderr, "failed to get version\n");
        eprog_close_device(p_prog);
        return -1;
    }

    if ((eprog_get_pagesize(p_prog, &p_prog->pagesize_max) < 0) ||
        (p_prog->pagesize_max == 0)
       )
    {
        fprintf(stderr, "failed to get pagesize\n");
        eprog_close_device(p_prog);
        return -1;
    }

    p_prog->pagesize = MIN(p_prog->pagesize, p_prog->pagesize_max);
    if (eprog_set_config(p_prog, p_prog->eprom_type, p_prog->pagesize, p_prog->reset_polarity) < 0)
    {
        fprintf(stderr, "failed to set configuration\n");
        eprog_close_device(p_prog);
        return -1;
    }

    if (eprog_set_progmode(p_prog, PROGMODE_ENABLED) < 0)
    {
        fprintf(stderr, "failed to enter progmode\n");
        eprog_close_device(p_prog);
        return -1;
    }

    p_prog->progmode = PROGMODE_ENABLED;

    printf("device         : %-16s\n",
           p_prog->device);

    printf("version        : %-32s\n",
           p_prog->version);

    printf("reset polarity : %-4s\n",
           p_prog->reset_polarity ? "high" : "low");

    printf("EPROM size     : 0x%05x (%d)\n",
           p_prog->eprom_type * 1024,
           p_prog->eprom_type * 1024);

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x20, %rsp
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rax
cmpq $0x0, (%rax)
jne 0x3936
movq 0xc6df(%rip), %rax      # 0xfff8
movq (%rax), %rdi
leaq 0x7a82(%rip), %rsi      # 0xb3a5
movb $0x0, %al
callq 0x2160
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x3bad
movq -0x18(%rbp), %rax
movzbl 0x69(%rax), %eax
cmpl $0x0, %eax
jne 0x3967
movq 0xc6ae(%rip), %rax      # 0xfff8
movq (%rax), %rdi
leaq 0x7eaf(%rip), %rsi      # 0xb803
movb $0x0, %al
callq 0x2160
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x3bad
movq -0x18(%rbp), %rdi
callq 0x42b0
cmpl $0x0, %eax
jge 0x3981
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x3bad
movq -0x18(%rbp), %rdi
callq 0x43b0
cmpl $0x0, %eax
jge 0x39bc
movq 0xc662(%rip), %rax      # 0xfff8
movq (%rax), %rdi
leaq 0x7e7f(%rip), %rsi      # 0xb81f
movb $0x0, %al
callq 0x2160
movq -0x18(%rbp), %rdi
callq 0x4470
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x3bad
movq -0x18(%rbp), %rdi
movq -0x18(%rbp), %rsi
addq $0x48, %rsi
movl $0x20, %edx
callq 0x44b0
cmpl $0x0, %eax
jge 0x3a04
movq 0xc61a(%rip), %rax      # 0xfff8
movq (%rax), %rdi
leaq 0x7e47(%rip), %rsi      # 0xb82f
movb $0x0, %al
callq 0x2160
movq -0x18(%rbp), %rdi
callq 0x4470
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x3bad
movq -0x18(%rbp), %rdi
movq -0x18(%rbp), %rsi
addq $0x68, %rsi
callq 0x4560
cmpl $0x0, %eax
jl 0x3a27
movq -0x18(%rbp), %rax
movzbl 0x68(%rax), %eax
cmpl $0x0, %eax
jne 0x3a54
movq 0xc5ca(%rip), %rax      # 0xfff8
movq (%rax), %rdi
leaq 0x7e0e(%rip), %rsi      # 0xb846
movb $0x0, %al
callq 0x2160
movq -0x18(%rbp), %rdi
callq 0x4470
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x3bad
movq -0x18(%rbp), %rax
movzbl 0x6a(%rax), %eax
movq -0x18(%rbp), %rcx
movzbl 0x68(%rcx), %ecx
cmpl %ecx, %eax
jge 0x3a75
movq -0x18(%rbp), %rax
movzbl 0x6a(%rax), %eax
movl %eax, -0x1c(%rbp)
jmp 0x3a80
movq -0x18(%rbp), %rax
movzbl 0x68(%rax), %eax
movl %eax, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
movb %al, %cl
movq -0x18(%rbp), %rax
movb %cl, 0x6a(%rax)
movq -0x18(%rbp), %rdi
movq -0x18(%rbp), %rax
movb 0x69(%rax), %dl
movq -0x18(%rbp), %rax
movb 0x6a(%rax), %cl
movq -0x18(%rbp), %rax
movl 0x6c(%rax), %eax
movzbl %dl, %esi
movzbl %cl, %edx
movzbl %al, %ecx
callq 0x45f0
cmpl $0x0, %eax
jge 0x3ae5
movq 0xc539(%rip), %rax      # 0xfff8
movq (%rax), %rdi
leaq 0x7d95(%rip), %rsi      # 0xb85e
movb $0x0, %al
callq 0x2160
movq -0x18(%rbp), %rdi
callq 0x4470
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x3bad
movq -0x18(%rbp), %rdi
movl $0x1, %esi
callq 0x4690
cmpl $0x0, %eax
jge 0x3b25
movq 0xc4f9(%rip), %rax      # 0xfff8
movq (%rax), %rdi
leaq 0x78b4(%rip), %rsi      # 0xb3bd
movb $0x0, %al
callq 0x2160
movq -0x18(%rbp), %rdi
callq 0x4470
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x3bad
movq -0x18(%rbp), %rax
movb $0x1, 0x70(%rax)
movq -0x18(%rbp), %rax
movq (%rax), %rsi
leaq 0x7d40(%rip), %rdi      # 0xb87b
movb $0x0, %al
callq 0x20d0
movq -0x18(%rbp), %rsi
addq $0x48, %rsi
leaq 0x7d42(%rip), %rdi      # 0xb893
movb $0x0, %al
callq 0x20d0
movq -0x18(%rbp), %rax
movl 0x6c(%rax), %ecx
leaq 0x7a7b(%rip), %rsi      # 0xb5e1
leaq 0x7a6f(%rip), %rax      # 0xb5dc
cmpl $0x0, %ecx
cmovneq %rax, %rsi
leaq 0x7d30(%rip), %rdi      # 0xb8ab
movb $0x0, %al
callq 0x20d0
movq -0x18(%rbp), %rax
movzbl 0x69(%rax), %esi
shll $0xa, %esi
movq -0x18(%rbp), %rax
movzbl 0x69(%rax), %edx
shll $0xa, %edx
leaq 0x7d23(%rip), %rdi      # 0xb8c2
movb $0x0, %al
callq 0x20d0
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x20, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)