#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;
};
struct dirent {
  int /*<<< orphan*/ d_name;
};
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
scalar_t__ S_ISDIR(int /*<<< orphan*/);
scalar_t__ closedir(int /*<<< orphan*/ *);
scalar_t__ lstat(char *, struct stat *);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ printf(char *, char *);
struct dirent *readdir(int /*<<< orphan*/ *);
scalar_t__ strcmp(int /*<<< orphan*/, char *);
int /*<<< orphan*/ strcpy(char *, int /*<<< orphan*/);
int strlen(char *);
int suffix(char *);
int walk(char *path) {
  struct stat statbuf;
  struct dirent *dirp;
  DIR *dir;
  int n;
  if (lstat(path, &statbuf) < 0) {
    return -1;
  }
  if (S_ISDIR(statbuf.st_mode) == 0) {
    return suffix(path);
  }
  n = strlen(path);
  path[n++] = '/';
  path[n] = 0;
  if ((dir = opendir(path)) == NULL) {
    return -1;
  }
  while ((dirp = readdir(dir)) != NULL) {
    if (strcmp(dirp->d_name, ".") == 0 || strcmp(dirp->d_name, "..") == 0) {
      continue;
    }
    strcpy(&path[n], dirp->d_name);
    if (walk(path) != 0) {
      break;
    }
  }
  path[n - 1] = 0;
  if (closedir(dir) < 0) {
    printf("can't close directory %s\n", path);
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<walk>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1090 <lstat@plt>
cmp    $0x0,%rax
jge    11df <walk+0x2f>
movl   $0xffffffff,-0x4(%rbp)
jmp    1325 <walk+0x175>
mov    -0x18(%rbp),%edi
call   1070 <S_ISDIR@plt>
cmp    $0x0,%rax
jne    1202 <walk+0x52>
mov    -0x10(%rbp),%rdi
call   10c0 <suffix@plt>
mov    %eax,-0x4(%rbp)
jmp    1325 <walk+0x175>
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x2c(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0x2c(%rbp)
movslq %ecx,%rcx
movb   $0x2f,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    1253 <walk+0xa3>
movl   $0xffffffff,-0x4(%rbp)
jmp    1325 <walk+0x175>
jmp    1258 <walk+0xa8>
mov    -0x28(%rbp),%rdi
call   10b0 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     12de <walk+0x12e>
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
lea    0xd85(%rip),%rsi
call   10a0 <strcmp@plt>
cmp    $0x0,%rax
je     12a7 <walk+0xf7>
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
lea    0xd68(%rip),%rsi
call   10a0 <strcmp@plt>
cmp    $0x0,%rax
jne    12ac <walk+0xfc>
jmp    1258 <walk+0xa8>
mov    -0x10(%rbp),%rdi
movslq -0x2c(%rbp),%rax
add    %rax,%rdi
mov    -0x20(%rbp),%rax
mov    (%rax),%esi
call   1030 <strcpy@plt>
mov    -0x10(%rbp),%rdi
call   10d0 <walk@plt>
cmp    $0x0,%eax
je     12d9 <walk+0x129>
jmp    12de <walk+0x12e>
jmp    1258 <walk+0xa8>
mov    -0x10(%rbp),%rax
mov    -0x2c(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rdi
call   1080 <closedir@plt>
cmp    $0x0,%rax
jge    131e <walk+0x16e>
mov    -0x10(%rbp),%rsi
lea    0xcf6(%rip),%rdi
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1325 <walk+0x175>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<walk>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r14
lea    0x8(%rsp),%rsi
call   1090 <lstat@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
js     12c4 <walk+0x114>
mov    0x8(%rsp),%edi
call   1070 <S_ISDIR@plt>
test   %rax,%rax
je     12d5 <walk+0x125>
mov    %r14,%rdi
call   1050 <strlen@plt>
cltq
mov    %rax,0x10(%rsp)
movw   $0x2f,(%r14,%rax,1)
mov    %r14,%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     12c4 <walk+0x114>
mov    %rax,%rbx
mov    %rax,%rdi
call   10b0 <readdir@plt>
test   %rax,%rax
je     1297 <walk+0xe7>
mov    %rax,%rbp
mov    0x10(%rsp),%rax
lea    (%r14,%rax,1),%r15
add    $0x1,%r15
lea    0xdc8(%rip),%r12
lea    0xdc0(%rip),%r13
jmp    1260 <walk+0xb0>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rbx,%rdi
call   10b0 <readdir@plt>
mov    %rax,%rbp
test   %rax,%rax
je     1297 <walk+0xe7>
mov    0x0(%rbp),%edi
mov    %r12,%rsi
call   10a0 <strcmp@plt>
test   %rax,%rax
je     1250 <walk+0xa0>
mov    0x0(%rbp),%edi
mov    %r13,%rsi
call   10a0 <strcmp@plt>
test   %rax,%rax
je     1250 <walk+0xa0>
mov    0x0(%rbp),%esi
mov    %r15,%rdi
call   1030 <strcpy@plt>
mov    %r14,%rdi
call   10d0 <walk@plt>
test   %eax,%eax
je     1250 <walk+0xa0>
mov    0x10(%rsp),%rax
movb   $0x0,(%r14,%rax,1)
mov    %rbx,%rdi
call   1080 <closedir@plt>
xor    %ebp,%ebp
test   %rax,%rax
jns    12c4 <walk+0x114>
lea    0xd4c(%rip),%rdi
mov    %r14,%rsi
call   1060 <printf@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r14,%rdi
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    10c0 <suffix@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
%struct.dirent = type { i32 }
@.str = [2 x i8] c".\00"
@.str.1 = [3 x i8] c"..\00"
@.str.2 = [26 x i8] c"can't close directory %s\0A\00"
define i32 @walk(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.stat
%5 = alloca %struct.dirent*
%6 = alloca i32*
%7 = alloca i32
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = call i64 @lstat(i8* %8, %struct.stat* %4)
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %1
store i32 -1, i32* %2
br label %81
12: ; preds = %1
%13 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%14 = load i32, i32* %13
%15 = call i64 @S_ISDIR(i32 %14)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %12
%18 = load i8*, i8** %3
%19 = call i32 @suffix(i8* %18)
store i32 %19, i32* %2
br label %81
20: ; preds = %12
%21 = load i8*, i8** %3
%22 = call i32 @strlen(i8* %21)
store i32 %22, i32* %7
%23 = load i8*, i8** %3
%24 = load i32, i32* %7
%25 = add nsw i32 %24, 1
store i32 %25, i32* %7
%26 = sext i32 %24 to i64
%27 = getelementptr inbounds i8, i8* %23, i64 %26
store i8 47, i8* %27
%28 = load i8*, i8** %3
%29 = load i32, i32* %7
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %28, i64 %30
store i8 0, i8* %31
%32 = load i8*, i8** %3
%33 = call i32* @opendir(i8* %32)
store i32* %33, i32** %6
%34 = icmp eq i32* %33, null
br i1 %34, label %35, label %36
35: ; preds = %20
store i32 -1, i32* %2
br label %81
36: ; preds = %20
br label %37
37: ; preds = %67, %53, %36
%38 = load i32*, i32** %6
%39 = call %struct.dirent* @readdir(i32* %38)
store %struct.dirent* %39, %struct.dirent** %5
%40 = icmp ne %struct.dirent* %39, null
br i1 %40, label %41, label %68
41: ; preds = %37
%42 = load %struct.dirent*, %struct.dirent** %5
%43 = getelementptr inbounds %struct.dirent, %struct.dirent* %42, i32 0, i32 0
%44 = load i32, i32* %43
%45 = call i64 @strcmp(i32 %44, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%46 = icmp eq i64 %45, 0
br i1 %46, label %53, label %47
47: ; preds = %41
%48 = load %struct.dirent*, %struct.dirent** %5
%49 = getelementptr inbounds %struct.dirent, %struct.dirent* %48, i32 0, i32 0
%50 = load i32, i32* %49
%51 = call i64 @strcmp(i32 %50, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%52 = icmp eq i64 %51, 0
br i1 %52, label %53, label %54
53: ; preds = %47, %41
br label %37
54: ; preds = %47
%55 = load i8*, i8** %3
%56 = load i32, i32* %7
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds i8, i8* %55, i64 %57
%59 = load %struct.dirent*, %struct.dirent** %5
%60 = getelementptr inbounds %struct.dirent, %struct.dirent* %59, i32 0, i32 0
%61 = load i32, i32* %60
%62 = call i32 @strcpy(i8* %58, i32 %61)
%63 = load i8*, i8** %3
%64 = call i32 @walk(i8* %63)
%65 = icmp ne i32 %64, 0
br i1 %65, label %66, label %67
66: ; preds = %54
br label %68
67: ; preds = %54
br label %37
68: ; preds = %66, %37
%69 = load i8*, i8** %3
%70 = load i32, i32* %7
%71 = sub nsw i32 %70, 1
%72 = sext i32 %71 to i64
%73 = getelementptr inbounds i8, i8* %69, i64 %72
store i8 0, i8* %73
%74 = load i32*, i32** %6
%75 = call i64 @closedir(i32* %74)
%76 = icmp slt i64 %75, 0
br i1 %76, label %77, label %80
77: ; preds = %68
%78 = load i8*, i8** %3
%79 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* %78)
store i32 -1, i32* %2
br label %81
80: ; preds = %68
store i32 0, i32* %2
br label %81
81: ; preds = %80, %77, %35, %17, %11
%82 = load i32, i32* %2
ret i32 %82
}

LLVM IR (-O2):
%struct.stat = type { i32 }
%struct.dirent = type { i32 }
@.str = [2 x i8] c".\00"
@.str.1 = [3 x i8] c"..\00"
@.str.2 = [26 x i8] c"can't close directory %s\0A\00"
define i32 @walk(i8* %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = call i64 @lstat(i8* %0, %struct.stat* %2)
%5 = icmp slt i64 %4, 0
br i1 %5, label %48, label %6
6: ; preds = %1
%7 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%8 = load i32, i32* %7
%9 = tail call i64 @S_ISDIR(i32 %8)
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %6
%12 = tail call i32 @suffix(i8* %0)
br label %48
13: ; preds = %6
%14 = tail call i32 @strlen(i8* %0)
%15 = add nsw i32 %14, 1
%16 = sext i32 %14 to i64
%17 = getelementptr inbounds i8, i8* %0, i64 %16
store i8 47, i8* %17
%18 = sext i32 %15 to i64
%19 = getelementptr inbounds i8, i8* %0, i64 %18
store i8 0, i8* %19
%20 = tail call i32* @opendir(i8* %0)
%21 = icmp eq i32* %20, null
br i1 %21, label %48, label %22
22: ; preds = %13
%23 = tail call %struct.dirent* @readdir(i32* %20)
%24 = icmp eq %struct.dirent* %23, null
br i1 %24, label %43, label %25
25: ; preds = %22, %35
%26 = phi %struct.dirent* [ %36, %35 ], [ %23, %22 ]
%27 = getelementptr inbounds %struct.dirent, %struct.dirent* %26, i64 0, i32 0
%28 = load i32, i32* %27
%29 = tail call i64 @strcmp(i32 %28, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%30 = icmp eq i64 %29, 0
br i1 %30, label %35, label %31
31: ; preds = %25
%32 = load i32, i32* %27
%33 = tail call i64 @strcmp(i32 %32, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%34 = icmp eq i64 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %25, %31, %38
%36 = tail call %struct.dirent* @readdir(i32* %20)
%37 = icmp eq %struct.dirent* %36, null
br i1 %37, label %43, label %25
38: ; preds = %31
%39 = load i32, i32* %27
%40 = tail call i32 @strcpy(i8* %19, i32 %39)
%41 = tail call i32 @walk(i8* %0)
%42 = icmp eq i32 %41, 0
br i1 %42, label %35, label %43
43: ; preds = %35, %38, %22
store i8 0, i8* %17
%44 = tail call i64 @closedir(i32* %20)
%45 = icmp slt i64 %44, 0
br i1 %45, label %46, label %48
46: ; preds = %43
%47 = tail call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %48
48: ; preds = %43, %13, %1, %46, %11
%49 = phi i32 [ %12, %11 ], [ -1, %46 ], [ -1, %1 ], [ -1, %13 ], [ 0, %43 ]
ret i32 %49
}