// File: /workspace/github_c_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq 0x8(%rdi), %rbx
movq (%rbx), %rdi
testq %rdi, %rdi
je 0x3ecf
cmpb $0x0, 0x69(%rbx)
je 0x3ed6
movl $0x80102, %esi          # imm = 0x80102
xorl %eax, %eax
callq 0x2200
movl %eax, 0x44(%rbx)
testl %eax, %eax
js 0x3edd
leaq 0x44(%rbx), %r14
leaq 0x8(%rbx), %rsi
movl %eax, %edi
callq 0x21e0
testl %eax, %eax
js 0x3eeb
xorps %xmm0, %xmm0
leaq 0x10(%rsp), %rdx
movaps %xmm0, (%rdx)
movaps %xmm0, 0x10(%rdx)
movups %xmm0, 0x2c(%rdx)
movaps %xmm0, 0x20(%rdx)
movl $0x1, (%rdx)
orl $0x18b2, 0x8(%rdx)      # imm = 0x18B2
movw $0x100, 0x16(%rdx)      # imm = 0x100
movl (%r14), %edi
xorl %esi, %esi
callq 0x21f0
testl %eax, %eax
js 0x3ef5
movl $0xff, %ebp
leaq 0xf(%rsp), %r15
testl %ebp, %ebp
jle 0x3d37
movb $0x0, 0xf(%rsp)
movl (%r14), %edi
movl $0x1, %edx
movq %r15, %rsi
callq 0x2090
xorl %r12d, %r12d
testl %eax, %eax
jle 0x3d26
movl (%r14), %edi
leaq 0x10(%rsp), %rsi
movl $0x3, %edx
movl $0x5, %ecx
callq 0x443a
cmpl $0x3, %eax
jne 0x3d2b
movb 0x10(%rsp), %al
negb %al
jno 0x3d2b
cmpb $0x1, 0x11(%rsp)
jne 0x3d2b
cmpb $0x1, 0x12(%rsp)
jne 0x3d2b
xorl %r13d, %r13d
jmp 0x3d30
movl %eax, %r13d
jmp 0x3d30
decl %ebp
movb $0x1, %r12b
testb %r12b, %r12b
jne 0x3ccf
jmp 0x3d3d
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
testl %r13d, %r13d
js 0x3f0b
leaq 0x48(%rbx), %r15
movl 0x44(%rbx), %edi
leaq 0x4ad7(%rip), %rsi      # 0x882b
movl $0x2, %edx
callq 0x2090
testl %eax, %eax
jle 0x3d97
movl (%r14), %edi
movl $0x81, %esi
movq %r15, %rdx
movl $0x20, %ecx
callq 0x45ad
testl %eax, %eax
jle 0x3d92
movl %eax, %ecx
addq %r15, %rcx
leaq 0x67(%rbx), %rdx
cmpl $0x20, %eax
cmovlq %rcx, %rdx
movb $0x0, (%rdx)
xorl %eax, %eax
jmp 0x3d97
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
testl %eax, %eax
js 0x3f15
movl 0x44(%rbx), %edi
leaq 0x4b48(%rip), %rsi      # 0x88f1
movl $0x2, %edx
callq 0x2090
testl %eax, %eax
jle 0x3de3
movl (%r14), %edi
leaq 0x10(%rsp), %rdx
movl $0x82, %esi
movl $0x1, %ecx
callq 0x45ad
movl %eax, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl $0x1, %ecx
jne 0x3de3
movb 0x10(%rsp), %al
movb %al, 0x68(%rbx)
xorl %eax, %eax
testl %eax, %eax
js 0x3ec5
movb 0x68(%rbx), %al
testb %al, %al
je 0x3ec5
movzbl 0x6a(%rbx), %ecx
movzbl %al, %edx
cmpb %al, %cl
cmovbl %ecx, %edx
movb %dl, 0x6a(%rbx)
movb 0x69(%rbx), %al
movb 0x6c(%rbx), %cl
leaq 0x10(%rsp), %rsi
movw $0x303, (%rsi)          # imm = 0x303
movb %al, 0x2(%rsi)
movb %dl, 0x3(%rsi)
movb %cl, 0x4(%rsi)
movl 0x44(%rbx), %edi
movl $0x5, %edx
callq 0x2090
testl %eax, %eax
jle 0x3e43
movl (%r14), %edi
movl $0x83, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x45ad
sarl $0x1f, %eax
testl %eax, %eax
js 0x3f1f
movq %rbx, %rdi
movl $0x1, %esi
callq 0x43f0
testl %eax, %eax
js 0x3f29
movb $0x1, 0x70(%rbx)
movq (%rbx), %rsi
leaq 0x4958(%rip), %rdi      # 0x87c6
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x20f0
leaq 0x4960(%rip), %rdi      # 0x87de
movq %r15, %rsi
xorl %eax, %eax
callq 0x20f0
cmpl $0x0, 0x6c(%rbx)
leaq 0x4699(%rip), %rax      # 0x852c
leaq 0x468d(%rip), %rsi      # 0x8527
cmoveq %rax, %rsi
leaq 0x4951(%rip), %rdi      # 0x87f6
xorl %eax, %eax
callq 0x20f0
movzbl 0x69(%rbx), %edx
shll $0xa, %edx
leaq 0x4953(%rip), %rdi      # 0x880d
movl %edx, %esi
xorl %eax, %eax
callq 0x20f0
jmp 0x3f36
movq %rbx, %rdi
callq 0x2557
jmp 0x3f31
callq 0x2609
jmp 0x3f31
callq 0x25ed
jmp 0x3f31
leaq 0x4a94(%rip), %rdi      # 0x8978
callq 0x2230
jmp 0x3f31
movq %r14, %rdi
callq 0x25d5
jmp 0x3f31
leaq 0x4547(%rip), %rdi      # 0x8443
callq 0x2230
movl (%r14), %edi
callq 0x2140
jmp 0x3f31
movq %rbx, %rdi
callq 0x25ab
jmp 0x3f31
movq %rbx, %rdi
callq 0x2581
jmp 0x3f31
movq %rbx, %rdi
callq 0x252d
jmp 0x3f31
movq %rbx, %rdi
callq 0x2503
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movl %ebp, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq