// File: /workspace/github_c_makefile_star2_O1/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 0x9a5ab
movq %rdx, %r15
movl %esi, %r12d
movq %rdi, %r14
movl $0xf0, %edi
callq 0xb810
xorl %r13d, %r13d
testq %rax, %rax
je 0x9a5ab
movq %rax, %rbx
movabsq $0x200000000000, %rax   # imm = 0x200000000000
movq %rax, 0x28(%rbx)
movq $0x0, 0x78(%rbx)
movl %r13d, 0x18(%rbx)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movq %rax, 0x58(%rbx)
movl %r13d, 0x40(%rbx)
movb (%r15), %sil
testb %sil, %sil
je 0x9a488
incq %r15
xorl %eax, %eax
leaq 0x5da56(%rip), %rcx     # 0xf7de4
xorl %edx, %edx
leal -0x30(%rsi), %edi
cmpb $0x9, %dil
ja 0x9a3b0
movzbl %dil, %esi
movl %esi, 0x58(%rbx)
movb (%r15), %sil
incq %r15
testb %sil, %sil
jne 0x9a390
jmp 0x9a465
movzbl %sil, %esi
cmpl $0x60, %esi
jle 0x9a3d8
cmpl $0x71, %esi
jg 0x9a3f0
addl $-0x61, %esi
cmpl $0x7, %esi
ja 0x9a3a0
movslq (%rcx,%rsi,4), %rsi
addq %rcx, %rsi
jmpq *%rsi
movl $0x1, 0x18(%rbx)
jmp 0x9a3a0
cmpl $0x51, %esi
jle 0x9a408
cmpl $0x52, %esi
je 0x9a435
cmpl $0x54, %esi
jne 0x9a3a0
movl $0x1, 0x40(%rbx)
jmp 0x9a3a0
cmpl $0x78, %esi
je 0x9a44d
cmpl $0x77, %esi
je 0x9a441
cmpl $0x72, %esi
jne 0x9a3a0
movl $0x1c4f, 0x18(%rbx)     # imm = 0x1C4F
jmp 0x9a3a0
cmpl $0x46, %esi
jne 0x9a457
movl $0x4, 0x5c(%rbx)
jmp 0x9a3a0
movl $0x2, 0x5c(%rbx)
jmp 0x9a3a0
movl $0x1, 0x5c(%rbx)
jmp 0x9a3a0
movl $0x1, %edx
jmp 0x9a3a0
movl $0x3, 0x5c(%rbx)
jmp 0x9a3a0
movl $0x79b1, 0x18(%rbx)     # imm = 0x79B1
jmp 0x9a3a0
movl $0x1, %eax
jmp 0x9a3a0
cmpl $0x2b, %esi
jne 0x9a3a0
jmp 0x9a5a3
movq %r14, %rcx
xorl %r13d, %r13d
testl %edx, %edx
setne %r13b
shll $0x13, %r13d
xorl %r14d, %r14d
testl %eax, %eax
setne %r14b
shll $0x7, %r14d
orl $0x41, %r14d
jmp 0x9a491
movq %r14, %rcx
movl $0x41, %r14d
movl 0x18(%rbx), %ebp
cmpl $0x1c4f, %ebp           # imm = 0x1C4F
je 0x9a4a6
testl %ebp, %ebp
je 0x9a5a3
jmp 0x9a4b7
cmpl $0x0, 0x40(%rbx)
jne 0x9a5a3
movl $0x1, 0x40(%rbx)
movl %r12d, 0xc(%rsp)
movq %rcx, %rdi
movq %rcx, 0x10(%rsp)
callq 0xb260
movq %rax, %r12
incq %r12
movq %r12, %rdi
callq 0xb810
movq %rax, 0x20(%rbx)
testq %rax, %rax
je 0x9a5a3
movq %rax, %r15
leaq 0x5465b(%rip), %rdx     # 0xeeb49
movq %rax, %rdi
movq %r12, %rsi
movq 0x10(%rsp), %rcx
xorl %eax, %eax
callq 0xba10
movl 0xc(%rsp), %r12d
testl %r12d, %r12d
jns 0x9a545
xorl %eax, %eax
cmpl $0x79b1, %ebp           # imm = 0x79B1
setne %al
shll $0x9, %eax
cmpl $0x1c4f, %ebp           # imm = 0x1C4F
leal 0x200(%rax,%r14), %esi
movl $0x0, %eax
cmovel %eax, %esi
orl %r13d, %esi
movq 0x10(%rsp), %rdi
movl $0x1b6, %edx            # imm = 0x1B6
xorl %eax, %eax
callq 0xb450
movl %eax, %r12d
movl %r12d, 0x1c(%rbx)
cmpl $-0x1, %r12d
je 0x9a59b
cmpl $0x1, %ebp
jne 0x9a56a
movl %r12d, %edi
xorl %esi, %esi
movl $0x2, %edx
callq 0xb570
movl $0x79b1, 0x18(%rbx)     # imm = 0x79B1
cmpl $0x1c4f, 0x18(%rbx)     # imm = 0x1C4F
jne 0x9a591
xorl %r14d, %r14d
movl %r12d, %edi
xorl %esi, %esi
movl $0x1, %edx
callq 0xb570
cmpq $-0x1, %rax
cmoveq %r14, %rax
movq %rax, 0x48(%rbx)
movq %rbx, %rdi
callq 0x9a6b0
jmp 0x9a5ad
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