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

/* Variables and functions */
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_RDWR;
int O_TRUNC;
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ check_arg(int, char **);
int /*<<< orphan*/ check_file(int /*<<< orphan*/ *, char *);
int copy(int, int);
int /*<<< orphan*/ *getcwd(char *, int);
int open(char *, int, ...);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ usage(char *);
int main(int argc, char **argv) {
  if (argc != 3)
    usage(argv[0]);
  check_arg(argc, argv);
  DIR *dp;
  dp = opendir(argv[2]);
  check_file(dp, argv[1]);
  char src_path[64] = {0};
  char dst_path[64] = {0};
  getcwd(src_path, 64);
  chdir(argv[2]);
  if (getcwd(dst_path, 64) == NULL)
    perror("getcwd() wrong");
  int fd_src, fd_dst;
  chdir(src_path);
  fd_src = open(argv[1], O_RDONLY);
  chdir(dst_path);
  fd_dst = open(argv[1], O_RDWR | O_CREAT | O_TRUNC, 0644);
  int bytes = copy(fd_src, fd_dst);
  printf("%d bytes have been copied.\n", bytes);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     11df <main+0x2f>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   10b0 <usage@plt>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   10a0 <check_arg@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
call   1090 <check_file@plt>
lea    -0x60(%rbp),%rdi
xor    %esi,%esi
mov    $0x40,%edx
call   1060 <memset@plt>
lea    -0xa0(%rbp),%rdi
xor    %esi,%esi
mov    $0x40,%edx
call   1060 <memset@plt>
lea    -0x60(%rbp),%rdi
mov    $0x40,%esi
call   1070 <getcwd@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1040 <chdir@plt>
lea    -0xa0(%rbp),%rdi
mov    $0x40,%esi
call   1070 <getcwd@plt>
cmp    $0x0,%rax
jne    1272 <main+0xc2>
lea    0xd93(%rip),%rdi
call   10d0 <perror@plt>
lea    -0x60(%rbp),%rdi
call   1040 <chdir@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2d46(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   10c0 <open@plt>
mov    %eax,-0xa4(%rbp)
lea    -0xa0(%rbp),%rdi
call   1040 <chdir@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2d24(%rip),%rax
mov    (%rax),%esi
mov    0x2d3b(%rip),%rax
or     (%rax),%esi
mov    0x2d2a(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
mov    $0x0,%al
call   10c0 <open@plt>
mov    %eax,-0xa8(%rbp)
mov    -0xa4(%rbp),%edi
mov    -0xa8(%rbp),%esi
call   1080 <copy@plt>
mov    %eax,-0xac(%rbp)
mov    -0xac(%rbp),%esi
lea    0xd11(%rip),%rdi
call   1050 <printf@plt>
xor    %eax,%eax
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x88,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
cmp    $0x3,%edi
je     11bb <main+0x1b>
mov    (%rbx),%rdi
call   10a0 <usage@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
call   1090 <check_arg@plt>
mov    0x10(%rbx),%rdi
call   1030 <opendir@plt>
mov    0x8(%rbx),%rsi
mov    %rax,%rdi
call   1080 <check_file@plt>
xorps  %xmm0,%xmm0
movaps %xmm0,0x70(%rsp)
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
movaps %xmm0,(%rsp)
lea    0x40(%rsp),%rdi
mov    $0x40,%esi
call   1060 <getcwd@plt>
mov    0x10(%rbx),%rdi
call   1040 <chdir@plt>
mov    %rsp,%rdi
mov    $0x40,%esi
call   1060 <getcwd@plt>
test   %rax,%rax
je     12a4 <main+0x104>
lea    0x40(%rsp),%rdi
call   1040 <chdir@plt>
mov    0x8(%rbx),%rdi
mov    0x2d8d(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   10b0 <open@plt>
mov    %eax,%ebp
mov    %rsp,%rdi
call   1040 <chdir@plt>
mov    0x2d7b(%rip),%rax
mov    0x2d94(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x8(%rbx),%rdi
mov    0x2d7d(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
xor    %eax,%eax
call   10b0 <open@plt>
mov    %ebp,%edi
mov    %eax,%esi
call   1070 <copy@plt>
lea    0xd7e(%rip),%rdi
mov    %eax,%esi
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x88,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xd55(%rip),%rdi
call   10c0 <perror@plt>
jmp    122e <main+0x8e>

LLVM IR (-O0):
@.str = [15 x i8] c"getcwd() wrong\00"
@O_RDONLY = global i32 0
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.1 = [28 x i8] c"%d bytes have been copied.\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca [64 x i8]
%8 = alloca [64 x i8]
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* %4
%13 = icmp ne i32 %12, 3
br i1 %13, label %14, label %19
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 0
%17 = load i8*, i8** %16
%18 = call i32 @usage(i8* %17)
br label %19
19: ; preds = %14, %2
%20 = load i32, i32* %4
%21 = load i8**, i8*** %5
%22 = call i32 @check_arg(i32 %20, i8** %21)
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 2
%25 = load i8*, i8** %24
%26 = call i32* @opendir(i8* %25)
store i32* %26, i32** %6
%27 = load i32*, i32** %6
%28 = load i8**, i8*** %5
%29 = getelementptr inbounds i8*, i8** %28, i64 1
%30 = load i8*, i8** %29
%31 = call i32 @check_file(i32* %27, i8* %30)
%32 = bitcast [64 x i8]* %7 to i8*
%33 = bitcast [64 x i8]* %8 to i8*
%34 = getelementptr inbounds [64 x i8], [64 x i8]* %7, i64 0, i64 0
%35 = call i32* @getcwd(i8* %34, i32 64)
%36 = load i8**, i8*** %5
%37 = getelementptr inbounds i8*, i8** %36, i64 2
%38 = load i8*, i8** %37
%39 = call i32 @chdir(i8* %38)
%40 = getelementptr inbounds [64 x i8], [64 x i8]* %8, i64 0, i64 0
%41 = call i32* @getcwd(i8* %40, i32 64)
%42 = icmp eq i32* %41, null
br i1 %42, label %43, label %45
43: ; preds = %19
%44 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %45
45: ; preds = %43, %19
%46 = getelementptr inbounds [64 x i8], [64 x i8]* %7, i64 0, i64 0
%47 = call i32 @chdir(i8* %46)
%48 = load i8**, i8*** %5
%49 = getelementptr inbounds i8*, i8** %48, i64 1
%50 = load i8*, i8** %49
%51 = load i32, i32* @O_RDONLY
%52 = call i32 (i8*, i32, ...) @open(i8* %50, i32 %51)
store i32 %52, i32* %9
%53 = getelementptr inbounds [64 x i8], [64 x i8]* %8, i64 0, i64 0
%54 = call i32 @chdir(i8* %53)
%55 = load i8**, i8*** %5
%56 = getelementptr inbounds i8*, i8** %55, i64 1
%57 = load i8*, i8** %56
%58 = load i32, i32* @O_RDWR
%59 = load i32, i32* @O_CREAT
%60 = or i32 %58, %59
%61 = load i32, i32* @O_TRUNC
%62 = or i32 %60, %61
%63 = call i32 (i8*, i32, ...) @open(i8* %57, i32 %62, i32 420)
store i32 %63, i32* %10
%64 = load i32, i32* %9
%65 = load i32, i32* %10
%66 = call i32 @copy(i32 %64, i32 %65)
store i32 %66, i32* %11
%67 = load i32, i32* %11
%68 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 %67)
ret i32 0
}

LLVM IR (-O2):
@.str = [15 x i8] c"getcwd() wrong\00"
@O_RDONLY = global i32 0
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.1 = [28 x i8] c"%d bytes have been copied.\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca [64 x i8]
%4 = alloca [64 x i8]
%5 = icmp eq i32 %0, 3
br i1 %5, label %9, label %6
6: ; preds = %2
%7 = load i8*, i8** %1
%8 = tail call i32 @usage(i8* %7)
br label %9
9: ; preds = %6, %2
%10 = tail call i32 @check_arg(i32 %0, i8** %1)
%11 = getelementptr inbounds i8*, i8** %1, i64 2
%12 = load i8*, i8** %11
%13 = tail call i32* @opendir(i8* %12)
%14 = getelementptr inbounds i8*, i8** %1, i64 1
%15 = load i8*, i8** %14
%16 = tail call i32 @check_file(i32* %13, i8* %15)
%17 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%18 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%19 = call i32* @getcwd(i8* %17, i32 64)
%20 = load i8*, i8** %11
%21 = call i32 @chdir(i8* %20)
%22 = call i32* @getcwd(i8* %18, i32 64)
%23 = icmp eq i32* %22, null
br i1 %23, label %24, label %26
24: ; preds = %9
%25 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %26
26: ; preds = %24, %9
%27 = call i32 @chdir(i8* %17)
%28 = load i8*, i8** %14
%29 = load i32, i32* @O_RDONLY
%30 = call i32 (i8*, i32, ...) @open(i8* %28, i32 %29)
%31 = call i32 @chdir(i8* %18)
%32 = load i8*, i8** %14
%33 = load i32, i32* @O_RDWR
%34 = load i32, i32* @O_CREAT
%35 = or i32 %34, %33
%36 = load i32, i32* @O_TRUNC
%37 = or i32 %35, %36
%38 = call i32 (i8*, i32, ...) @open(i8* %32, i32 %37, i32 420)
%39 = call i32 @copy(i32 %30, i32 %38)
%40 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 %39)
ret i32 0
}