#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 */
typedef int /*<<< orphan*/ buffer;

/* Variables and functions */
int MAXPATHLEN;
int /*<<< orphan*/ X_OK;
scalar_t__ access(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ free(char *);
char *getenv(char *);
int snprintf(char *, int, char *, char *, char const *);
char *strchr(char const *, char);
char *strdup(char const *);
char *findcc(const char *progname) {
  char *path, *dir, *next;
  char buffer[MAXPATHLEN];
  if ((next = strchr(progname, ' ')) != NULL) {
    *next = '\0';
  }
  if (strchr(progname, '/') != NULL)
    return access(progname, X_OK) ? NULL : strdup(progname);
  if (((path = getenv("PATH")) == NULL) || ((path = strdup(path)) == NULL))
    return NULL;
  dir = path;
  while (dir != NULL) {
    if ((next = strchr(dir, ':')) != NULL)
      *next++ = '\0';
    if (snprintf(buffer, sizeof(buffer), "%s/%s", dir, progname) <
        (int)sizeof(buffer)) {
      if (!access(buffer, X_OK)) {
        free(path);
        return strdup(buffer);
      }
    }
    dir = next;
  }
  free(path);
  return NULL;
}


x86 assembly (clang, -O0):
<findcc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e75(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x50(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x20,%esi
call   1050 <strchr@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     11bf <findcc+0x5f>
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
call   1050 <strchr@plt>
cmp    $0x0,%rax
je     121f <findcc+0xbf>
mov    -0x10(%rbp),%rdi
mov    0x2dfe(%rip),%rax
mov    (%rax),%esi
call   1070 <access@plt>
cmp    $0x0,%rax
je     11fe <findcc+0x9e>
xor    %eax,%eax
mov    %rax,-0x58(%rbp)
jmp    120b <findcc+0xab>
mov    -0x10(%rbp),%rdi
call   1080 <strdup@plt>
mov    %rax,-0x58(%rbp)
mov    -0x58(%rbp),%rax
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    1337 <findcc+0x1d7>
lea    0xdda(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     1250 <findcc+0xf0>
mov    -0x18(%rbp),%rdi
call   1080 <strdup@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1264 <findcc+0x104>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    1337 <findcc+0x1d7>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     131f <findcc+0x1bf>
mov    -0x20(%rbp),%rdi
mov    $0x3a,%esi
call   1050 <strchr@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     12a5 <findcc+0x145>
mov    -0x28(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x28(%rbp)
movb   $0x0,(%rax)
mov    -0x50(%rbp),%rax
mov    -0x48(%rbp),%rdi
mov    %eax,%esi
mov    -0x20(%rbp),%rcx
mov    -0x10(%rbp),%r8
lea    0xd47(%rip),%rdx
call   1060 <snprintf@plt>
mov    -0x50(%rbp),%rcx
cmp    %ecx,%eax
jge    1312 <findcc+0x1b2>
mov    -0x48(%rbp),%rdi
mov    0x2d06(%rip),%rax
mov    (%rax),%esi
call   1070 <access@plt>
cmp    $0x0,%rax
jne    130d <findcc+0x1ad>
mov    -0x18(%rbp),%rdi
call   1040 <free@plt>
mov    -0x48(%rbp),%rdi
call   1080 <strdup@plt>
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    1337 <findcc+0x1d7>
jmp    1312 <findcc+0x1b2>
mov    -0x28(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    126c <findcc+0x10c>
mov    -0x18(%rbp),%rdi
call   1040 <free@plt>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x3c(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<findcc>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
mov    0x2e70(%rip),%rax
mov    (%rax),%r15d
mov    %rsp,%rbx
lea    0xf(%r15),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    $0x20,%esi
call   1050 <strchr@plt>
test   %rax,%rax
je     119e <findcc+0x3e>
movb   $0x0,(%rax)
mov    %r14,%rdi
mov    $0x2f,%esi
call   1050 <strchr@plt>
test   %rax,%rax
je     11e0 <findcc+0x80>
mov    0x2e29(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   1070 <access@plt>
test   %rax,%rax
jne    1292 <findcc+0x132>
mov    %r14,%rdi
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1080 <strdup@plt>
lea    0xe19(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     1292 <findcc+0x132>
mov    %rax,%rdi
call   1080 <strdup@plt>
test   %rax,%rax
je     1292 <findcc+0x132>
mov    %rax,-0x30(%rbp)
mov    %rax,%r12
jmp    1218 <findcc+0xb8>
nop
mov    %r13,%r12
test   %r13,%r13
je     1289 <findcc+0x129>
mov    %r12,%rdi
mov    $0x3a,%esi
call   1050 <strchr@plt>
test   %rax,%rax
je     1240 <findcc+0xe0>
mov    %rax,%r13
add    $0x1,%r13
movb   $0x0,(%rax)
jmp    1243 <findcc+0xe3>
cs nopw 0x0(%rax,%rax,1)
xor    %r13d,%r13d
mov    %rbx,%rdi
mov    %r15d,%esi
lea    0xdb5(%rip),%rdx
mov    %r12,%rcx
mov    %r14,%r8
call   1060 <snprintf@plt>
cmp    %r15d,%eax
jge    1210 <findcc+0xb0>
mov    0x2d79(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1070 <access@plt>
test   %rax,%rax
jne    1210 <findcc+0xb0>
mov    -0x30(%rbp),%rdi
call   1040 <free@plt>
mov    %rbx,%rdi
call   1080 <strdup@plt>
jmp    1294 <findcc+0x134>
mov    -0x30(%rbp),%rdi
call   1040 <free@plt>
xor    %eax,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@MAXPATHLEN = global i32 0
@X_OK = global i32 0
@.str = [5 x i8] c"PATH\00"
@.str.1 = [6 x i8] c"%s/%s\00"
define i8* @findcc(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
store i8* %0, i8** %3
%10 = load i32, i32* @MAXPATHLEN
%11 = zext i32 %10 to i64
%12 = call i8* @llvm.stacksave()
store i8* %12, i8** %7
%13 = alloca i8, i64 %11
store i64 %11, i64* %8
%14 = load i8*, i8** %3
%15 = call i8* @strchr(i8* %14, i8 signext 32)
store i8* %15, i8** %6
%16 = icmp ne i8* %15, null
br i1 %16, label %17, label %19
17: ; preds = %1
%18 = load i8*, i8** %6
store i8 0, i8* %18
br label %19
19: ; preds = %17, %1
%20 = load i8*, i8** %3
%21 = call i8* @strchr(i8* %20, i8 signext 47)
%22 = icmp ne i8* %21, null
br i1 %22, label %23, label %34
23: ; preds = %19
%24 = load i8*, i8** %3
%25 = load i32, i32* @X_OK
%26 = call i64 @access(i8* %24, i32 %25)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %23
br label %32
29: ; preds = %23
%30 = load i8*, i8** %3
%31 = call i8* @strdup(i8* %30)
br label %32
32: ; preds = %29, %28
%33 = phi i8* [ null, %28 ], [ %31, %29 ]
store i8* %33, i8** %2
store i32 1, i32* %9
br label %75
34: ; preds = %19
%35 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i8* %35, i8** %4
%36 = icmp eq i8* %35, null
br i1 %36, label %41, label %37
37: ; preds = %34
%38 = load i8*, i8** %4
%39 = call i8* @strdup(i8* %38)
store i8* %39, i8** %4
%40 = icmp eq i8* %39, null
br i1 %40, label %41, label %42
41: ; preds = %37, %34
store i8* null, i8** %2
store i32 1, i32* %9
br label %75
42: ; preds = %37
%43 = load i8*, i8** %4
store i8* %43, i8** %5
br label %44
44: ; preds = %70, %42
%45 = load i8*, i8** %5
%46 = icmp ne i8* %45, null
br i1 %46, label %47, label %72
47: ; preds = %44
%48 = load i8*, i8** %5
%49 = call i8* @strchr(i8* %48, i8 signext 58)
store i8* %49, i8** %6
%50 = icmp ne i8* %49, null
br i1 %50, label %51, label %54
51: ; preds = %47
%52 = load i8*, i8** %6
%53 = getelementptr inbounds i8, i8* %52, i32 1
store i8* %53, i8** %6
store i8 0, i8* %52
br label %54
54: ; preds = %51, %47
%55 = trunc i64 %11 to i32
%56 = load i8*, i8** %5
%57 = load i8*, i8** %3
%58 = call i32 @snprintf(i8* %13, i32 %55, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %56, i8* %57)
%59 = trunc i64 %11 to i32
%60 = icmp slt i32 %58, %59
br i1 %60, label %61, label %70
61: ; preds = %54
%62 = load i32, i32* @X_OK
%63 = call i64 @access(i8* %13, i32 %62)
%64 = icmp ne i64 %63, 0
br i1 %64, label %69, label %65
65: ; preds = %61
%66 = load i8*, i8** %4
%67 = call i32 @free(i8* %66)
%68 = call i8* @strdup(i8* %13)
store i8* %68, i8** %2
store i32 1, i32* %9
br label %75
69: ; preds = %61
br label %70
70: ; preds = %69, %54
%71 = load i8*, i8** %6
store i8* %71, i8** %5
br label %44
72: ; preds = %44
%73 = load i8*, i8** %4
%74 = call i32 @free(i8* %73)
store i8* null, i8** %2
store i32 1, i32* %9
br label %75
75: ; preds = %72, %65, %41, %32
%76 = load i8*, i8** %7
%77 = load i8*, i8** %2
ret i8* %77
}

LLVM IR (-O2):
@MAXPATHLEN = global i32 0
@X_OK = global i32 0
@.str = [5 x i8] c"PATH\00"
@.str.1 = [6 x i8] c"%s/%s\00"
define noalias i8* @findcc(i8* %0) {
%2 = load i32, i32* @MAXPATHLEN
%3 = zext i32 %2 to i64
%4 = alloca i8, i64 %3
%5 = tail call i8* @strchr(i8* dereferenceable(1) %0, i8 signext 32)
%6 = icmp eq i8* %5, null
br i1 %6, label %8, label %7
7: ; preds = %1
store i8 0, i8* %5
br label %8
8: ; preds = %7, %1
%9 = tail call i8* @strchr(i8* dereferenceable(1) %0, i8 signext 47)
%10 = icmp eq i8* %9, null
br i1 %10, label %17, label %11
11: ; preds = %8
%12 = load i32, i32* @X_OK
%13 = tail call i64 @access(i8* %0, i32 %12)
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %44
15: ; preds = %11
%16 = tail call i8* @strdup(i8* %0)
br label %44
17: ; preds = %8
%18 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%19 = icmp eq i8* %18, null
br i1 %19, label %44, label %20
20: ; preds = %17
%21 = tail call i8* @strdup(i8* %18)
%22 = icmp eq i8* %21, null
br i1 %22, label %44, label %23
23: ; preds = %20, %40
%24 = phi i8* [ %30, %40 ], [ %21, %20 ]
%25 = call i8* @strchr(i8* %24, i8 signext 58)
%26 = icmp eq i8* %25, null
br i1 %26, label %29, label %27
27: ; preds = %23
%28 = getelementptr inbounds i8, i8* %25, i64 1
store i8 0, i8* %25
br label %29
29: ; preds = %27, %23
%30 = phi i8* [ %28, %27 ], [ null, %23 ]
%31 = call i32 @snprintf(i8* %4, i32 %2, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %24, i8* %0)
%32 = icmp slt i32 %31, %2
br i1 %32, label %33, label %40
33: ; preds = %29
%34 = load i32, i32* @X_OK
%35 = call i64 @access(i8* %4, i32 %34)
%36 = icmp eq i64 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %33
%38 = call i32 @free(i8* %21)
%39 = call i8* @strdup(i8* %4)
br label %44
40: ; preds = %33, %29
%41 = icmp eq i8* %30, null
br i1 %41, label %42, label %23
42: ; preds = %40
%43 = call i32 @free(i8* %21)
br label %44
44: ; preds = %17, %20, %15, %11, %42, %37
%45 = phi i8* [ %39, %37 ], [ null, %42 ], [ %16, %15 ], [ null, %11 ], [ null, %20 ], [ null, %17 ]
ret i8* %45
}