#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 */
struct dirent {
  char *d_name;
};
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int /*<<< orphan*/ cfree(char *);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
char *cmalloc(int);
int /*<<< orphan*/ free(char *);
char *getenv(char *);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int strlen(char *);
int /*<<< orphan*/ strncat(char *, char *, int);
scalar_t__ strncmp(char *, char *, size_t);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *strtok(char *, char *);
char *look_up_command(char *word, int nb, int afc) {
  char *p_env = getenv("PATH");
  char *p = p_env;
  char def[] = "./:../:";
  if (strncmp("\0", word, 1) == 0) {
    p = p_env;
  }
  if (afc) {
    if (strncmp("\0", word, 1) == 0) {
      p = def;
    } else {
      char *ptemp = word + (strlen(word) - 1);
      char *pathtmp = cmalloc((strlen(word) + 1) * sizeof(char));
      int cpt = strlen(word);
      while (*ptemp != '/' && ptemp >= word) {
        ptemp--;
        cpt--;
      }
      if (ptemp < word) {
        p = def;
      } else {
        strncpy(pathtmp, word, cpt);
        *(pathtmp + cpt) = '\0';
        p = pathtmp;
      }
      word = ptemp + 1;
    }
  } else {
    if (strncmp("./", word, 2) == 0) {
      p = def;
      word = word + 2;
    }
    if (strncmp("../", word, 3) == 0) {
      p = def + 3;
      word = word + 3;
    }
  }
  char *token = cmalloc(sizeof(char) * strlen(p));
  strncpy(token, p, strlen(p) + 1);
  char *path = strtok(token, ":");
  size_t n = strlen(word);
  int found = nb;
  struct dirent *cur_dir;
  DIR *rep;
  char *command1 = cmalloc(256 * sizeof(char));
  char *command2 = cmalloc(256 * sizeof(char));
  char *command = command2;
  while (found > 0) {
    if (path != NULL) {
      rep = opendir(path);
      if (rep != NULL) {
        while ((cur_dir = readdir(rep)) && found > 0) {
          if (strncmp(word, cur_dir->d_name, n) == 0) {
            found--;
          }
          if (found == 0) {
            strncpy(command2, cur_dir->d_name, strlen(cur_dir->d_name) + 1);
            if (afc || p == def || p == def + 3) {
              strncpy(command1, path, strlen(path) + 1);
              strncat(command1, command2, strlen(command2) + 1);
              cfree(command2);
              if (opendir(command1) != NULL) {
                strncat(command1, "/", 1);
              }
              command = command1;
            } else {
              cfree(command1);
            }
          }
        }
      }
      path = strtok(NULL, ":");
      closedir(rep);
    } else {
      found = 0;
      strncpy(command2, "", 1);
    }
  }
  free(token);
  return command;
}


x86 assembly (clang, -O0):
<look_up_command>:
push   %rbp
mov    %rsp,%rbp
sub    $0xe0,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
lea    0xe24(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    0xe11(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rsi
lea    0xe0e(%rip),%rdi
mov    $0x1,%edx
call   1060 <strncmp@plt>
cmp    $0x0,%rax
jne    121f <look_up_command+0x5f>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpl   $0x0,-0x10(%rbp)
je     134e <look_up_command+0x18e>
mov    -0x8(%rbp),%rsi
lea    0xddd(%rip),%rdi
mov    $0x1,%edx
call   1060 <strncmp@plt>
cmp    $0x0,%rax
jne    1255 <look_up_command+0x95>
lea    -0x28(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    1349 <look_up_command+0x189>
mov    -0x8(%rbp),%rax
mov    %rax,-0x90(%rbp)
mov    -0x8(%rbp),%rdi
call   1080 <strlen@plt>
mov    %eax,%ecx
mov    -0x90(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
call   1080 <strlen@plt>
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   10c0 <cmalloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%rdi
call   1080 <strlen@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x30(%rbp),%rax
movsbl (%rax),%ecx
xor    %eax,%eax
cmp    $0x2f,%ecx
mov    %al,-0x91(%rbp)
je     12d1 <look_up_command+0x111>
mov    -0x30(%rbp),%rax
cmp    -0x8(%rbp),%rax
setae  %al
mov    %al,-0x91(%rbp)
mov    -0x91(%rbp),%al
test   $0x1,%al
jne    12e4 <look_up_command+0x124>
jmp    12fe <look_up_command+0x13e>
mov    -0x30(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x30(%rbp)
mov    -0x3c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x3c(%rbp)
jmp    12a8 <look_up_command+0xe8>
mov    -0x30(%rbp),%rax
cmp    -0x8(%rbp),%rax
jae    1319 <look_up_command+0x159>
lea    -0x28(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    133d <look_up_command+0x17d>
mov    -0x38(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x3c(%rbp),%edx
call   1050 <strncpy@plt>
mov    -0x38(%rbp),%rax
movslq -0x3c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x38(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    13bd <look_up_command+0x1fd>
mov    -0x8(%rbp),%rsi
lea    0xcb5(%rip),%rdi
mov    $0x2,%edx
call   1060 <strncmp@plt>
cmp    $0x0,%rax
jne    1381 <look_up_command+0x1c1>
lea    -0x28(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rsi
lea    0xc81(%rip),%rdi
mov    $0x3,%edx
call   1060 <strncmp@plt>
cmp    $0x0,%rax
jne    13b8 <look_up_command+0x1f8>
lea    -0x28(%rbp),%rax
add    $0x3,%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
add    $0x3,%rax
mov    %rax,-0x8(%rbp)
jmp    13bd <look_up_command+0x1fd>
mov    -0x20(%rbp),%rdi
call   1080 <strlen@plt>
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   10c0 <cmalloc@plt>
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rax
mov    %rax,-0xa8(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0xa0(%rbp)
mov    -0x20(%rbp),%rdi
call   1080 <strlen@plt>
mov    -0xa8(%rbp),%rdi
mov    -0xa0(%rbp),%rsi
mov    %eax,%edx
add    $0x1,%edx
call   1050 <strncpy@plt>
mov    -0x48(%rbp),%rdi
lea    0xbf2(%rip),%rsi
call   10d0 <strtok@plt>
mov    %rax,-0x50(%rbp)
mov    -0x8(%rbp),%rdi
call   1080 <strlen@plt>
cltq
mov    %rax,-0x58(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x5c(%rbp)
mov    $0x100,%edi
call   10c0 <cmalloc@plt>
mov    %rax,-0x78(%rbp)
mov    $0x100,%edi
call   10c0 <cmalloc@plt>
mov    %rax,-0x80(%rbp)
mov    -0x80(%rbp),%rax
mov    %rax,-0x88(%rbp)
cmpl   $0x0,-0x5c(%rbp)
jle    167a <look_up_command+0x4ba>
cmpq   $0x0,-0x50(%rbp)
je     1659 <look_up_command+0x499>
mov    -0x50(%rbp),%rdi
call   1070 <opendir@plt>
mov    %rax,-0x70(%rbp)
cmpq   $0x0,-0x70(%rbp)
je     1637 <look_up_command+0x477>
jmp    1490 <look_up_command+0x2d0>
mov    -0x70(%rbp),%rdi
call   10b0 <readdir@plt>
mov    %rax,%rcx
mov    %rcx,-0x68(%rbp)
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0xa9(%rbp)
je     14bf <look_up_command+0x2ff>
cmpl   $0x0,-0x5c(%rbp)
setg   %al
mov    %al,-0xa9(%rbp)
mov    -0xa9(%rbp),%al
test   $0x1,%al
jne    14d2 <look_up_command+0x312>
jmp    1632 <look_up_command+0x472>
mov    -0x8(%rbp),%rdi
mov    -0x68(%rbp),%rax
mov    (%rax),%rsi
mov    -0x58(%rbp),%rdx
call   1060 <strncmp@plt>
cmp    $0x0,%rax
jne    14f9 <look_up_command+0x339>
mov    -0x5c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x5c(%rbp)
cmpl   $0x0,-0x5c(%rbp)
jne    162d <look_up_command+0x46d>
mov    -0x80(%rbp),%rax
mov    %rax,-0xc0(%rbp)
mov    -0x68(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0xb8(%rbp)
mov    -0x68(%rbp),%rax
mov    (%rax),%rdi
call   1080 <strlen@plt>
mov    -0xc0(%rbp),%rdi
mov    -0xb8(%rbp),%rsi
mov    %eax,%edx
add    $0x1,%edx
call   1050 <strncpy@plt>
cmpl   $0x0,-0x10(%rbp)
jne    1570 <look_up_command+0x3b0>
mov    -0x20(%rbp),%rax
lea    -0x28(%rbp),%rcx
cmp    %rcx,%rax
je     1570 <look_up_command+0x3b0>
mov    -0x20(%rbp),%rax
lea    -0x28(%rbp),%rcx
add    $0x3,%rcx
cmp    %rcx,%rax
jne    161f <look_up_command+0x45f>
mov    -0x78(%rbp),%rax
mov    %rax,-0xe0(%rbp)
mov    -0x50(%rbp),%rax
mov    %rax,-0xd8(%rbp)
mov    -0x50(%rbp),%rdi
call   1080 <strlen@plt>
mov    -0xe0(%rbp),%rdi
mov    -0xd8(%rbp),%rsi
mov    %eax,%edx
add    $0x1,%edx
call   1050 <strncpy@plt>
mov    -0x78(%rbp),%rax
mov    %rax,-0xd0(%rbp)
mov    -0x80(%rbp),%rax
mov    %rax,-0xc8(%rbp)
mov    -0x80(%rbp),%rdi
call   1080 <strlen@plt>
mov    -0xd0(%rbp),%rdi
mov    -0xc8(%rbp),%rsi
mov    %eax,%edx
add    $0x1,%edx
call   1090 <strncat@plt>
mov    -0x80(%rbp),%rdi
call   10e0 <cfree@plt>
mov    -0x78(%rbp),%rdi
call   1070 <opendir@plt>
cmp    $0x0,%rax
je     160f <look_up_command+0x44f>
mov    -0x78(%rbp),%rdi
lea    0xa0a(%rip),%rsi
mov    $0x1,%edx
call   1090 <strncat@plt>
mov    -0x78(%rbp),%rax
mov    %rax,-0x88(%rbp)
jmp    1628 <look_up_command+0x468>
mov    -0x78(%rbp),%rdi
call   10e0 <cfree@plt>
jmp    162d <look_up_command+0x46d>
jmp    1490 <look_up_command+0x2d0>
jmp    1637 <look_up_command+0x477>
xor    %eax,%eax
mov    %eax,%edi
lea    0x9c9(%rip),%rsi
call   10d0 <strtok@plt>
mov    %rax,-0x50(%rbp)
mov    -0x70(%rbp),%rdi
call   10a0 <closedir@plt>
jmp    1675 <look_up_command+0x4b5>
movl   $0x0,-0x5c(%rbp)
mov    -0x80(%rbp),%rdi
lea    0x9a5(%rip),%rsi
mov    $0x1,%edx
call   1050 <strncpy@plt>
jmp    145e <look_up_command+0x29e>
mov    -0x48(%rbp),%rdi
call   1040 <free@plt>
mov    -0x88(%rbp),%rax
add    $0xe0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<look_up_command>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %edx,%r12d
mov    %esi,%r14d
mov    %rdi,%r15
lea    0xe22(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rbp
movabs $0x3a2f2e2e3a2f2e,%rax
mov    %rax,0x10(%rsp)
lea    0xe0f(%rip),%rdi
mov    $0x1,%edx
mov    %r15,%rsi
call   1060 <strncmp@plt>
test   %r12d,%r12d
je     12c9 <look_up_command+0x109>
lea    0xdf2(%rip),%rdi
mov    $0x1,%edx
mov    %r15,%rsi
call   1060 <strncmp@plt>
test   %rax,%rax
je     1316 <look_up_command+0x156>
mov    %r15,%rdi
call   1080 <strlen@plt>
movslq %eax,%r13
lea    (%r15,%r13,1),%rbx
add    $0xffffffffffffffff,%rbx
mov    %r15,%rdi
call   1080 <strlen@plt>
lea    0x1(%rax),%edi
call   10c0 <cmalloc@plt>
mov    %rax,0x18(%rsp)
mov    %r15,%rdi
call   1080 <strlen@plt>
mov    %eax,%ebp
test   %r13d,%r13d
jle    131d <look_up_command+0x15d>
cmpb   $0x2f,-0x1(%r15,%r13,1)
je     131d <look_up_command+0x15d>
add    $0xffffffffffffffff,%r13
mov    %r15,%rsi
lea    (%r15,%r13,1),%rax
add    $0xffffffffffffffff,%rax
cs nopw 0x0(%rax,%rax,1)
nop
add    $0xffffffff,%ebp
lea    -0x1(%rax),%rbx
cmp    %rsi,%rax
jb     12a4 <look_up_command+0xe4>
cmpb   $0x2f,(%rax)
mov    %rbx,%rax
jne    1290 <look_up_command+0xd0>
add    $0x1,%rbx
cmp    %rsi,%rbx
jb     1325 <look_up_command+0x165>
mov    0x18(%rsp),%r15
mov    %r15,%rdi
mov    %ebp,%edx
call   1050 <strncpy@plt>
movslq %ebp,%rax
mov    %r15,%rbp
movb   $0x0,(%r15,%rax,1)
jmp    132a <look_up_command+0x16a>
lea    0xd36(%rip),%rdi
mov    $0x2,%edx
mov    %r15,%rsi
call   1060 <strncmp@plt>
xor    %ecx,%ecx
test   %rax,%rax
sete   %cl
lea    0x10(%rsp),%rax
cmove  %rax,%rbp
lea    (%r15,%rcx,2),%r15
lea    0xd0c(%rip),%rdi
mov    $0x3,%edx
mov    %r15,%rsi
call   1060 <strncmp@plt>
test   %rax,%rax
jne    1331 <look_up_command+0x171>
lea    0x13(%rsp),%rbp
add    $0x3,%r15
jmp    1331 <look_up_command+0x171>
lea    0x10(%rsp),%rbp
jmp    1331 <look_up_command+0x171>
mov    %r15,%rsi
cmp    %rsi,%rbx
jae    12ad <look_up_command+0xed>
lea    0x10(%rsp),%rbp
add    $0x1,%rbx
mov    %rbx,%r15
mov    %rbp,%rdi
call   1080 <strlen@plt>
mov    %eax,%edi
call   10c0 <cmalloc@plt>
mov    %rax,%rbx
mov    %rbp,%rdi
call   1080 <strlen@plt>
lea    0x1(%rax),%edx
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1050 <strncpy@plt>
lea    0xca9(%rip),%rsi
mov    %rbx,0x38(%rsp)
mov    %rbx,%rdi
call   10d0 <strtok@plt>
mov    %rax,%rbx
mov    %r15,%rdi
call   1080 <strlen@plt>
mov    %eax,%r13d
mov    $0x100,%edi
call   10c0 <cmalloc@plt>
mov    %rax,0x18(%rsp)
mov    $0x100,%edi
call   10c0 <cmalloc@plt>
test   %r14d,%r14d
jle    1532 <look_up_command+0x372>
movslq %r13d,%r13
test   %r12d,%r12d
setne  %dl
lea    0x10(%rsp),%rcx
cmp    %rcx,%rbp
sete   %cl
or     %dl,%cl
lea    0x13(%rsp),%rdx
cmp    %rdx,%rbp
sete   %dl
or     %cl,%dl
mov    %dl,0xf(%rsp)
mov    %rax,0x20(%rsp)
mov    %rax,%rdi
mov    %rax,0x30(%rsp)
jmp    1407 <look_up_command+0x247>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
xor    %edi,%edi
lea    0xc20(%rip),%rsi
call   10d0 <strtok@plt>
mov    %rax,%rbx
mov    %rbp,%rdi
call   10a0 <closedir@plt>
test   %r14d,%r14d
mov    0x30(%rsp),%rdi
jle    154a <look_up_command+0x38a>
test   %rbx,%rbx
je     1539 <look_up_command+0x379>
mov    %rbx,%rdi
call   1070 <opendir@plt>
mov    %rax,%rbp
test   %rax,%rax
je     13e0 <look_up_command+0x220>
mov    %rbp,%rdi
call   10b0 <readdir@plt>
test   %rax,%rax
je     13e0 <look_up_command+0x220>
mov    %rax,%r12
mov    %rbx,0x40(%rsp)
mov    %rbp,0x28(%rsp)
nopw   0x0(%rax,%rax,1)
mov    (%r12),%rsi
mov    %r15,%rdi
mov    %r13,%rdx
call   1060 <strncmp@plt>
xor    %ecx,%ecx
test   %rax,%rax
sete   %cl
sub    %ecx,%r14d
jne    1510 <look_up_command+0x350>
mov    (%r12),%rbp
mov    %rbp,%rdi
call   1080 <strlen@plt>
lea    0x1(%rax),%edx
mov    0x30(%rsp),%rbx
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1050 <strncpy@plt>
cmpb   $0x0,0xf(%rsp)
je     14fb <look_up_command+0x33b>
mov    0x40(%rsp),%r12
mov    %r12,%rdi
call   1080 <strlen@plt>
lea    0x1(%rax),%edx
mov    0x18(%rsp),%rbp
mov    %rbp,%rdi
mov    %r12,%rsi
call   1050 <strncpy@plt>
mov    %rbx,%rdi
call   1080 <strlen@plt>
lea    0x1(%rax),%edx
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1090 <strncat@plt>
mov    %rbx,%rdi
call   10e0 <cfree@plt>
mov    %rbp,%rdi
call   1070 <opendir@plt>
mov    %rbp,0x20(%rsp)
test   %rax,%rax
mov    0x28(%rsp),%rbp
je     1510 <look_up_command+0x350>
mov    0x18(%rsp),%rbx
mov    %rbx,%rdi
lea    0xb1d(%rip),%rsi
mov    $0x1,%edx
call   1090 <strncat@plt>
mov    %rbx,0x20(%rsp)
jmp    1510 <look_up_command+0x350>
mov    0x18(%rsp),%rdi
call   10e0 <cfree@plt>
mov    0x28(%rsp),%rbp
nopw   0x0(%rax,%rax,1)
mov    %rbp,%rdi
call   10b0 <readdir@plt>
test   %rax,%rax
je     13e0 <look_up_command+0x220>
mov    %rax,%r12
test   %r14d,%r14d
jg     1440 <look_up_command+0x280>
jmp    13e0 <look_up_command+0x220>
mov    %rax,0x20(%rsp)
jmp    154a <look_up_command+0x38a>
lea    0xac8(%rip),%rsi
mov    $0x1,%edx
call   1050 <strncpy@plt>
mov    0x38(%rsp),%rdi
call   1040 <free@plt>
mov    0x20(%rsp),%rax
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
@.str = [5 x i8] c"PATH\00"
@__const.look_up_command.def = [8 x i8] c"./:../:\00"
@.str.1 = [2 x i8] zeroinitializer
@.str.2 = [3 x i8] c"./\00"
@.str.3 = [4 x i8] c"../\00"
@.str.4 = [2 x i8] c":\00"
@.str.5 = [2 x i8] c"/\00"
@.str.6 = [1 x i8] zeroinitializer
define i8* @look_up_command(i8* %0, i32 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca [8 x i8]
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i32
%13 = alloca i8*
%14 = alloca i8*
%15 = alloca i64
%16 = alloca i32
%17 = alloca %struct.dirent*
%18 = alloca i32*
%19 = alloca i8*
%20 = alloca i8*
%21 = alloca i8*
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%22 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i8* %22, i8** %7
%23 = load i8*, i8** %7
store i8* %23, i8** %8
%24 = bitcast [8 x i8]* %9 to i8*
%25 = load i8*, i8** %4
%26 = call i64 @strncmp(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8* %25, i64 1)
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %3
%29 = load i8*, i8** %7
store i8* %29, i8** %8
br label %30
30: ; preds = %28, %3
%31 = load i32, i32* %6
%32 = icmp ne i32 %31, 0
br i1 %32, label %33, label %91
33: ; preds = %30
%34 = load i8*, i8** %4
%35 = call i64 @strncmp(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8* %34, i64 1)
%36 = icmp eq i64 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %33
%38 = getelementptr inbounds [8 x i8], [8 x i8]* %9, i64 0, i64 0
store i8* %38, i8** %8
br label %90
39: ; preds = %33
%40 = load i8*, i8** %4
%41 = load i8*, i8** %4
%42 = call i32 @strlen(i8* %41)
%43 = sub nsw i32 %42, 1
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i8, i8* %40, i64 %44
store i8* %45, i8** %10
%46 = load i8*, i8** %4
%47 = call i32 @strlen(i8* %46)
%48 = add nsw i32 %47, 1
%49 = sext i32 %48 to i64
%50 = mul i64 %49, 1
%51 = trunc i64 %50 to i32
%52 = call i8* @cmalloc(i32 %51)
store i8* %52, i8** %11
%53 = load i8*, i8** %4
%54 = call i32 @strlen(i8* %53)
store i32 %54, i32* %12
br label %55
55: ; preds = %66, %39
%56 = load i8*, i8** %10
%57 = load i8, i8* %56
%58 = sext i8 %57 to i32
%59 = icmp ne i32 %58, 47
br i1 %59, label %60, label %64
60: ; preds = %55
%61 = load i8*, i8** %10
%62 = load i8*, i8** %4
%63 = icmp uge i8* %61, %62
br label %64
64: ; preds = %60, %55
%65 = phi i1 [ false, %55 ], [ %63, %60 ]
br i1 %65, label %66, label %71
66: ; preds = %64
%67 = load i8*, i8** %10
%68 = getelementptr inbounds i8, i8* %67, i32 -1
store i8* %68, i8** %10
%69 = load i32, i32* %12
%70 = add nsw i32 %69, -1
store i32 %70, i32* %12
br label %55
71: ; preds = %64
%72 = load i8*, i8** %10
%73 = load i8*, i8** %4
%74 = icmp ult i8* %72, %73
br i1 %74, label %75, label %77
75: ; preds = %71
%76 = getelementptr inbounds [8 x i8], [8 x i8]* %9, i64 0, i64 0
store i8* %76, i8** %8
br label %87
77: ; preds = %71
%78 = load i8*, i8** %11
%79 = load i8*, i8** %4
%80 = load i32, i32* %12
%81 = call i32 @strncpy(i8* %78, i8* %79, i32 %80)
%82 = load i8*, i8** %11
%83 = load i32, i32* %12
%84 = sext i32 %83 to i64
%85 = getelementptr inbounds i8, i8* %82, i64 %84
store i8 0, i8* %85
%86 = load i8*, i8** %11
store i8* %86, i8** %8
br label %87
87: ; preds = %77, %75
%88 = load i8*, i8** %10
%89 = getelementptr inbounds i8, i8* %88, i64 1
store i8* %89, i8** %4
br label %90
90: ; preds = %87, %37
br label %109
91: ; preds = %30
%92 = load i8*, i8** %4
%93 = call i64 @strncmp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %92, i64 2)
%94 = icmp eq i64 %93, 0
br i1 %94, label %95, label %99
95: ; preds = %91
%96 = getelementptr inbounds [8 x i8], [8 x i8]* %9, i64 0, i64 0
store i8* %96, i8** %8
%97 = load i8*, i8** %4
%98 = getelementptr inbounds i8, i8* %97, i64 2
store i8* %98, i8** %4
br label %99
99: ; preds = %95, %91
%100 = load i8*, i8** %4
%101 = call i64 @strncmp(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %100, i64 3)
%102 = icmp eq i64 %101, 0
br i1 %102, label %103, label %108
103: ; preds = %99
%104 = getelementptr inbounds [8 x i8], [8 x i8]* %9, i64 0, i64 0
%105 = getelementptr inbounds i8, i8* %104, i64 3
store i8* %105, i8** %8
%106 = load i8*, i8** %4
%107 = getelementptr inbounds i8, i8* %106, i64 3
store i8* %107, i8** %4
br label %108
108: ; preds = %103, %99
br label %109
109: ; preds = %108, %90
%110 = load i8*, i8** %8
%111 = call i32 @strlen(i8* %110)
%112 = sext i32 %111 to i64
%113 = mul i64 1, %112
%114 = trunc i64 %113 to i32
%115 = call i8* @cmalloc(i32 %114)
store i8* %115, i8** %13
%116 = load i8*, i8** %13
%117 = load i8*, i8** %8
%118 = load i8*, i8** %8
%119 = call i32 @strlen(i8* %118)
%120 = add nsw i32 %119, 1
%121 = call i32 @strncpy(i8* %116, i8* %117, i32 %120)
%122 = load i8*, i8** %13
%123 = call i8* @strtok(i8* %122, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i8* %123, i8** %14
%124 = load i8*, i8** %4
%125 = call i32 @strlen(i8* %124)
%126 = sext i32 %125 to i64
store i64 %126, i64* %15
%127 = load i32, i32* %5
store i32 %127, i32* %16
%128 = call i8* @cmalloc(i32 256)
store i8* %128, i8** %19
%129 = call i8* @cmalloc(i32 256)
store i8* %129, i8** %20
%130 = load i8*, i8** %20
store i8* %130, i8** %21
br label %131
131: ; preds = %224, %109
%132 = load i32, i32* %16
%133 = icmp sgt i32 %132, 0
br i1 %133, label %134, label %225
134: ; preds = %131
%135 = load i8*, i8** %14
%136 = icmp ne i8* %135, null
br i1 %136, label %137, label %221
137: ; preds = %134
%138 = load i8*, i8** %14
%139 = call i32* @opendir(i8* %138)
store i32* %139, i32** %18
%140 = load i32*, i32** %18
%141 = icmp ne i32* %140, null
br i1 %141, label %142, label %217
142: ; preds = %137
br label %143
143: ; preds = %215, %142
%144 = load i32*, i32** %18
%145 = call %struct.dirent* @readdir(i32* %144)
store %struct.dirent* %145, %struct.dirent** %17
%146 = icmp ne %struct.dirent* %145, null
br i1 %146, label %147, label %150
147: ; preds = %143
%148 = load i32, i32* %16
%149 = icmp sgt i32 %148, 0
br label %150
150: ; preds = %147, %143
%151 = phi i1 [ false, %143 ], [ %149, %147 ]
br i1 %151, label %152, label %216
152: ; preds = %150
%153 = load i8*, i8** %4
%154 = load %struct.dirent*, %struct.dirent** %17
%155 = getelementptr inbounds %struct.dirent, %struct.dirent* %154, i32 0, i32 0
%156 = load i8*, i8** %155
%157 = load i64, i64* %15
%158 = call i64 @strncmp(i8* %153, i8* %156, i64 %157)
%159 = icmp eq i64 %158, 0
br i1 %159, label %160, label %163
160: ; preds = %152
%161 = load i32, i32* %16
%162 = add nsw i32 %161, -1
store i32 %162, i32* %16
br label %163
163: ; preds = %160, %152
%164 = load i32, i32* %16
%165 = icmp eq i32 %164, 0
br i1 %165, label %166, label %215
166: ; preds = %163
%167 = load i8*, i8** %20
%168 = load %struct.dirent*, %struct.dirent** %17
%169 = getelementptr inbounds %struct.dirent, %struct.dirent* %168, i32 0, i32 0
%170 = load i8*, i8** %169
%171 = load %struct.dirent*, %struct.dirent** %17
%172 = getelementptr inbounds %struct.dirent, %struct.dirent* %171, i32 0, i32 0
%173 = load i8*, i8** %172
%174 = call i32 @strlen(i8* %173)
%175 = add nsw i32 %174, 1
%176 = call i32 @strncpy(i8* %167, i8* %170, i32 %175)
%177 = load i32, i32* %6
%178 = icmp ne i32 %177, 0
br i1 %178, label %188, label %179
179: ; preds = %166
%180 = load i8*, i8** %8
%181 = getelementptr inbounds [8 x i8], [8 x i8]* %9, i64 0, i64 0
%182 = icmp eq i8* %180, %181
br i1 %182, label %188, label %183
183: ; preds = %179
%184 = load i8*, i8** %8
%185 = getelementptr inbounds [8 x i8], [8 x i8]* %9, i64 0, i64 0
%186 = getelementptr inbounds i8, i8* %185, i64 3
%187 = icmp eq i8* %184, %186
br i1 %187, label %188, label %211
188: ; preds = %183, %179, %166
%189 = load i8*, i8** %19
%190 = load i8*, i8** %14
%191 = load i8*, i8** %14
%192 = call i32 @strlen(i8* %191)
%193 = add nsw i32 %192, 1
%194 = call i32 @strncpy(i8* %189, i8* %190, i32 %193)
%195 = load i8*, i8** %19
%196 = load i8*, i8** %20
%197 = load i8*, i8** %20
%198 = call i32 @strlen(i8* %197)
%199 = add nsw i32 %198, 1
%200 = call i32 @strncat(i8* %195, i8* %196, i32 %199)
%201 = load i8*, i8** %20
%202 = call i32 @cfree(i8* %201)
%203 = load i8*, i8** %19
%204 = call i32* @opendir(i8* %203)
%205 = icmp ne i32* %204, null
br i1 %205, label %206, label %209
206: ; preds = %188
%207 = load i8*, i8** %19
%208 = call i32 @strncat(i8* %207, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0), i32 1)
br label %209
209: ; preds = %206, %188
%210 = load i8*, i8** %19
store i8* %210, i8** %21
br label %214
211: ; preds = %183
%212 = load i8*, i8** %19
%213 = call i32 @cfree(i8* %212)
br label %214
214: ; preds = %211, %209
br label %215
215: ; preds = %214, %163
br label %143
216: ; preds = %150
br label %217
217: ; preds = %216, %137
%218 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i8* %218, i8** %14
%219 = load i32*, i32** %18
%220 = call i32 @closedir(i32* %219)
br label %224
221: ; preds = %134
store i32 0, i32* %16
%222 = load i8*, i8** %20
%223 = call i32 @strncpy(i8* %222, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.6, i64 0, i64 0), i32 1)
br label %224
224: ; preds = %221, %217
br label %131
225: ; preds = %131
%226 = load i8*, i8** %13
%227 = call i32 @free(i8* %226)
%228 = load i8*, i8** %21
ret i8* %228
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [5 x i8] c"PATH\00"
@.str.1 = [2 x i8] zeroinitializer
@.str.2 = [3 x i8] c"./\00"
@.str.3 = [4 x i8] c"../\00"
@.str.4 = [2 x i8] c":\00"
@.str.5 = [2 x i8] c"/\00"
@.str.6 = [1 x i8] zeroinitializer
define i8* @look_up_command(i8* %0, i32 %1, i32 %2) {
%4 = alloca i64
%5 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%6 = bitcast i64* %4 to i8*
store i64 16377424039784238, i64* %4
%7 = tail call i64 @strncmp(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8* %0, i64 1)
%8 = icmp ne i32 %2, 0
br i1 %8, label %9, label %45
9: ; preds = %3
%10 = tail call i64 @strncmp(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8* %0, i64 1)
%11 = icmp eq i64 %10, 0
br i1 %11, label %56, label %12
12: ; preds = %9
%13 = tail call i32 @strlen(i8* %0)
%14 = add nsw i32 %13, -1
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds i8, i8* %0, i64 %15
%17 = tail call i32 @strlen(i8* %0)
%18 = add nsw i32 %17, 1
%19 = tail call i8* @cmalloc(i32 %18)
%20 = tail call i32 @strlen(i8* %0)
%21 = load i8, i8* %16
%22 = icmp ne i8 %21, 47
%23 = icmp sgt i32 %13, 0
%24 = and i1 %23, %22
br i1 %24, label %25, label %34
25: ; preds = %12, %25
%26 = phi i8* [ %28, %25 ], [ %16, %12 ]
%27 = phi i32 [ %29, %25 ], [ %20, %12 ]
%28 = getelementptr inbounds i8, i8* %26, i64 -1
%29 = add nsw i32 %27, -1
%30 = load i8, i8* %28
%31 = icmp ne i8 %30, 47
%32 = icmp uge i8* %28, %0
%33 = and i1 %32, %31
br i1 %33, label %25, label %34
34: ; preds = %25, %12
%35 = phi i32 [ %20, %12 ], [ %29, %25 ]
%36 = phi i8* [ %16, %12 ], [ %28, %25 ]
%37 = icmp ult i8* %36, %0
br i1 %37, label %42, label %38
38: ; preds = %34
%39 = tail call i32 @strncpy(i8* %19, i8* %0, i32 %35)
%40 = sext i32 %35 to i64
%41 = getelementptr inbounds i8, i8* %19, i64 %40
store i8 0, i8* %41
br label %42
42: ; preds = %34, %38
%43 = phi i8* [ %19, %38 ], [ %6, %34 ]
%44 = getelementptr inbounds i8, i8* %36, i64 1
br label %56
45: ; preds = %3
%46 = tail call i64 @strncmp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %0, i64 2)
%47 = icmp eq i64 %46, 0
%48 = select i1 %47, i8* %6, i8* %5
%49 = select i1 %47, i64 2, i64 0
%50 = getelementptr i8, i8* %0, i64 %49
%51 = tail call i64 @strncmp(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %50, i64 3)
%52 = icmp eq i64 %51, 0
br i1 %52, label %53, label %56
53: ; preds = %45
%54 = getelementptr inbounds i8, i8* %6, i64 3
%55 = getelementptr inbounds i8, i8* %50, i64 3
br label %56
56: ; preds = %9, %45, %53, %42
%57 = phi i8* [ %43, %42 ], [ %54, %53 ], [ %48, %45 ], [ %6, %9 ]
%58 = phi i8* [ %44, %42 ], [ %55, %53 ], [ %50, %45 ], [ %0, %9 ]
%59 = call i32 @strlen(i8* %57)
%60 = call i8* @cmalloc(i32 %59)
%61 = call i32 @strlen(i8* %57)
%62 = add nsw i32 %61, 1
%63 = call i32 @strncpy(i8* %60, i8* %57, i32 %62)
%64 = call i8* @strtok(i8* %60, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%65 = call i32 @strlen(i8* %58)
%66 = sext i32 %65 to i64
%67 = call i8* @cmalloc(i32 256)
%68 = call i8* @cmalloc(i32 256)
%69 = icmp sgt i32 %1, 0
br i1 %69, label %70, label %131
70: ; preds = %56
%71 = icmp eq i8* %57, %6
%72 = or i1 %8, %71
%73 = getelementptr inbounds i8, i8* %6, i64 3
%74 = icmp eq i8* %57, %73
%75 = select i1 %72, i1 true, i1 %74
br label %76
76: ; preds = %70, %125
%77 = phi i8* [ %68, %70 ], [ %127, %125 ]
%78 = phi i32 [ %1, %70 ], [ %126, %125 ]
%79 = phi i8* [ %64, %70 ], [ %128, %125 ]
%80 = icmp eq i8* %79, null
br i1 %80, label %123, label %81
81: ; preds = %76
%82 = call i32* @opendir(i8* %79)
%83 = icmp eq i32* %82, null
br i1 %83, label %125, label %84
84: ; preds = %81
%85 = call %struct.dirent* @readdir(i32* %82)
%86 = icmp eq %struct.dirent* %85, null
br i1 %86, label %125, label %87
87: ; preds = %84, %117
%88 = phi %struct.dirent* [ %119, %117 ], [ %85, %84 ]
%89 = phi i8* [ %118, %117 ], [ %77, %84 ]
%90 = phi i32 [ %96, %117 ], [ %78, %84 ]
%91 = getelementptr inbounds %struct.dirent, %struct.dirent* %88, i64 0, i32 0
%92 = load i8*, i8** %91
%93 = call i64 @strncmp(i8* %58, i8* %92, i64 %66)
%94 = icmp eq i64 %93, 0
%95 = sext i1 %94 to i32
%96 = add nsw i32 %90, %95
%97 = icmp eq i32 %96, 0
br i1 %97, label %98, label %117
98: ; preds = %87
%99 = load i8*, i8** %91
%100 = call i32 @strlen(i8* %99)
%101 = add nsw i32 %100, 1
%102 = call i32 @strncpy(i8* %68, i8* %99, i32 %101)
br i1 %75, label %103, label %115
103: ; preds = %98
%104 = call i32 @strlen(i8* %79)
%105 = add nsw i32 %104, 1
%106 = call i32 @strncpy(i8* %67, i8* %79, i32 %105)
%107 = call i32 @strlen(i8* %68)
%108 = add nsw i32 %107, 1
%109 = call i32 @strncat(i8* %67, i8* %68, i32 %108)
%110 = call i32 @cfree(i8* %68)
%111 = call i32* @opendir(i8* %67)
%112 = icmp eq i32* %111, null
br i1 %112, label %117, label %113
113: ; preds = %103
%114 = call i32 @strncat(i8* %67, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0), i32 1)
br label %117
115: ; preds = %98
%116 = call i32 @cfree(i8* %67)
br label %117
117: ; preds = %103, %113, %115, %87
%118 = phi i8* [ %89, %115 ], [ %89, %87 ], [ %67, %113 ], [ %67, %103 ]
%119 = call %struct.dirent* @readdir(i32* %82)
%120 = icmp ne %struct.dirent* %119, null
%121 = icmp sgt i32 %96, 0
%122 = select i1 %120, i1 %121, i1 false
br i1 %122, label %87, label %125
123: ; preds = %76
%124 = call i32 @strncpy(i8* %68, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.6, i64 0, i64 0), i32 1)
br label %131
125: ; preds = %117, %81, %84
%126 = phi i32 [ %78, %81 ], [ %78, %84 ], [ %96, %117 ]
%127 = phi i8* [ %77, %81 ], [ %77, %84 ], [ %118, %117 ]
%128 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%129 = call i32 @closedir(i32* %82)
%130 = icmp sgt i32 %126, 0
br i1 %130, label %76, label %131
131: ; preds = %125, %123, %56
%132 = phi i8* [ %68, %56 ], [ %77, %123 ], [ %127, %125 ]
%133 = call i32 @free(i8* %60)
ret i8* %132
}