// File: /workspace/github_c_makefile_star2_O0/nkanaev[P]tipsy/src/tigr.c
// Function: tigrLoadPng
static Tigr* tigrLoadPng(PNG* png) {
    const unsigned char *ihdr, *idat, *plte, *trns, *first;
    int trnsSize = 0;
    int depth, ctype, bipp;
    int datalen = 0;
    unsigned char *data = NULL, *out;
    Tigr* bmp = NULL;

    CHECK(memcmp(png->p, "\211PNG\r\n\032\n", 8) == 0);  // PNG signature
    png->p += 8;
    first = png->p;

    // Read IHDR
    ihdr = find(png, "IHDR", 13);
    CHECK(ihdr);
    depth = ihdr[8];
    ctype = ihdr[9];
    switch (ctype) {
        case 0:
            bipp = depth;
            break;  // greyscale
        case 2:
            bipp = 3 * depth;
            break;  // RGB
        case 3:
            bipp = depth;
            break;  // paletted
        case 4:
            bipp = 2 * depth;
            break;  // grey+alpha
        case 6:
            bipp = 4 * depth;
            break;  // RGBA
        default:
            FAIL();
    }

    // Allocate bitmap (+1 width to save room for stupid PNG filter bytes)
    bmp = tigrBitmap(get32(ihdr + 0) + 1, get32(ihdr + 4));
    CHECK(bmp);
    bmp->w--;

    // We support 8-bit color components and 1, 2, 4 and 8 bit palette formats.
    // No interlacing, or wacky filter types.
    CHECK((depth != 16) && ihdr[10] == 0 && ihdr[11] == 0 && ihdr[12] == 0);

    // Join IDAT chunks.
    for (idat = find(png, "IDAT", 0); idat; idat = find(png, "IDAT", 0)) {
        unsigned len = get32(idat - 8);
        data = (unsigned char*)realloc(data, datalen + len);
        if (!data)
            break;

        memcpy(data + datalen, idat, len);
        datalen += len;
    }

    // Find palette.
    png->p = first;
    plte = find(png, "PLTE", 0);

    // Find transparency info.
    png->p = first;
    trns = find(png, "tRNS", 0);
    if (trns) {
        trnsSize = get32(trns - 8);
    }

    CHECK(data && datalen >= 6);
    CHECK((data[0] & 0x0f) == 0x08     // compression method (RFC 1950)
          && (data[0] & 0xf0) <= 0x70  // window size
          && (data[1] & 0x20) == 0);   // preset dictionary present

    out = (unsigned char*)bmp->pix + outsize(bmp, 32) - outsize(bmp, bipp);
    CHECK(tigrInflate(out, outsize(bmp, bipp), data + 2, datalen - 6));
    CHECK(unfilter(bmp->w, bmp->h, bipp, out));

    if (ctype == 3) {
        CHECK(plte);
        depalette(bmp->w, bmp->h, out, bmp->pix, bipp, plte, trns, trnsSize);
    } else {
        CHECK(bipp % 8 == 0);
        convert(bipp / 8, bmp->w, bmp->h, out, bmp->pix, trns);
    }

    free(data);
    return bmp;

err:
    if (data)
        free(data);
    if (bmp)
        tigrFree(bmp);
    return NULL;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xb0, %rsp
movq %rdi, -0x10(%rbp)
movl $0x0, -0x3c(%rbp)
movl $0x0, -0x4c(%rbp)
movq $0x0, -0x58(%rbp)
movq $0x0, -0x68(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq (%rax), %rax
movabsq $0xa1a0a0d474e5089, %rcx # imm = 0xA1A0A0D474E5089
subq %rcx, %rax
setne %al
movzbl %al, %eax
cmpl $0x0, %eax
je 0x506f
callq 0x3050
movl $0x16, (%rax)
jmp 0x5473
movq -0x10(%rbp), %rax
movq (%rax), %rcx
addq $0x8, %rcx
movq %rcx, (%rax)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rdi
leaq 0xb5a3(%rip), %rsi      # 0x10636
movl $0xd, %edx
callq 0x9450
movq %rax, -0x18(%rbp)
cmpq $0x0, -0x18(%rbp)
jne 0x50b8
callq 0x3050
movl $0x16, (%rax)
jmp 0x5473
movq -0x18(%rbp), %rax
movzbl 0x8(%rax), %eax
movl %eax, -0x40(%rbp)
movq -0x18(%rbp), %rax
movzbl 0x9(%rax), %eax
movl %eax, -0x44(%rbp)
movl -0x44(%rbp), %eax
movq %rax, -0x78(%rbp)
subq $0x6, %rax
ja 0x511d
movq -0x78(%rbp), %rax
leaq 0x9f3a(%rip), %rcx      # 0xf020
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl -0x40(%rbp), %eax
movl %eax, -0x48(%rbp)
jmp 0x512d
imull $0x3, -0x40(%rbp), %eax
movl %eax, -0x48(%rbp)
jmp 0x512d
movl -0x40(%rbp), %eax
movl %eax, -0x48(%rbp)
jmp 0x512d
movl -0x40(%rbp), %eax
shll %eax
movl %eax, -0x48(%rbp)
jmp 0x512d
movl -0x40(%rbp), %eax
shll $0x2, %eax
movl %eax, -0x48(%rbp)
jmp 0x512d
callq 0x3050
movl $0x16, (%rax)
jmp 0x5473
movq -0x18(%rbp), %rdi
callq 0x9500
addl $0x1, %eax
movl %eax, -0x7c(%rbp)
movq -0x18(%rbp), %rdi
addq $0x4, %rdi
callq 0x9500
movl -0x7c(%rbp), %edi
movl %eax, %esi
callq 0x3980
movq %rax, -0x68(%rbp)
cmpq $0x0, -0x68(%rbp)
jne 0x516e
callq 0x3050
movl $0x16, (%rax)
jmp 0x5473
movq -0x68(%rbp), %rax
movl (%rax), %ecx
addl $-0x1, %ecx
movl %ecx, (%rax)
cmpl $0x10, -0x40(%rbp)
je 0x51a6
movq -0x18(%rbp), %rax
movzbl 0xa(%rax), %eax
cmpl $0x0, %eax
jne 0x51a6
movq -0x18(%rbp), %rax
movzbl 0xb(%rax), %eax
cmpl $0x0, %eax
jne 0x51a6
movq -0x18(%rbp), %rax
movzbl 0xc(%rax), %eax
cmpl $0x0, %eax
je 0x51b6
callq 0x3050
movl $0x16, (%rax)
jmp 0x5473
movq -0x10(%rbp), %rdi
leaq 0xb47a(%rip), %rsi      # 0x1063b
xorl %edx, %edx
callq 0x9450
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
je 0x523d
movq -0x20(%rbp), %rdi
addq $-0x8, %rdi
callq 0x9500
movl %eax, -0x6c(%rbp)
movq -0x58(%rbp), %rdi
movl -0x4c(%rbp), %eax
addl -0x6c(%rbp), %eax
movl %eax, %eax
movl %eax, %esi
callq 0x3680
movq %rax, -0x58(%rbp)
cmpq $0x0, -0x58(%rbp)
jne 0x5203
jmp 0x523d
movq -0x58(%rbp), %rdi
movslq -0x4c(%rbp), %rax
addq %rax, %rdi
movq -0x20(%rbp), %rsi
movl -0x6c(%rbp), %eax
movl %eax, %edx
callq 0x32d0
movl -0x6c(%rbp), %eax
addl -0x4c(%rbp), %eax
movl %eax, -0x4c(%rbp)
movq -0x10(%rbp), %rdi
leaq 0xb40b(%rip), %rsi      # 0x1063b
xorl %edx, %edx
callq 0x9450
movq %rax, -0x20(%rbp)
jmp 0x51cc
movq -0x38(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, (%rax)
movq -0x10(%rbp), %rdi
leaq 0xb3ed(%rip), %rsi      # 0x10640
xorl %edx, %edx
callq 0x9450
movq %rax, -0x28(%rbp)
movq -0x38(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, (%rax)
movq -0x10(%rbp), %rdi
leaq 0xb3d1(%rip), %rsi      # 0x10645
xorl %edx, %edx
callq 0x9450
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
je 0x5296
movq -0x30(%rbp), %rdi
addq $-0x8, %rdi
callq 0x9500
movl %eax, -0x3c(%rbp)
cmpq $0x0, -0x58(%rbp)
je 0x52a3
cmpl $0x6, -0x4c(%rbp)
jge 0x52b3
callq 0x3050
movl $0x16, (%rax)
jmp 0x5473
movq -0x58(%rbp), %rax
movzbl (%rax), %eax
andl $0xf, %eax
cmpl $0x8, %eax
jne 0x52e3
movq -0x58(%rbp), %rax
movzbl (%rax), %eax
andl $0xf0, %eax
cmpl $0x70, %eax
jg 0x52e3
movq -0x58(%rbp), %rax
movzbl 0x1(%rax), %eax
andl $0x20, %eax
cmpl $0x0, %eax
je 0x52f3
callq 0x3050
movl $0x16, (%rax)
jmp 0x5473
movq -0x68(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x98(%rbp)
movq -0x68(%rbp), %rdi
movl $0x20, %esi
callq 0x9540
movl %eax, %ecx
movq -0x98(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x90(%rbp)
movq -0x68(%rbp), %rdi
movl -0x48(%rbp), %esi
callq 0x9540
movl %eax, %ecx
movq -0x90(%rbp), %rax
movslq %ecx, %rdx
xorl %ecx, %ecx
subq %rdx, %rcx
addq %rcx, %rax
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x68(%rbp), %rdi
movl -0x48(%rbp), %esi
callq 0x9540
movq -0x88(%rbp), %rdi
movl %eax, %esi
movq -0x58(%rbp), %rdx
addq $0x2, %rdx
movl -0x4c(%rbp), %ecx
subl $0x6, %ecx
callq 0x5c60
cmpl $0x0, %eax
jne 0x5392
callq 0x3050
movl $0x16, (%rax)
jmp 0x5473
movq -0x68(%rbp), %rax
movl (%rax), %edi
movq -0x68(%rbp), %rax
movl 0x4(%rax), %esi
movl -0x48(%rbp), %edx
movq -0x60(%rbp), %rcx
callq 0x9570
cmpl $0x0, %eax
jne 0x53c0
callq 0x3050
movl $0x16, (%rax)
jmp 0x5473
cmpl $0x3, -0x44(%rbp)
jne 0x5414
cmpq $0x0, -0x28(%rbp)
jne 0x53dd
callq 0x3050
movl $0x16, (%rax)
jmp 0x5473
movq -0x68(%rbp), %rax
movl (%rax), %edi
movq -0x68(%rbp), %rax
movl 0x4(%rax), %esi
movq -0x60(%rbp), %rdx
movq -0x68(%rbp), %rax
movq 0x18(%rax), %rcx
movl -0x48(%rbp), %r8d
movq -0x28(%rbp), %r9
movq -0x30(%rbp), %r10
movl -0x3c(%rbp), %eax
movq %r10, (%rsp)
movl %eax, 0x8(%rsp)
callq 0x9870
jmp 0x5460
movl -0x48(%rbp), %eax
movl $0x8, %ecx
cltd
idivl %ecx
cmpl $0x0, %edx
je 0x5431
callq 0x3050
movl $0x16, (%rax)
jmp 0x5473
movl -0x48(%rbp), %eax
movl $0x8, %ecx
cltd
idivl %ecx
movl %eax, %edi
movq -0x68(%rbp), %rax
movl (%rax), %esi
movq -0x68(%rbp), %rax
movl 0x4(%rax), %edx
movq -0x60(%rbp), %rcx
movq -0x68(%rbp), %rax
movq 0x18(%rax), %r8
movq -0x30(%rbp), %r9
callq 0x9a10
movq -0x58(%rbp), %rdi
callq 0x3410
movq -0x68(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x549b
cmpq $0x0, -0x58(%rbp)
je 0x5483
movq -0x58(%rbp), %rdi
callq 0x3410
cmpq $0x0, -0x68(%rbp)
je 0x5493
movq -0x68(%rbp), %rdi
callq 0x6890
movq $0x0, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0xb0, %rsp
popq %rbp
retq
nopl (%rax,%rax)