// File: /workspace/github_c_makefile_star2_O3/nkanaev[P]tipsy/src/tigr.c
// Function: tigrSaveImage
int tigrSaveImage(const char* fileName, Tigr* bmp) {
    Save s;
    long dataPos, dataSize, err;

    // TODO - unicode?
    FILE* out = fopen(fileName, "wb");
    if (!out)
        return 1;

    s.out = out;
    s.adler = 1;
    s.bits = 0x80;
    s.prev = 0xffff;
    s.runlen = 0;

    savePngHeader(&s, bmp);
    dataPos = ftell(s.out);
    dataSize = savePngData(&s, bmp, dataPos);

    // End chunk.
    begin(&s, "IEND", 0);
    put32(&s, ~s.crc);

    // Write back payload size.
    fseek(out, dataPos, SEEK_SET);
    put32(&s, dataSize);

    err = ferror(out);
    fclose(out);
    return !err;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x448, %rsp            # imm = 0x448
movq %rsi, %rbp
leaq 0x7d2f(%rip), %rsi      # 0xca40
callq 0x3400
testq %rax, %rax
je 0x50ac
leaq 0x28(%rsp), %r12
movq %rax, 0x18(%r12)
movaps 0x6320(%rip), %xmm0     # 0xb050
movups %xmm0, 0x4(%r12)
leaq 0x7ea8(%rip), %rdi      # 0xcbe5
movl $0x8, %esi
movl $0x1, %edx
movq %rax, 0x20(%rsp)
movq %rax, %rcx
callq 0x37d0
leaq 0x7e93(%rip), %rsi      # 0xcbee
movq %r12, %rdi
movl $0xd, %edx
callq 0x50c3
movl (%rbp), %esi
movq %r12, %rdi
callq 0x5111
movl 0x4(%rbp), %esi
movq %r12, %rdi
callq 0x5111
movq 0x18(%r12), %r15
movl $0x8, %edi
movq %r15, %rsi
callq 0x3480
movl (%r12), %eax
movl %eax, %ecx
shrl $0x4, %ecx
andl $0xf, %eax
xorl $0x8, %eax
leaq 0x7a0a(%rip), %rbx      # 0xc7b0
xorl (%rbx,%rax,4), %ecx
movl %ecx, %r14d
shrl $0x4, %r14d
andl $0xf, %ecx
xorl (%rbx,%rcx,4), %r14d
movl $0x6, %edi
movq %r15, %rsi
callq 0x3480
movl %r14d, %eax
shrl $0x4, %eax
andl $0xf, %r14d
xorl $0x6, %r14d
xorl (%rbx,%r14,4), %eax
movl %eax, %r14d
shrl $0x4, %r14d
andl $0xf, %eax
xorl (%rbx,%rax,4), %r14d
xorl %edi, %edi
movq %r15, %rsi
callq 0x3480
movl %r14d, %eax
shrl $0x4, %eax
andl $0xf, %r14d
xorl (%rbx,%r14,4), %eax
movl %eax, %r14d
shrl $0x4, %r14d
andl $0xf, %eax
xorl (%rbx,%rax,4), %r14d
xorl %edi, %edi
movq %r15, %rsi
callq 0x3480
movl %r14d, %eax
shrl $0x4, %eax
andl $0xf, %r14d
xorl (%rbx,%r14,4), %eax
movl %eax, %r14d
shrl $0x4, %r14d
andl $0xf, %eax
xorl (%rbx,%rax,4), %r14d
xorl %edi, %edi
movq %r15, %rsi
xorl %r15d, %r15d
callq 0x3480
movl %r14d, %eax
shrl $0x4, %eax
andl $0xf, %r14d
xorl (%rbx,%r14,4), %eax
movl %eax, %esi
shrl $0x4, %esi
andl $0xf, %eax
xorl (%rbx,%rax,4), %esi
movl %esi, (%r12)
notl %esi
movq %r12, %rdi
callq 0x5111
movq 0x18(%r12), %rdi
callq 0x3030
movq %rax, 0x18(%rsp)
leaq 0x7d79(%rip), %rsi      # 0xcbf3
movq %r12, %rdi
xorl %edx, %edx
callq 0x50c3
movq 0x18(%r12), %r13
movl $0x8, %edi
movq %r13, %rsi
callq 0x3480
movl (%r12), %eax
movl %eax, %ecx
shrl $0x4, %ecx
andl $0xf, %eax
xorl $0x8, %eax
xorl (%rbx,%rax,4), %ecx
movl %ecx, %r14d
shrl $0x4, %r14d
andl $0xf, %ecx
xorl (%rbx,%rcx,4), %r14d
movl $0x1d, %edi
movq %r13, %rsi
callq 0x3480
movl %r14d, %eax
shrl $0x4, %eax
andl $0xf, %r14d
xorl $0xd, %r14d
xorl (%rbx,%r14,4), %eax
movl %eax, %ecx
shrl $0x4, %ecx
andl $0xf, %eax
xorl $0x1, %eax
xorl (%rbx,%rax,4), %ecx
movl %ecx, (%r12)
movq %r12, %rdi
movl $0x3, %esi
movl $0x3, %edx
callq 0x4c36
cmpl $0x0, 0x4(%rbp)
jle 0x4fd3
movl (%rbp), %ebx
leaq 0x28(%rsp), %r12
movq %rbp, 0x10(%rsp)
movq 0x18(%rbp), %r14
movq %r12, %rdi
movl $0x1, %esi
callq 0x7e7c
movl (%rbp), %ecx
testl %ecx, %ecx
jle 0x4fc4
movl %r15d, 0xc(%rsp)
imull %r15d, %ebx
movslq %ebx, %rax
leaq (%r14,%rax,4), %rbx
addq $0x3, %rbx
xorl %r13d, %r13d
xorl %eax, %eax
xorl %r14d, %r14d
xorl %ebp, %ebp
xorl %r15d, %r15d
movb -0x3(%rbx,%r13,4), %cl
subb %al, %cl
movzbl %cl, %esi
movq %r12, %rdi
callq 0x7e7c
movb -0x2(%rbx,%r13,4), %al
subb %r14b, %al
movzbl %al, %esi
movq %r12, %rdi
callq 0x7e7c
movb -0x1(%rbx,%r13,4), %al
subb %bpl, %al
movzbl %al, %esi
movq %r12, %rdi
callq 0x7e7c
movb (%rbx,%r13,4), %al
subb %r15b, %al
movzbl %al, %esi
movq %r12, %rdi
callq 0x7e7c
movb -0x3(%rbx,%r13,4), %al
movb -0x2(%rbx,%r13,4), %r14b
movb -0x1(%rbx,%r13,4), %bpl
movb (%rbx,%r13,4), %r15b
incq %r13
movq 0x10(%rsp), %rcx
movslq (%rcx), %rcx
cmpq %rcx, %r13
jl 0x4f4d
movq 0x10(%rsp), %rbp
movl 0xc(%rsp), %r15d
incl %r15d
movl %ecx, %ebx
cmpl 0x4(%rbp), %r15d
jl 0x4f10
leaq 0x28(%rsp), %r14
movq %r14, %rdi
callq 0x7eec
movl $0xfffffff9, %ebx       # imm = 0xFFFFFFF9
movq %r14, %rdi
xorl %esi, %esi
movl $0x1, %edx
callq 0x4c36
incl %ebx
jne 0x4fe5
cmpl $0x80, 0x30(%rsp)
je 0x5020
leaq 0x28(%rsp), %r14
movq %r14, %rdi
xorl %esi, %esi
movl $0x1, %edx
callq 0x4c36
cmpl $0x80, 0x30(%rsp)
jne 0x5007
leaq 0x28(%rsp), %r14
movl 0x4(%r14), %esi
movq %r14, %rdi
callq 0x5111
movq 0x18(%r14), %rdi
callq 0x3030
movq %rax, %r12
movq 0x18(%rsp), %r15
subl %r15d, %r12d
movl (%r14), %esi
notl %esi
movq %r14, %rdi
callq 0x5111
leaq 0x79ea(%rip), %rsi      # 0xca43
movq %r14, %rdi
xorl %edx, %edx
callq 0x50c3
movl (%r14), %esi
notl %esi
movq %r14, %rdi
callq 0x5111
movq 0x20(%rsp), %rbx
movq %rbx, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0x33a0
addl $-0x8, %r12d
movq %r14, %rdi
movl %r12d, %esi
callq 0x5111
movq %rbx, %rdi
callq 0x3770
movl %eax, %ebp
movq %rbx, %rdi
callq 0x3350
xorl %eax, %eax
testl %ebp, %ebp
sete %al
jmp 0x50b1
movl $0x1, %eax
addq $0x448, %rsp            # imm = 0x448
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq