#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*/ FILE;

/* Variables and functions */
int PATH_MAX;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ get_short_filename(char *, char *);
scalar_t__ is_dir(char const *);
int /*<<< orphan*/ printf(char *, char const *, char *);
int /*<<< orphan*/ sprintf(char *, char *, char const *, char *);
char *strerror(int /*<<< orphan*/);
int cp_file(const char *source, const char *dest) {
  FILE *fp_source;
  FILE *fp_dest;
  char short_filename[PATH_MAX];
  char dest_filename[PATH_MAX];
  int nb_bytes;
  char buffer[512];
  if (source == NULL || dest == NULL)
    return -1;
  fp_source = fopen(source, "rb");
  if (fp_source == NULL) {
    printf("Could not open file %s : %s\n", source, strerror(errno));
    return -1;
  }
  if (is_dir(dest)) {
    get_short_filename((char *)source, short_filename);
    sprintf(dest_filename, "%s/%s", dest, short_filename);
    fp_dest = fopen(dest_filename, "w+");
  } else {
    fp_dest = fopen(dest, "wb");
  }
  if (fp_dest == NULL) {
    printf("Could not open file %s : %s\n", dest, strerror(errno));
    fclose(fp_source);
    return -1;
  }
  while ((nb_bytes = fread(buffer, 1, 512, fp_source)) != 0)
    fwrite(buffer, 1, nb_bytes, fp_dest);
  fclose(fp_source);
  fclose(fp_dest);
  return 0;
}


