#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 */
int /*<<< orphan*/ S_ISDIR(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ strcat(char *, char *);
int strlen(char *);
int enum_index(char *path) {
  struct stat buf;
  char *p_end = path + strlen(path);
  printf("enum_index in\n");
  strcat(path, "index.htm");
  if (stat(path, &buf) == 0 && !S_ISDIR(buf.st_mode))
    return 0;
  *p_end = '\0';
  strcat(path, "index.html");
  if (stat(path, &buf) == 0 && !S_ISDIR(buf.st_mode))
    return 0;
  *p_end = '\0';
  strcat(path, "index.cgi");
  if (stat(path, &buf) == 0 && !S_ISDIR(buf.st_mode))
    return 0;
  *p_end = '\0';
  printf("enum_index out\n");
  return -1;
}


x86 assembly (clang, -O0):
<enum_index>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
lea    0xe7c(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
lea    0xe7b(%rip),%rsi
call   1070 <strcat@plt>
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1060 <stat@plt>
cmp    $0x0,%rax
jne    11cd <enum_index+0x7d>
mov    -0x18(%rbp),%edi
call   1050 <S_ISDIR@plt>
cmp    $0x0,%eax
jne    11cd <enum_index+0x7d>
movl   $0x0,-0x4(%rbp)
jmp    127d <enum_index+0x12d>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
lea    0xe3a(%rip),%rsi
call   1070 <strcat@plt>
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1060 <stat@plt>
cmp    $0x0,%rax
jne    1218 <enum_index+0xc8>
mov    -0x18(%rbp),%edi
call   1050 <S_ISDIR@plt>
cmp    $0x0,%eax
jne    1218 <enum_index+0xc8>
movl   $0x0,-0x4(%rbp)
jmp    127d <enum_index+0x12d>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
lea    0xdfa(%rip),%rsi
call   1070 <strcat@plt>
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1060 <stat@plt>
cmp    $0x0,%rax
jne    1263 <enum_index+0x113>
mov    -0x18(%rbp),%edi
call   1050 <S_ISDIR@plt>
cmp    $0x0,%eax
jne    1263 <enum_index+0x113>
movl   $0x0,-0x4(%rbp)
jmp    127d <enum_index+0x12d>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
lea    0xdbd(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<enum_index>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
call   1040 <strlen@plt>
mov    %eax,%ebp
lea    0xebb(%rip),%rdi
call   1030 <puts@plt>
lea    0xe90(%rip),%rsi
mov    %rbx,%rdi
call   1070 <strcat@plt>
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1060 <stat@plt>
test   %rax,%rax
jne    1194 <enum_index+0x44>
mov    (%rsp),%edi
call   1050 <S_ISDIR@plt>
test   %eax,%eax
je     120c <enum_index+0xbc>
movslq %ebp,%rbp
movb   $0x0,(%rbx,%rbp,1)
lea    0xe68(%rip),%rsi
mov    %rbx,%rdi
call   1070 <strcat@plt>
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1060 <stat@plt>
test   %rax,%rax
jne    11c6 <enum_index+0x76>
mov    (%rsp),%edi
call   1050 <S_ISDIR@plt>
test   %eax,%eax
je     120c <enum_index+0xbc>
movb   $0x0,(%rbx,%rbp,1)
lea    0xe44(%rip),%rsi
mov    %rbx,%rdi
call   1070 <strcat@plt>
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1060 <stat@plt>
test   %rax,%rax
jne    11f5 <enum_index+0xa5>
mov    (%rsp),%edi
call   1050 <S_ISDIR@plt>
test   %eax,%eax
je     120c <enum_index+0xbc>
movb   $0x0,(%rbx,%rbp,1)
lea    0xe2d(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%eax
jmp    120e <enum_index+0xbe>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [15 x i8] c"enum_index in\0A\00"
@.str.1 = [10 x i8] c"index.htm\00"
@.str.2 = [11 x i8] c"index.html\00"
@.str.3 = [10 x i8] c"index.cgi\00"
@.str.4 = [16 x i8] c"enum_index out\0A\00"
define i32 @enum_index(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.stat
%5 = alloca i8*
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = load i8*, i8** %3
%8 = call i32 @strlen(i8* %7)
%9 = sext i32 %8 to i64
%10 = getelementptr inbounds i8, i8* %6, i64 %9
store i8* %10, i8** %5
%11 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%12 = load i8*, i8** %3
%13 = call i32 @strcat(i8* %12, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%14 = load i8*, i8** %3
%15 = call i64 @stat(i8* %14, %struct.stat* %4)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %23
17: ; preds = %1
%18 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%19 = load i32, i32* %18
%20 = call i32 @S_ISDIR(i32 %19)
%21 = icmp ne i32 %20, 0
br i1 %21, label %23, label %22
22: ; preds = %17
store i32 0, i32* %2
br label %52
23: ; preds = %17, %1
%24 = load i8*, i8** %5
store i8 0, i8* %24
%25 = load i8*, i8** %3
%26 = call i32 @strcat(i8* %25, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%27 = load i8*, i8** %3
%28 = call i64 @stat(i8* %27, %struct.stat* %4)
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %36
30: ; preds = %23
%31 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%32 = load i32, i32* %31
%33 = call i32 @S_ISDIR(i32 %32)
%34 = icmp ne i32 %33, 0
br i1 %34, label %36, label %35
35: ; preds = %30
store i32 0, i32* %2
br label %52
36: ; preds = %30, %23
%37 = load i8*, i8** %5
store i8 0, i8* %37
%38 = load i8*, i8** %3
%39 = call i32 @strcat(i8* %38, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
%40 = load i8*, i8** %3
%41 = call i64 @stat(i8* %40, %struct.stat* %4)
%42 = icmp eq i64 %41, 0
br i1 %42, label %43, label %49
43: ; preds = %36
%44 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%45 = load i32, i32* %44
%46 = call i32 @S_ISDIR(i32 %45)
%47 = icmp ne i32 %46, 0
br i1 %47, label %49, label %48
48: ; preds = %43
store i32 0, i32* %2
br label %52
49: ; preds = %43, %36
%50 = load i8*, i8** %5
store i8 0, i8* %50
%51 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %2
br label %52
52: ; preds = %49, %48, %35, %22
%53 = load i32, i32* %2
ret i32 %53
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str.1 = [10 x i8] c"index.htm\00"
@.str.2 = [11 x i8] c"index.html\00"
@.str.3 = [10 x i8] c"index.cgi\00"
@str = [14 x i8] c"enum_index in\00"
@str.5 = [15 x i8] c"enum_index out\00"
define i32 @enum_index(i8* %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = tail call i32 @strlen(i8* %0)
%5 = sext i32 %4 to i64
%6 = getelementptr inbounds i8, i8* %0, i64 %5
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str, i64 0, i64 0))
%8 = tail call i32 @strcat(i8* %0, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%9 = call i64 @stat(i8* %0, %struct.stat* %2)
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %16
11: ; preds = %1
%12 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%13 = load i32, i32* %12
%14 = call i32 @S_ISDIR(i32 %13)
%15 = icmp eq i32 %14, 0
br i1 %15, label %36, label %16
16: ; preds = %11, %1
store i8 0, i8* %6
%17 = call i32 @strcat(i8* %0, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%18 = call i64 @stat(i8* %0, %struct.stat* %2)
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %25
20: ; preds = %16
%21 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%22 = load i32, i32* %21
%23 = call i32 @S_ISDIR(i32 %22)
%24 = icmp eq i32 %23, 0
br i1 %24, label %36, label %25
25: ; preds = %20, %16
store i8 0, i8* %6
%26 = call i32 @strcat(i8* %0, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
%27 = call i64 @stat(i8* %0, %struct.stat* %2)
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %34
29: ; preds = %25
%30 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%31 = load i32, i32* %30
%32 = call i32 @S_ISDIR(i32 %31)
%33 = icmp eq i32 %32, 0
br i1 %33, label %36, label %34
34: ; preds = %29, %25
store i8 0, i8* %6
%35 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.5, i64 0, i64 0))
br label %36
36: ; preds = %29, %20, %11, %34
%37 = phi i32 [ -1, %34 ], [ 0, %11 ], [ 0, %20 ], [ 0, %29 ]
ret i32 %37
}