// File: /workspace/github_c_makefile_star2_O3/erik[P]gamut/flip.c
// Function: main
int main(int argc, char** argv)
{
    uint32_t width = 0,
             height = 0;
    bool flip_v = false,
         flip_h = false;
    pixel_t* image = NULL, temp;

    if (argc < 2) {
        fprintf(stderr, USAGE, argv[0]);
        return 1;
    }

    ARGBEGIN {
    case 'v':
        flip_v = true; break;
    case 'h':
        flip_h = true; break;
    default:
        fprintf(stderr, USAGE, argv[0]);
        return 1;
        ARGEND;
    }

    if (!flip_v && !flip_h) {
        fprintf(stderr, "need to specify a flip direction\n");
        return 1;
    }

    read_header(&width, &height);

    if (!(image = calloc(width * height, sizeof(pixel_t)))) {
        perror("couldn't allocate memory");
        return 1;
    }

    read_image(image, width, height);
    write_header(width, height);

    if (flip_h) {
        for (uint32_t y = 0; y < height; y++) {
            for (uint32_t x = 0; x < width / 2; x++) {
                uint32_t swap = (width - 1) - x;

                memcpy(&temp, PIXEL(&image, x, y), sizeof(pixel_t));
                memcpy(PIXEL(&image, x, y), PIXEL(&image, swap, y), sizeof(pixel_t));
                memcpy(PIXEL(&image, swap, y), &temp, sizeof(pixel_t));
            }
        }
    }

    if (flip_v) {
        for (uint32_t y = 0; y < height / 2; y++) {
            uint32_t swap = (height - 1) - y;
            for (uint32_t x = 0; x < width; x++) {
                memcpy(&temp, PIXEL(&image, x, y), sizeof(pixel_t));
                memcpy(PIXEL(&image, x, y), PIXEL(&image, x, swap), sizeof(pixel_t));
                memcpy(PIXEL(&image, x, swap), &temp, sizeof(pixel_t));
            }
        }
    }

    write_image(image, width, height);

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
cmpl $0x1, %edi
jle 0x162b
movq (%rsi), %rax
movq %rax, 0x2da3(%rip)      # 0x4070
movq 0x8(%rsi), %rdx
testq %rdx, %rdx
je 0x1624
addq $0x8, %rsi
xorl %r15d, %r15d
xorl %ebp, %ebp
movb 0x1(%rdx), %al
testb %al, %al
je 0x1334
cmpb $0x2d, (%rdx)
jne 0x1334
cmpb $0x2d, %al
jne 0x130a
cmpb $0x0, 0x2(%rdx)
jne 0x130a
jmp 0x1334
cmpl $0x68, %eax
jne 0x1607
decq %rdx
movb $0x1, %bpl
incq %rdx
movl %r15d, %ecx
movl %ecx, %r15d
movq %rdx, (%rsi)
movzbl (%rdx), %eax
incq %rdx
movb $0x1, %cl
cmpl $0x76, %eax
je 0x1310
testl %eax, %eax
jne 0x12fb
movq 0x8(%rsi), %rdx
addq $0x8, %rsi
testq %rdx, %rdx
jne 0x12e3
movl %r15d, %eax
orb %bpl, %al
testb $0x1, %al
je 0x1624
pxor %xmm0, %xmm0
leaq 0x20(%rsp), %rdi
movdqa %xmm0, (%rdi)
movq 0x2c82(%rip), %rbx      # 0x3fd8
movq (%rbx), %rcx
movl $0x1, %esi
movl $0x10, %edx
callq 0x1040
cmpq $0x10, %rax
jne 0x165f
movl %ebp, 0x1c(%rsp)
movabsq $0x646c656662726166, %rax # imm = 0x646C656662726166
cmpq %rax, 0x20(%rsp)
jne 0x1664
movl 0x28(%rsp), %ebp
movl 0x2c(%rsp), %eax
movl %ebp, %r12d
bswapl %r12d
movl %eax, 0xc(%rsp)
movl %eax, %r13d
bswapl %r13d
movq %r13, 0x10(%rsp)
imull %r12d, %r13d
movl $0x8, %esi
movq %r13, %rdi
callq 0x1060
testq %rax, %rax
je 0x1635
movq %rax, %r14
movq (%rbx), %rcx
movl $0x8, %esi
movq %rax, %rdi
movq %r13, %rdx
callq 0x1040
cmpq %r13, %rax
jne 0x1669
movl %r12d, %r9d
cmpl $0x0, 0xc(%rsp)
movq 0x10(%rsp), %r8
je 0x142e
xorl %eax, %eax
xorl %ecx, %ecx
movl %eax, %edx
movq %r9, %rsi
testl %ebp, %ebp
je 0x1424
movl %edx, %edi
movq (%r14,%rdi,8), %xmm0
movdqa %xmm0, %xmm1
psrlw $0x8, %xmm1
psllw $0x8, %xmm0
por %xmm1, %xmm0
movq %xmm0, (%r14,%rdi,8)
incl %edx
decq %rsi
jne 0x13fd
incl %ecx
addl %r12d, %eax
cmpl %r8d, %ecx
jne 0x13f4
movq %r9, 0x38(%rsp)
movq 0x2b96(%rip), %rbx      # 0x3fd0
movq (%rbx), %rsi
leaq 0xc59(%rip), %rdi       # 0x209d
callq 0x1050
leaq 0x20(%rsp), %rdi
movl %ebp, (%rdi)
movq (%rbx), %rcx
movl $0x4, %esi
movl $0x1, %edx
callq 0x10a0
cmpq $0x1, %rax
jne 0x1655
movl %ebp, 0x18(%rsp)
movq %r13, 0x40(%rsp)
leaq 0x20(%rsp), %rdi
movl 0xc(%rsp), %ebp
movl %ebp, (%rdi)
movq (%rbx), %rcx
movl $0x4, %esi
movl $0x1, %edx
callq 0x10a0
cmpq $0x1, %rax
jne 0x1655
movl %r15d, %eax
xorb $0x1, %al
orb 0x1c(%rsp), %al
testl %ebp, %ebp
sete %cl
notb %al
orb %cl, %al
testb $0x1, %al
jne 0x1507
movl %r12d, %eax
shrl %eax
movq 0x10(%rsp), %rcx
cmpl $0x1, %ecx
adcl $0x0, %ecx
leal -0x1(%r12), %edx
xorl %esi, %esi
xorl %edi, %edi
movq %rax, %r8
movl %edx, %r9d
movl %esi, %r10d
cmpl $0x2, %r12d
jb 0x14fb
movl %r10d, %r11d
movq (%r14,%r11,8), %rbx
movl %r9d, %r13d
movq (%r14,%r13,8), %rbp
movq %rbp, (%r14,%r11,8)
movq %rbx, (%r14,%r13,8)
incl %r10d
decl %r9d
decq %r8
jne 0x14da
incl %edi
addl %r12d, %esi
addl %r12d, %edx
cmpl %ecx, %edi
jne 0x14cb
testb $0x1, %r15b
movl 0x18(%rsp), %ebp
je 0x156f
cmpl $0x2, 0x10(%rsp)
jb 0x156f
movq 0x10(%rsp), %rdx
movl %edx, %eax
shrl %eax
cmpl $0x1, %r12d
movl %r12d, %ecx
adcl $0x0, %ecx
decl %edx
imull %r12d, %edx
xorl %esi, %esi
xorl %edi, %edi
movq %rcx, %r8
movl %esi, %r9d
movl %edx, %r10d
testl %ebp, %ebp
je 0x1563
movl %r9d, %r11d
movq (%r14,%r11,8), %rbx
movl %r10d, %r15d
movq (%r14,%r15,8), %r13
movq %r13, (%r14,%r11,8)
movq %rbx, (%r14,%r15,8)
incl %r10d
incl %r9d
decq %r8
jne 0x1542
incl %edi
subl %r12d, %edx
addl %r12d, %esi
cmpl %eax, %edi
jne 0x1535
movl $0x8, %esi
movq 0x40(%rsp), %rbx
movq %rbx, %rdi
callq 0x1060
testq %rax, %rax
je 0x166e
movq %rax, %r15
cmpl $0x0, 0xc(%rsp)
movq 0x10(%rsp), %r8
movq 0x38(%rsp), %r9
movq 0x2a2d(%rip), %r10      # 0x3fd0
je 0x15e3
xorl %eax, %eax
xorl %ecx, %ecx
movl %eax, %edx
movq %r9, %rsi
testl %ebp, %ebp
je 0x15d9
movl %edx, %edi
movq (%r14,%rdi,8), %xmm0
movdqa %xmm0, %xmm1
psrlw $0x8, %xmm1
psllw $0x8, %xmm0
por %xmm1, %xmm0
movq %xmm0, (%r15,%rdi,8)
incl %edx
decq %rsi
jne 0x15b2
incl %ecx
addl %r12d, %eax
cmpl %r8d, %ecx
jne 0x15a9
movq (%r10), %rcx
movl $0x8, %esi
movq %r15, %rdi
movq %rbx, %rdx
callq 0x10a0
cmpq %rbx, %rax
jne 0x1673
movq %r15, %rdi
callq 0x1030
xorl %eax, %eax
jmp 0x1646
movq 0x29ea(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
decq %rdx
leaq 0x9e9(%rip), %rsi       # 0x2004
xorl %eax, %eax
callq 0x1070
jmp 0x1641
callq 0x1179
jmp 0x1641
movq %rsi, %rdi
callq 0x1195
jmp 0x1641
leaq 0xa35(%rip), %rdi       # 0x2071
callq 0x1080
movl $0x1, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x1, %edi
callq 0x1090
callq 0x10c0
callq 0x10e5
callq 0x110a
callq 0x1154
callq 0x112f