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

/* Variables and functions */
scalar_t__ DT_DIR;
int PATH_MAX;
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
scalar_t__ errno;
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ perror(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, int /*<<< orphan*/);
scalar_t__ strcmp(int /*<<< orphan*/, char *);
int /*<<< orphan*/ write_dir_header(char *, int);
int /*<<< orphan*/ write_file_to_arch(char *, int);
void add_to_arch(char *path, int arch_fd) {
  DIR *dir;
  struct dirent *dp;
  char path_next[PATH_MAX], file_name[PATH_MAX];
  write_dir_header(path, arch_fd);
  dir = opendir(path);
  if (dir == NULL) {
    perror("Couldn't open");
    return;
  }
  do {
    errno = 0;
    dp = readdir(dir);
    if (dp != NULL) {
      if (strcmp(dp->d_name, ".") == 0 || strcmp(dp->d_name, "..") == 0)
        continue;
      if (dp->d_type == DT_DIR) {
        snprintf(path_next, sizeof(path_next), "%s%s/", path, dp->d_name);
        add_to_arch(path_next, arch_fd);
      } else {
        snprintf(file_name, sizeof(file_name), "%s%s", path, dp->d_name);
        write_file_to_arch(file_name, arch_fd);
      }
    }
  } while (dp != NULL);
  if (errno != 0) {
    perror("error reading directory");
    return;
  }
  closedir(dir);
}


