// File: /workspace/github_c_makefile_star2_O2/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 $0x48, %rsp
movq 0x8(%rdi), %rbx
movq (%rbx), %rdi
testq %rdi, %rdi
je 0x3211
cmpb $0x0, 0x69(%rbx)
je 0x3226
movl $0x80102, %esi          # imm = 0x80102
xorl %eax, %eax
callq 0x2210
movl %eax, 0x44(%rbx)
testl %eax, %eax
js 0x3247
leaq 0x8(%rbx), %rsi
movl %eax, %edi
callq 0x21f0
testl %eax, %eax
js 0x3258
xorps %xmm0, %xmm0
leaq 0xc(%rsp), %rdx
movups %xmm0, 0x4(%rdx)
movups %xmm0, 0x14(%rdx)
movups %xmm0, 0x24(%rdx)
andq $0x0, 0x34(%rdx)
movl $0x1, (%rdx)
movl $0x18b2, 0x8(%rdx)      # imm = 0x18B2
movb $0x1, 0x17(%rdx)
movl 0x44(%rbx), %edi
xorl %esi, %esi
callq 0x2200
testl %eax, %eax
js 0x3261
movl $0xffffff01, %r12d      # imm = 0xFFFFFF01
leaq 0xb(%rsp), %r14
pushq $0x1
popq %r15
pushq $0x3
popq %rbp
pushq $0x5
popq %r13
testl %r12d, %r12d
je 0x3277
movb $0x0, 0xb(%rsp)
movl 0x44(%rbx), %edi
movq %r14, %rsi
movq %r15, %rdx
callq 0x2090
testl %eax, %eax
jle 0x308c
movl 0x44(%rbx), %edi
leaq 0xc(%rsp), %rsi
movl %ebp, %edx
movl %r13d, %ecx
callq 0x37f6
cmpl $0x3, %eax
jne 0x3087
movb 0xc(%rsp), %al
negb %al
jno 0x3087
cmpb $0x1, 0xd(%rsp)
jne 0x3087
cmpb $0x1, 0xe(%rsp)
je 0x3092
incl %r12d
jmp 0x303a
js 0x3277
leaq 0x48(%rbx), %r14
movl 0x44(%rbx), %edi
leaq 0x578b(%rip), %rsi      # 0x882b
pushq $0x2
popq %rdx
callq 0x2090
testl %eax, %eax
jle 0x30d7
movl 0x44(%rbx), %edi
pushq $0x20
popq %rcx
movl $0x81, %esi
movq %r14, %rdx
callq 0x3906
testl %eax, %eax
jle 0x328c
cmpl $0x1f, %eax
pushq $0x1f
popq %rcx
cmovbl %eax, %ecx
movb $0x0, (%r14,%rcx)
jmp 0x30dd
js 0x328c
movl 0x44(%rbx), %edi
leaq 0x580a(%rip), %rsi      # 0x88f1
pushq $0x2
popq %rdx
callq 0x2090
testl %eax, %eax
jle 0x311a
movl 0x44(%rbx), %edi
leaq 0xc(%rsp), %rdx
pushq $0x1
popq %rcx
movl $0x82, %esi
callq 0x3906
cmpl $0x1, %eax
jne 0x31f9
movb 0xc(%rsp), %al
movb %al, 0x68(%rbx)
jmp 0x3123
js 0x31f9
movb 0x68(%rbx), %al
testb %al, %al
je 0x31f9
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 0xc(%rsp), %rsi
movw $0x303, (%rsi)          # imm = 0x303
movb %al, 0x2(%rsi)
movb %dl, 0x3(%rsi)
movb %cl, 0x4(%rsi)
movl 0x44(%rbx), %edi
pushq $0x5
popq %rdx
callq 0x2090
testl %eax, %eax
jle 0x3176
movl 0x44(%rbx), %edi
movl $0x83, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x3906
sarl $0x1f, %eax
testl %eax, %eax
js 0x32a1
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0x37ae
testl %eax, %eax
js 0x32b6
movb $0x1, 0x70(%rbx)
movq (%rbx), %rsi
leaq 0x5627(%rip), %rdi      # 0x87c6
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x20f0
leaq 0x562f(%rip), %rdi      # 0x87de
movq %r14, %rsi
xorl %eax, %eax
callq 0x20f0
cmpl $0x0, 0x6c(%rbx)
leaq 0x5368(%rip), %rax      # 0x852c
leaq 0x535c(%rip), %rsi      # 0x8527
cmoveq %rax, %rsi
leaq 0x5620(%rip), %rdi      # 0x87f6
xorl %eax, %eax
callq 0x20f0
movzbl 0x69(%rbx), %edx
shll $0xa, %edx
leaq 0x5622(%rip), %rdi      # 0x880d
movl %edx, %esi
xorl %eax, %eax
callq 0x20f0
jmp 0x32dd
movq 0x9df8(%rip), %rax      # 0xcff8
movq (%rax), %rcx
leaq 0x5587(%rip), %rdi      # 0x8791
pushq $0x17
jmp 0x32c9
movq 0x9de0(%rip), %rax      # 0xcff8
movq (%rax), %rcx
leaq 0x50cf(%rip), %rdi      # 0x82f1
pushq $0x17
jmp 0x3239
movq 0x9dcb(%rip), %rax      # 0xcff8
movq (%rax), %rcx
leaq 0x5517(%rip), %rdi      # 0x874e
pushq $0x1b
popq %rsi
pushq $0x1
popq %rdx
callq 0x2280
jmp 0x32da
leaq 0x572a(%rip), %rdi      # 0x8978
callq 0x2240
jmp 0x32da
leaq 0x51d2(%rip), %rdi      # 0x8431
jmp 0x3268
leaq 0x51db(%rip), %rdi      # 0x8443
callq 0x2240
movl 0x44(%rbx), %edi
callq 0x2140
jmp 0x32da
movq 0x9d7a(%rip), %rax      # 0xcff8
movq (%rax), %rcx
leaq 0x54e2(%rip), %rdi      # 0x876a
pushq $0xf
jmp 0x32c9
movq 0x9d65(%rip), %rax      # 0xcff8
movq (%rax), %rcx
leaq 0x54dd(%rip), %rdi      # 0x877a
pushq $0x16
jmp 0x32c9
movq 0x9d50(%rip), %rax      # 0xcff8
movq (%rax), %rcx
leaq 0x54f7(%rip), %rdi      # 0x87a9
pushq $0x1c
jmp 0x32c9
movq 0x9d3b(%rip), %rax      # 0xcff8
movq (%rax), %rcx
leaq 0x5042(%rip), %rdi      # 0x8309
pushq $0x19
popq %rsi
pushq $0x1
popq %rdx
callq 0x2280
movq %rbx, %rdi
callq 0x3789
pushq $-0x1
popq %rbp
movl %ebp, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq