// File: /workspace/github_c_makefile_star2_O3/stetre[P]moonsndfile/src/sndfile.c
// Function: Open
static int Open(lua_State *L, int openfd)
    {
    SF_INFO info;
    sndfile_t sndfile;
    const char *path;
    luaL_Stream *s;
    int mode, fd = -1, close_desc = 1;

    if(openfd) //@@FIXME: does this work under MINGW?
        {
        s = (luaL_Stream*)luaL_testudata(L, 1, LUA_FILEHANDLE);
        if(s)
            {
            fd = fileno(s->f); /* POSIX */
            if(fd == -1)
                return luaL_argerror(L, 1, errstring(ERR_TYPE));
            }
        else
            fd = luaL_checkinteger(L, 1);

        mode = checkopenmode(L, 2);
        close_desc = optboolean(L, 4, 0);
        }
    else
        {
        path = luaL_checkstring(L, 1);
        mode = checkopenmode(L, 2);
        }

    /* openmode="w" or format="raw" require the user to specify format, channels and samplerate.
     * openmode="r", the info is obtained from the file (provided it exists)
     * openmode="rw", the info is obtained from the file (provided it exists)
     * @@ if openmode="rw" and the file does not exist, then a new file is created
     *    but an error is raised because info.format is 0.
     */
    memset(&info, 0, sizeof(info));
    if(!lua_isnoneornil(L, 3))
        {
        checkinfo(L, 3, &info);
        if((((info.format & SF_FORMAT_TYPEMASK) != SF_FORMAT_RAW)) && (mode != SFM_WRITE))
            return luaL_argerror(L, 3, "info must be specified only for format='raw' or openmode ='w'");
//      if(!sf.format_check(&info))
//          return luaL_argerror(L, 3, "invalid combination of info values");
        }
    else if(mode == SFM_WRITE)
        return luaL_argerror(L, 3, "missing info, required for openmode ='w'");

    if(openfd)
        sndfile = sf.open_fd(fd, mode, &info, close_desc);
    else    
        sndfile = sf.open(path, mode, &info);

    if(!sndfile)
        return sndfileerror(L, NULL);

    return Create(L, sndfile, &info);
    }

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl %esi, %ebp
movq %rdi, %rbx
testl %esi, %esi
je 0xa46d
leaq 0x410c(%rip), %rdx      # 0xe53d
movq %rbx, %rdi
movl $0x1, %esi
callq 0x4570
testq %rax, %rax
je 0xa4a4
movq (%rax), %rdi
callq 0x44f0
movl %eax, %r14d
cmpl $-0x1, %eax
jne 0xa4b4
movl $0xfffffffe, %edi       # imm = 0xFFFFFFFE
callq 0x4660
movq %rbx, %rdi
movl $0x1, %esi
movq %rax, %rdx
jmp 0xa55b
movl $0x1, %r15d
movq %rbx, %rdi
movl $0x1, %esi
xorl %edx, %edx
callq 0x4380
movq %rax, 0x8(%rsp)
movq %rbx, %rdi
movl $0x6, %esi
movl $0x2, %edx
callq 0x4130
movl %eax, %r12d
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
jmp 0xa4db
movq %rbx, %rdi
movl $0x1, %esi
callq 0x4470
movq %rax, %r14
movq %rbx, %rdi
movl $0x6, %esi
movl $0x2, %edx
callq 0x4130
movl %eax, %r12d
movq %rbx, %rdi
movl $0x4, %esi
xorl %edx, %edx
callq 0x41b0
movl %eax, %r15d
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
movaps %xmm0, 0x10(%rsp)
movq %rbx, %rdi
movl $0x3, %esi
callq 0x4730
testl %eax, %eax
jle 0xa546
leaq 0x10(%rsp), %r13
movq %rbx, %rdi
movl $0x3, %esi
movq %r13, %rdx
callq 0x45f0
movl $0xfff0000, %eax        # imm = 0xFFF0000
andl 0x10(%r13), %eax
cmpl $0x40000, %eax          # imm = 0x40000
setne %al
cmpl $0x20, %r12d
setne %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0xa56e
leaq 0x400f(%rip), %rdx      # 0xe543
movq %rbx, %rdi
movl $0x3, %esi
callq 0x4110
jmp 0xa612
cmpl $0x20, %r12d
jne 0xa56e
leaq 0x402e(%rip), %rdx      # 0xe581
movq %rbx, %rdi
movl $0x3, %esi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x4110
movq 0x8a6b(%rip), %rax      # 0x12fe0
leaq 0x10(%rsp), %rdx
testl %ebp, %ebp
je 0xa58c
movl %r14d, %edi
movl %r12d, %esi
movl %r15d, %ecx
callq *0x8(%rax)
jmp 0xa596
movq 0x8(%rsp), %rdi
movl %r12d, %esi
callq *(%rax)
movq %rax, %r14
testq %rax, %rax
je 0xa608
leaq 0x2af2(%rip), %rdx      # 0xd097
movq %rbx, %rdi
movq %r14, %rsi
callq 0x46e0
movq $0x0, 0x10(%rax)
leaq 0x62(%rip), %rcx        # 0xa621
movq %rcx, 0x8(%rax)
leaq 0x20(%rax), %r15
movaps 0x10(%rsp), %xmm0
movaps 0x20(%rsp), %xmm1
movups %xmm1, 0x30(%rax)
movups %xmm0, 0x20(%rax)
movq 0x8a18(%rip), %rax      # 0x12ff8
cmpl $0x0, (%rax)
je 0xa5f6
leaq 0x3fbe(%rip), %rdi      # 0xe5aa
movq %r14, %rsi
xorl %eax, %eax
callq 0x4290
movq %rbx, %rdi
movq %r15, %rsi
callq 0x43b0
movl $0x2, %eax
jmp 0xa612
movq %rbx, %rdi
xorl %esi, %esi
callq 0x4340
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq