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

/* Variables and functions */
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ error(char *);
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(size_t);
int /*<<< orphan*/ *opendir(char const *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ scan_archive(char const *);
int /*<<< orphan*/ strcat(char *, char *);
scalar_t__ strcmp(char const *, char *);
int /*<<< orphan*/ strcpy(char *, char const *);
int strlen(char const *);
void scan_dir_or_archive(const char path[]) {
  DIR *dir = opendir(path);
  if (dir == NULL) {
    scan_archive(path);
  } else {
    struct dirent *entry;
    while ((entry = readdir(dir)) != NULL) {
      if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) {
        size_t len = strlen(path) + 1 + strlen(entry->d_name);
        char *epath = (char *)malloc(len + 1);
        if (epath == NULL)
          error("Out of memory");
        strcpy(epath, path);
        strcat(epath, "/");
        strcat(epath, entry->d_name);
        scan_dir_or_archive(epath);
        free(epath);
      }
    }
    closedir(dir);
  }
}


x86 assembly (clang, -O0):
<scan_dir_or_archive>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <opendir@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11f2 <scan_dir_or_archive+0x32>
mov    -0x8(%rbp),%rdi
call   1040 <scan_archive@plt>
jmp    12e5 <scan_dir_or_archive+0x125>
jmp    11f7 <scan_dir_or_archive+0x37>
mov    -0x10(%rbp),%rdi
call   10a0 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     12dc <scan_dir_or_archive+0x11c>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
lea    0xde5(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
je     12d7 <scan_dir_or_archive+0x117>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
lea    0xdc7(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
je     12d7 <scan_dir_or_archive+0x117>
mov    -0x8(%rbp),%rdi
call   1070 <strlen@plt>
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
call   1070 <strlen@plt>
mov    %eax,%ecx
mov    -0x2c(%rbp),%eax
add    %ecx,%eax
cltq
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
add    $0x1,%rdi
call   10b0 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1298 <scan_dir_or_archive+0xd8>
lea    0xd70(%rip),%rdi
call   10d0 <error@plt>
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1050 <strcpy@plt>
mov    -0x28(%rbp),%rdi
lea    0xd61(%rip),%rsi
call   10e0 <strcat@plt>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
call   10e0 <strcat@plt>
mov    -0x28(%rbp),%rdi
call   10c0 <scan_dir_or_archive@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
jmp    11f7 <scan_dir_or_archive+0x37>
mov    -0x10(%rbp),%rdi
call   1080 <closedir@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<scan_dir_or_archive>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
call   1060 <opendir@plt>
test   %rax,%rax
je     12cf <scan_dir_or_archive+0x10f>
mov    %rax,%rbx
mov    %rax,%rdi
call   10a0 <readdir@plt>
test   %rax,%rax
je     12b9 <scan_dir_or_archive+0xf9>
mov    %rax,%r13
lea    0xe07(%rip),%r15
lea    0xdff(%rip),%r12
jmp    1256 <scan_dir_or_archive+0x96>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rbp,%rdi
mov    %r14,%rsi
call   1050 <strcpy@plt>
mov    %rbp,%rdi
lea    0xdec(%rip),%rsi
call   10e0 <strcat@plt>
mov    0x0(%r13),%rsi
mov    %rbp,%rdi
call   10e0 <strcat@plt>
mov    %rbp,%rdi
call   10c0 <scan_dir_or_archive@plt>
mov    %rbp,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   10a0 <readdir@plt>
mov    %rax,%r13
test   %rax,%rax
je     12b9 <scan_dir_or_archive+0xf9>
mov    0x0(%r13),%rdi
mov    %r15,%rsi
call   1090 <strcmp@plt>
test   %rax,%rax
je     1246 <scan_dir_or_archive+0x86>
mov    0x0(%r13),%rdi
mov    %r12,%rsi
call   1090 <strcmp@plt>
test   %rax,%rax
je     1246 <scan_dir_or_archive+0x86>
mov    %r14,%rdi
call   1070 <strlen@plt>
mov    %eax,%ebp
mov    0x0(%r13),%rdi
call   1070 <strlen@plt>
add    %ebp,%eax
add    $0x1,%eax
movslq %eax,%rdi
add    $0x1,%rdi
call   10b0 <malloc@plt>
mov    %rax,%rbp
test   %rax,%rax
jne    1210 <scan_dir_or_archive+0x50>
lea    0xd54(%rip),%rdi
call   10d0 <error@plt>
jmp    1210 <scan_dir_or_archive+0x50>
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1080 <closedir@plt>
mov    %r14,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <scan_archive@plt>

LLVM IR (-O0):
%struct.dirent = type { i8* }
@.str = [2 x i8] c".\00"
@.str.1 = [3 x i8] c"..\00"
@.str.2 = [14 x i8] c"Out of memory\00"
@.str.3 = [2 x i8] c"/\00"
define void @scan_dir_or_archive(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca %struct.dirent*
%5 = alloca i64
%6 = alloca i8*
store i8* %0, i8** %2
%7 = load i8*, i8** %2
%8 = call i32* @opendir(i8* %7)
store i32* %8, i32** %3
%9 = load i32*, i32** %3
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %14
11: ; preds = %1
%12 = load i8*, i8** %2
%13 = call i32 @scan_archive(i8* %12)
br label %67
14: ; preds = %1
br label %15
15: ; preds = %63, %14
%16 = load i32*, i32** %3
%17 = call %struct.dirent* @readdir(i32* %16)
store %struct.dirent* %17, %struct.dirent** %4
%18 = icmp ne %struct.dirent* %17, null
br i1 %18, label %19, label %64
19: ; preds = %15
%20 = load %struct.dirent*, %struct.dirent** %4
%21 = getelementptr inbounds %struct.dirent, %struct.dirent* %20, i32 0, i32 0
%22 = load i8*, i8** %21
%23 = call i64 @strcmp(i8* %22, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %63
25: ; preds = %19
%26 = load %struct.dirent*, %struct.dirent** %4
%27 = getelementptr inbounds %struct.dirent, %struct.dirent* %26, i32 0, i32 0
%28 = load i8*, i8** %27
%29 = call i64 @strcmp(i8* %28, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%30 = icmp ne i64 %29, 0
br i1 %30, label %31, label %63
31: ; preds = %25
%32 = load i8*, i8** %2
%33 = call i32 @strlen(i8* %32)
%34 = add nsw i32 %33, 1
%35 = load %struct.dirent*, %struct.dirent** %4
%36 = getelementptr inbounds %struct.dirent, %struct.dirent* %35, i32 0, i32 0
%37 = load i8*, i8** %36
%38 = call i32 @strlen(i8* %37)
%39 = add nsw i32 %34, %38
%40 = sext i32 %39 to i64
store i64 %40, i64* %5
%41 = load i64, i64* %5
%42 = add i64 %41, 1
%43 = call i64 @malloc(i64 %42)
%44 = inttoptr i64 %43 to i8*
store i8* %44, i8** %6
%45 = load i8*, i8** %6
%46 = icmp eq i8* %45, null
br i1 %46, label %47, label %49
47: ; preds = %31
%48 = call i32 @error(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %49
49: ; preds = %47, %31
%50 = load i8*, i8** %6
%51 = load i8*, i8** %2
%52 = call i32 @strcpy(i8* %50, i8* %51)
%53 = load i8*, i8** %6
%54 = call i32 @strcat(i8* %53, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%55 = load i8*, i8** %6
%56 = load %struct.dirent*, %struct.dirent** %4
%57 = getelementptr inbounds %struct.dirent, %struct.dirent* %56, i32 0, i32 0
%58 = load i8*, i8** %57
%59 = call i32 @strcat(i8* %55, i8* %58)
%60 = load i8*, i8** %6
%61 = load i8*, i8** %6
%62 = call i32 @free(i8* %61)
br label %63
63: ; preds = %49, %25, %19
br label %15
64: ; preds = %15
%65 = load i32*, i32** %3
%66 = call i32 @closedir(i32* %65)
br label %67
67: ; preds = %64, %11
ret void
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [2 x i8] c".\00"
@.str.1 = [3 x i8] c"..\00"
@.str.2 = [14 x i8] c"Out of memory\00"
@.str.3 = [2 x i8] c"/\00"
define void @scan_dir_or_archive(i8* %0) {
%2 = tail call i32* @opendir(i8* %0)
%3 = icmp eq i32* %2, null
br i1 %3, label %7, label %4
4: ; preds = %1
%5 = tail call %struct.dirent* @readdir(i32* %2)
%6 = icmp eq %struct.dirent* %5, null
br i1 %6, label %41, label %9
7: ; preds = %1
%8 = tail call i32 @scan_archive(i8* %0)
br label %43
9: ; preds = %4, %38
%10 = phi %struct.dirent* [ %39, %38 ], [ %5, %4 ]
%11 = getelementptr inbounds %struct.dirent, %struct.dirent* %10, i64 0, i32 0
%12 = load i8*, i8** %11
%13 = tail call i64 @strcmp(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%14 = icmp eq i64 %13, 0
br i1 %14, label %38, label %15
15: ; preds = %9
%16 = load i8*, i8** %11
%17 = tail call i64 @strcmp(i8* %16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%18 = icmp eq i64 %17, 0
br i1 %18, label %38, label %19
19: ; preds = %15
%20 = tail call i32 @strlen(i8* %0)
%21 = add nsw i32 %20, 1
%22 = load i8*, i8** %11
%23 = tail call i32 @strlen(i8* %22)
%24 = add nsw i32 %21, %23
%25 = sext i32 %24 to i64
%26 = add nsw i64 %25, 1
%27 = tail call i64 @malloc(i64 %26)
%28 = inttoptr i64 %27 to i8*
%29 = icmp eq i64 %27, 0
br i1 %29, label %30, label %32
30: ; preds = %19
%31 = tail call i32 @error(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %32
32: ; preds = %30, %19
%33 = tail call i32 @strcpy(i8* %28, i8* %0)
%34 = tail call i32 @strcat(i8* %28, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%35 = load i8*, i8** %11
%36 = tail call i32 @strcat(i8* %28, i8* %35)
tail call void @scan_dir_or_archive(i8* %28)
%37 = tail call i32 @free(i8* %28)
br label %38
38: ; preds = %32, %15, %9
%39 = tail call %struct.dirent* @readdir(i32* %2)
%40 = icmp eq %struct.dirent* %39, null
br i1 %40, label %41, label %9
41: ; preds = %38, %4
%42 = tail call i32 @closedir(i32* %2)
br label %43
43: ; preds = %41, %7
ret void
}