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

/* Variables and functions */
int chdir(char const *);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ do_preformat(struct stat *);
int /*<<< orphan*/ do_stat(struct stat *, char const *);
int /*<<< orphan*/ exit(int);
scalar_t__ lstat(char const *, struct stat *);
int /*<<< orphan*/ *opendir(char const *);
int /*<<< orphan*/ perror(char const *);
struct dirent *readdir(int /*<<< orphan*/ *);
void do_dir(const char *path, int preformat) {
  DIR *dir;
  struct dirent *dent;
  struct stat st;
  if (chdir(path) == -1) {
    perror(path);
    exit(1);
  }
  dir = opendir(path);
  if (dir == NULL) {
    perror(path);
    exit(1);
  }
  while ((dent = readdir(dir)) != NULL) {
    if (lstat(dent->d_name, &st)) {
      perror(dent->d_name);
      exit(1);
    }
    (preformat) ? do_preformat(&st) : do_stat(&st, dent->d_name);
  }
  closedir(dir);
}


x86 assembly (clang, -O0):
<do_dir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <chdir@plt>
cmp    $0xffffffff,%eax
jne    11c4 <do_dir+0x34>
mov    -0x8(%rbp),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x8(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11ef <do_dir+0x5f>
mov    -0x8(%rbp),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
jmp    11f4 <do_dir+0x64>
mov    -0x18(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     1268 <do_dir+0xd8>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
lea    -0x28(%rbp),%rsi
call   1070 <lstat@plt>
cmp    $0x0,%rax
je     123b <do_dir+0xab>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
cmpl   $0x0,-0xc(%rbp)
je     1253 <do_dir+0xc3>
lea    -0x28(%rbp),%rdi
call   1030 <do_preformat@plt>
jmp    1263 <do_dir+0xd3>
mov    -0x20(%rbp),%rax
mov    (%rax),%rsi
lea    -0x28(%rbp),%rdi
call   10b0 <do_stat@plt>
jmp    11f4 <do_dir+0x64>
mov    -0x18(%rbp),%rdi
call   1060 <closedir@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_dir>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %rdi,%rbx
call   1050 <chdir@plt>
cmp    $0xffffffff,%eax
je     122f <do_dir+0x9f>
mov    %rbx,%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     122f <do_dir+0x9f>
mov    %rax,%r15
mov    %rax,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
je     120a <do_dir+0x7a>
mov    %rax,%rbx
mov    %rsp,%rbp
jmp    11eb <do_dir+0x5b>
mov    (%rbx),%rsi
mov    %rbp,%rdi
call   10b0 <do_stat@plt>
mov    %r15,%rdi
call   1080 <readdir@plt>
mov    %rax,%rbx
test   %rax,%rax
je     120a <do_dir+0x7a>
mov    (%rbx),%rdi
mov    %rbp,%rsi
call   1070 <lstat@plt>
test   %rax,%rax
jne    121d <do_dir+0x8d>
test   %r14d,%r14d
je     11d0 <do_dir+0x40>
mov    %rbp,%rdi
call   1030 <do_preformat@plt>
jmp    11db <do_dir+0x4b>
mov    %r15,%rdi
call   1060 <closedir@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%rbx),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    %rbx,%rdi
jmp    1220 <do_dir+0x90>

LLVM IR (-O0):
%struct.dirent = type { i8* }
%struct.stat = type { i32 }
define void @do_dir(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32*
%6 = alloca %struct.dirent*
%7 = alloca %struct.stat
store i8* %0, i8** %3
store i32 %1, i32* %4
%8 = load i8*, i8** %3
%9 = call i32 @chdir(i8* %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %15
11: ; preds = %2
%12 = load i8*, i8** %3
%13 = call i32 @perror(i8* %12)
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %2
%16 = load i8*, i8** %3
%17 = call i32* @opendir(i8* %16)
store i32* %17, i32** %5
%18 = load i32*, i32** %5
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %24
20: ; preds = %15
%21 = load i8*, i8** %3
%22 = call i32 @perror(i8* %21)
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %15
br label %25
25: ; preds = %51, %24
%26 = load i32*, i32** %5
%27 = call %struct.dirent* @readdir(i32* %26)
store %struct.dirent* %27, %struct.dirent** %6
%28 = icmp ne %struct.dirent* %27, null
br i1 %28, label %29, label %53
29: ; preds = %25
%30 = load %struct.dirent*, %struct.dirent** %6
%31 = getelementptr inbounds %struct.dirent, %struct.dirent* %30, i32 0, i32 0
%32 = load i8*, i8** %31
%33 = call i64 @lstat(i8* %32, %struct.stat* %7)
%34 = icmp ne i64 %33, 0
br i1 %34, label %35, label %41
35: ; preds = %29
%36 = load %struct.dirent*, %struct.dirent** %6
%37 = getelementptr inbounds %struct.dirent, %struct.dirent* %36, i32 0, i32 0
%38 = load i8*, i8** %37
%39 = call i32 @perror(i8* %38)
%40 = call i32 @exit(i32 1)
unreachable
41: ; preds = %29
%42 = load i32, i32* %4
%43 = icmp ne i32 %42, 0
br i1 %43, label %44, label %46
44: ; preds = %41
%45 = call i32 @do_preformat(%struct.stat* %7)
br label %51
46: ; preds = %41
%47 = load %struct.dirent*, %struct.dirent** %6
%48 = getelementptr inbounds %struct.dirent, %struct.dirent* %47, i32 0, i32 0
%49 = load i8*, i8** %48
%50 = call i32 @do_stat(%struct.stat* %7, i8* %49)
br label %51
51: ; preds = %46, %44
%52 = phi i32 [ %45, %44 ], [ %50, %46 ]
br label %25
53: ; preds = %25
%54 = load i32*, i32** %5
%55 = call i32 @closedir(i32* %54)
ret void
}

LLVM IR (-O2):
%struct.stat = type { i32 }
%struct.dirent = type { i8* }
define void @do_dir(i8* %0, i32 %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = tail call i32 @chdir(i8* %0)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %10
7: ; preds = %2
%8 = tail call i32 @perror(i8* %0)
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %2
%11 = tail call i32* @opendir(i8* %0)
%12 = icmp eq i32* %11, null
br i1 %12, label %18, label %13
13: ; preds = %10
%14 = call %struct.dirent* @readdir(i32* %11)
%15 = icmp eq %struct.dirent* %14, null
br i1 %15, label %40, label %16
16: ; preds = %13
%17 = icmp eq i32 %1, 0
br label %21
18: ; preds = %10
%19 = tail call i32 @perror(i8* %0)
%20 = tail call i32 @exit(i32 1)
unreachable
21: ; preds = %16, %37
%22 = phi %struct.dirent* [ %14, %16 ], [ %38, %37 ]
%23 = getelementptr inbounds %struct.dirent, %struct.dirent* %22, i64 0, i32 0
%24 = load i8*, i8** %23
%25 = call i64 @lstat(i8* %24, %struct.stat* %3)
%26 = icmp eq i64 %25, 0
br i1 %26, label %31, label %27
27: ; preds = %21
%28 = load i8*, i8** %23
%29 = call i32 @perror(i8* %28)
%30 = call i32 @exit(i32 1)
unreachable
31: ; preds = %21
br i1 %17, label %34, label %32
32: ; preds = %31
%33 = call i32 @do_preformat(%struct.stat* %3)
br label %37
34: ; preds = %31
%35 = load i8*, i8** %23
%36 = call i32 @do_stat(%struct.stat* %3, i8* %35)
br label %37
37: ; preds = %34, %32
%38 = call %struct.dirent* @readdir(i32* %11)
%39 = icmp eq %struct.dirent* %38, null
br i1 %39, label %40, label %21
40: ; preds = %37, %13
%41 = call i32 @closedir(i32* %11)
ret void
}