// File: /workspace/github_c_makefile_star2_O3/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 %r12
pushq %rbx
subq $0x40, %rsp
movq 0x8(%rdi), %rbx
movq (%rbx), %rdi
testq %rdi, %rdi
je 0x3e96
cmpb $0x0, 0x69(%rbx)
je 0x3e9d
movl $0x80102, %esi          # imm = 0x80102
xorl %eax, %eax
callq 0x2210
movl %eax, 0x44(%rbx)
testl %eax, %eax
js 0x3ea4
leaq 0x44(%rbx), %r14
leaq 0x8(%rbx), %rsi
movl %eax, %edi
callq 0x21f0
testl %eax, %eax
js 0x3eb2
xorps %xmm0, %xmm0
leaq 0x4(%rsp), %rdx
movups %xmm0, 0x4(%rdx)
movups %xmm0, 0x14(%rdx)
movups %xmm0, 0x24(%rdx)
movq $0x0, 0x34(%rdx)
movl $0x1, (%rdx)
movl $0x18b2, 0x8(%rdx)      # imm = 0x18B2
movb $0x1, 0x17(%rdx)
movl (%r14), %edi
xorl %esi, %esi
callq 0x2200
testl %eax, %eax
js 0x3ebc
movl $0x100, %ebp            # imm = 0x100
leaq 0x3(%rsp), %r15
leaq 0x4(%rsp), %r12
movb $0x0, 0x3(%rsp)
movl (%r14), %edi
movl $0x1, %edx
movq %r15, %rsi
callq 0x2090
testl %eax, %eax
jle 0x3d14
movl (%r14), %edi
movq %r12, %rsi
movl $0x3, %edx
movl $0x5, %ecx
callq 0x43eb
cmpl $0x3, %eax
jne 0x3d08
movb 0x4(%rsp), %al
negb %al
jno 0x3d08
cmpb $0x1, 0x5(%rsp)
jne 0x3d08
cmpb $0x1, 0x6(%rsp)
je 0x3d1a
decl %ebp
cmpl $0x1, %ebp
ja 0x3cbf
jmp 0x3ed2
js 0x3ed2
leaq 0x48(%rbx), %r15
movl 0x44(%rbx), %edi
leaq 0x4b03(%rip), %rsi      # 0x882b
movl $0x2, %edx
callq 0x2090
testl %eax, %eax
jle 0x3d65
movl (%r14), %edi
movl $0x81, %esi
movq %r15, %rdx
movl $0x20, %ecx
callq 0x452c
testl %eax, %eax
jle 0x3edc
cmpl $0x1f, %eax
movl $0x1f, %ecx
cmovbl %eax, %ecx
movb $0x0, (%r15,%rcx)
jmp 0x3d6b
js 0x3edc
movl 0x44(%rbx), %edi
leaq 0x4b7c(%rip), %rsi      # 0x88f1
movl $0x2, %edx
callq 0x2090
testl %eax, %eax
jle 0x3dac
movl (%r14), %edi
leaq 0x4(%rsp), %rdx
movl $0x82, %esi
movl $0x1, %ecx
callq 0x452c
cmpl $0x1, %eax
jne 0x3e8c
movb 0x4(%rsp), %al
movb %al, 0x68(%rbx)
jmp 0x3db5
js 0x3e8c
movb 0x68(%rbx), %al
testb %al, %al
je 0x3e8c
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 0x4(%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 0x3e0a
movl (%r14), %edi
movl $0x83, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x452c
sarl $0x1f, %eax
testl %eax, %eax
js 0x3ee6
movq %rbx, %rdi
movl $0x1, %esi
callq 0x43a1
testl %eax, %eax
js 0x3ef0
movb $0x1, 0x70(%rbx)
movq (%rbx), %rsi
leaq 0x4991(%rip), %rdi      # 0x87c6
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x20f0
leaq 0x4999(%rip), %rdi      # 0x87de
movq %r15, %rsi
xorl %eax, %eax
callq 0x20f0
cmpl $0x0, 0x6c(%rbx)
leaq 0x46d2(%rip), %rax      # 0x852c
leaq 0x46c6(%rip), %rsi      # 0x8527
cmoveq %rax, %rsi
leaq 0x498a(%rip), %rdi      # 0x87f6
xorl %eax, %eax
callq 0x20f0
movzbl 0x69(%rbx), %edx
shll $0xa, %edx
leaq 0x498c(%rip), %rdi      # 0x880d
movl %edx, %esi
xorl %eax, %eax
callq 0x20f0
jmp 0x3efd
movq %rbx, %rdi
callq 0x24f2
jmp 0x3ef8
callq 0x25a4
jmp 0x3ef8
callq 0x2588
jmp 0x3ef8
leaq 0x4adf(%rip), %rdi      # 0x898a
callq 0x2240
jmp 0x3ef8
movq %r14, %rdi
callq 0x2570
jmp 0x3ef8
leaq 0x4580(%rip), %rdi      # 0x8443
callq 0x2240
movl (%r14), %edi
callq 0x2140
jmp 0x3ef8
movq %rbx, %rdi
callq 0x2546
jmp 0x3ef8
movq %rbx, %rdi
callq 0x251c
jmp 0x3ef8
movq %rbx, %rdi
callq 0x24c8
jmp 0x3ef8
movq %rbx, %rdi
callq 0x249e
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movl %ebp, %eax
addq $0x40, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq