// File: /workspace/github_cpp_makefile_star2_O3/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 %r14d, %r14d
xorl %esi, %esi
xorl %eax, %eax
callq 0x3220
cmpl $-0x1, %eax
je 0x1d6ea
movl %eax, %ebx
leaq 0x11(%rsp), %r14
movl $0x3, %edx
movl %eax, %edi
movq %r14, %rsi
callq 0x3410
movzwl (%r14), %eax
xorl $0x4947, %eax           # imm = 0x4947
movzbl 0x2(%r14), %ecx
xorl $0x46, %ecx
orw %ax, %cx
je 0x1d56b
movl %ebx, %edi
callq 0x35e0
xorl %r14d, %r14d
jmp 0x1d6ea
leaq 0x11(%rsp), %r14
movl $0x3, %edx
movl %ebx, %edi
movq %r14, %rsi
callq 0x3410
movzwl (%r14), %eax
xorl $0x3938, %eax           # imm = 0x3938
movzbl 0x2(%r14), %ecx
xorl $0x61, %ecx
orw %ax, %cx
jne 0x1d6fc
leaq 0x14(%rsp), %r14
movl $0x2, %edx
movl %ebx, %edi
movq %r14, %rsi
callq 0x3410
movzwl (%r14), %ebp
leaq 0x16(%rsp), %r14
movl $0x2, %edx
movl %ebx, %edi
movq %r14, %rsi
callq 0x3410
movzwl (%r14), %r12d
leaq 0x10(%rsp), %r14
movl $0x1, %edx
movl %ebx, %edi
movq %r14, %rsi
callq 0x3410
movzbl (%r14), %r13d
testb %r13b, %r13b
jns 0x1d706
movzwl %bp, %eax
movzwl %r12w, %r15d
imull %eax, %r15d
leaq 0xe(%rsp), %rsi
movl $0x1, %edx
movl %ebx, %edi
callq 0x3410
leaq 0xf(%rsp), %rsi
movl $0x1, %edx
movl %ebx, %edi
callq 0x3410
leal (%r15,%r15,4), %esi
addl $0x670, %esi            # imm = 0x670
movl $0x1, %edi
callq 0x3200
testq %rax, %rax
je 0x1d55c
movq %rax, %r14
movl %r13d, %ecx
andb $0x7, %cl
incb %cl
movl $0x1, %eax
shll %cl, %eax
shrb $0x4, %r13b
andb $0x7, %r13b
incb %r13b
movzbl %r13b, %edx
movl %ebx, (%r14)
movw %bp, 0x10(%r14)
movw %r12w, 0x12(%r14)
movw %dx, 0x14(%r14)
leaq 0x334(%r14), %r12
movl %eax, 0x334(%r14)
leaq 0x338(%r14), %rsi
movl $0x3, %edx
shlq %cl, %rdx
movl %ebx, %edi
callq 0x3410
movq %r12, 0x28(%r14)
movb 0xe(%rsp), %al
movb %al, 0x658(%r14)
leaq 0x670(%r14), %rcx
movq %rcx, 0x660(%r14)
shll $0x2, %r15d
leaq (%r14,%r15), %rdi
addq $0x670, %rdi            # imm = 0x670
movq %rdi, 0x668(%r14)
testb %al, %al
je 0x1d6d8
movzwl 0x10(%r14), %ecx
movzwl 0x12(%r14), %edx
imulq %rcx, %rdx
movzbl %al, %esi
callq 0x3180
movl %ebx, %edi
xorl %esi, %esi
movl $0x1, %edx
callq 0x35b0
movq %rax, 0x8(%r14)
movq %r14, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x3840
jmp 0x1d55c
callq 0x385c
jmp 0x1d55c