// File: /workspace/github_cpp_makefile_star2_O2/cloudinary[P]fuif/./import/read_gif.h
// Function: gd_open_gif(char const*)
gd_GIF *
gd_open_gif(const char *fname)
{
    int fd;
    uint8_t sigver[3];
    uint16_t width, height, depth;
    uint8_t fdsz, bgidx, aspect;
    int gct_sz;
    gd_GIF *gif;

    fd = open(fname, O_RDONLY);
    if (fd == -1) return NULL;
    /* Header */
    read(fd, sigver, 3);
    if (memcmp(sigver, "GIF", 3) != 0) {
//        fprintf(stderr, "invalid signature\n");
        goto fail;
    }
    /* Version */
    read(fd, sigver, 3);
    if (memcmp(sigver, "89a", 3) != 0) {
        fprintf(stderr, "invalid GIF version\n");
        goto fail;
    }
    /* Width x Height */
    width  = read_num(fd);
    height = read_num(fd);
    /* FDSZ */
    read(fd, &fdsz, 1);
    /* Presence of GCT */
    if (!(fdsz & 0x80)) {
        fprintf(stderr, "no global color table\n");
        goto fail;
    }
    /* Color Space's Depth */
    depth = ((fdsz >> 4) & 7) + 1;
    /* Ignore Sort Flag. */
    /* GCT Size */
    gct_sz = 1 << ((fdsz & 0x07) + 1);
    /* Background Color Index */
    read(fd, &bgidx, 1);
    /* Aspect Ratio */
    read(fd, &aspect, 1);
    /* Create gd_GIF Structure. */
    gif = (gd_GIF*) calloc(1, sizeof(*gif) + 5 * width * height);
    if (!gif) goto fail;
    gif->fd = fd;
    gif->width  = width;
    gif->height = height;
    gif->depth  = depth;
    /* Read GCT */
    gif->gct.size = gct_sz;
    read(fd, gif->gct.colors, 3 * gif->gct.size);
    gif->palette = &gif->gct;
    gif->bgindex = bgidx;
    gif->canvas = (uint8_t *) &gif[1];
    gif->frame = &gif->canvas[4 * width * height];
    if (gif->bgindex)
        memset(gif->frame, gif->bgindex, gif->width * gif->height);
    gif->anim_start = lseek(fd, 0, SEEK_CUR);
    goto ok;
fail:
    close(fd);
ok:
    return gif;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
xorl %r15d, %r15d
xorl %esi, %esi
xorl %eax, %eax
callq 0x3240
cmpl $-0x1, %eax
je 0x171c7
movl %eax, %ebx
leaq 0x15(%rsp), %r14
pushq $0x3
popq %r15
movl %eax, %edi
movq %r14, %rsi
movq %r15, %rdx
callq 0x3450
leaq 0x8097(%rip), %rsi      # 0x1f0dc
movq %r14, %rdi
movq %r15, %rdx
callq 0x3300
testl %eax, %eax
je 0x17063
movl %ebx, %edi
callq 0x3620
xorl %r15d, %r15d
jmp 0x171c7
leaq 0x15(%rsp), %r15
pushq $0x3
popq %r14
movl %ebx, %edi
movq %r15, %rsi
movq %r14, %rdx
callq 0x3450
leaq 0x8060(%rip), %rsi      # 0x1f0e0
movq %r15, %rdi
movq %r14, %rdx
callq 0x3300
testl %eax, %eax
jne 0x171d9
movl %ebx, %edi
callq 0x17a94
movl %eax, %r13d
movl %ebx, %edi
callq 0x17a94
movl %eax, %r12d
leaq 0x14(%rsp), %r15
pushq $0x1
popq %rdx
movl %ebx, %edi
movq %r15, %rsi
callq 0x3450
movzbl (%r15), %ebp
testb %bpl, %bpl
jns 0x171ee
leaq 0xf(%rsp), %rsi
pushq $0x1
popq %r15
movl %ebx, %edi
movq %r15, %rdx
callq 0x3450
leaq 0x13(%rsp), %rsi
movl %ebx, %edi
movq %r15, %rdx
callq 0x3450
movw %r13w, 0x10(%rsp)
movzwl %r13w, %eax
movzwl %r12w, %r13d
imull %eax, %r13d
leal 0x670(%r13,%r13,4), %esi
movq %r15, %rdi
callq 0x3220
testq %rax, %rax
je 0x17054
movq %rax, %r15
movl %ebp, %ecx
andb $0x7, %cl
incb %cl
pushq $0x1
popq %rax
shll %cl, %eax
shrb $0x4, %bpl
andb $0x7, %bpl
incb %bpl
movzbl %bpl, %edx
movl %ebx, (%r15)
movzwl 0x10(%rsp), %esi
movw %si, 0x10(%r15)
movw %r12w, 0x12(%r15)
movw %dx, 0x14(%r15)
leaq 0x334(%r15), %r12
movl %eax, 0x334(%r15)
leaq 0x338(%r15), %rsi
shlq %cl, %r14
movl %ebx, %edi
movq %r14, %rdx
callq 0x3450
movq %r12, 0x28(%r15)
movb 0xf(%rsp), %al
movb %al, 0x658(%r15)
leaq 0x670(%r15), %rcx
movq %rcx, 0x660(%r15)
shll $0x2, %r13d
leaq (%r15,%r13), %rdi
addq $0x670, %rdi            # imm = 0x670
movq %rdi, 0x668(%r15)
testb %al, %al
je 0x171b7
movzwl 0x10(%r15), %ecx
movzwl 0x12(%r15), %edx
imulq %rcx, %rdx
movzbl %al, %esi
callq 0x31a0
pushq $0x1
popq %rdx
movl %ebx, %edi
xorl %esi, %esi
callq 0x35f0
movq %rax, 0x8(%r15)
movq %r15, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x14df8(%rip), %rax     # 0x2bfd8
movq (%rax), %rcx
leaq 0x7efa(%rip), %rdi      # 0x1f0e4
pushq $0x14
jmp 0x17201
movq 0x14de3(%rip), %rax     # 0x2bfd8
movq (%rax), %rcx
leaq 0x7efa(%rip), %rdi      # 0x1f0f9
pushq $0x16
popq %rsi
pushq $0x1
popq %rdx
callq 0x3610
jmp 0x17054