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

/* Variables and functions */
int F_OK;
int X_OK;
int access(char *, int);
char *getenv(char *);
int /*<<< orphan*/ printf(char *);
int snprintf(char *, int, char *, char *, char const *);
scalar_t__ strcmp(char *, char *);
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *strtok(char *, char *);
const char *find_gcc(const char *gcc_exe) {
  static char fname[256];
  char spath[1024 * 10];
  char *p;
  p = getenv("PATH");
  if (strlen(p) >= sizeof(spath)) {
    printf("Error: find_gcc() not enough buffer 1\n");
    return NULL;
  }
  strncpy(spath, p, sizeof(spath));
  p = strtok(spath, ";");
  while (p) {
    int len;
    if (strcmp(p, ".") == 0) {
      p = strtok(NULL, ";");
      continue;
    }
    len = snprintf(fname, sizeof(fname), "%s\\%s", p, gcc_exe);
    if (len < 0 || len >= sizeof(fname)) {
      printf("Error: find_gcc() not enough buffer 2\n");
      return NULL;
    }
    if (access(fname, F_OK | X_OK) != -1) {
      return fname;
    }
    p = strtok(NULL, ";");
  }
  return NULL;
}


x86 assembly (clang, -O0):
<find_gcc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2820,%rsp
mov    %rdi,-0x10(%rbp)
lea    0xe6a(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x2818(%rbp)
mov    -0x2818(%rbp),%rdi
call   1050 <strlen@plt>
cltq
cmp    $0x2800,%rax
jb     11d5 <find_gcc+0x55>
lea    0xe42(%rip),%rdi
call   1060 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    130b <find_gcc+0x18b>
lea    -0x2810(%rbp),%rdi
mov    -0x2818(%rbp),%rsi
mov    $0x2800,%edx
call   1040 <strncpy@plt>
lea    -0x2810(%rbp),%rdi
lea    0xe31(%rip),%rsi
call   10a0 <strtok@plt>
mov    %rax,-0x2818(%rbp)
cmpq   $0x0,-0x2818(%rbp)
je     1303 <find_gcc+0x183>
mov    -0x2818(%rbp),%rdi
lea    0xe0b(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
jne    124e <find_gcc+0xce>
xor    %eax,%eax
mov    %eax,%edi
lea    0xdef(%rip),%rsi
call   10a0 <strtok@plt>
mov    %rax,-0x2818(%rbp)
jmp    1207 <find_gcc+0x87>
mov    -0x2818(%rbp),%rcx
mov    -0x10(%rbp),%r8
lea    0x2e10(%rip),%rdi
mov    $0x100,%esi
lea    0xdc4(%rip),%rdx
call   1070 <snprintf@plt>
mov    %eax,-0x281c(%rbp)
cmpl   $0x0,-0x281c(%rbp)
jl     1297 <find_gcc+0x117>
movslq -0x281c(%rbp),%rax
cmp    $0x100,%rax
jb     12b0 <find_gcc+0x130>
lea    0xd98(%rip),%rdi
call   1060 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    130b <find_gcc+0x18b>
mov    0x2d19(%rip),%rax
mov    (%rax),%esi
mov    0x2d28(%rip),%rax
or     (%rax),%esi
lea    0x2da7(%rip),%rdi
call   1090 <access@plt>
cmp    $0xffffffff,%eax
je     12e7 <find_gcc+0x167>
lea    0x2d92(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    130b <find_gcc+0x18b>
xor    %eax,%eax
mov    %eax,%edi
lea    0xd3a(%rip),%rsi
call   10a0 <strtok@plt>
mov    %rax,-0x2818(%rbp)
jmp    1207 <find_gcc+0x87>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x2820,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_gcc>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x2808,%rsp
mov    %rdi,%r13
lea    0xe65(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1060 <strlen@plt>
cmp    $0x2800,%eax
jb     11c3 <find_gcc+0x43>
lea    0xe7c(%rip),%rdi
call   1050 <puts@plt>
jmp    1277 <find_gcc+0xf7>
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    $0x2800,%edx
call   1040 <strncpy@plt>
lea    0xe28(%rip),%rsi
mov    %rbp,%rdi
call   10a0 <strtok@plt>
test   %rax,%rax
je     1277 <find_gcc+0xf7>
mov    %rax,%rbp
lea    0xe0f(%rip),%r15
lea    0xe06(%rip),%r12
xor    %r14d,%r14d
lea    0x2e67(%rip),%rbx
jmp    1222 <find_gcc+0xa2>
nopl   0x0(%rax,%rax,1)
xor    %edi,%edi
mov    %r12,%rsi
call   10a0 <strtok@plt>
mov    %rax,%rbp
test   %rax,%rax
je     127a <find_gcc+0xfa>
mov    %rbp,%rdi
mov    %r15,%rsi
call   1080 <strcmp@plt>
test   %rax,%rax
je     1210 <find_gcc+0x90>
mov    %rbx,%rdi
mov    $0x100,%esi
lea    0xdc8(%rip),%rdx
mov    %rbp,%rcx
mov    %r13,%r8
call   1070 <snprintf@plt>
cmp    $0x100,%eax
jae    128f <find_gcc+0x10f>
mov    0x2d8e(%rip),%rax
mov    (%rax),%esi
mov    0x2d6d(%rip),%rax
or     (%rax),%esi
mov    %rbx,%rdi
call   1090 <access@plt>
cmp    $0xffffffff,%eax
je     1210 <find_gcc+0x90>
mov    %rbx,%r14
jmp    127a <find_gcc+0xfa>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x2808,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd79(%rip),%rdi
jmp    11b9 <find_gcc+0x39>

LLVM IR (-O0):
@find_gcc.fname = internal global [256 x i8] zeroinitializer
@.str = [5 x i8] c"PATH\00"
@.str.1 = [39 x i8] c"Error: find_gcc() not enough buffer 1\0A\00"
@.str.2 = [2 x i8] c";\00"
@.str.3 = [2 x i8] c".\00"
@.str.4 = [6 x i8] c"%s\\%s\00"
@.str.5 = [39 x i8] c"Error: find_gcc() not enough buffer 2\0A\00"
@F_OK = global i32 0
@X_OK = global i32 0
define i8* @find_gcc(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca [10240 x i8]
%5 = alloca i8*
%6 = alloca i32
store i8* %0, i8** %3
%7 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i8* %7, i8** %5
%8 = load i8*, i8** %5
%9 = call i32 @strlen(i8* %8)
%10 = sext i32 %9 to i64
%11 = icmp uge i64 %10, 10240
br i1 %11, label %12, label %14
12: ; preds = %1
%13 = call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0))
store i8* null, i8** %2
br label %51
14: ; preds = %1
%15 = getelementptr inbounds [10240 x i8], [10240 x i8]* %4, i64 0, i64 0
%16 = load i8*, i8** %5
%17 = call i32 @strncpy(i8* %15, i8* %16, i32 10240)
%18 = getelementptr inbounds [10240 x i8], [10240 x i8]* %4, i64 0, i64 0
%19 = call i8* @strtok(i8* %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i8* %19, i8** %5
br label %20
20: ; preds = %48, %27, %14
%21 = load i8*, i8** %5
%22 = icmp ne i8* %21, null
br i1 %22, label %23, label %50
23: ; preds = %20
%24 = load i8*, i8** %5
%25 = call i64 @strcmp(i8* %24, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %23
%28 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i8* %28, i8** %5
br label %20
29: ; preds = %23
%30 = load i8*, i8** %5
%31 = load i8*, i8** %3
%32 = call i32 @snprintf(i8* getelementptr inbounds ([256 x i8], [256 x i8]* @find_gcc.fname, i64 0, i64 0), i32 256, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i8* %30, i8* %31)
store i32 %32, i32* %6
%33 = load i32, i32* %6
%34 = icmp slt i32 %33, 0
br i1 %34, label %39, label %35
35: ; preds = %29
%36 = load i32, i32* %6
%37 = sext i32 %36 to i64
%38 = icmp uge i64 %37, 256
br i1 %38, label %39, label %41
39: ; preds = %35, %29
%40 = call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.5, i64 0, i64 0))
store i8* null, i8** %2
br label %51
41: ; preds = %35
%42 = load i32, i32* @F_OK
%43 = load i32, i32* @X_OK
%44 = or i32 %42, %43
%45 = call i32 @access(i8* getelementptr inbounds ([256 x i8], [256 x i8]* @find_gcc.fname, i64 0, i64 0), i32 %44)
%46 = icmp ne i32 %45, -1
br i1 %46, label %47, label %48
47: ; preds = %41
store i8* getelementptr inbounds ([256 x i8], [256 x i8]* @find_gcc.fname, i64 0, i64 0), i8** %2
br label %51
48: ; preds = %41
%49 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i8* %49, i8** %5
br label %20
50: ; preds = %20
store i8* null, i8** %2
br label %51
51: ; preds = %50, %47, %39, %12
%52 = load i8*, i8** %2
ret i8* %52
}

LLVM IR (-O2):
@find_gcc.fname = internal global [256 x i8] zeroinitializer
@.str = [5 x i8] c"PATH\00"
@.str.2 = [2 x i8] c";\00"
@.str.3 = [2 x i8] c".\00"
@.str.4 = [6 x i8] c"%s\\%s\00"
@F_OK = global i32 0
@X_OK = global i32 0
@str = [38 x i8] c"Error: find_gcc() not enough buffer 2\00"
@str.6 = [38 x i8] c"Error: find_gcc() not enough buffer 1\00"
define i8* @find_gcc(i8* %0) {
%2 = alloca [10240 x i8]
%3 = getelementptr inbounds [10240 x i8], [10240 x i8]* %2, i64 0, i64 0
%4 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%5 = tail call i32 @strlen(i8* %4)
%6 = icmp ugt i32 %5, 10239
br i1 %6, label %7, label %9
7: ; preds = %1
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @str.6, i64 0, i64 0))
br label %31
9: ; preds = %1
%10 = call i32 @strncpy(i8* %3, i8* %4, i32 10240)
%11 = call i8* @strtok(i8* %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%12 = icmp eq i8* %11, null
br i1 %12, label %31, label %13
13: ; preds = %9, %28
%14 = phi i8* [ %29, %28 ], [ %11, %9 ]
%15 = call i64 @strcmp(i8* %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%16 = icmp eq i64 %15, 0
br i1 %16, label %28, label %17
17: ; preds = %13
%18 = call i32 @snprintf(i8* getelementptr inbounds ([256 x i8], [256 x i8]* @find_gcc.fname, i64 0, i64 0), i32 256, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i8* %14, i8* %0)
%19 = icmp ugt i32 %18, 255
br i1 %19, label %20, label %22
20: ; preds = %17
%21 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @str, i64 0, i64 0))
br label %31
22: ; preds = %17
%23 = load i32, i32* @F_OK
%24 = load i32, i32* @X_OK
%25 = or i32 %24, %23
%26 = call i32 @access(i8* getelementptr inbounds ([256 x i8], [256 x i8]* @find_gcc.fname, i64 0, i64 0), i32 %25)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %31
28: ; preds = %22, %13
%29 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%30 = icmp eq i8* %29, null
br i1 %30, label %31, label %13
31: ; preds = %28, %22, %9, %20, %7
%32 = phi i8* [ null, %7 ], [ null, %20 ], [ null, %9 ], [ null, %28 ], [ getelementptr inbounds ([256 x i8], [256 x i8]* @find_gcc.fname, i64 0, i64 0), %22 ]
ret i8* %32
}