#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__ ENOENT;
scalar_t__ ENOTDIR;
scalar_t__ S_ISREG(int /*<<< orphan*/);
scalar_t__ errno;
char *getenv(char *);
int /*<<< orphan*/ setenv(char *, char *, int);
int stat(char *, struct stat *);
int strlen(char *);
void split_path(char *pn) {
  struct stat st;
  char *slash = NULL;
  for (;;) {
    int r = stat(pn, &st);
    if (r < 0) {
      if (errno != ENOTDIR && errno != ENOENT)
        break;
    } else {
      if (S_ISREG(st.st_mode))
        break;
    }
    if (slash)
      *slash = '/';
    else
      slash = pn + strlen(pn);
    while (--slash > pn) {
      if (*slash == '/') {
        *slash = '\0';
        break;
      }
    }
    if (slash == pn) {
      slash = NULL;
      break;
    }
  }
  if (slash) {
    *slash = '/';
    setenv("PATH_INFO", slash, 1);
    *slash = '\0';
  }
  setenv("SCRIPT_NAME", pn + strlen(getenv("DOCUMENT_ROOT")), 1);
  setenv("SCRIPT_FILENAME", pn, 1);
}


x86 assembly (clang, -O0):
<split_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x10(%rbp),%rsi
call   1060 <stat@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11bc <split_path+0x6c>
mov    0x2e53(%rip),%rax
mov    (%rax),%rax
mov    0x2e69(%rip),%rcx
cmp    (%rcx),%rax
je     11b7 <split_path+0x67>
mov    0x2e39(%rip),%rax
mov    (%rax),%rax
mov    0x2e1f(%rip),%rcx
cmp    (%rcx),%rax
je     11b7 <split_path+0x67>
jmp    126c <split_path+0x11c>
jmp    11d8 <split_path+0x88>
mov    -0x10(%rbp),%edi
call   1070 <S_ISREG@plt>
cmp    $0x0,%rax
je     11d3 <split_path+0x83>
jmp    126c <split_path+0x11c>
jmp    11d8 <split_path+0x88>
cmpq   $0x0,-0x18(%rbp)
je     11ef <split_path+0x9f>
mov    -0x18(%rbp),%rax
movb   $0x2f,(%rax)
jmp    1210 <split_path+0xc0>
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
jmp    1215 <split_path+0xc5>
mov    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
cmp    -0x8(%rbp),%rax
jbe    124c <split_path+0xfc>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    1247 <split_path+0xf7>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
jmp    124c <split_path+0xfc>
jmp    1215 <split_path+0xc5>
mov    -0x18(%rbp),%rax
cmp    -0x8(%rbp),%rax
jne    1267 <split_path+0x117>
movq   $0x0,-0x18(%rbp)
jmp    126c <split_path+0x11c>
jmp    1164 <split_path+0x14>
cmpq   $0x0,-0x18(%rbp)
je     129a <split_path+0x14a>
mov    -0x18(%rbp),%rax
movb   $0x2f,(%rax)
mov    -0x18(%rbp),%rsi
lea    0xd77(%rip),%rdi
mov    $0x1,%edx
call   1040 <setenv@plt>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
lea    0xd6d(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rdi
call   1050 <strlen@plt>
mov    -0x30(%rbp),%rsi
cltq
add    %rax,%rsi
lea    0xd44(%rip),%rdi
mov    $0x1,%edx
call   1040 <setenv@plt>
mov    -0x8(%rbp),%rsi
lea    0xd49(%rip),%rdi
mov    $0x1,%edx
call   1040 <setenv@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<split_path>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
xor    %ebx,%ebx
mov    %rsp,%r15
mov    0x2e6e(%rip),%r12
mov    0x2e87(%rip),%r13
mov    0x2e50(%rip),%rbp
jmp    1189 <split_path+0x39>
nopw   0x0(%rax,%rax,1)
cmp    %r14,%rbx
je     1229 <split_path+0xd9>
mov    %r14,%rdi
mov    %r15,%rsi
call   1060 <stat@plt>
test   %eax,%eax
js     11b0 <split_path+0x60>
mov    (%rsp),%edi
call   1070 <S_ISREG@plt>
test   %rax,%rax
je     11c0 <split_path+0x70>
jmp    120a <split_path+0xba>
nopw   0x0(%rax,%rax,1)
mov    (%r12),%rax
cmp    0x0(%r13),%rax
je     11c0 <split_path+0x70>
cmp    0x0(%rbp),%rax
jne    120a <split_path+0xba>
test   %rbx,%rbx
je     11d0 <split_path+0x80>
movb   $0x2f,(%rbx)
jmp    11de <split_path+0x8e>
nopw   0x0(%rax,%rax,1)
mov    %r14,%rdi
call   1050 <strlen@plt>
movslq %eax,%rbx
add    %r14,%rbx
add    $0xffffffffffffffff,%rbx
mov    %rbx,%rax
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rax,%rbx
cmp    %r14,%rax
jbe    1180 <split_path+0x30>
lea    -0x1(%rbx),%rax
cmpb   $0x2f,(%rbx)
jne    11f0 <split_path+0xa0>
movb   $0x0,0x1(%rax)
jmp    1180 <split_path+0x30>
test   %rbx,%rbx
je     1229 <split_path+0xd9>
movb   $0x2f,(%rbx)
lea    0xde7(%rip),%rdi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1040 <setenv@plt>
movb   $0x0,(%rbx)
lea    0xde6(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rdi
call   1050 <strlen@plt>
movslq %eax,%rsi
add    %r14,%rsi
lea    0xdc0(%rip),%rdi
mov    $0x1,%edx
call   1040 <setenv@plt>
lea    0xdc9(%rip),%rdi
mov    %r14,%rsi
mov    $0x1,%edx
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <setenv@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@errno = global i64 0
@ENOTDIR = global i64 0
@ENOENT = global i64 0
@.str = [10 x i8] c"PATH_INFO\00"
@.str.1 = [12 x i8] c"SCRIPT_NAME\00"
@.str.2 = [14 x i8] c"DOCUMENT_ROOT\00"
@.str.3 = [16 x i8] c"SCRIPT_FILENAME\00"
define void @split_path(i8* %0) {
%2 = alloca i8*
%3 = alloca %struct.stat
%4 = alloca i8*
%5 = alloca i32
store i8* %0, i8** %2
store i8* null, i8** %4
br label %6
6: ; preds = %58, %1
%7 = load i8*, i8** %2
%8 = call i32 @stat(i8* %7, %struct.stat* %3)
store i32 %8, i32* %5
%9 = load i32, i32* %5
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %21
11: ; preds = %6
%12 = load i64, i64* @errno
%13 = load i64, i64* @ENOTDIR
%14 = icmp ne i64 %12, %13
br i1 %14, label %15, label %20
15: ; preds = %11
%16 = load i64, i64* @errno
%17 = load i64, i64* @ENOENT
%18 = icmp ne i64 %16, %17
br i1 %18, label %19, label %20
19: ; preds = %15
br label %59
20: ; preds = %15, %11
br label %28
21: ; preds = %6
%22 = getelementptr inbounds %struct.stat, %struct.stat* %3, i32 0, i32 0
%23 = load i32, i32* %22
%24 = call i64 @S_ISREG(i32 %23)
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %21
br label %59
27: ; preds = %21
br label %28
28: ; preds = %27, %20
%29 = load i8*, i8** %4
%30 = icmp ne i8* %29, null
br i1 %30, label %31, label %33
31: ; preds = %28
%32 = load i8*, i8** %4
store i8 47, i8* %32
br label %39
33: ; preds = %28
%34 = load i8*, i8** %2
%35 = load i8*, i8** %2
%36 = call i32 @strlen(i8* %35)
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i8, i8* %34, i64 %37
store i8* %38, i8** %4
br label %39
39: ; preds = %33, %31
br label %40
40: ; preds = %52, %39
%41 = load i8*, i8** %4
%42 = getelementptr inbounds i8, i8* %41, i32 -1
store i8* %42, i8** %4
%43 = load i8*, i8** %2
%44 = icmp ugt i8* %42, %43
br i1 %44, label %45, label %53
45: ; preds = %40
%46 = load i8*, i8** %4
%47 = load i8, i8* %46
%48 = sext i8 %47 to i32
%49 = icmp eq i32 %48, 47
br i1 %49, label %50, label %52
50: ; preds = %45
%51 = load i8*, i8** %4
store i8 0, i8* %51
br label %53
52: ; preds = %45
br label %40
53: ; preds = %50, %40
%54 = load i8*, i8** %4
%55 = load i8*, i8** %2
%56 = icmp eq i8* %54, %55
br i1 %56, label %57, label %58
57: ; preds = %53
store i8* null, i8** %4
br label %59
58: ; preds = %53
br label %6
59: ; preds = %57, %26, %19
%60 = load i8*, i8** %4
%61 = icmp ne i8* %60, null
br i1 %61, label %62, label %67
62: ; preds = %59
%63 = load i8*, i8** %4
store i8 47, i8* %63
%64 = load i8*, i8** %4
%65 = call i32 @setenv(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %64, i32 1)
%66 = load i8*, i8** %4
store i8 0, i8* %66
br label %67
67: ; preds = %62, %59
%68 = load i8*, i8** %2
%69 = call i8* @getenv(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%70 = call i32 @strlen(i8* %69)
%71 = sext i32 %70 to i64
%72 = getelementptr inbounds i8, i8* %68, i64 %71
%73 = call i32 @setenv(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* %72, i32 1)
%74 = load i8*, i8** %2
%75 = call i32 @setenv(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i8* %74, i32 1)
ret void
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@errno = global i64 0
@ENOTDIR = global i64 0
@ENOENT = global i64 0
@.str = [10 x i8] c"PATH_INFO\00"
@.str.1 = [12 x i8] c"SCRIPT_NAME\00"
@.str.2 = [14 x i8] c"DOCUMENT_ROOT\00"
@.str.3 = [16 x i8] c"SCRIPT_FILENAME\00"
define void @split_path(i8* %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
br label %5
5: ; preds = %37, %1
%6 = phi i8* [ null, %1 ], [ %31, %37 ]
%7 = call i32 @stat(i8* %0, %struct.stat* %2)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %16
9: ; preds = %5
%10 = load i64, i64* @errno
%11 = load i64, i64* @ENOTDIR
%12 = icmp eq i64 %10, %11
%13 = load i64, i64* @ENOENT
%14 = icmp eq i64 %10, %13
%15 = select i1 %12, i1 true, i1 %14
br i1 %15, label %20, label %39
16: ; preds = %5
%17 = load i32, i32* %4
%18 = tail call i64 @S_ISREG(i32 %17)
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %39
20: ; preds = %16, %9
%21 = icmp eq i8* %6, null
br i1 %21, label %23, label %22
22: ; preds = %20
store i8 47, i8* %6
br label %27
23: ; preds = %20
%24 = tail call i32 @strlen(i8* %0)
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %0, i64 %25
br label %27
27: ; preds = %23, %22
%28 = phi i8* [ %26, %23 ], [ %6, %22 ]
br label %29
29: ; preds = %27, %33
%30 = phi i8* [ %31, %33 ], [ %28, %27 ]
%31 = getelementptr inbounds i8, i8* %30, i64 -1
%32 = icmp ugt i8* %31, %0
br i1 %32, label %33, label %37
33: ; preds = %29
%34 = load i8, i8* %31
%35 = icmp eq i8 %34, 47
br i1 %35, label %36, label %29
36: ; preds = %33
store i8 0, i8* %31
br label %37
37: ; preds = %29, %36
%38 = icmp eq i8* %31, %0
br i1 %38, label %43, label %5
39: ; preds = %9, %16
%40 = icmp eq i8* %6, null
br i1 %40, label %43, label %41
41: ; preds = %39
store i8 47, i8* %6
%42 = tail call i32 @setenv(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %6, i32 1)
store i8 0, i8* %6
br label %43
43: ; preds = %37, %41, %39
%44 = tail call i8* @getenv(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%45 = tail call i32 @strlen(i8* %44)
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds i8, i8* %0, i64 %46
%48 = tail call i32 @setenv(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* %47, i32 1)
%49 = tail call i32 @setenv(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i8* %0, i32 1)
ret void
}