// File: coduin[P]epiphany-bin-info/epiphany-bin-info.cpp
// Function: parseProgramHeader(Elf32_Phdr*, unsigned long)
void parseProgramHeader(Elf32_Phdr* phdr, size_t count) {
    int localsize = 0, extmemsize = 0;
    for (size_t ihdr = 0; ihdr < count; ihdr++) {
        if (isExtmem(phdr[ihdr].p_vaddr))
            extmemsize += phdr[ihdr].p_memsz;
        else
            localsize += phdr[ihdr].p_memsz;
    }

    cout << "Total size in local memory:    " << ashex(localsize) << " = "
         << localsize / 1024 << " KB" << endl;
    cout << "Total size in external memory: " << ashex(extmemsize) << " = "
         << extmemsize / 1024 << " KB" << endl;

    if (showProgramHeaders) {
        cout << "\nProgram header table (runtime info):\n";
        cout << "VADDR       SIZE\t\t\tFILESIZE\n";
        cout << setfill('0') << hex;
        for (size_t ihdr = 0; ihdr < count; ihdr++) {
            cout << addr(phdr[ihdr].p_vaddr);
            cout << "  " << ashex(phdr[ihdr].p_memsz);
            cout << " (" << phdr[ihdr].p_memsz / 1024 << " KB)";
            cout << "\t" << ashex(phdr[ihdr].p_filesz) << endl;
        }
    }
}

