#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 stat {
  int /*<<< orphan*/ st_mode;
};
typedef int int64_t;

/* Variables and functions */
scalar_t__ ENOTDIR;
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_TRUNC;
int O_WRONLY;
int PATH_MAX;
int /*<<< orphan*/ S_IRWXU;
int /*<<< orphan*/ close(int);
int copy_fd_to_fd(int, int);
scalar_t__ create_dir(char *, int /*<<< orphan*/);
scalar_t__ errno;
int fstat(int, struct stat *);
int open(char const *, int, ...);
int /*<<< orphan*/ path_dirname(char const *, char *);
int64_t copy_file_to_file(const char *input, const char *output) {
  int in = open(input, O_RDONLY);
  if (in == -1)
    return -1;
  struct stat info;
  if (fstat(in, &info) == -1) {
    close(in);
    return -1;
  }
  int out = open(output, O_WRONLY | O_CREAT | O_TRUNC, info.st_mode);
  if (out == -1 && errno == ENOTDIR) {
    char dir[PATH_MAX];
    path_dirname(output, dir);
    if (create_dir(dir, S_IRWXU)) {
      out = open(output, O_WRONLY | O_CREAT | O_TRUNC, info.st_mode);
    }
  }
  if (out == -1) {
    close(in);
    return -1;
  }
  int64_t total = copy_fd_to_fd(in, out);
  close(in);
  close(out);
  return total;
}


