// File: /workspace/github_c_makefile_star2_O2/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 0x9f4e
leaq 0x3625(%rip), %rdx      # 0xd53d
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0x4570
testq %rax, %rax
je 0x9f7b
movq (%rax), %rdi
callq 0x44f0
movl %eax, %r14d
cmpl $-0x1, %eax
jne 0x9f89
pushq $-0x2
popq %rdi
callq 0x4660
pushq $0x1
popq %rsi
movq %rbx, %rdi
movq %rax, %rdx
jmp 0xa022
pushq $0x1
popq %r15
movq %rbx, %rdi
movl %r15d, %esi
xorl %edx, %edx
callq 0x4380
movq %rax, 0x8(%rsp)
pushq $0x6
popq %rsi
pushq $0x2
popq %rdx
movq %rbx, %rdi
callq 0x4130
movl %eax, %r12d
pushq $-0x1
popq %r14
jmp 0x9faa
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0x4470
movq %rax, %r14
pushq $0x6
popq %rsi
pushq $0x2
popq %rdx
movq %rbx, %rdi
callq 0x4130
movl %eax, %r12d
pushq $0x4
popq %rsi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x41b0
movl %eax, %r15d
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
movaps %xmm0, 0x10(%rsp)
pushq $0x3
popq %rsi
movq %rbx, %rdi
callq 0x4730
testl %eax, %eax
jle 0xa00f
pushq $0x3
popq %rsi
leaq 0x10(%rsp), %r13
movq %rbx, %rdi
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 0xa035
leaq 0x3544(%rip), %rdx      # 0xd543
pushq $0x3
popq %rsi
movq %rbx, %rdi
callq 0x4110
jmp 0xa0d4
cmpl $0x20, %r12d
jne 0xa035
leaq 0x3565(%rip), %rdx      # 0xd581
pushq $0x3
popq %rsi
movq %rbx, %rdi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x4110
movq 0x7fa4(%rip), %rax      # 0x11fe0
leaq 0x10(%rsp), %rdx
testl %ebp, %ebp
je 0xa053
movl %r14d, %edi
movl %r12d, %esi
movl %r15d, %ecx
callq *0x8(%rax)
jmp 0xa05d
movq 0x8(%rsp), %rdi
movl %r12d, %esi
callq *(%rax)
movq %rax, %r14
testq %rax, %rax
je 0xa0ca
leaq 0x202b(%rip), %rdx      # 0xc097
movq %rbx, %rdi
movq %r14, %rsi
callq 0x46e0
andq $0x0, 0x10(%rax)
leaq 0x64(%rip), %rcx        # 0xa0e7
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 0x7f54(%rip), %rax      # 0x11ff8
cmpl $0x0, (%rax)
je 0xa0ba
leaq 0x34fa(%rip), %rdi      # 0xd5aa
movq %r14, %rsi
xorl %eax, %eax
callq 0x4290
pushq $0x2
popq %rbp
movq %rbx, %rdi
movq %r15, %rsi
callq 0x43b0
jmp 0xa0d6
movq %rbx, %rdi
xorl %esi, %esi
callq 0x4340
movl %eax, %ebp
movl %ebp, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq