// File: /workspace/github_c_makefile_star2_O2/genometools[P]genometools/src/external/zlib-1.3.0.1/gzlib.c
// Function: gz_open
local gzFile gz_open(const void *path, int fd, const char *mode) {
    gz_statep state;
    z_size_t len;
    int oflag;
#ifdef O_CLOEXEC
    int cloexec = 0;
#endif
#ifdef O_EXCL
    int exclusive = 0;
#endif

    /* check input */
    if (path == NULL)
        return NULL;

    /* allocate gzFile structure to return */
    state = (gz_statep)malloc(sizeof(gz_state));
    if (state == NULL)
        return NULL;
    state->size = 0;            /* no buffers allocated yet */
    state->want = GZBUFSIZE;    /* requested buffer size */
    state->msg = NULL;          /* no error message yet */

    /* interpret mode */
    state->mode = GZ_NONE;
    state->level = Z_DEFAULT_COMPRESSION;
    state->strategy = Z_DEFAULT_STRATEGY;
    state->direct = 0;
    while (*mode) {
        if (*mode >= '0' && *mode <= '9')
            state->level = *mode - '0';
        else
            switch (*mode) {
            case 'r':
                state->mode = GZ_READ;
                break;
#ifndef NO_GZCOMPRESS
            case 'w':
                state->mode = GZ_WRITE;
                break;
            case 'a':
                state->mode = GZ_APPEND;
                break;
#endif
            case '+':       /* can't read and write at the same time */
                free(state);
                return NULL;
            case 'b':       /* ignore -- will request binary anyway */
                break;
#ifdef O_CLOEXEC
            case 'e':
                cloexec = 1;
                break;
#endif
#ifdef O_EXCL
            case 'x':
                exclusive = 1;
                break;
#endif
            case 'f':
                state->strategy = Z_FILTERED;
                break;
            case 'h':
                state->strategy = Z_HUFFMAN_ONLY;
                break;
            case 'R':
                state->strategy = Z_RLE;
                break;
            case 'F':
                state->strategy = Z_FIXED;
                break;
            case 'T':
                state->direct = 1;
                break;
            default:        /* could consider as an error, but just ignore */
                ;
            }
        mode++;
    }

    /* must provide an "r", "w", or "a" */
    if (state->mode == GZ_NONE) {
        free(state);
        return NULL;
    }

    /* can't force transparent read */
    if (state->mode == GZ_READ) {
        if (state->direct) {
            free(state);
            return NULL;
        }
        state->direct = 1;      /* for empty file */
    }

    /* save the path name for error messages */
#ifdef WIDECHAR
    if (fd == -2) {
        len = wcstombs(NULL, path, 0);
        if (len == (z_size_t)-1)
            len = 0;
    }
    else
#endif
        len = strlen((const char *)path);
    state->path = (char *)malloc(len + 1);
    if (state->path == NULL) {
        free(state);
        return NULL;
    }
#ifdef WIDECHAR
    if (fd == -2)
        if (len)
            wcstombs(state->path, path, len + 1);
        else
            *(state->path) = 0;
    else
#endif
#if !defined(NO_snprintf) && !defined(NO_vsnprintf)
        (void)snprintf(state->path, len + 1, "%s", (const char *)path);
#else
        strcpy(state->path, path);
#endif

    /* compute the flags for open() */
    oflag =
#ifdef O_LARGEFILE
        O_LARGEFILE |
#endif
#ifdef O_BINARY
        O_BINARY |
#endif
#ifdef O_CLOEXEC
        (cloexec ? O_CLOEXEC : 0) |
#endif
        (state->mode == GZ_READ ?
         O_RDONLY :
         (O_WRONLY | O_CREAT |
#ifdef O_EXCL
          (exclusive ? O_EXCL : 0) |
#endif
          (state->mode == GZ_WRITE ?
           O_TRUNC :
           O_APPEND)));

    /* open the file with the appropriate flags (or just use fd) */
    state->fd = fd > -1 ? fd : (
#ifdef WIDECHAR
        fd == -2 ? _wopen(path, oflag, 0666) :
#endif
        open((const char *)path, oflag, 0666));
    if (state->fd == -1) {
        free(state->path);
        free(state);
        return NULL;
    }
    if (state->mode == GZ_APPEND) {
        LSEEK(state->fd, 0, SEEK_END);  /* so gzoffset() is correct */
        state->mode = GZ_WRITE;         /* simplify later checks */
    }

    /* save the current position for rewinding (only if reading) */
    if (state->mode == GZ_READ) {
        state->start = LSEEK(state->fd, 0, SEEK_CUR);
        if (state->start == -1) state->start = 0;
    }

    /* initialize stream */
    gz_reset(state);

    /* return stream */
    return (gzFile)state;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
testq %rdi, %rdi
je 0x7a2d6
movq %rdx, %r15
movl %esi, %r12d
movq %rdi, %r14
movl $0xf0, %edi
callq 0xbce8
movq %rax, %rbx
xorl %eax, %eax
testq %rbx, %rbx
je 0x7a2d6
movabsq $0x200000000000, %rcx   # imm = 0x200000000000
movq %rcx, 0x28(%rbx)
andq $0x0, 0x78(%rbx)
andl $0x0, 0x18(%rbx)
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movq %rcx, 0x58(%rbx)
andl $0x0, 0x40(%rbx)
pushq $0x1
popq %rcx
leaq 0x66fe6(%rip), %rdx     # 0xe10c4
xorl %ebp, %ebp
xorl %r9d, %r9d
xorl %r8d, %r8d
movzbl (%r15), %esi
testl %esi, %esi
je 0x7a1a9
leal -0x30(%rsi), %edi
cmpb $0x9, %dil
ja 0x7a107
movzbl %dil, %esi
movl %esi, 0x58(%rbx)
incq %r15
jmp 0x7a0e6
leal -0x61(%rsi), %edi
cmpl $0x7, %edi
jbe 0x7a137
cmpl $0x46, %esi
je 0x7a18c
cmpl $0x52, %esi
je 0x7a162
cmpl $0x54, %esi
je 0x7a16b
cmpl $0x72, %esi
je 0x7a17b
cmpl $0x77, %esi
je 0x7a198
cmpl $0x78, %esi
je 0x7a176
cmpl $0x2b, %esi
jne 0x7a102
jmp 0x7a2ce
movslq (%rdx,%rdi,4), %rsi
addq %rdx, %rsi
jmpq *%rsi
movl $0x1, 0x18(%rbx)
movl %ecx, %ebp
jmp 0x7a102
movl %ecx, %r9d
jmp 0x7a102
movl $0x1, 0x5c(%rbx)
jmp 0x7a102
movl $0x2, 0x5c(%rbx)
jmp 0x7a102
movl $0x3, 0x5c(%rbx)
jmp 0x7a102
movl $0x1, 0x40(%rbx)
movl %ecx, %eax
jmp 0x7a102
movl %ecx, %r8d
jmp 0x7a102
movl $0x1c4f, 0x18(%rbx)     # imm = 0x1C4F
movl $0x1c4f, %ebp           # imm = 0x1C4F
jmp 0x7a102
movl $0x4, 0x5c(%rbx)
jmp 0x7a102
movl $0x79b1, 0x18(%rbx)     # imm = 0x79B1
movl $0x79b1, %ebp           # imm = 0x79B1
jmp 0x7a102
cmpl $0x1c4f, %ebp           # imm = 0x1C4F
je 0x7a1bb
testl %ebp, %ebp
je 0x7a2ce
jmp 0x7a1ca
testl %eax, %eax
jne 0x7a2ce
movl $0x1, 0x40(%rbx)
movl %r9d, 0x10(%rsp)
movl %r8d, 0xc(%rsp)
movl %r12d, 0x14(%rsp)
movq %r14, %rdi
callq 0xb260
movq %rax, %r13
incq %r13
movq %r13, %rdi
callq 0xbce8
movq %rax, 0x20(%rbx)
testq %rax, %rax
je 0x7a2ce
movq %rax, %r15
leaq 0x5d8a3(%rip), %rdx     # 0xd7aa9
xorl %r12d, %r12d
movq %rax, %rdi
movq %r13, %rsi
movq %r14, %rcx
xorl %eax, %eax
callq 0xba00
xorl %eax, %eax
cmpl $0x0, 0x10(%rsp)
setne %cl
cmpl $0x1c4f, %ebp           # imm = 0x1C4F
je 0x7a252
xorl %edx, %edx
cmpl $0x0, 0xc(%rsp)
setne %dl
shll $0x7, %edx
xorl %esi, %esi
cmpl $0x79b1, %ebp           # imm = 0x79B1
setne %sil
shll $0x9, %esi
leal (%rdx,%rsi), %r12d
addl $0x241, %r12d           # imm = 0x241
movl 0x14(%rsp), %edi
testl %edi, %edi
js 0x7a25f
movl %edi, 0x1c(%rbx)
jmp 0x7a283
movb %cl, %al
shll $0x13, %eax
orl %eax, %r12d
movq %r14, %rdi
movl %r12d, %esi
movl $0x1b6, %edx            # imm = 0x1B6
xorl %eax, %eax
callq 0xb450
movl %eax, %edi
movl %eax, 0x1c(%rbx)
cmpl $-0x1, %eax
je 0x7a2c6
cmpl $0x1, %ebp
jne 0x7a29b
pushq $0x2
popq %rdx
xorl %esi, %esi
callq 0xb570
movl $0x79b1, 0x18(%rbx)     # imm = 0x79B1
jmp 0x7a2bc
cmpl $0x1c4f, %ebp           # imm = 0x1C4F
jne 0x7a2bc
xorl %r14d, %r14d
pushq $0x1
popq %rdx
xorl %esi, %esi
callq 0xb570
cmpq $-0x1, %rax
cmoveq %r14, %rax
movq %rax, 0x48(%rbx)
movq %rbx, %rdi
callq 0x7a3cf
jmp 0x7a2d8
movq %r15, %rdi
callq 0xb790
movq %rbx, %rdi
callq 0xb790
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq