#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
int /*<<< orphan*/ R_OK;
scalar_t__ access(char *, int /*<<< orphan*/);
char *bmalloc(size_t);
int /*<<< orphan*/ free(char *);
char *getenv(char const *);
int /*<<< orphan*/ snprintf(char *, size_t, char *, char *, char const *);
int strlen(char const *);
char *test_file_path(const char *file) {
  char *base;
  char *path = NULL;
  size_t length;
  const char *envs[] = {"BUILD", "SOURCE", NULL};
  int i;
  for (i = 0; envs[i] != NULL; i++) {
    base = getenv(envs[i]);
    if (base == NULL)
      continue;
    length = strlen(base) + 1 + strlen(file) + 1;
    path = bmalloc(length);
    snprintf(path, length, "%s/%s", base, file);
    if (access(path, R_OK) == 0)
      break;
    free(path);
    path = NULL;
  }
  return path;
}


x86 assembly (clang, -O0):
<test_file_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x18(%rbp)
mov    0x2c85(%rip),%rax
mov    %rax,-0x40(%rbp)
mov    0x2c82(%rip),%rax
mov    %rax,-0x38(%rbp)
mov    0x2c7f(%rip),%rax
mov    %rax,-0x30(%rbp)
movl   $0x0,-0x44(%rbp)
movslq -0x44(%rbp),%rax
cmpq   $0x0,-0x40(%rbp,%rax,8)
je     125f <test_file_path+0xff>
movslq -0x44(%rbp),%rax
mov    -0x40(%rbp,%rax,8),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11ce <test_file_path+0x6e>
jmp    1251 <test_file_path+0xf1>
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
add    $0x1,%eax
mov    %eax,-0x48(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,%ecx
mov    -0x48(%rbp),%eax
add    %ecx,%eax
add    $0x1,%eax
cltq
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <bmalloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x10(%rbp),%rcx
mov    -0x8(%rbp),%r8
lea    0xdf3(%rip),%rdx
call   1070 <snprintf@plt>
mov    -0x18(%rbp),%rdi
mov    0x2db6(%rip),%rax
mov    (%rax),%esi
call   1080 <access@plt>
cmp    $0x0,%rax
jne    1240 <test_file_path+0xe0>
jmp    125f <test_file_path+0xff>
mov    -0x18(%rbp),%rdi
call   1040 <free@plt>
movq   $0x0,-0x18(%rbp)
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
jmp    119c <test_file_path+0x3c>
mov    -0x18(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_file_path>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
lea    0xe8e(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     11da <test_file_path+0x7a>
mov    %rax,%r15
mov    %rax,%rdi
call   1060 <strlen@plt>
mov    %eax,%ebx
mov    %r14,%rdi
call   1060 <strlen@plt>
add    %ebx,%eax
add    $0x2,%eax
movslq %eax,%r12
mov    %r12,%rdi
call   1050 <bmalloc@plt>
mov    %rax,%rbx
lea    0xe62(%rip),%rdx
mov    %rax,%rdi
mov    %r12,%rsi
mov    %r15,%rcx
mov    %r14,%r8
call   1070 <snprintf@plt>
mov    0x2e1d(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1080 <access@plt>
test   %rax,%rax
je     124b <test_file_path+0xeb>
mov    %rbx,%rdi
call   1040 <free@plt>
lea    0xe25(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     1249 <test_file_path+0xe9>
mov    %rax,%r15
mov    %rax,%rdi
call   1060 <strlen@plt>
mov    %eax,%ebx
mov    %r14,%rdi
call   1060 <strlen@plt>
add    %ebx,%eax
add    $0x2,%eax
movslq %eax,%r12
mov    %r12,%rdi
call   1050 <bmalloc@plt>
mov    %rax,%rbx
lea    0xdf3(%rip),%rdx
mov    %rax,%rdi
mov    %r12,%rsi
mov    %r15,%rcx
mov    %r14,%r8
call   1070 <snprintf@plt>
mov    0x2dae(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1080 <access@plt>
test   %rax,%rax
je     124b <test_file_path+0xeb>
mov    %rbx,%rdi
call   1040 <free@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [6 x i8] c"BUILD\00"
@.str.1 = [7 x i8] c"SOURCE\00"
@__const.test_file_path.envs = [3 x i8*] [i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i32 0, i32 0), i8* null]
@.str.2 = [6 x i8] c"%s/%s\00"
@R_OK = global i32 0
define i8* @test_file_path(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca [3 x i8*]
%7 = alloca i32
store i8* %0, i8** %2
store i8* null, i8** %4
%8 = bitcast [3 x i8*]* %6 to i8*
store i32 0, i32* %7
br label %9
9: ; preds = %48, %1
%10 = load i32, i32* %7
%11 = sext i32 %10 to i64
%12 = getelementptr inbounds [3 x i8*], [3 x i8*]* %6, i64 0, i64 %11
%13 = load i8*, i8** %12
%14 = icmp ne i8* %13, null
br i1 %14, label %15, label %51
15: ; preds = %9
%16 = load i32, i32* %7
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds [3 x i8*], [3 x i8*]* %6, i64 0, i64 %17
%19 = load i8*, i8** %18
%20 = call i8* @getenv(i8* %19)
store i8* %20, i8** %3
%21 = load i8*, i8** %3
%22 = icmp eq i8* %21, null
br i1 %22, label %23, label %24
23: ; preds = %15
br label %48
24: ; preds = %15
%25 = load i8*, i8** %3
%26 = call i32 @strlen(i8* %25)
%27 = add nsw i32 %26, 1
%28 = load i8*, i8** %2
%29 = call i32 @strlen(i8* %28)
%30 = add nsw i32 %27, %29
%31 = add nsw i32 %30, 1
%32 = sext i32 %31 to i64
store i64 %32, i64* %5
%33 = load i64, i64* %5
%34 = call i8* @bmalloc(i64 %33)
store i8* %34, i8** %4
%35 = load i8*, i8** %4
%36 = load i64, i64* %5
%37 = load i8*, i8** %3
%38 = load i8*, i8** %2
%39 = call i32 @snprintf(i8* %35, i64 %36, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %37, i8* %38)
%40 = load i8*, i8** %4
%41 = load i32, i32* @R_OK
%42 = call i64 @access(i8* %40, i32 %41)
%43 = icmp eq i64 %42, 0
br i1 %43, label %44, label %45
44: ; preds = %24
br label %51
45: ; preds = %24
%46 = load i8*, i8** %4
%47 = call i32 @free(i8* %46)
store i8* null, i8** %4
br label %48
48: ; preds = %45, %23
%49 = load i32, i32* %7
%50 = add nsw i32 %49, 1
store i32 %50, i32* %7
br label %9
51: ; preds = %44, %9
%52 = load i8*, i8** %4
ret i8* %52
}

LLVM IR (-O2):
@.str = [6 x i8] c"BUILD\00"
@.str.1 = [7 x i8] c"SOURCE\00"
@.str.2 = [6 x i8] c"%s/%s\00"
@R_OK = global i32 0
define i8* @test_file_path(i8* %0) {
%2 = tail call i8* @getenv(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i8* %2, null
br i1 %3, label %17, label %4
4: ; preds = %1
%5 = tail call i32 @strlen(i8* %2)
%6 = tail call i32 @strlen(i8* %0)
%7 = add i32 %5, 2
%8 = add i32 %7, %6
%9 = sext i32 %8 to i64
%10 = tail call i8* @bmalloc(i64 %9)
%11 = tail call i32 @snprintf(i8* %10, i64 %9, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %2, i8* %0)
%12 = load i32, i32* @R_OK
%13 = tail call i64 @access(i8* %10, i32 %12)
%14 = icmp eq i64 %13, 0
br i1 %14, label %33, label %15
15: ; preds = %4
%16 = tail call i32 @free(i8* %10)
br label %17
17: ; preds = %1, %15
%18 = tail call i8* @getenv(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%19 = icmp eq i8* %18, null
br i1 %19, label %33, label %20
20: ; preds = %17
%21 = tail call i32 @strlen(i8* %18)
%22 = tail call i32 @strlen(i8* %0)
%23 = add i32 %21, 2
%24 = add i32 %23, %22
%25 = sext i32 %24 to i64
%26 = tail call i8* @bmalloc(i64 %25)
%27 = tail call i32 @snprintf(i8* %26, i64 %25, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %18, i8* %0)
%28 = load i32, i32* @R_OK
%29 = tail call i64 @access(i8* %26, i32 %28)
%30 = icmp eq i64 %29, 0
br i1 %30, label %33, label %31
31: ; preds = %20
%32 = tail call i32 @free(i8* %26)
br label %33
33: ; preds = %17, %31, %20, %4
%34 = phi i8* [ %10, %4 ], [ %26, %20 ], [ null, %31 ], [ null, %17 ]
ret i8* %34
}