// File: /workspace/github_c_makefile_star2_O0/sasairc[P]clangsay/src/cow.c
// Function: check_cowfile_exists
static
int check_cowfile_exists(char* file, char** dest)
{
    int     i       = 0,
            res     = 0,
            status  = 0;

    char*   env     = NULL,
        *   path    = NULL,
        *   envp    = NULL;

    env_t*  envt    = NULL;

    /* check env $COWPATH */
    if ((env = getenv("COWPATH")) == NULL)
        env = COWPATH;

    /* env string to struct */
    if (split_env(env, &envt) < 0) {
        status = -1; goto ERR;
    }

    /*
     * # check_file_exists()
     *
     * 0: not found
     * 1: keep argument(path)
     * 2: long argument(filename.cow)
     * 3: short argument(filename)
     */
    do {
        if ((res = check_file_exists(*(envt->envs + i), file)) != 0) {
            envp = *(envt->envs + i);

            break;
        }
        i++;
    } while (i < envt->envc);

    /* cow file not found */
    if (res == 0) {
        status = -2; goto ERR;
    }

    /* concat path */
    if (concat_file_path(res, &path, envp, file) < 0) {
        status = -3; goto ERR;
    }

    release_env_t(envt);
    *dest = path;

    return 0;

ERR:
    switch (status) {
        case    -1:
            fprintf(stderr, "%s: check_file_exists(): split_env() failure\n",
                    PROGNAME);
            break;
        case    -2:
            fprintf(stderr, "%s: %s: cowfile not found\n",
                    PROGNAME, file);
        case    -3:
        default:
            if (envt != NULL)
                release_env_t(envt);

            if (path != NULL)
                free(path);
    }

    return status;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl $0x0, -0x1c(%rbp)
movl $0x0, -0x20(%rbp)
movl $0x0, -0x24(%rbp)
movq $0x0, -0x30(%rbp)
movq $0x0, -0x38(%rbp)
movq $0x0, -0x40(%rbp)
movq $0x0, -0x48(%rbp)
leaq 0x1c4e(%rip), %rdi      # 0x123ea
callq 0x2340
movq %rax, -0x30(%rbp)
cmpq $0x0, %rax
jne 0x107b6
leaq 0x1c40(%rip), %rax      # 0x123f2
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rdi
leaq -0x48(%rbp), %rsi
callq 0x46c0
cmpl $0x0, %eax
jge 0x107d4
movl $0xffffffff, -0x24(%rbp) # imm = 0xFFFFFFFF
jmp 0x10872
jmp 0x107d6
movq -0x48(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x1c(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
movq -0x10(%rbp), %rsi
callq 0x10910
movl %eax, -0x20(%rbp)
cmpl $0x0, %eax
je 0x1080d
movq -0x48(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x1c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x40(%rbp)
jmp 0x10821
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
movq -0x48(%rbp), %rcx
cmpl (%rcx), %eax
jl 0x107d6
cmpl $0x0, -0x20(%rbp)
jne 0x10830
movl $0xfffffffe, -0x24(%rbp) # imm = 0xFFFFFFFE
jmp 0x10872
movl -0x20(%rbp), %edi
movq -0x40(%rbp), %rdx
movq -0x10(%rbp), %rcx
leaq -0x38(%rbp), %rsi
callq 0x10a50
cmpl $0x0, %eax
jge 0x10852
movl $0xfffffffd, -0x24(%rbp) # imm = 0xFFFFFFFD
jmp 0x10872
movq -0x48(%rbp), %rdi
callq 0x48f0
movq -0x38(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, (%rax)
movl $0x0, -0x4(%rbp)
jmp 0x10901
movl -0x24(%rbp), %eax
movl %eax, -0x4c(%rbp)
subl $-0x3, %eax
je 0x108d7
jmp 0x1087f
movl -0x4c(%rbp), %eax
subl $-0x2, %eax
je 0x108b4
jmp 0x10889
movl -0x4c(%rbp), %eax
subl $-0x1, %eax
jne 0x108d9
jmp 0x10893
movq 0x674e(%rip), %rax      # 0x16fe8
movq (%rax), %rdi
leaq 0x1b6e(%rip), %rsi      # 0x12412
leaq 0x1ae6(%rip), %rdx      # 0x12391
movb $0x0, %al
callq 0x23f0
jmp 0x108fb
movq 0x672d(%rip), %rax      # 0x16fe8
movq (%rax), %rdi
movq -0x10(%rbp), %rcx
leaq 0x1b77(%rip), %rsi      # 0x12440
leaq 0x1ac1(%rip), %rdx      # 0x12391
movb $0x0, %al
callq 0x23f0
jmp 0x108d9
cmpq $0x0, -0x48(%rbp)
je 0x108e9
movq -0x48(%rbp), %rdi
callq 0x48f0
cmpq $0x0, -0x38(%rbp)
je 0x108f9
movq -0x38(%rbp), %rdi
callq 0x2180
jmp 0x108fb
movl -0x24(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nopw (%rax,%rax)