x86 assembly (clang, -O0):
<cp_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x280,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e46(%rip),%rax
mov    (%rax),%ecx
mov    %rsp,%rdx
mov    %rdx,-0x30(%rbp)
mov    %rcx,%rsi
add    $0xf,%rsi
and    $0xfffffffffffffff0,%rsi
mov    %rsp,%rdx
sub    %rsi,%rdx
mov    %rdx,-0x268(%rbp)
mov    %rdx,%rsp
mov    %rcx,-0x38(%rbp)
mov    (%rax),%eax
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x260(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1209 <cp_file+0x79>
cmpq   $0x0,-0x18(%rbp)
jne    121f <cp_file+0x8f>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x254(%rbp)
jmp    13b8 <cp_file+0x228>
mov    -0x10(%rbp),%rdi
lea    0xdd6(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1283 <cp_file+0xf3>
mov    -0x10(%rbp),%rax
mov    %rax,-0x270(%rbp)
mov    0x2d88(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x270(%rbp),%rsi
mov    %rax,%rdx
lea    0xd9b(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x254(%rbp)
jmp    13b8 <cp_file+0x228>
mov    -0x18(%rbp),%rdi
call   1060 <is_dir@plt>
cmp    $0x0,%rax
je     12e0 <cp_file+0x150>
mov    -0x268(%rbp),%rsi
mov    -0x10(%rbp),%rdi
call   1070 <get_short_filename@plt>
mov    -0x268(%rbp),%rcx
mov    -0x260(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xd61(%rip),%rsi
call   1090 <sprintf@plt>
mov    -0x260(%rbp),%rdi
lea    0xd54(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x28(%rbp)
jmp    12f4 <cp_file+0x164>
mov    -0x18(%rbp),%rdi
lea    0xd3e(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    134d <cp_file+0x1bd>
mov    -0x18(%rbp),%rax
mov    %rax,-0x278(%rbp)
mov    0x2cc7(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x278(%rbp),%rsi
mov    %rax,%rdx
lea    0xcda(%rip),%rdi
call   1050 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x254(%rbp)
jmp    13b8 <cp_file+0x228>
jmp    1352 <cp_file+0x1c2>
lea    -0x250(%rbp),%rdi
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
mov    $0x200,%edx
call   1030 <fread@plt>
mov    %eax,-0x44(%rbp)
cmp    $0x0,%eax
je     1395 <cp_file+0x205>
lea    -0x250(%rbp),%rdi
mov    -0x44(%rbp),%edx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   10a0 <fwrite@plt>
jmp    1352 <cp_file+0x1c2>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x254(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cp_file>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x208,%rsp
mov    0x2e45(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %rsp,%r14
sub    %rax,%r14
mov    %r14,%rsp
mov    $0xffffffff,%eax
test   %rdi,%rdi
je     130c <cp_file+0x17c>
mov    %rsi,%r15
test   %rsi,%rsi
je     130c <cp_file+0x17c>
mov    %rdi,%r13
lea    0xe15(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     1231 <cp_file+0xa1>
mov    %rax,%r12
mov    %r15,%rdi
call   1060 <is_dir@plt>
test   %rax,%rax
je     1256 <cp_file+0xc6>
mov    %r13,%rdi
mov    %rbx,%rsi
call   1070 <get_short_filename@plt>
lea    0xe09(%rip),%rsi
mov    %r14,%rdi
mov    %r15,%rdx
mov    %rbx,%rcx
call   1090 <sprintf@plt>
lea    0xdfa(%rip),%rsi
mov    %r14,%rdi
jmp    1260 <cp_file+0xd0>
mov    0x2da0(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xdbd(%rip),%rdi
mov    %r13,%rsi
mov    %rax,%rdx
call   1050 <printf@plt>
jmp    1307 <cp_file+0x177>
lea    0xdcc(%rip),%rsi
mov    %r15,%rdi
call   1080 <fopen@plt>
mov    %rax,%r14
test   %rax,%rax
je     12df <cp_file+0x14f>
lea    -0x230(%rbp),%rdi
mov    $0x1,%esi
mov    $0x200,%edx
mov    %r12,%rcx
call   1030 <fread@plt>
test   %eax,%eax
je     12cb <cp_file+0x13b>
lea    -0x230(%rbp),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %eax,%edx
mov    %r14,%rcx
call   10a0 <fwrite@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x200,%edx
mov    %r12,%rcx
call   1030 <fread@plt>
test   %eax,%eax
jne    12a0 <cp_file+0x110>
mov    %r12,%rdi
call   1040 <fclose@plt>
mov    %r14,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
jmp    130c <cp_file+0x17c>
mov    0x2cf2(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xd0f(%rip),%rdi
mov    %r15,%rsi
mov    %rax,%rdx
call   1050 <printf@plt>
mov    %r12,%rdi
call   1040 <fclose@plt>
mov    $0xffffffff,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@PATH_MAX = global i32 0
@.str = [3 x i8] c"rb\00"
@.str.1 = [29 x i8] c"Could not open file %s : %s\0A\00"
@errno = global i32 0
@.str.2 = [6 x i8] c"%s/%s\00"
@.str.3 = [3 x i8] c"w+\00"
@.str.4 = [3 x i8] c"wb\00"
define i32 @cp_file(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i64
%11 = alloca i32
%12 = alloca [512 x i8]
%13 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%14 = load i32, i32* @PATH_MAX
%15 = zext i32 %14 to i64
%16 = call i8* @llvm.stacksave()
store i8* %16, i8** %8
%17 = alloca i8, i64 %15
store i64 %15, i64* %9
%18 = load i32, i32* @PATH_MAX
%19 = zext i32 %18 to i64
%20 = alloca i8, i64 %19
store i64 %19, i64* %10
%21 = load i8*, i8** %4
%22 = icmp eq i8* %21, null
br i1 %22, label %26, label %23
23: ; preds = %2
%24 = load i8*, i8** %5
%25 = icmp eq i8* %24, null
br i1 %25, label %26, label %27
26: ; preds = %23, %2
store i32 -1, i32* %3
store i32 1, i32* %13
br label %76
27: ; preds = %23
%28 = load i8*, i8** %4
%29 = call i32* @fopen(i8* %28, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %29, i32** %6
%30 = load i32*, i32** %6
%31 = icmp eq i32* %30, null
br i1 %31, label %32, label %37
32: ; preds = %27
%33 = load i8*, i8** %4
%34 = load i32, i32* @errno
%35 = call i8* @strerror(i32 %34)
%36 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8* %33, i8* %35)
store i32 -1, i32* %3
store i32 1, i32* %13
br label %76
37: ; preds = %27
%38 = load i8*, i8** %5
%39 = call i64 @is_dir(i8* %38)
%40 = icmp ne i64 %39, 0
br i1 %40, label %41, label %47
41: ; preds = %37
%42 = load i8*, i8** %4
%43 = call i32 @get_short_filename(i8* %42, i8* %17)
%44 = load i8*, i8** %5
%45 = call i32 @sprintf(i8* %20, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %44, i8* %17)
%46 = call i32* @fopen(i8* %20, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
store i32* %46, i32** %7
br label %50
47: ; preds = %37
%48 = load i8*, i8** %5
%49 = call i32* @fopen(i8* %48, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
store i32* %49, i32** %7
br label %50
50: ; preds = %47, %41
%51 = load i32*, i32** %7
%52 = icmp eq i32* %51, null
br i1 %52, label %53, label %60
53: ; preds = %50
%54 = load i8*, i8** %5
%55 = load i32, i32* @errno
%56 = call i8* @strerror(i32 %55)
%57 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8* %54, i8* %56)
%58 = load i32*, i32** %6
%59 = call i32 @fclose(i32* %58)
store i32 -1, i32* %3
store i32 1, i32* %13
br label %76
60: ; preds = %50
br label %61
61: ; preds = %66, %60
%62 = getelementptr inbounds [512 x i8], [512 x i8]* %12, i64 0, i64 0
%63 = load i32*, i32** %6
%64 = call i32 @fread(i8* %62, i32 1, i32 512, i32* %63)
store i32 %64, i32* %11
%65 = icmp ne i32 %64, 0
br i1 %65, label %66, label %71
66: ; preds = %61
%67 = getelementptr inbounds [512 x i8], [512 x i8]* %12, i64 0, i64 0
%68 = load i32, i32* %11
%69 = load i32*, i32** %7
%70 = call i32 @fwrite(i8* %67, i32 1, i32 %68, i32* %69)
br label %61
71: ; preds = %61
%72 = load i32*, i32** %6
%73 = call i32 @fclose(i32* %72)
%74 = load i32*, i32** %7
%75 = call i32 @fclose(i32* %74)
store i32 0, i32* %3
store i32 1, i32* %13
br label %76
76: ; preds = %71, %53, %32, %26
%77 = load i8*, i8** %8
%78 = load i32, i32* %3
ret i32 %78
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [3 x i8] c"rb\00"
@.str.1 = [29 x i8] c"Could not open file %s : %s\0A\00"
@errno = global i32 0
@.str.2 = [6 x i8] c"%s/%s\00"
@.str.3 = [3 x i8] c"w+\00"
@.str.4 = [3 x i8] c"wb\00"
define i32 @cp_file(i8* %0, i8* %1) {
%3 = alloca [512 x i8]
%4 = load i32, i32* @PATH_MAX
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
%7 = alloca i8, i64 %5
%8 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%9 = icmp eq i8* %0, null
%10 = icmp eq i8* %1, null
%11 = or i1 %9, %10
br i1 %11, label %47, label %12
12: ; preds = %2
%13 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %19
15: ; preds = %12
%16 = load i32, i32* @errno
%17 = tail call i8* @strerror(i32 %16)
%18 = tail call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %17)
br label %47
19: ; preds = %12
%20 = tail call i64 @is_dir(i8* %1)
%21 = icmp eq i64 %20, 0
br i1 %21, label %26, label %22
22: ; preds = %19
%23 = call i32 @get_short_filename(i8* %0, i8* %6)
%24 = call i32 @sprintf(i8* %7, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %1, i8* %6)
%25 = call i32* @fopen(i8* %7, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
br label %28
26: ; preds = %19
%27 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
br label %28
28: ; preds = %26, %22
%29 = phi i32* [ %25, %22 ], [ %27, %26 ]
%30 = icmp eq i32* %29, null
br i1 %30, label %34, label %31
31: ; preds = %28
%32 = call i32 @fread(i8* %8, i32 1, i32 512, i32* %13)
%33 = icmp eq i32 %32, 0
br i1 %33, label %44, label %39
34: ; preds = %28
%35 = load i32, i32* @errno
%36 = call i8* @strerror(i32 %35)
%37 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8* %1, i8* %36)
%38 = call i32 @fclose(i32* %13)
br label %47
39: ; preds = %31, %39
%40 = phi i32 [ %42, %39 ], [ %32, %31 ]
%41 = call i32 @fwrite(i8* %8, i32 1, i32 %40, i32* %29)
%42 = call i32 @fread(i8* %8, i32 1, i32 512, i32* %13)
%43 = icmp eq i32 %42, 0
br i1 %43, label %44, label %39
44: ; preds = %39, %31
%45 = call i32 @fclose(i32* %13)
%46 = call i32 @fclose(i32* %29)
br label %47
47: ; preds = %2, %44, %34, %15
%48 = phi i32 [ -1, %15 ], [ -1, %34 ], [ 0, %44 ], [ -1, %2 ]
ret i32 %48
}