#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*/ X_OK;
scalar_t__ access(char *, int /*<<< orphan*/);
int /*<<< orphan*/ die(int, char *, ...);
int /*<<< orphan*/ free(char *);
char *get_current_dir_name();
char *getenv(char *);
char *merge_paths(char *, char const *);
int /*<<< orphan*/ *strchr(char const *, char);
char *strdup(char const *);
char *strtok_r(char *, char *, char **);
char *search_program(const char *progname) {
  char *launch = NULL;
  char *cwd;
  if (progname[0] == '/') {
    launch = strdup(progname);
    if (!launch) {
      die(1, "allocating program name buffer");
    }
  } else if (strchr(progname, '/') != NULL) {
    cwd = get_current_dir_name();
    launch = merge_paths(cwd, progname);
    free(cwd);
  } else {
    char *path = getenv("PATH");
    char *saveptr = NULL;
    char *token;
    if (path == NULL) {
      die(1, "could not get PATH environment variable");
    }
    path = strdup(path);
    for (token = strtok_r(path, ":", &saveptr); token != NULL;
         token = strtok_r(NULL, ":", &saveptr)) {
      launch = merge_paths(token, progname);
      if (access(launch, X_OK) == 0)
        break;
      free(launch);
      launch = NULL;
    }
    free(path);
    if (launch == NULL) {
      die(1, "could not locate program \"%s\" to launch \n", progname);
    }
    if (launch[0] != '/') {
      char *relative = launch;
      cwd = get_current_dir_name();
      launch = merge_paths(cwd, relative);
      free(cwd);
      free(relative);
    }
  }
  return launch;
}


