#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 */
scalar_t__ MMalloc(long);
scalar_t__ check_file(char *, char *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ sprintf(char *, char *, char *, char *);
char *strchr(char *, char);
scalar_t__ strcmp(char *, char *);
scalar_t__ strlen(char *);
char *util_strsav(char *);
char *util_tilde_expand(char *);
char *util_file_search(char *file, char *path, char *mode) {
  int quit;
  char *buffer, *filename, *save_path, *cp;
  if (path == 0 || strcmp(path, "") == 0) {
    path = (char *)".";
  }
  save_path = path = util_strsav(path);
  quit = 0;
  do {
    cp = strchr(path, ':');
    if (cp != 0) {
      *cp = '\0';
    } else {
      quit = 1;
    }
    if (strcmp(path, ".") == 0) {
      buffer = util_strsav(file);
    } else {
      buffer = ((char *)MMalloc((long)sizeof(char) *
                                (long)(strlen(path) + strlen(file) + 4)));
      (void)sprintf(buffer, "%s/%s", path, file);
    }
    filename = util_tilde_expand(buffer);
    ((buffer) ? (free((char *)(buffer)), (buffer) = 0) : 0);
    if (check_file(filename, mode)) {
      ((save_path) ? (free((char *)(save_path)), (save_path) = 0) : 0);
      return filename;
    }
    ((filename) ? (free((char *)(filename)), (filename) = 0) : 0);
    path = ++cp;
  } while (!quit);
  ((save_path) ? (free((char *)(save_path)), (save_path) = 0) : 0);
  return 0;
}


x86 assembly (clang, -O0):
<util_file_search>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11c9 <util_file_search+0x39>
mov    -0x18(%rbp),%rdi
lea    0xe47(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
jne    11d4 <util_file_search+0x44>
lea    0xe30(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <util_strsav@plt>
mov    %rax,-0x18(%rbp)
mov    %rax,-0x40(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x18(%rbp),%rdi
mov    $0x3a,%esi
call   1070 <strchr@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
je     1215 <util_file_search+0x85>
mov    -0x48(%rbp),%rax
movb   $0x0,(%rax)
jmp    121c <util_file_search+0x8c>
movl   $0x1,-0x24(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xdd9(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
jne    1248 <util_file_search+0xb8>
mov    -0x10(%rbp),%rdi
call   1050 <util_strsav@plt>
mov    %rax,-0x30(%rbp)
jmp    128e <util_file_search+0xfe>
mov    -0x18(%rbp),%rdi
call   1060 <strlen@plt>
mov    %rax,-0x50(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x50(%rbp),%rdi
add    %rax,%rdi
add    $0x4,%rdi
shl    $0x0,%rdi
call   1040 <MMalloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0xd79(%rip),%rsi
call   10a0 <sprintf@plt>
mov    -0x30(%rbp),%rdi
call   10b0 <util_tilde_expand@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     12bc <util_file_search+0x12c>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x30(%rbp)
jmp    12c1 <util_file_search+0x131>
jmp    12c1 <util_file_search+0x131>
mov    -0x38(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1090 <check_file@plt>
cmp    $0x0,%rax
je     130b <util_file_search+0x17b>
cmpq   $0x0,-0x40(%rbp)
je     12f9 <util_file_search+0x169>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x40(%rbp)
jmp    12fe <util_file_search+0x16e>
jmp    12fe <util_file_search+0x16e>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1380 <util_file_search+0x1f0>
cmpq   $0x0,-0x38(%rbp)
je     132c <util_file_search+0x19c>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x38(%rbp)
jmp    1331 <util_file_search+0x1a1>
jmp    1331 <util_file_search+0x1a1>
mov    -0x48(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x48(%rbp)
mov    %rax,-0x18(%rbp)
cmpl   $0x0,-0x24(%rbp)
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    11ec <util_file_search+0x5c>
cmpq   $0x0,-0x40(%rbp)
je     1373 <util_file_search+0x1e3>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x40(%rbp)
jmp    1378 <util_file_search+0x1e8>
jmp    1378 <util_file_search+0x1e8>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<util_file_search>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rdi,%r13
test   %rsi,%rsi
je     11bd <util_file_search+0x2d>
mov    %rsi,%rbx
lea    0xe51(%rip),%rsi
mov    %rbx,%rdi
call   1080 <strcmp@plt>
test   %rax,%rax
jne    11c4 <util_file_search+0x34>
lea    0xe3c(%rip),%rbx
mov    %rbx,%rdi
call   1050 <util_strsav@plt>
mov    %rax,%r15
mov    %rax,(%rsp)
jmp    11ed <util_file_search+0x5d>
cs nopw 0x0(%rax,%rax,1)
nop
lea    0x1(%rbp),%r15
test   %rbp,%rbp
je     12a9 <util_file_search+0x119>
mov    %r15,%rdi
mov    $0x3a,%esi
call   1070 <strchr@plt>
mov    %rax,%rbp
test   %rax,%rax
je     1206 <util_file_search+0x76>
movb   $0x0,0x0(%rbp)
mov    %r15,%rdi
lea    0xdf0(%rip),%rsi
call   1080 <strcmp@plt>
test   %rax,%rax
je     1260 <util_file_search+0xd0>
mov    %r15,%rdi
call   1060 <strlen@plt>
mov    %rax,%rbx
mov    %r13,%rdi
call   1060 <strlen@plt>
lea    (%rbx,%rax,1),%rdi
add    $0x4,%rdi
call   1040 <MMalloc@plt>
mov    %rax,%r12
mov    %rax,%rdi
lea    0xdbb(%rip),%rsi
mov    %r15,%rdx
mov    %r13,%rcx
call   10a0 <sprintf@plt>
jmp    126b <util_file_search+0xdb>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r13,%rdi
call   1050 <util_strsav@plt>
mov    %rax,%r12
mov    %r12,%rdi
call   10b0 <util_tilde_expand@plt>
mov    %rax,%rbx
test   %r12,%r12
je     1283 <util_file_search+0xf3>
mov    %r12,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1090 <check_file@plt>
test   %rax,%rax
jne    12ab <util_file_search+0x11b>
test   %rbx,%rbx
je     11e0 <util_file_search+0x50>
mov    %rbx,%rdi
call   1030 <free@plt>
jmp    11e0 <util_file_search+0x50>
xor    %ebx,%ebx
mov    (%rsp),%rdi
test   %rdi,%rdi
je     12b9 <util_file_search+0x129>
call   1030 <free@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [1 x i8] zeroinitializer
@.str.1 = [2 x i8] c".\00"
@.str.2 = [6 x i8] c"%s/%s\00"
define i8* @util_file_search(i8* %0, i8* %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i8*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
%13 = load i8*, i8** %6
%14 = icmp eq i8* %13, null
br i1 %14, label %19, label %15
15: ; preds = %3
%16 = load i8*, i8** %6
%17 = call i64 @strcmp(i8* %16, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %15, %3
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8** %6
br label %20
20: ; preds = %19, %15
%21 = load i8*, i8** %6
%22 = call i8* @util_strsav(i8* %21)
store i8* %22, i8** %6
store i8* %22, i8** %11
store i32 0, i32* %8
br label %23
23: ; preds = %88, %20
%24 = load i8*, i8** %6
%25 = call i8* @strchr(i8* %24, i8 signext 58)
store i8* %25, i8** %12
%26 = load i8*, i8** %12
%27 = icmp ne i8* %26, null
br i1 %27, label %28, label %30
28: ; preds = %23
%29 = load i8*, i8** %12
store i8 0, i8* %29
br label %31
30: ; preds = %23
store i32 1, i32* %8
br label %31
31: ; preds = %30, %28
%32 = load i8*, i8** %6
%33 = call i64 @strcmp(i8* %32, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%34 = icmp eq i64 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %31
%36 = load i8*, i8** %5
%37 = call i8* @util_strsav(i8* %36)
store i8* %37, i8** %9
br label %52
38: ; preds = %31
%39 = load i8*, i8** %6
%40 = call i64 @strlen(i8* %39)
%41 = load i8*, i8** %5
%42 = call i64 @strlen(i8* %41)
%43 = add nsw i64 %40, %42
%44 = add nsw i64 %43, 4
%45 = mul nsw i64 1, %44
%46 = call i64 @MMalloc(i64 %45)
%47 = inttoptr i64 %46 to i8*
store i8* %47, i8** %9
%48 = load i8*, i8** %9
%49 = load i8*, i8** %6
%50 = load i8*, i8** %5
%51 = call i32 @sprintf(i8* %48, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %49, i8* %50)
br label %52
52: ; preds = %38, %35
%53 = load i8*, i8** %9
%54 = call i8* @util_tilde_expand(i8* %53)
store i8* %54, i8** %10
%55 = load i8*, i8** %9
%56 = icmp ne i8* %55, null
br i1 %56, label %57, label %60
57: ; preds = %52
%58 = load i8*, i8** %9
%59 = call i32 @free(i8* %58)
store i8* null, i8** %9
br label %61
60: ; preds = %52
br label %61
61: ; preds = %60, %57
%62 = phi i8* [ null, %57 ], [ null, %60 ]
%63 = load i8*, i8** %10
%64 = load i8*, i8** %7
%65 = call i64 @check_file(i8* %63, i8* %64)
%66 = icmp ne i64 %65, 0
br i1 %66, label %67, label %77
67: ; preds = %61
%68 = load i8*, i8** %11
%69 = icmp ne i8* %68, null
br i1 %69, label %70, label %73
70: ; preds = %67
%71 = load i8*, i8** %11
%72 = call i32 @free(i8* %71)
store i8* null, i8** %11
br label %74
73: ; preds = %67
br label %74
74: ; preds = %73, %70
%75 = phi i8* [ null, %70 ], [ null, %73 ]
%76 = load i8*, i8** %10
store i8* %76, i8** %4
br label %101
77: ; preds = %61
%78 = load i8*, i8** %10
%79 = icmp ne i8* %78, null
br i1 %79, label %80, label %83
80: ; preds = %77
%81 = load i8*, i8** %10
%82 = call i32 @free(i8* %81)
store i8* null, i8** %10
br label %84
83: ; preds = %77
br label %84
84: ; preds = %83, %80
%85 = phi i8* [ null, %80 ], [ null, %83 ]
%86 = load i8*, i8** %12
%87 = getelementptr inbounds i8, i8* %86, i32 1
store i8* %87, i8** %12
store i8* %87, i8** %6
br label %88
88: ; preds = %84
%89 = load i32, i32* %8
%90 = icmp ne i32 %89, 0
%91 = xor i1 %90, true
br i1 %91, label %23, label %92
92: ; preds = %88
%93 = load i8*, i8** %11
%94 = icmp ne i8* %93, null
br i1 %94, label %95, label %98
95: ; preds = %92
%96 = load i8*, i8** %11
%97 = call i32 @free(i8* %96)
store i8* null, i8** %11
br label %99
98: ; preds = %92
br label %99
99: ; preds = %98, %95
%100 = phi i8* [ null, %95 ], [ null, %98 ]
store i8* null, i8** %4
br label %101
101: ; preds = %99, %74
%102 = load i8*, i8** %4
ret i8* %102
}

LLVM IR (-O2):
@.str = [1 x i8] zeroinitializer
@.str.1 = [2 x i8] c".\00"
@.str.2 = [6 x i8] c"%s/%s\00"
define i8* @util_file_search(i8* %0, i8* %1, i8* %2) {
%4 = icmp eq i8* %1, null
br i1 %4, label %8, label %5
5: ; preds = %3
%6 = tail call i64 @strcmp(i8* %1, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %9
8: ; preds = %5, %3
br label %9
9: ; preds = %8, %5
%10 = phi i8* [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), %8 ], [ %1, %5 ]
%11 = tail call i8* @util_strsav(i8* %10)
br label %12
12: ; preds = %45, %9
%13 = phi i8* [ %11, %9 ], [ %46, %45 ]
%14 = tail call i8* @strchr(i8* dereferenceable(1) %13, i8 signext 58)
%15 = icmp eq i8* %14, null
br i1 %15, label %17, label %16
16: ; preds = %12
store i8 0, i8* %14
br label %17
17: ; preds = %12, %16
%18 = tail call i64 @strcmp(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %17
%21 = tail call i8* @util_strsav(i8* %0)
br label %30
22: ; preds = %17
%23 = tail call i64 @strlen(i8* dereferenceable(1) %13)
%24 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%25 = add i64 %23, 4
%26 = add i64 %25, %24
%27 = tail call i64 @MMalloc(i64 %26)
%28 = inttoptr i64 %27 to i8*
%29 = tail call i32 @sprintf(i8* dereferenceable(1) %28, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %13, i8* %0)
br label %30
30: ; preds = %22, %20
%31 = phi i8* [ %21, %20 ], [ %28, %22 ]
%32 = tail call i8* @util_tilde_expand(i8* %31)
%33 = icmp eq i8* %31, null
br i1 %33, label %36, label %34
34: ; preds = %30
%35 = tail call i32 @free(i8* %31)
br label %36
36: ; preds = %30, %34
%37 = tail call i64 @check_file(i8* %32, i8* %2)
%38 = icmp eq i64 %37, 0
br i1 %38, label %41, label %39
39: ; preds = %36
%40 = icmp eq i8* %11, null
br i1 %40, label %52, label %49
41: ; preds = %36
%42 = icmp eq i8* %32, null
br i1 %42, label %45, label %43
43: ; preds = %41
%44 = tail call i32 @free(i8* %32)
br label %45
45: ; preds = %41, %43
%46 = getelementptr inbounds i8, i8* %14, i64 1
br i1 %15, label %47, label %12
47: ; preds = %45
%48 = icmp eq i8* %11, null
br i1 %48, label %52, label %49
49: ; preds = %47, %39
%50 = phi i8* [ %32, %39 ], [ null, %47 ]
%51 = tail call i32 @free(i8* %11)
br label %52
52: ; preds = %49, %47, %39
%53 = phi i8* [ %32, %39 ], [ null, %47 ], [ %50, %49 ]
ret i8* %53
}