x86 assembly (clang, -O0):
<copy_file_to_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e45(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1070 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    119d <copy_file_to_file+0x3d>
movl   $0xffffffff,-0x4(%rbp)
jmp    12dc <copy_file_to_file+0x17c>
mov    -0x1c(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1080 <fstat@plt>
cmp    $0xffffffff,%eax
jne    11c6 <copy_file_to_file+0x66>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12dc <copy_file_to_file+0x17c>
mov    -0x18(%rbp),%rdi
mov    0x2dcf(%rip),%rax
mov    (%rax),%esi
mov    0x2e1e(%rip),%rax
or     (%rax),%esi
mov    0x2e05(%rip),%rax
or     (%rax),%esi
mov    -0x20(%rbp),%edx
mov    $0x0,%al
call   1070 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    129a <copy_file_to_file+0x13a>
mov    0x2dad(%rip),%rax
mov    (%rax),%rax
mov    0x2de3(%rip),%rcx
cmp    (%rcx),%rax
jne    129a <copy_file_to_file+0x13a>
mov    0x2dbb(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rsi
sub    %rcx,%rsi
mov    %rsi,-0x48(%rbp)
mov    %rsi,%rsp
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <path_dirname@plt>
mov    -0x48(%rbp),%rdi
mov    0x2d72(%rip),%rax
mov    (%rax),%esi
call   1060 <create_dir@plt>
cmp    $0x0,%rax
je     1293 <copy_file_to_file+0x133>
mov    -0x18(%rbp),%rdi
mov    0x2d2e(%rip),%rax
mov    (%rax),%esi
mov    0x2d7d(%rip),%rax
or     (%rax),%esi
mov    0x2d64(%rip),%rax
or     (%rax),%esi
mov    -0x20(%rbp),%edx
mov    $0x0,%al
call   1070 <open@plt>
mov    %eax,-0x24(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
cmpl   $0xffffffff,-0x24(%rbp)
jne    12b8 <copy_file_to_file+0x158>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12dc <copy_file_to_file+0x17c>
mov    -0x1c(%rbp),%edi
mov    -0x24(%rbp),%esi
call   1040 <copy_fd_to_fd@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_file_to_file>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
mov    0x2e45(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     1293 <copy_file_to_file+0x133>
mov    %eax,%r12d
lea    -0x30(%rbp),%rsi
mov    %eax,%edi
call   1080 <fstat@plt>
cmp    $0xffffffff,%eax
je     128b <copy_file_to_file+0x12b>
mov    0x2df5(%rip),%rbx
mov    0x2e46(%rip),%rax
mov    (%rax),%esi
or     (%rbx),%esi
mov    0x2e2b(%rip),%rax
or     (%rax),%esi
mov    -0x30(%rbp),%r15d
mov    %r14,%rdi
mov    %r15d,%edx
xor    %eax,%eax
call   1070 <open@plt>
mov    %eax,%r13d
cmp    $0xffffffff,%eax
je     11fa <copy_file_to_file+0x9a>
mov    %r12d,%edi
mov    %r13d,%esi
call   1040 <copy_fd_to_fd@plt>
mov    %eax,%ebx
mov    %r12d,%edi
call   1050 <close@plt>
mov    %r13d,%edi
call   1050 <close@plt>
jmp    1298 <copy_file_to_file+0x138>
mov    0x2daf(%rip),%rax
mov    (%rax),%rax
mov    0x2de5(%rip),%rcx
cmp    (%rcx),%rax
jne    128b <copy_file_to_file+0x12b>
mov    %rbx,%r13
mov    %rsp,-0x38(%rbp)
mov    0x2dba(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %r14,%rdi
mov    %rbx,%rsi
call   1030 <path_dirname@plt>
mov    0x2d85(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1060 <create_dir@plt>
test   %rax,%rax
je     1287 <copy_file_to_file+0x127>
mov    0x2d9f(%rip),%rax
mov    (%rax),%esi
or     0x0(%r13),%esi
mov    0x2d82(%rip),%rax
or     (%rax),%esi
mov    %r14,%rdi
mov    %r15d,%edx
xor    %eax,%eax
call   1070 <open@plt>
mov    %eax,%r13d
mov    -0x38(%rbp),%rsp
cmp    $0xffffffff,%eax
jne    11d8 <copy_file_to_file+0x78>
jmp    128b <copy_file_to_file+0x12b>
mov    -0x38(%rbp),%rsp
mov    %r12d,%edi
call   1050 <close@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@errno = global i64 0
@ENOTDIR = global i64 0
@PATH_MAX = global i32 0
@S_IRWXU = global i32 0
define i32 @copy_file_to_file(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca %struct.stat
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%12 = load i8*, i8** %4
%13 = load i32, i32* @O_RDONLY
%14 = call i32 (i8*, i32, ...) @open(i8* %12, i32 %13)
store i32 %14, i32* %6
%15 = load i32, i32* %6
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %18
17: ; preds = %2
store i32 -1, i32* %3
br label %78
18: ; preds = %2
%19 = load i32, i32* %6
%20 = call i32 @fstat(i32 %19, %struct.stat* %7)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %25
22: ; preds = %18
%23 = load i32, i32* %6
%24 = call i32 @close(i32 %23)
store i32 -1, i32* %3
br label %78
25: ; preds = %18
%26 = load i8*, i8** %5
%27 = load i32, i32* @O_WRONLY
%28 = load i32, i32* @O_CREAT
%29 = or i32 %27, %28
%30 = load i32, i32* @O_TRUNC
%31 = or i32 %29, %30
%32 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%33 = load i32, i32* %32
%34 = call i32 (i8*, i32, ...) @open(i8* %26, i32 %31, i32 %33)
store i32 %34, i32* %8
%35 = load i32, i32* %8
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %63
37: ; preds = %25
%38 = load i64, i64* @errno
%39 = load i64, i64* @ENOTDIR
%40 = icmp eq i64 %38, %39
br i1 %40, label %41, label %63
41: ; preds = %37
%42 = load i32, i32* @PATH_MAX
%43 = zext i32 %42 to i64
%44 = call i8* @llvm.stacksave()
store i8* %44, i8** %9
%45 = alloca i8, i64 %43
store i64 %43, i64* %10
%46 = load i8*, i8** %5
%47 = call i32 @path_dirname(i8* %46, i8* %45)
%48 = load i32, i32* @S_IRWXU
%49 = call i64 @create_dir(i8* %45, i32 %48)
%50 = icmp ne i64 %49, 0
br i1 %50, label %51, label %61
51: ; preds = %41
%52 = load i8*, i8** %5
%53 = load i32, i32* @O_WRONLY
%54 = load i32, i32* @O_CREAT
%55 = or i32 %53, %54
%56 = load i32, i32* @O_TRUNC
%57 = or i32 %55, %56
%58 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%59 = load i32, i32* %58
%60 = call i32 (i8*, i32, ...) @open(i8* %52, i32 %57, i32 %59)
store i32 %60, i32* %8
br label %61
61: ; preds = %51, %41
%62 = load i8*, i8** %9
br label %63
63: ; preds = %61, %37, %25
%64 = load i32, i32* %8
%65 = icmp eq i32 %64, -1
br i1 %65, label %66, label %69
66: ; preds = %63
%67 = load i32, i32* %6
%68 = call i32 @close(i32 %67)
store i32 -1, i32* %3
br label %78
69: ; preds = %63
%70 = load i32, i32* %6
%71 = load i32, i32* %8
%72 = call i32 @copy_fd_to_fd(i32 %70, i32 %71)
store i32 %72, i32* %11
%73 = load i32, i32* %6
%74 = call i32 @close(i32 %73)
%75 = load i32, i32* %8
%76 = call i32 @close(i32 %75)
%77 = load i32, i32* %11
store i32 %77, i32* %3
br label %78
78: ; preds = %69, %66, %22, %17
%79 = load i32, i32* %3
ret i32 %79
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@errno = global i64 0
@ENOTDIR = global i64 0
@PATH_MAX = global i32 0
@S_IRWXU = global i32 0
define i32 @copy_file_to_file(i8* nocapture %0, i8* %1) {
%3 = alloca %struct.stat
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 (i8*, i32, ...) @open(i8* %0, i32 %4)
%6 = icmp eq i32 %5, -1
br i1 %6, label %54, label %7
7: ; preds = %2
%8 = bitcast %struct.stat* %3 to i8*
%9 = call i32 @fstat(i32 %5, %struct.stat* %3)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %7
%12 = tail call i32 @close(i32 %5)
br label %52
13: ; preds = %7
%14 = load i32, i32* @O_WRONLY
%15 = load i32, i32* @O_CREAT
%16 = or i32 %15, %14
%17 = load i32, i32* @O_TRUNC
%18 = or i32 %16, %17
%19 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%20 = load i32, i32* %19
%21 = tail call i32 (i8*, i32, ...) @open(i8* %1, i32 %18, i32 %20)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %47
23: ; preds = %13
%24 = load i64, i64* @errno
%25 = load i64, i64* @ENOTDIR
%26 = icmp eq i64 %24, %25
br i1 %26, label %27, label %45
27: ; preds = %23
%28 = load i32, i32* @PATH_MAX
%29 = zext i32 %28 to i64
%30 = tail call i8* @llvm.stacksave()
%31 = alloca i8, i64 %29
%32 = call i32 @path_dirname(i8* %1, i8* %31)
%33 = load i32, i32* @S_IRWXU
%34 = call i64 @create_dir(i8* %31, i32 %33)
%35 = icmp eq i64 %34, 0
br i1 %35, label %36, label %37
36: ; preds = %27
br label %45
37: ; preds = %27
%38 = load i32, i32* @O_WRONLY
%39 = load i32, i32* @O_CREAT
%40 = or i32 %39, %38
%41 = load i32, i32* @O_TRUNC
%42 = or i32 %40, %41
%43 = call i32 (i8*, i32, ...) @open(i8* %1, i32 %42, i32 %20)
%44 = icmp eq i32 %43, -1
br i1 %44, label %45, label %47
45: ; preds = %23, %36, %37
%46 = call i32 @close(i32 %5)
br label %52
47: ; preds = %13, %37
%48 = phi i32 [ %43, %37 ], [ %21, %13 ]
%49 = call i32 @copy_fd_to_fd(i32 %5, i32 %48)
%50 = call i32 @close(i32 %5)
%51 = call i32 @close(i32 %48)
br label %52
52: ; preds = %45, %47, %11
%53 = phi i32 [ -1, %11 ], [ -1, %45 ], [ %49, %47 ]
br label %54
54: ; preds = %2, %52
%55 = phi i32 [ %53, %52 ], [ -1, %2 ]
ret i32 %55
}