#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 stat {
  int /*<<< orphan*/ st_mode;
};

/* Variables and functions */
scalar_t__ S_ISREG(int /*<<< orphan*/);
char *config_getSysPathForUrlPath(char *);
scalar_t__ config_isCGI(char *);
int /*<<< orphan*/ free(char *);
scalar_t__ stat(char *, struct stat *);
char *strdup(char const *);
char *strrchr(char *, char);
bool config_findCGI(const char *urlPath, char **cgiExeBuf, char **cgiUrlBuf,
                    char **cgiSubPathBuf) {
  char *cgiExe = NULL, *cgiUrl = NULL, *cgiSubPath = NULL, *slashPos;
  struct stat st;
  bool isCGI;
  cgiUrl = strdup(urlPath);
  isCGI = config_isCGI(cgiUrl) &&
          (cgiExe = config_getSysPathForUrlPath(cgiUrl)) != NULL &&
          stat(cgiExe, &st) == 0 && S_ISREG(st.st_mode);
  if (!isCGI) {
    free(cgiExe);
    slashPos = strrchr(cgiUrl, '/');
    while (!isCGI && slashPos != NULL) {
      *slashPos = '\0';
      if (config_isCGI(cgiUrl[0] ? cgiUrl : "/")) {
        cgiExe = config_getSysPathForUrlPath(cgiUrl);
        if (cgiExe != NULL && stat(cgiExe, &st) == 0 && S_ISREG(st.st_mode)) {
          isCGI = true;
          cgiSubPath = slashPos;
        } else
          free(cgiExe);
      }
      *slashPos = '/';
      while ((slashPos = slashPos == cgiUrl ? NULL : slashPos - 1) &&
             *slashPos != '/')
        ;
    }
  }
  if (isCGI) {
    *cgiExeBuf = cgiExe;
    *cgiUrlBuf = cgiUrl;
    if (cgiSubPath != NULL) {
      slashPos = cgiSubPath;
      cgiSubPath = strdup(cgiSubPath);
      *slashPos = '\0';
    }
    *cgiSubPathBuf = cgiSubPath;
  } else {
    free(cgiUrl);
  }
  return cgiSubPath != NULL;
}


x86 assembly (clang, -O0):
<config_findCGI>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x8(%rbp),%rdi
call   1080 <strdup@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
call   1070 <config_isCGI@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x4d(%rbp)
je     1218 <config_findCGI+0xa8>
mov    -0x30(%rbp),%rdi
call   1090 <config_getSysPathForUrlPath@plt>
mov    %rax,%rcx
mov    %rcx,-0x28(%rbp)
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x4d(%rbp)
je     1218 <config_findCGI+0xa8>
mov    -0x28(%rbp),%rdi
lea    -0x48(%rbp),%rsi
call   1050 <stat@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x4d(%rbp)
jne    1218 <config_findCGI+0xa8>
mov    -0x48(%rbp),%edi
call   1060 <S_ISREG@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,-0x4d(%rbp)
mov    -0x4d(%rbp),%al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x4c(%rbp)
cmpl   $0x0,-0x4c(%rbp)
jne    138e <config_findCGI+0x21e>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rdi
mov    $0x2f,%esi
call   1040 <strrchr@plt>
mov    %rax,-0x40(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x4c(%rbp)
mov    %al,-0x4e(%rbp)
jne    1262 <config_findCGI+0xf2>
cmpq   $0x0,-0x40(%rbp)
setne  %al
mov    %al,-0x4e(%rbp)
mov    -0x4e(%rbp),%al
test   $0x1,%al
jne    1272 <config_findCGI+0x102>
jmp    1389 <config_findCGI+0x219>
mov    -0x40(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x30(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     1296 <config_findCGI+0x126>
mov    -0x30(%rbp),%rax
mov    %rax,-0x58(%rbp)
jmp    12a6 <config_findCGI+0x136>
lea    0xd63(%rip),%rax
mov    %rax,-0x58(%rbp)
jmp    12a6 <config_findCGI+0x136>
mov    -0x58(%rbp),%rdi
call   1070 <config_isCGI@plt>
cmp    $0x0,%rax
je     131c <config_findCGI+0x1ac>
mov    -0x30(%rbp),%rdi
call   1090 <config_getSysPathForUrlPath@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     130e <config_findCGI+0x19e>
mov    -0x28(%rbp),%rdi
lea    -0x48(%rbp),%rsi
call   1050 <stat@plt>
cmp    $0x0,%rax
jne    130e <config_findCGI+0x19e>
mov    -0x48(%rbp),%edi
call   1060 <S_ISREG@plt>
cmp    $0x0,%rax
je     130e <config_findCGI+0x19e>
movl   $0x1,-0x4c(%rbp)
mov    -0x40(%rbp),%rax
mov    %rax,-0x38(%rbp)
jmp    1317 <config_findCGI+0x1a7>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
jmp    131c <config_findCGI+0x1ac>
mov    -0x40(%rbp),%rax
movb   $0x2f,(%rax)
mov    -0x40(%rbp),%rax
cmp    -0x30(%rbp),%rax
jne    133c <config_findCGI+0x1cc>
xor    %eax,%eax
mov    %rax,-0x60(%rbp)
jmp    1348 <config_findCGI+0x1d8>
mov    -0x40(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x60(%rbp)
mov    -0x60(%rbp),%rcx
mov    %rcx,-0x40(%rbp)
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x61(%rbp)
je     136f <config_findCGI+0x1ff>
mov    -0x40(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
setne  %al
mov    %al,-0x61(%rbp)
mov    -0x61(%rbp),%al
test   $0x1,%al
jne    137f <config_findCGI+0x20f>
jmp    1384 <config_findCGI+0x214>
jmp    1323 <config_findCGI+0x1b3>
jmp    1248 <config_findCGI+0xd8>
jmp    138e <config_findCGI+0x21e>
cmpl   $0x0,-0x4c(%rbp)
je     13e5 <config_findCGI+0x275>
mov    -0x28(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x30(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
cmpq   $0x0,-0x38(%rbp)
je     13d5 <config_findCGI+0x265>
mov    -0x38(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x38(%rbp),%rdi
call   1080 <strdup@plt>
mov    %rax,-0x38(%rbp)
mov    -0x40(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x38(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
jmp    13ee <config_findCGI+0x27e>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
cmpq   $0x0,-0x38(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<config_findCGI>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rcx,%r15
mov    %rdx,%r14
mov    %rsi,%r12
call   1080 <strdup@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1070 <config_isCGI@plt>
test   %rax,%rax
je     11db <config_findCGI+0x6b>
mov    %rbx,%rdi
call   1090 <config_getSysPathForUrlPath@plt>
test   %rax,%rax
je     11db <config_findCGI+0x6b>
mov    %rax,%r13
lea    0x8(%rsp),%rsi
mov    %rax,%rdi
call   1050 <stat@plt>
test   %rax,%rax
jne    11de <config_findCGI+0x6e>
mov    0x8(%rsp),%edi
call   1060 <S_ISREG@plt>
test   %rax,%rax
je     11de <config_findCGI+0x6e>
mov    %r13,(%r12)
mov    %rbx,(%r14)
xor    %r14d,%r14d
jmp    12e5 <config_findCGI+0x175>
xor    %r13d,%r13d
mov    %r13,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
mov    $0x2f,%esi
call   1040 <strrchr@plt>
test   %rax,%rax
je     12d0 <config_findCGI+0x160>
mov    %rax,%rbp
mov    %r12,0x18(%rsp)
mov    %r14,0x20(%rsp)
mov    %r15,0x10(%rsp)
xor    %r14d,%r14d
lea    0xde8(%rip),%r15
movb   $0x0,0x0(%rbp)
cmpb   $0x0,(%rbx)
mov    %rbx,%rdi
cmove  %r15,%rdi
call   1070 <config_isCGI@plt>
mov    $0x1,%r12b
test   %rax,%rax
je     125d <config_findCGI+0xed>
mov    %rbx,%rdi
call   1090 <config_getSysPathForUrlPath@plt>
mov    %rax,%r13
test   %rax,%rax
je     1255 <config_findCGI+0xe5>
mov    %r13,%rdi
lea    0x8(%rsp),%rsi
call   1050 <stat@plt>
test   %rax,%rax
je     128f <config_findCGI+0x11f>
mov    %r13,%rdi
call   1030 <free@plt>
movb   $0x2f,0x0(%rbp)
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
cmp    %rbx,%rbp
je     12a5 <config_findCGI+0x135>
mov    %rbp,%rax
add    $0xffffffffffffffff,%rbp
cmpb   $0x2f,-0x1(%rax)
jne    1270 <config_findCGI+0x100>
cmp    %rbx,%rax
setne  %al
test   %al,%r12b
jne    1218 <config_findCGI+0xa8>
jmp    12a5 <config_findCGI+0x135>
mov    0x8(%rsp),%edi
call   1060 <S_ISREG@plt>
test   %rax,%rax
je     1255 <config_findCGI+0xe5>
xor    %r12d,%r12d
mov    %rbp,%r14
jmp    125d <config_findCGI+0xed>
test   %r12b,%r12b
jne    12d3 <config_findCGI+0x163>
mov    0x18(%rsp),%rax
mov    %r13,(%rax)
mov    0x20(%rsp),%rax
mov    %rbx,(%rax)
test   %r14,%r14
je     12dd <config_findCGI+0x16d>
mov    %r14,%rdi
call   1080 <strdup@plt>
movb   $0x0,(%r14)
mov    %rax,%r14
jmp    12e0 <config_findCGI+0x170>
xor    %r14d,%r14d
mov    %rbx,%rdi
call   1030 <free@plt>
jmp    12e8 <config_findCGI+0x178>
xor    %r14d,%r14d
mov    0x10(%rsp),%r15
mov    %r14,(%r15)
xor    %eax,%eax
test   %r14,%r14
setne  %al
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [2 x i8] c"/\00"
define i32 @config_findCGI(i8* nocapture %0, i8** nocapture writeonly %1, i8** nocapture writeonly %2, i8** nocapture writeonly %3) {
%5 = alloca %struct.stat
%6 = bitcast %struct.stat* %5 to i8*
%7 = tail call i8* @strdup(i8* %0)
%8 = tail call i64 @config_isCGI(i8* %7)
%9 = icmp eq i64 %8, 0
br i1 %9, label %22, label %10
10: ; preds = %4
%11 = tail call i8* @config_getSysPathForUrlPath(i8* %7)
%12 = icmp eq i8* %11, null
br i1 %12, label %22, label %13
13: ; preds = %10
%14 = call i64 @stat(i8* %11, %struct.stat* %5)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %22
16: ; preds = %13
%17 = getelementptr inbounds %struct.stat, %struct.stat* %5, i64 0, i32 0
%18 = load i32, i32* %17
%19 = call i64 @S_ISREG(i32 %18)
%20 = icmp eq i64 %19, 0
br i1 %20, label %22, label %21
21: ; preds = %16
store i8* %11, i8** %1
store i8* %7, i8** %2
br label %69
22: ; preds = %4, %10, %13, %16
%23 = phi i8* [ %11, %16 ], [ null, %4 ], [ null, %10 ], [ %11, %13 ]
%24 = call i32 @free(i8* %23)
%25 = call i8* @strrchr(i8* dereferenceable(1) %7, i8 signext 47)
%26 = icmp eq i8* %25, null
br i1 %26, label %71, label %27
27: ; preds = %22
%28 = getelementptr inbounds %struct.stat, %struct.stat* %5, i64 0, i32 0
br label %29
29: ; preds = %27, %61
%30 = phi i8* [ %25, %27 ], [ %57, %61 ]
%31 = phi i8* [ null, %27 ], [ %52, %61 ]
%32 = phi i8* [ %23, %27 ], [ %51, %61 ]
store i8 0, i8* %30
%33 = load i8, i8* %7
%34 = icmp eq i8 %33, 0
%35 = select i1 %34, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8* %7
%36 = call i64 @config_isCGI(i8* %35)
%37 = icmp eq i64 %36, 0
br i1 %37, label %50, label %38
38: ; preds = %29
%39 = call i8* @config_getSysPathForUrlPath(i8* %7)
%40 = icmp eq i8* %39, null
br i1 %40, label %48, label %41
41: ; preds = %38
%42 = call i64 @stat(i8* %39, %struct.stat* %5)
%43 = icmp eq i64 %42, 0
br i1 %43, label %44, label %48
44: ; preds = %41
%45 = load i32, i32* %28
%46 = call i64 @S_ISREG(i32 %45)
%47 = icmp eq i64 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %44, %41, %38
%49 = call i32 @free(i8* %39)
br label %50
50: ; preds = %44, %48, %29
%51 = phi i8* [ %39, %48 ], [ %32, %29 ], [ %39, %44 ]
%52 = phi i8* [ %31, %48 ], [ %31, %29 ], [ %30, %44 ]
%53 = phi i1 [ true, %48 ], [ true, %29 ], [ false, %44 ]
store i8 47, i8* %30
br label %54
54: ; preds = %58, %50
%55 = phi i8* [ %30, %50 ], [ %57, %58 ]
%56 = icmp eq i8* %55, %7
%57 = getelementptr inbounds i8, i8* %55, i64 -1
br i1 %56, label %64, label %58
58: ; preds = %54
%59 = load i8, i8* %57
%60 = icmp eq i8 %59, 47
br i1 %60, label %61, label %54
61: ; preds = %58
%62 = xor i1 %56, true
%63 = select i1 %53, i1 %62, i1 false
br i1 %63, label %29, label %64
64: ; preds = %61, %54
br i1 %53, label %71, label %65
65: ; preds = %64
store i8* %51, i8** %1
store i8* %7, i8** %2
%66 = icmp eq i8* %52, null
br i1 %66, label %69, label %67
67: ; preds = %65
%68 = call i8* @strdup(i8* %52)
store i8 0, i8* %52
br label %69
69: ; preds = %21, %67, %65
%70 = phi i8* [ %68, %67 ], [ null, %65 ], [ null, %21 ]
store i8* %70, i8** %3
br label %74
71: ; preds = %22, %64
%72 = phi i8* [ %52, %64 ], [ null, %22 ]
%73 = call i32 @free(i8* %7)
br label %74
74: ; preds = %71, %69
%75 = phi i8* [ %70, %69 ], [ %72, %71 ]
%76 = icmp ne i8* %75, null
%77 = zext i1 %76 to i32
ret i32 %77
}