x86 assembly (clang, -O0):
<add_to_arch>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    0x2e4a(%rip),%rax
mov    (%rax),%ecx
mov    %rcx,-0x60(%rbp)
mov    %rsp,%rdx
mov    %rdx,-0x28(%rbp)
mov    %rcx,%rsi
add    $0xf,%rsi
and    $0xfffffffffffffff0,%rsi
mov    %rsp,%rdx
sub    %rsi,%rdx
mov    %rdx,-0x58(%rbp)
mov    %rdx,%rsp
mov    %rcx,-0x30(%rbp)
mov    (%rax),%eax
mov    %rax,-0x50(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1040 <write_dir_header@plt>
mov    -0x8(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    122d <add_to_arch+0x9d>
lea    0xde4(%rip),%rdi
call   10b0 <perror@plt>
movl   $0x1,-0x3c(%rbp)
jmp    135d <add_to_arch+0x1cd>
jmp    1232 <add_to_arch+0xa2>
mov    0x2d97(%rip),%rax
movq   $0x0,(%rax)
mov    -0x18(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1314 <add_to_arch+0x184>
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%edi
lea    0xda9(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
je     1292 <add_to_arch+0x102>
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%edi
lea    0xd8b(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    1297 <add_to_arch+0x107>
jmp    1319 <add_to_arch+0x189>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    0x2d33(%rip),%rcx
cmp    (%rcx),%rax
jne    12e1 <add_to_arch+0x151>
mov    -0x58(%rbp),%rdi
mov    -0x60(%rbp),%rax
mov    %eax,%esi
mov    -0x8(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%r8d
lea    0xd46(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x58(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1090 <add_to_arch@plt>
jmp    130f <add_to_arch+0x17f>
mov    -0x48(%rbp),%rdi
mov    -0x50(%rbp),%rax
mov    %eax,%esi
mov    -0x8(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%r8d
lea    0xd19(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x48(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   10a0 <write_file_to_arch@plt>
jmp    1314 <add_to_arch+0x184>
jmp    1319 <add_to_arch+0x189>
cmpq   $0x0,-0x20(%rbp)
jne    1232 <add_to_arch+0xa2>
mov    0x2ca5(%rip),%rax
cmpq   $0x0,(%rax)
je     134d <add_to_arch+0x1bd>
lea    0xce0(%rip),%rdi
call   10b0 <perror@plt>
movl   $0x1,-0x3c(%rbp)
jmp    135d <add_to_arch+0x1cd>
mov    -0x18(%rbp),%rdi
call   1060 <closedir@plt>
movl   $0x0,-0x3c(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<add_to_arch>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdi,%r14
mov    0x2e45(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rax,-0x38(%rbp)
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rcx
mov    %rcx,-0x50(%rbp)
mov    %rcx,%rsp
mov    %rsp,%rcx
sub    %rax,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rcx,%rsp
mov    %esi,-0x2c(%rbp)
call   1040 <write_dir_header@plt>
mov    %r14,-0x40(%rbp)
mov    %r14,%rdi
call   1030 <opendir@plt>
test   %rax,%rax
je     12f2 <add_to_arch+0x162>
mov    %rax,%r14
mov    0x2dd6(%rip),%r13
movq   $0x0,0x0(%r13)
mov    %rax,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
je     12d4 <add_to_arch+0x144>
mov    %rax,%r12
lea    0xdf2(%rip),%r15
jmp    1268 <add_to_arch+0xd8>
mov    -0x48(%rbp),%rbx
mov    %rbx,%rdi
mov    -0x38(%rbp),%rsi
lea    0xde6(%rip),%rdx
mov    -0x40(%rbp),%rcx
call   1050 <snprintf@plt>
mov    %rbx,%rdi
mov    -0x2c(%rbp),%esi
call   10a0 <write_file_to_arch@plt>
cs nopw 0x0(%rax,%rax,1)
nop
movq   $0x0,0x0(%r13)
mov    %r14,%rdi
call   1080 <readdir@plt>
mov    %rax,%r12
test   %rax,%rax
je     12d4 <add_to_arch+0x144>
mov    0x8(%r12),%edi
mov    %r15,%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
je     1250 <add_to_arch+0xc0>
mov    0x8(%r12),%edi
lea    0xd88(%rip),%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
je     1250 <add_to_arch+0xc0>
mov    (%r12),%rax
mov    0x8(%r12),%r8d
mov    0x2d38(%rip),%rcx
cmp    (%rcx),%rax
jne    121f <add_to_arch+0x8f>
mov    -0x50(%rbp),%rbx
mov    %rbx,%rdi
mov    -0x38(%rbp),%rsi
lea    0xd56(%rip),%rdx
mov    -0x40(%rbp),%rcx
call   1050 <snprintf@plt>
mov    %rbx,%rdi
mov    -0x2c(%rbp),%esi
call   1090 <add_to_arch@plt>
jmp    1250 <add_to_arch+0xc0>
cmpq   $0x0,0x0(%r13)
jne    130c <add_to_arch+0x17c>
mov    %r14,%rdi
call   1060 <closedir@plt>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd07(%rip),%rdi
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    10b0 <perror@plt>
lea    0xd09(%rip),%rdi
call   10b0 <perror@plt>
jmp    12e3 <add_to_arch+0x153>

LLVM IR (-O0):
%struct.dirent = type { i64, i32 }
@PATH_MAX = global i32 0
@.str = [14 x i8] c"Couldn't open\00"
@errno = global i64 0
@.str.1 = [2 x i8] c".\00"
@.str.2 = [3 x i8] c"..\00"
@DT_DIR = global i64 0
@.str.3 = [6 x i8] c"%s%s/\00"
@.str.4 = [5 x i8] c"%s%s\00"
@.str.5 = [24 x i8] c"error reading directory\00"
define void @add_to_arch(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32*
%6 = alloca %struct.dirent*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i64
%10 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%11 = load i32, i32* @PATH_MAX
%12 = zext i32 %11 to i64
%13 = call i8* @llvm.stacksave()
store i8* %13, i8** %7
%14 = alloca i8, i64 %12
store i64 %12, i64* %8
%15 = load i32, i32* @PATH_MAX
%16 = zext i32 %15 to i64
%17 = alloca i8, i64 %16
store i64 %16, i64* %9
%18 = load i8*, i8** %3
%19 = load i32, i32* %4
%20 = call i32 @write_dir_header(i8* %18, i32 %19)
%21 = load i8*, i8** %3
%22 = call i32* @opendir(i8* %21)
store i32* %22, i32** %5
%23 = load i32*, i32** %5
%24 = icmp eq i32* %23, null
br i1 %24, label %25, label %27
25: ; preds = %2
%26 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %10
br label %82
27: ; preds = %2
br label %28
28: ; preds = %71, %27
store i64 0, i64* @errno
%29 = load i32*, i32** %5
%30 = call %struct.dirent* @readdir(i32* %29)
store %struct.dirent* %30, %struct.dirent** %6
%31 = load %struct.dirent*, %struct.dirent** %6
%32 = icmp ne %struct.dirent* %31, null
br i1 %32, label %33, label %70
33: ; preds = %28
%34 = load %struct.dirent*, %struct.dirent** %6
%35 = getelementptr inbounds %struct.dirent, %struct.dirent* %34, i32 0, i32 1
%36 = load i32, i32* %35
%37 = call i64 @strcmp(i32 %36, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%38 = icmp eq i64 %37, 0
br i1 %38, label %45, label %39
39: ; preds = %33
%40 = load %struct.dirent*, %struct.dirent** %6
%41 = getelementptr inbounds %struct.dirent, %struct.dirent* %40, i32 0, i32 1
%42 = load i32, i32* %41
%43 = call i64 @strcmp(i32 %42, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%44 = icmp eq i64 %43, 0
br i1 %44, label %45, label %46
45: ; preds = %39, %33
br label %71
46: ; preds = %39
%47 = load %struct.dirent*, %struct.dirent** %6
%48 = getelementptr inbounds %struct.dirent, %struct.dirent* %47, i32 0, i32 0
%49 = load i64, i64* %48
%50 = load i64, i64* @DT_DIR
%51 = icmp eq i64 %49, %50
br i1 %51, label %52, label %60
52: ; preds = %46
%53 = trunc i64 %12 to i32
%54 = load i8*, i8** %3
%55 = load %struct.dirent*, %struct.dirent** %6
%56 = getelementptr inbounds %struct.dirent, %struct.dirent* %55, i32 0, i32 1
%57 = load i32, i32* %56
%58 = call i32 @snprintf(i8* %14, i32 %53, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %54, i32 %57)
%59 = load i32, i32* %4
br label %69
60: ; preds = %46
%61 = trunc i64 %16 to i32
%62 = load i8*, i8** %3
%63 = load %struct.dirent*, %struct.dirent** %6
%64 = getelementptr inbounds %struct.dirent, %struct.dirent* %63, i32 0, i32 1
%65 = load i32, i32* %64
%66 = call i32 @snprintf(i8* %17, i32 %61, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i8* %62, i32 %65)
%67 = load i32, i32* %4
%68 = call i32 @write_file_to_arch(i8* %17, i32 %67)
br label %69
69: ; preds = %60, %52
br label %70
70: ; preds = %69, %28
br label %71
71: ; preds = %70, %45
%72 = load %struct.dirent*, %struct.dirent** %6
%73 = icmp ne %struct.dirent* %72, null
br i1 %73, label %28, label %74
74: ; preds = %71
%75 = load i64, i64* @errno
%76 = icmp ne i64 %75, 0
br i1 %76, label %77, label %79
77: ; preds = %74
%78 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %10
br label %82
79: ; preds = %74
%80 = load i32*, i32** %5
%81 = call i32 @closedir(i32* %80)
store i32 0, i32* %10
br label %82
82: ; preds = %79, %77, %25
%83 = load i8*, i8** %7
%84 = load i32, i32* %10
switch i32 %84, label %86 [
i32 0, label %85
i32 1, label %85
]
85: ; preds = %82, %82
ret void
86: ; preds = %82
unreachable
}

LLVM IR (-O2):
%struct.dirent = type { i64, i32 }
@PATH_MAX = global i32 0
@.str = [14 x i8] c"Couldn't open\00"
@errno = global i64 0
@.str.1 = [2 x i8] c".\00"
@.str.2 = [3 x i8] c"..\00"
@DT_DIR = global i64 0
@.str.3 = [6 x i8] c"%s%s/\00"
@.str.4 = [5 x i8] c"%s%s\00"
@.str.5 = [24 x i8] c"error reading directory\00"
define void @add_to_arch(i8* %0, i32 %1) {
%3 = load i32, i32* @PATH_MAX
%4 = zext i32 %3 to i64
%5 = alloca i8, i64 %4
%6 = alloca i8, i64 %4
%7 = tail call i32 @write_dir_header(i8* %0, i32 %1)
%8 = tail call i32* @opendir(i8* %0)
%9 = icmp eq i32* %8, null
br i1 %9, label %13, label %10
10: ; preds = %2
store i64 0, i64* @errno
%11 = call %struct.dirent* @readdir(i32* %8)
%12 = icmp eq %struct.dirent* %11, null
br i1 %12, label %39, label %15
13: ; preds = %2
%14 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %46
15: ; preds = %10, %36
%16 = phi %struct.dirent* [ %37, %36 ], [ %11, %10 ]
%17 = getelementptr inbounds %struct.dirent, %struct.dirent* %16, i64 0, i32 1
%18 = load i32, i32* %17
%19 = call i64 @strcmp(i32 %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%20 = icmp eq i64 %19, 0
br i1 %20, label %36, label %21
21: ; preds = %15
%22 = load i32, i32* %17
%23 = call i64 @strcmp(i32 %22, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%24 = icmp eq i64 %23, 0
br i1 %24, label %36, label %25
25: ; preds = %21
%26 = getelementptr inbounds %struct.dirent, %struct.dirent* %16, i64 0, i32 0
%27 = load i64, i64* %26
%28 = load i64, i64* @DT_DIR
%29 = icmp eq i64 %27, %28
%30 = load i32, i32* %17
br i1 %29, label %31, label %33
31: ; preds = %25
%32 = call i32 @snprintf(i8* %5, i32 %3, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %0, i32 %30)
br label %36
33: ; preds = %25
%34 = call i32 @snprintf(i8* %6, i32 %3, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i8* %0, i32 %30)
%35 = call i32 @write_file_to_arch(i8* %6, i32 %1)
br label %36
36: ; preds = %33, %31, %15, %21
store i64 0, i64* @errno
%37 = call %struct.dirent* @readdir(i32* %8)
%38 = icmp eq %struct.dirent* %37, null
br i1 %38, label %39, label %15
39: ; preds = %36, %10
%40 = load i64, i64* @errno
%41 = icmp eq i64 %40, 0
br i1 %41, label %44, label %42
42: ; preds = %39
%43 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0))
br label %46
44: ; preds = %39
%45 = call i32 @closedir(i32* %8)
br label %46
46: ; preds = %44, %42, %13
ret void
}