#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;
int /*<<< orphan*/ access(char *, int /*<<< orphan*/);
int /*<<< orphan*/ getenv(char *);
scalar_t__ malloc(int);
int ntokenize(int, char *, char **, char *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, char *);
int /*<<< orphan*/ strncpy(char *, int /*<<< orphan*/, int);
char *get_path_cmd(char *cmd) {
  char *path_vect[25];
  int num_elem_path;
  int i;
  char path[500];
  int bufsize = 200;
  char *path_aux = (char *)malloc(bufsize);
  strncpy(path, getenv("PATH"), 500);
  num_elem_path = ntokenize(25, path, path_vect, ":");
  for (i = 0; i < num_elem_path; i++) {
    snprintf(path_aux, bufsize, "%s/%s", path_vect[i], cmd);
    if (!access(path_aux, X_OK)) {
      return path_aux;
    }
  }
  return NULL;
}


x86 assembly (clang, -O0):
<get_path_cmd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x300,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0xc8,-0x2e4(%rbp)
mov    -0x2e4(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,-0x2f0(%rbp)
lea    -0x2e0(%rbp),%rax
mov    %rax,-0x2f8(%rbp)
lea    0xe60(%rip),%rdi
call   1030 <getenv@plt>
mov    -0x2f8(%rbp),%rdi
mov    %eax,%esi
mov    $0x1f4,%edx
call   1040 <strncpy@plt>
lea    -0x2e0(%rbp),%rsi
lea    -0xe0(%rbp),%rdx
mov    $0x19,%edi
lea    0xe33(%rip),%rcx
call   1070 <ntokenize@plt>
mov    %eax,-0xe4(%rbp)
movl   $0x0,-0xe8(%rbp)
mov    -0xe8(%rbp),%eax
cmp    -0xe4(%rbp),%eax
jge    126c <get_path_cmd+0x10c>
mov    -0x2f0(%rbp),%rdi
mov    -0x2e4(%rbp),%esi
movslq -0xe8(%rbp),%rax
mov    -0xe0(%rbp,%rax,8),%rcx
ff
mov    -0x10(%rbp),%r8
lea    0xde7(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x2f0(%rbp),%rdi
mov    0x2db5(%rip),%rax
mov    (%rax),%esi
call   1080 <access@plt>
cmp    $0x0,%eax
jne    1253 <get_path_cmd+0xf3>
mov    -0x2f0(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1274 <get_path_cmd+0x114>
jmp    1258 <get_path_cmd+0xf8>
mov    -0xe8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xe8(%rbp)
jmp    11e7 <get_path_cmd+0x87>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x300,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_path_cmd>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x2c8,%rsp
mov    %rdi,%r14
mov    $0xc8,%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
lea    0xe78(%rip),%rdi
call   1030 <getenv@plt>
lea    0xd0(%rsp),%r15
mov    %r15,%rdi
mov    %eax,%esi
mov    $0x1f4,%edx
call   1040 <strncpy@plt>
lea    0xe5a(%rip),%rcx
mov    %rsp,%rdx
mov    $0x19,%edi
mov    %r15,%rsi
call   1070 <ntokenize@plt>
test   %eax,%eax
jle    120f <get_path_cmd+0xaf>
mov    %eax,%r13d
lea    0xe3e(%rip),%r12
mov    0x2e18(%rip),%rbp
xor    %r15d,%r15d
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    (%rsp,%r15,8),%rcx
mov    %rbx,%rdi
mov    $0xc8,%esi
mov    %r12,%rdx
mov    %r14,%r8
call   1050 <snprintf@plt>
mov    0x0(%rbp),%esi
mov    %rbx,%rdi
call   1080 <access@plt>
test   %eax,%eax
je     1213 <get_path_cmd+0xb3>
add    $0x1,%r15
cmp    %r15,%r13
jne    11e0 <get_path_cmd+0x80>
xor    %eax,%eax
jmp    1216 <get_path_cmd+0xb6>
mov    %rbx,%rax
add    $0x2c8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"PATH\00"
@.str.1 = [2 x i8] c":\00"
@.str.2 = [6 x i8] c"%s/%s\00"
@X_OK = global i32 0
define i8* @get_path_cmd(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca [25 x i8*]
%5 = alloca i32
%6 = alloca i32
%7 = alloca [500 x i8]
%8 = alloca i32
%9 = alloca i8*
store i8* %0, i8** %3
store i32 200, i32* %8
%10 = load i32, i32* %8
%11 = call i64 @malloc(i32 %10)
%12 = inttoptr i64 %11 to i8*
store i8* %12, i8** %9
%13 = getelementptr inbounds [500 x i8], [500 x i8]* %7, i64 0, i64 0
%14 = call i32 @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%15 = call i32 @strncpy(i8* %13, i32 %14, i32 500)
%16 = getelementptr inbounds [500 x i8], [500 x i8]* %7, i64 0, i64 0
%17 = getelementptr inbounds [25 x i8*], [25 x i8*]* %4, i64 0, i64 0
%18 = call i32 @ntokenize(i32 25, i8* %16, i8** %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32 %18, i32* %5
store i32 0, i32* %6
br label %19
19: ; preds = %39, %1
%20 = load i32, i32* %6
%21 = load i32, i32* %5
%22 = icmp slt i32 %20, %21
br i1 %22, label %23, label %42
23: ; preds = %19
%24 = load i8*, i8** %9
%25 = load i32, i32* %8
%26 = load i32, i32* %6
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds [25 x i8*], [25 x i8*]* %4, i64 0, i64 %27
%29 = load i8*, i8** %28
%30 = load i8*, i8** %3
%31 = call i32 @snprintf(i8* %24, i32 %25, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %29, i8* %30)
%32 = load i8*, i8** %9
%33 = load i32, i32* @X_OK
%34 = call i32 @access(i8* %32, i32 %33)
%35 = icmp ne i32 %34, 0
br i1 %35, label %38, label %36
36: ; preds = %23
%37 = load i8*, i8** %9
store i8* %37, i8** %2
br label %43
38: ; preds = %23
br label %39
39: ; preds = %38
%40 = load i32, i32* %6
%41 = add nsw i32 %40, 1
store i32 %41, i32* %6
br label %19
42: ; preds = %19
store i8* null, i8** %2
br label %43
43: ; preds = %42, %36
%44 = load i8*, i8** %2
ret i8* %44
}

LLVM IR (-O2):
@.str = [5 x i8] c"PATH\00"
@.str.1 = [2 x i8] c":\00"
@.str.2 = [6 x i8] c"%s/%s\00"
@X_OK = global i32 0
define i8* @get_path_cmd(i8* %0) {
%2 = alloca [25 x i8*]
%3 = alloca [500 x i8]
%4 = bitcast [25 x i8*]* %2 to i8*
%5 = getelementptr inbounds [500 x i8], [500 x i8]* %3, i64 0, i64 0
%6 = tail call i64 @malloc(i32 200)
%7 = inttoptr i64 %6 to i8*
%8 = tail call i32 @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%9 = call i32 @strncpy(i8* %5, i32 %8, i32 500)
%10 = getelementptr inbounds [25 x i8*], [25 x i8*]* %2, i64 0, i64 0
%11 = call i32 @ntokenize(i32 25, i8* %5, i8** %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%12 = icmp sgt i32 %11, 0
br i1 %12, label %13, label %26
13: ; preds = %1
%14 = zext i32 %11 to i64
br label %17
15: ; preds = %17
%16 = icmp eq i64 %25, %14
br i1 %16, label %26, label %17
17: ; preds = %13, %15
%18 = phi i64 [ 0, %13 ], [ %25, %15 ]
%19 = getelementptr inbounds [25 x i8*], [25 x i8*]* %2, i64 0, i64 %18
%20 = load i8*, i8** %19
%21 = call i32 @snprintf(i8* %7, i32 200, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %20, i8* %0)
%22 = load i32, i32* @X_OK
%23 = call i32 @access(i8* %7, i32 %22)
%24 = icmp eq i32 %23, 0
%25 = add nuw nsw i64 %18, 1
br i1 %24, label %26, label %15
26: ; preds = %17, %15, %1
%27 = phi i8* [ null, %1 ], [ null, %15 ], [ %7, %17 ]
ret i8* %27
}