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

/* Variables and functions */
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ cp(char *, char *);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ perror(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, char *, ...);
int copy_from_dir(char *name) {
  DIR *dir;
  struct dirent *ent;
  dir = opendir(name);
  if (dir != NULL) {
    while ((ent = readdir(dir)) != NULL) {
      char source[4096], dest[4096];
      sprintf(source, "%s/%s", name, ent->d_name);
      sprintf(dest, "./%s", ent->d_name);
      cp(source, dest);
    }
    closedir(dir);
  } else {
    perror("oops!");
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<copy_from_dir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2020,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1205 <copy_from_dir+0xa5>
jmp    118c <copy_from_dir+0x2c>
mov    -0x18(%rbp),%rdi
call   1060 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     11f7 <copy_from_dir+0x97>
lea    -0x1020(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x20(%rbp),%rax
mov    (%rax),%rcx
lea    0xe44(%rip),%rsi
mov    $0x0,%al
call   1080 <sprintf@plt>
lea    -0x2020(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    (%rax),%rdx
lea    0xe2e(%rip),%rsi
mov    $0x0,%al
call   1080 <sprintf@plt>
lea    -0x1020(%rbp),%rdi
lea    -0x2020(%rbp),%rsi
call   1040 <cp@plt>
jmp    118c <copy_from_dir+0x2c>
mov    -0x18(%rbp),%rdi
call   1050 <closedir@plt>
jmp    121d <copy_from_dir+0xbd>
lea    0xdff(%rip),%rdi
call   1070 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1224 <copy_from_dir+0xc4>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x2020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_from_dir>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x2008,%rsp
mov    %rdi,%r14
call   1030 <opendir@plt>
test   %rax,%rax
je     120e <copy_from_dir+0xae>
mov    %rax,%r12
mov    %rax,%rdi
call   1060 <readdir@plt>
test   %rax,%rax
je     11f2 <copy_from_dir+0x92>
mov    %rax,%r15
lea    0x1000(%rsp),%rbx
lea    0xe62(%rip),%r13
mov    %rsp,%rbp
nopw   0x0(%rax,%rax,1)
mov    (%r15),%rcx
mov    %rbx,%rdi
lea    0xe43(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1080 <sprintf@plt>
mov    (%r15),%rdx
mov    %rbp,%rdi
mov    %r13,%rsi
xor    %eax,%eax
call   1080 <sprintf@plt>
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1040 <cp@plt>
mov    %r12,%rdi
call   1060 <readdir@plt>
mov    %rax,%r15
test   %rax,%rax
jne    11b0 <copy_from_dir+0x50>
mov    %r12,%rdi
call   1050 <closedir@plt>
xor    %eax,%eax
add    $0x2008,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdf6(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%eax
jmp    11fc <copy_from_dir+0x9c>

LLVM IR (-O0):
%struct.dirent = type { i8* }
@.str = [6 x i8] c"%s/%s\00"
@.str.1 = [5 x i8] c"./%s\00"
@.str.2 = [6 x i8] c"oops!\00"
define i32 @copy_from_dir(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca %struct.dirent*
%6 = alloca [4096 x i8]
%7 = alloca [4096 x i8]
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = call i32* @opendir(i8* %8)
store i32* %9, i32** %4
%10 = load i32*, i32** %4
%11 = icmp ne i32* %10, null
br i1 %11, label %12, label %35
12: ; preds = %1
br label %13
13: ; preds = %17, %12
%14 = load i32*, i32** %4
%15 = call %struct.dirent* @readdir(i32* %14)
store %struct.dirent* %15, %struct.dirent** %5
%16 = icmp ne %struct.dirent* %15, null
br i1 %16, label %17, label %32
17: ; preds = %13
%18 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%19 = load i8*, i8** %3
%20 = load %struct.dirent*, %struct.dirent** %5
%21 = getelementptr inbounds %struct.dirent, %struct.dirent* %20, i32 0, i32 0
%22 = load i8*, i8** %21
%23 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %18, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %19, i8* %22)
%24 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%25 = load %struct.dirent*, %struct.dirent** %5
%26 = getelementptr inbounds %struct.dirent, %struct.dirent* %25, i32 0, i32 0
%27 = load i8*, i8** %26
%28 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %24, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %27)
%29 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%30 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%31 = call i32 @cp(i8* %29, i8* %30)
br label %13
32: ; preds = %13
%33 = load i32*, i32** %4
%34 = call i32 @closedir(i32* %33)
br label %37
35: ; preds = %1
%36 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %2
br label %38
37: ; preds = %32
store i32 0, i32* %2
br label %38
38: ; preds = %37, %35
%39 = load i32, i32* %2
ret i32 %39
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [6 x i8] c"%s/%s\00"
@.str.1 = [5 x i8] c"./%s\00"
@.str.2 = [6 x i8] c"oops!\00"
define i32 @copy_from_dir(i8* %0) {
%2 = alloca [4096 x i8]
%3 = alloca [4096 x i8]
%4 = tail call i32* @opendir(i8* %0)
%5 = icmp eq i32* %4, null
br i1 %5, label %24, label %6
6: ; preds = %1
%7 = call %struct.dirent* @readdir(i32* %4)
%8 = icmp eq %struct.dirent* %7, null
br i1 %8, label %22, label %9
9: ; preds = %6
%10 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
%11 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
br label %12
12: ; preds = %9, %12
%13 = phi %struct.dirent* [ %7, %9 ], [ %20, %12 ]
%14 = getelementptr inbounds %struct.dirent, %struct.dirent* %13, i64 0, i32 0
%15 = load i8*, i8** %14
%16 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %10, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %15)
%17 = load i8*, i8** %14
%18 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %11, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %17)
%19 = call i32 @cp(i8* %10, i8* %11)
%20 = call %struct.dirent* @readdir(i32* %4)
%21 = icmp eq %struct.dirent* %20, null
br i1 %21, label %22, label %12
22: ; preds = %12, %6
%23 = call i32 @closedir(i32* %4)
br label %26
24: ; preds = %1
%25 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %26
26: ; preds = %22, %24
%27 = phi i32 [ 0, %22 ], [ 1, %24 ]
ret i32 %27
}