// Assembly:
subq $0xf8, %rsp
movq %rdi, 0xf0(%rsp)
movq %rsi, 0xe8(%rsp)
movl $0x0, 0xe4(%rsp)
movl $0x0, 0xe0(%rsp)
movq $0x0, 0xd8(%rsp)
movq 0xd8(%rsp), %rax
cmpq 0xe8(%rsp), %rax
jae 0x29c1
movq 0xf0(%rsp), %rax
movq 0xd8(%rsp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0x8(%rax), %edi
callq 0x2560
testb $0x1, %al
jne 0x2954
jmp 0x297e
movq 0xf0(%rsp), %rax
movq 0xd8(%rsp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0x14(%rax), %eax
addl 0xe0(%rsp), %eax
movl %eax, 0xe0(%rsp)
jmp 0x29a6
movq 0xf0(%rsp), %rax
movq 0xd8(%rsp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0x14(%rax), %eax
addl 0xe4(%rsp), %eax
movl %eax, 0xe4(%rsp)
jmp 0x29a8
movq 0xd8(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xd8(%rsp)
jmp 0x2919
movq 0x8608(%rip), %rdi      # 0xafd0
leaq 0x5672(%rip), %rsi      # 0x8041
callq 0x21d0
movq %rax, 0x88(%rsp)
movl 0xe4(%rsp), %esi
leaq 0xd4(%rsp), %rdi
movq %rdi, 0x90(%rsp)
callq 0x4720
movq 0x88(%rsp), %rdi
movq 0x90(%rsp), %rsi
callq 0x25e0
movq %rax, 0x98(%rsp)
jmp 0x2a17
movq 0x98(%rsp), %rdi
leaq 0x563b(%rip), %rsi      # 0x8061
callq 0x21d0
movq %rax, 0x80(%rsp)
jmp 0x2a35
movq 0x80(%rsp), %rdi
movl 0xe4(%rsp), %esi
movl %esi, %eax
addl $0x3ff, %eax            # imm = 0x3FF
testl %esi, %esi
cmovsl %eax, %esi
sarl $0xa, %esi
callq 0x2370
movq %rax, 0x78(%rsp)
jmp 0x2a5f
movq 0x78(%rsp), %rdi
leaq 0x55fa(%rip), %rsi      # 0x8065
callq 0x21d0
movq %rax, 0x70(%rsp)
jmp 0x2a77
movq 0x70(%rsp), %rdi
movq 0x853d(%rip), %rsi      # 0xafc0
callq 0x2200
jmp 0x2a8a
leaq 0xd4(%rsp), %rdi
callq 0x4740
movq 0x8532(%rip), %rdi      # 0xafd0
leaq 0x55c4(%rip), %rsi      # 0x8069
callq 0x21d0
movq %rax, 0x58(%rsp)
movl 0xe0(%rsp), %esi
leaq 0xc0(%rsp), %rdi
movq %rdi, 0x60(%rsp)
callq 0x4720
movq 0x58(%rsp), %rdi
movq 0x60(%rsp), %rsi
callq 0x25e0
movq %rax, 0x68(%rsp)
jmp 0x2ade
movq 0x68(%rsp), %rdi
leaq 0x5577(%rip), %rsi      # 0x8061
callq 0x21d0
movq %rax, 0x50(%rsp)
jmp 0x2af6
movq 0x50(%rsp), %rdi
movl 0xe0(%rsp), %esi
movl %esi, %eax
addl $0x3ff, %eax            # imm = 0x3FF
testl %esi, %esi
cmovsl %eax, %esi
sarl $0xa, %esi
callq 0x2370
movq %rax, 0x48(%rsp)
jmp 0x2b1d
movq 0x48(%rsp), %rdi
leaq 0x553c(%rip), %rsi      # 0x8065
callq 0x21d0
movq %rax, 0x40(%rsp)
jmp 0x2b35
movq 0x40(%rsp), %rdi
movq 0x847f(%rip), %rsi      # 0xafc0
callq 0x2200
jmp 0x2b48
leaq 0xc0(%rsp), %rdi
callq 0x4740
testb $0x1, 0x86c6(%rip)      # 0xb222
je 0x2e15
movq 0x8467(%rip), %rdi      # 0xafd0
leaq 0x5519(%rip), %rsi      # 0x8089
callq 0x21d0
movq 0x8454(%rip), %rdi      # 0xafd0
leaq 0x552d(%rip), %rsi      # 0x80b0
callq 0x21d0
movl $0x30, %edi
callq 0x46a0
movb %al, 0xbf(%rsp)
movq 0x8430(%rip), %rdi      # 0xafd0
movzbl 0xbf(%rsp), %esi
callq 0x2340
movq %rax, %rdi
leaq 0x1b09(%rip), %rsi      # 0x46c0
callq 0x20b0
movq $0x0, 0xb0(%rsp)
movq 0xb0(%rsp), %rax
cmpq 0xe8(%rsp), %rax
jae 0x2e13
movq 0xf0(%rsp), %rax
movq 0xb0(%rsp), %rcx
shlq $0x5, %rcx
movl 0x8(%rax,%rcx), %esi
leaq 0xac(%rsp), %rdi
movq %rdi, 0x38(%rsp)
callq 0x4750
movq 0x38(%rsp), %rsi
movq 0x83bc(%rip), %rdi      # 0xafd0
callq 0x2680
jmp 0x2c1b
leaq 0xac(%rsp), %rdi
callq 0x4770
movq 0x83a1(%rip), %rdi      # 0xafd0
leaq 0x54e6(%rip), %rsi      # 0x811c
callq 0x21d0
movq %rax, 0x28(%rsp)
movq 0xf0(%rsp), %rax
movq 0xb0(%rsp), %rcx
shlq $0x5, %rcx
movl 0x14(%rax,%rcx), %esi
leaq 0xa8(%rsp), %rdi
movq %rdi, 0x30(%rsp)
callq 0x4720
movq 0x28(%rsp), %rdi
movq 0x30(%rsp), %rsi
callq 0x25e0
jmp 0x2c7b
leaq 0xa8(%rsp), %rdi
callq 0x4740
movq 0x8341(%rip), %rdi      # 0xafd0
movq %rdi, 0x8(%rsp)
leaq 0x5432(%rip), %rsi      # 0x80cd
callq 0x21d0
movq %rax, %rdi
movq 0xf0(%rsp), %rax
movq 0xb0(%rsp), %rcx
shlq $0x5, %rcx
movl 0x14(%rax,%rcx), %esi
shrl $0xa, %esi
callq 0x22e0
movq %rax, %rdi
leaq 0x5403(%rip), %rsi      # 0x80d0
callq 0x21d0
movq 0x8(%rsp), %rdi
leaq 0x53f7(%rip), %rsi      # 0x80d5
callq 0x21d0
movq %rax, 0x10(%rsp)
movq 0xf0(%rsp), %rax
movq 0xb0(%rsp), %rcx
shlq $0x5, %rcx
movl 0x10(%rax,%rcx), %esi
leaq 0xa4(%rsp), %rdi
movq %rdi, 0x18(%rsp)
callq 0x4720
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rsi
callq 0x25e0
movq %rax, 0x20(%rsp)
jmp 0x2d28
movq 0x20(%rsp), %rdi
movq 0x828c(%rip), %rsi      # 0xafc0
callq 0x2200
jmp 0x2d3b
leaq 0xa4(%rsp), %rdi
callq 0x4740
movq 0xb0(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xb0(%rsp)
jmp 0x2bc8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xc8(%rsp)
movl %eax, 0xc4(%rsp)
leaq 0xd4(%rsp), %rdi
callq 0x4740
jmp 0x2e1d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xc8(%rsp)
movl %eax, 0xc4(%rsp)
leaq 0xc0(%rsp), %rdi
callq 0x4740
jmp 0x2e1d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xc8(%rsp)
movl %eax, 0xc4(%rsp)
leaq 0xac(%rsp), %rdi
callq 0x4770
jmp 0x2e1d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xc8(%rsp)
movl %eax, 0xc4(%rsp)
leaq 0xa8(%rsp), %rdi
callq 0x4740
jmp 0x2e1d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xc8(%rsp)
movl %eax, 0xc4(%rsp)
leaq 0xa4(%rsp), %rdi
callq 0x4740
jmp 0x2e1d
jmp 0x2e15
addq $0xf8, %rsp
retq
movq 0xc8(%rsp), %rdi
callq 0x23a0
nopw (%rax,%rax)