x86 assembly (clang, -O0):
<search_program>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    11e4 <search_program+0x54>
mov    -0x8(%rbp),%rdi
call   10b0 <strdup@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11df <search_program+0x4f>
mov    $0x1,%edi
lea    0xe28(%rip),%rsi
mov    $0x0,%al
call   10a0 <die@plt>
jmp    1367 <search_program+0x1d7>
mov    -0x8(%rbp),%rdi
mov    $0x2f,%esi
call   1050 <strchr@plt>
cmp    $0x0,%rax
je     1226 <search_program+0x96>
mov    $0x0,%al
call   1080 <get_current_dir_name@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1070 <merge_paths@plt>
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <free@plt>
jmp    1362 <search_program+0x1d2>
lea    0xdf2(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    125c <search_program+0xcc>
mov    $0x1,%edi
lea    0xdcf(%rip),%rsi
mov    $0x0,%al
call   10a0 <die@plt>
mov    -0x20(%rbp),%rdi
call   10b0 <strdup@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xdd8(%rip),%rsi
lea    -0x28(%rbp),%rdx
call   1060 <strtok_r@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     12ec <search_program+0x15c>
mov    -0x30(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1070 <merge_paths@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2d40(%rip),%rax
mov    (%rax),%esi
call   1090 <access@plt>
cmp    $0x0,%rax
jne    12be <search_program+0x12e>
jmp    12ec <search_program+0x15c>
mov    -0x10(%rbp),%rdi
call   1040 <free@plt>
movq   $0x0,-0x10(%rbp)
xor    %eax,%eax
mov    %eax,%edi
lea    0xd72(%rip),%rsi
lea    -0x28(%rbp),%rdx
call   1060 <strtok_r@plt>
mov    %rax,-0x30(%rbp)
jmp    1281 <search_program+0xf1>
mov    -0x20(%rbp),%rdi
call   1040 <free@plt>
cmpq   $0x0,-0x10(%rbp)
jne    1317 <search_program+0x187>
mov    -0x8(%rbp),%rdx
mov    $0x1,%edi
lea    0xd3e(%rip),%rsi
mov    $0x0,%al
call   10a0 <die@plt>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
je     135d <search_program+0x1cd>
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    $0x0,%al
call   1080 <get_current_dir_name@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x38(%rbp),%rsi
call   1070 <merge_paths@plt>
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <free@plt>
mov    -0x38(%rbp),%rdi
call   1040 <free@plt>
jmp    1362 <search_program+0x1d2>
jmp    1367 <search_program+0x1d7>
mov    -0x10(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<search_program>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
cmpb   $0x2f,(%rdi)
jne    11d1 <search_program+0x41>
mov    %r14,%rdi
call   10b0 <strdup@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1303 <search_program+0x173>
lea    0xe42(%rip),%rsi
xor    %ebx,%ebx
mov    $0x1,%edi
xor    %eax,%eax
call   10a0 <die@plt>
jmp    1303 <search_program+0x173>
mov    %r14,%rdi
mov    $0x2f,%esi
call   1050 <strchr@plt>
test   %rax,%rax
je     120b <search_program+0x7b>
xor    %eax,%eax
call   1080 <get_current_dir_name@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
call   1070 <merge_paths@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1040 <free@plt>
mov    %r14,%rax
jmp    1306 <search_program+0x176>
lea    0xe0d(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rbx
movq   $0x0,(%rsp)
test   %rax,%rax
jne    123a <search_program+0xaa>
lea    0xdf6(%rip),%rsi
mov    $0x1,%edi
xor    %eax,%eax
call   10a0 <die@plt>
mov    %rbx,%rdi
call   10b0 <strdup@plt>
mov    %rax,%r15
lea    0xe00(%rip),%rsi
mov    %rsp,%rdx
mov    %rax,%rdi
call   1060 <strtok_r@plt>
test   %rax,%rax
je     12a8 <search_program+0x118>
mov    0x2d85(%rip),%rbp
lea    0xde2(%rip),%r12
mov    %rsp,%r13
nopl   (%rax)
mov    %rax,%rdi
mov    %r14,%rsi
call   1070 <merge_paths@plt>
mov    %rax,%rbx
mov    0x0(%rbp),%esi
mov    %rax,%rdi
call   1090 <access@plt>
test   %rax,%rax
je     12c6 <search_program+0x136>
mov    %rbx,%rdi
call   1040 <free@plt>
xor    %edi,%edi
mov    %r12,%rsi
mov    %r13,%rdx
call   1060 <strtok_r@plt>
test   %rax,%rax
jne    1270 <search_program+0xe0>
mov    %r15,%rdi
call   1040 <free@plt>
lea    0xd97(%rip),%rsi
mov    $0x1,%edi
mov    %r14,%rdx
xor    %eax,%eax
call   10a0 <die@plt>
mov    %r15,%rdi
call   1040 <free@plt>
test   %rbx,%rbx
je     12b0 <search_program+0x120>
cmpb   $0x2f,(%rbx)
je     1303 <search_program+0x173>
xor    %eax,%eax
call   1080 <get_current_dir_name@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    %rbx,%rsi
call   1070 <merge_paths@plt>
mov    %rax,%r15
mov    %r14,%rdi
call   1040 <free@plt>
mov    %rbx,%rdi
call   1040 <free@plt>
mov    %r15,%rbx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [31 x i8] c"allocating program name buffer\00"
@.str.1 = [5 x i8] c"PATH\00"
@.str.2 = [40 x i8] c"could not get PATH environment variable\00"
@.str.3 = [2 x i8] c":\00"
@X_OK = global i32 0
@.str.4 = [42 x i8] c"could not locate program \22%s\22 to launch \0A\00"
define i8* @search_program(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
store i8* %0, i8** %2
store i8* null, i8** %3
%9 = load i8*, i8** %2
%10 = getelementptr inbounds i8, i8* %9, i64 0
%11 = load i8, i8* %10
%12 = sext i8 %11 to i32
%13 = icmp eq i32 %12, 47
br i1 %13, label %14, label %22
14: ; preds = %1
%15 = load i8*, i8** %2
%16 = call i8* @strdup(i8* %15)
store i8* %16, i8** %3
%17 = load i8*, i8** %3
%18 = icmp ne i8* %17, null
br i1 %18, label %21, label %19
19: ; preds = %14
%20 = call i32 (i32, i8*, ...) @die(i32 1, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
br label %21
21: ; preds = %19, %14
br label %87
22: ; preds = %1
%23 = load i8*, i8** %2
%24 = call i32* @strchr(i8* %23, i8 signext 47)
%25 = icmp ne i32* %24, null
br i1 %25, label %26, label %33
26: ; preds = %22
%27 = call i8* (...) @get_current_dir_name()
store i8* %27, i8** %4
%28 = load i8*, i8** %4
%29 = load i8*, i8** %2
%30 = call i8* @merge_paths(i8* %28, i8* %29)
store i8* %30, i8** %3
%31 = load i8*, i8** %4
%32 = call i32 @free(i8* %31)
br label %86
33: ; preds = %22
%34 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i8* %34, i8** %5
store i8* null, i8** %6
%35 = load i8*, i8** %5
%36 = icmp eq i8* %35, null
br i1 %36, label %37, label %39
37: ; preds = %33
%38 = call i32 (i32, i8*, ...) @die(i32 1, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0))
br label %39
39: ; preds = %37, %33
%40 = load i8*, i8** %5
%41 = call i8* @strdup(i8* %40)
store i8* %41, i8** %5
%42 = load i8*, i8** %5
%43 = call i8* @strtok_r(i8* %42, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i8** %6)
store i8* %43, i8** %7
br label %44
44: ; preds = %59, %39
%45 = load i8*, i8** %7
%46 = icmp ne i8* %45, null
br i1 %46, label %47, label %61
47: ; preds = %44
%48 = load i8*, i8** %7
%49 = load i8*, i8** %2
%50 = call i8* @merge_paths(i8* %48, i8* %49)
store i8* %50, i8** %3
%51 = load i8*, i8** %3
%52 = load i32, i32* @X_OK
%53 = call i64 @access(i8* %51, i32 %52)
%54 = icmp eq i64 %53, 0
br i1 %54, label %55, label %56
55: ; preds = %47
br label %61
56: ; preds = %47
%57 = load i8*, i8** %3
%58 = call i32 @free(i8* %57)
store i8* null, i8** %3
br label %59
59: ; preds = %56
%60 = call i8* @strtok_r(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i8** %6)
store i8* %60, i8** %7
br label %44
61: ; preds = %55, %44
%62 = load i8*, i8** %5
%63 = call i32 @free(i8* %62)
%64 = load i8*, i8** %3
%65 = icmp eq i8* %64, null
br i1 %65, label %66, label %69
66: ; preds = %61
%67 = load i8*, i8** %2
%68 = call i32 (i32, i8*, ...) @die(i32 1, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.4, i64 0, i64 0), i8* %67)
br label %69
69: ; preds = %66, %61
%70 = load i8*, i8** %3
%71 = getelementptr inbounds i8, i8* %70, i64 0
%72 = load i8, i8* %71
%73 = sext i8 %72 to i32
%74 = icmp ne i32 %73, 47
br i1 %74, label %75, label %85
75: ; preds = %69
%76 = load i8*, i8** %3
store i8* %76, i8** %8
%77 = call i8* (...) @get_current_dir_name()
store i8* %77, i8** %4
%78 = load i8*, i8** %4
%79 = load i8*, i8** %8
%80 = call i8* @merge_paths(i8* %78, i8* %79)
store i8* %80, i8** %3
%81 = load i8*, i8** %4
%82 = call i32 @free(i8* %81)
%83 = load i8*, i8** %8
%84 = call i32 @free(i8* %83)
br label %85
85: ; preds = %75, %69
br label %86
86: ; preds = %85, %26
br label %87
87: ; preds = %86, %21
%88 = load i8*, i8** %3
ret i8* %88
}

LLVM IR (-O2):
@.str = [31 x i8] c"allocating program name buffer\00"
@.str.1 = [5 x i8] c"PATH\00"
@.str.2 = [40 x i8] c"could not get PATH environment variable\00"
@.str.3 = [2 x i8] c":\00"
@X_OK = global i32 0
@.str.4 = [42 x i8] c"could not locate program \22%s\22 to launch \0A\00"
define i8* @search_program(i8* %0) {
%2 = alloca i8*
%3 = load i8, i8* %0
%4 = icmp eq i8 %3, 47
br i1 %4, label %5, label %10
5: ; preds = %1
%6 = tail call i8* @strdup(i8* %0)
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %54
8: ; preds = %5
%9 = tail call i32 (i32, i8*, ...) @die(i32 1, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
br label %54
10: ; preds = %1
%11 = tail call i32* @strchr(i8* %0, i8 signext 47)
%12 = icmp eq i32* %11, null
br i1 %12, label %17, label %13
13: ; preds = %10
%14 = tail call i8* (...) @get_current_dir_name()
%15 = tail call i8* @merge_paths(i8* %14, i8* %0)
%16 = tail call i32 @free(i8* %14)
br label %54
17: ; preds = %10
%18 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%19 = bitcast i8** %2 to i8*
store i8* null, i8** %2
%20 = icmp eq i8* %18, null
br i1 %20, label %21, label %23
21: ; preds = %17
%22 = tail call i32 (i32, i8*, ...) @die(i32 1, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0))
br label %23
23: ; preds = %21, %17
%24 = tail call i8* @strdup(i8* %18)
%25 = call i8* @strtok_r(i8* %24, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i8** %2)
%26 = icmp eq i8* %25, null
br i1 %26, label %27, label %29
27: ; preds = %35, %23
%28 = call i32 @free(i8* %24)
br label %42
29: ; preds = %23, %35
%30 = phi i8* [ %37, %35 ], [ %25, %23 ]
%31 = call i8* @merge_paths(i8* %30, i8* %0)
%32 = load i32, i32* @X_OK
%33 = call i64 @access(i8* %31, i32 %32)
%34 = icmp eq i64 %33, 0
br i1 %34, label %39, label %35
35: ; preds = %29
%36 = call i32 @free(i8* %31)
%37 = call i8* @strtok_r(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i8** %2)
%38 = icmp eq i8* %37, null
br i1 %38, label %27, label %29
39: ; preds = %29
%40 = call i32 @free(i8* %24)
%41 = icmp eq i8* %31, null
br i1 %41, label %42, label %44
42: ; preds = %27, %39
%43 = call i32 (i32, i8*, ...) @die(i32 1, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.4, i64 0, i64 0), i8* %0)
unreachable
44: ; preds = %39
%45 = load i8, i8* %31
%46 = icmp eq i8 %45, 47
br i1 %46, label %52, label %47
47: ; preds = %44
%48 = call i8* (...) @get_current_dir_name()
%49 = call i8* @merge_paths(i8* %48, i8* %31)
%50 = call i32 @free(i8* %48)
%51 = call i32 @free(i8* %31)
br label %52
52: ; preds = %47, %44
%53 = phi i8* [ %49, %47 ], [ %31, %44 ]
br label %54
54: ; preds = %13, %52, %5, %8
%55 = phi i8* [ %6, %5 ], [ null, %8 ], [ %15, %13 ], [ %53, %52 ]
ret i8* %55
}