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

/* Variables and functions */
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_WRONLY;
int /*<<< orphan*/ SEEK_END;
char *alloca(int);
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ close(int);
int lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ mkdir(char *, int);
int open(char const *, int, ...);
int read(int, char *, int);
char *strchr_local(char *, char);
int /*<<< orphan*/ strcpy(char *, char const *);
int strlen(char const *);
int write(int, char *, int);
long copy_file(const char *filename, const char *path) {
  int fd;
  int fd_in;
  int len;
  char *target;
  int target_len;
  char *pos;
  int res;
  int current;
  char buffer[1024];
  int count;
  target_len = strlen(path) + strlen(filename) + 1;
  target = alloca(target_len);
  strcpy(target, path);
  strcpy(target + strlen(path), filename);
  target[target_len - 1] = '\0';
  fd = open(target, O_RDONLY);
  if (fd >= 0) {
    close(fd);
    return 0;
  }
  fd = open(filename, O_RDONLY);
  if (fd < 0)
    return fd;
  len = lseek(fd, 0, SEEK_END);
  close(fd);
  if (len <= 0)
    return len;
  pos = strchr_local(target + 1, '/');
  while (pos) {
    *pos = '\0';
    mkdir(target, 0777);
    *pos = '/';
    pos = strchr_local(pos + 1, '/');
  }
  fd_in = open(filename, O_RDONLY);
  if (fd_in < 0)
    return fd_in;
  fd = open(target, O_WRONLY | O_CREAT, 0777);
  if (fd < 0)
    return fd;
  current = len;
  while (current) {
    count = ((current) < (1024) ? (current) : (1024));
    res = read(fd_in, buffer, count);
    assert(res == count);
    res = write(fd, buffer, count);
    current -= count;
  }
  close(fd);
  close(fd_in);
  return 0;
}


x86 assembly (clang, -O0):
<copy_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x470,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,-0x464(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,%ecx
mov    -0x464(%rbp),%eax
add    %ecx,%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%edi
call   10b0 <alloca@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x460(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x460(%rbp),%rdi
cltq
add    %rax,%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x30(%rbp),%rax
mov    -0x34(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x30(%rbp),%rdi
mov    0x2d8f(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   10c0 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jl     1274 <copy_file+0xc4>
mov    -0x1c(%rbp),%edi
call   1080 <close@plt>
movq   $0x0,-0x8(%rbp)
jmp    1451 <copy_file+0x2a1>
mov    -0x10(%rbp),%rdi
mov    0x2d59(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   10c0 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    12a2 <copy_file+0xf2>
movslq -0x1c(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1451 <copy_file+0x2a1>
mov    -0x1c(%rbp),%edi
mov    0x2d34(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1070 <lseek@plt>
mov    %eax,-0x24(%rbp)
mov    -0x1c(%rbp),%edi
call   1080 <close@plt>
cmpl   $0x0,-0x24(%rbp)
jg     12d7 <copy_file+0x127>
movslq -0x24(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1451 <copy_file+0x2a1>
mov    -0x30(%rbp),%rdi
add    $0x1,%rdi
mov    $0x2f,%esi
call   10d0 <strchr_local@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
je     132f <copy_file+0x17f>
mov    -0x40(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x30(%rbp),%rdi
mov    $0x1ff,%esi
call   1040 <mkdir@plt>
mov    -0x40(%rbp),%rax
movb   $0x2f,(%rax)
mov    -0x40(%rbp),%rdi
add    $0x1,%rdi
mov    $0x2f,%esi
call   10d0 <strchr_local@plt>
mov    %rax,-0x40(%rbp)
jmp    12ed <copy_file+0x13d>
mov    -0x10(%rbp),%rdi
mov    0x2c9e(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   10c0 <open@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    135d <copy_file+0x1ad>
movslq -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1451 <copy_file+0x2a1>
mov    -0x30(%rbp),%rdi
mov    0x2c58(%rip),%rax
mov    (%rax),%esi
mov    0x2c87(%rip),%rax
or     (%rax),%esi
mov    $0x1ff,%edx
mov    $0x0,%al
call   10c0 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1399 <copy_file+0x1e9>
movslq -0x1c(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1451 <copy_file+0x2a1>
mov    -0x24(%rbp),%eax
mov    %eax,-0x48(%rbp)
cmpl   $0x0,-0x48(%rbp)
je     1439 <copy_file+0x289>
cmpl   $0x400,-0x48(%rbp)
jge    13c4 <copy_file+0x214>
mov    -0x48(%rbp),%eax
mov    %eax,-0x468(%rbp)
jmp    13d4 <copy_file+0x224>
mov    $0x400,%eax
mov    %eax,-0x468(%rbp)
jmp    13d4 <copy_file+0x224>
mov    -0x468(%rbp),%eax
mov    %eax,-0x454(%rbp)
mov    -0x20(%rbp),%edi
lea    -0x450(%rbp),%rsi
mov    -0x454(%rbp),%edx
call   1090 <read@plt>
mov    %eax,-0x44(%rbp)
mov    -0x44(%rbp),%eax
cmp    -0x454(%rbp),%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   10a0 <assert@plt>
mov    -0x1c(%rbp),%edi
lea    -0x450(%rbp),%rsi
mov    -0x454(%rbp),%edx
call   1050 <write@plt>
mov    %eax,-0x44(%rbp)
mov    -0x454(%rbp),%ecx
mov    -0x48(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x48(%rbp)
jmp    139f <copy_file+0x1ef>
mov    -0x1c(%rbp),%edi
call   1080 <close@plt>
mov    -0x20(%rbp),%edi
call   1080 <close@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x470,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_file>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x400,%rsp
mov    %rsi,%rbx
mov    %rdi,%r14
mov    %rsi,%rdi
call   1060 <strlen@plt>
mov    %eax,%ebp
mov    %r14,%rdi
call   1060 <strlen@plt>
lea    (%rax,%rbp,1),%r12d
lea    (%rax,%rbp,1),%edi
add    $0x1,%edi
call   10b0 <alloca@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    %rbx,%rsi
call   1030 <strcpy@plt>
mov    %rbx,%rdi
call   1060 <strlen@plt>
movslq %eax,%rdi
add    %r15,%rdi
mov    %r14,%rsi
call   1030 <strcpy@plt>
movslq %r12d,%rax
movb   $0x0,(%r15,%rax,1)
mov    0x2dbf(%rip),%r12
mov    (%r12),%esi
mov    %r15,%rdi
xor    %eax,%eax
call   10c0 <open@plt>
test   %eax,%eax
js     1244 <copy_file+0x94>
mov    %eax,%edi
call   1080 <close@plt>
xor    %eax,%eax
add    $0x400,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%r12),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   10c0 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     1359 <copy_file+0x1a9>
mov    0x2d7d(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
xor    %esi,%esi
call   1070 <lseek@plt>
mov    %eax,%ebp
mov    %ebx,%edi
call   1080 <close@plt>
test   %ebp,%ebp
jle    1361 <copy_file+0x1b1>
mov    %r15,%rdi
add    $0x1,%rdi
mov    $0x2f,%esi
call   10d0 <strchr_local@plt>
test   %rax,%rax
je     12cc <copy_file+0x11c>
mov    %rax,%rbx
nopl   0x0(%rax,%rax,1)
movb   $0x0,(%rbx)
mov    %r15,%rdi
mov    $0x1ff,%esi
call   1040 <mkdir@plt>
movb   $0x2f,(%rbx)
add    $0x1,%rbx
mov    %rbx,%rdi
mov    $0x2f,%esi
call   10d0 <strchr_local@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    12a0 <copy_file+0xf0>
mov    (%r12),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   10c0 <open@plt>
mov    %eax,%r14d
test   %eax,%eax
js     1369 <copy_file+0x1b9>
mov    0x2cd4(%rip),%rax
mov    0x2d05(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %r15,%rdi
mov    $0x1ff,%edx
xor    %eax,%eax
call   10c0 <open@plt>
mov    %eax,%r15d
test   %eax,%eax
js     1371 <copy_file+0x1c1>
mov    %rsp,%r12
cmp    $0x400,%ebp
mov    $0x400,%ebx
cmovl  %ebp,%ebx
mov    %r14d,%edi
mov    %r12,%rsi
mov    %ebx,%edx
call   1090 <read@plt>
xor    %edi,%edi
cmp    %ebx,%eax
sete   %dil
call   10a0 <assert@plt>
mov    %r15d,%edi
mov    %r12,%rsi
mov    %ebx,%edx
call   1050 <write@plt>
sub    %ebx,%ebp
jne    1310 <copy_file+0x160>
mov    %r15d,%edi
call   1080 <close@plt>
mov    %r14d,%edi
jmp    122d <copy_file+0x7d>
movslq %ebx,%rax
jmp    1234 <copy_file+0x84>
movslq %ebp,%rax
jmp    1234 <copy_file+0x84>
movslq %r14d,%rax
jmp    1234 <copy_file+0x84>
movslq %r15d,%rax
jmp    1234 <copy_file+0x84>

LLVM IR (-O0):
@O_RDONLY = global i32 0
@SEEK_END = global i32 0
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
define i64 @copy_file(i8* %0, i8* %1) {
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i32
%13 = alloca i32
%14 = alloca [1024 x i8]
%15 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%16 = load i8*, i8** %5
%17 = call i32 @strlen(i8* %16)
%18 = load i8*, i8** %4
%19 = call i32 @strlen(i8* %18)
%20 = add nsw i32 %17, %19
%21 = add nsw i32 %20, 1
store i32 %21, i32* %10
%22 = load i32, i32* %10
%23 = call i8* @alloca(i32 %22)
store i8* %23, i8** %9
%24 = load i8*, i8** %9
%25 = load i8*, i8** %5
%26 = call i32 @strcpy(i8* %24, i8* %25)
%27 = load i8*, i8** %9
%28 = load i8*, i8** %5
%29 = call i32 @strlen(i8* %28)
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %27, i64 %30
%32 = load i8*, i8** %4
%33 = call i32 @strcpy(i8* %31, i8* %32)
%34 = load i8*, i8** %9
%35 = load i32, i32* %10
%36 = sub nsw i32 %35, 1
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i8, i8* %34, i64 %37
store i8 0, i8* %38
%39 = load i8*, i8** %9
%40 = load i32, i32* @O_RDONLY
%41 = call i32 (i8*, i32, ...) @open(i8* %39, i32 %40)
store i32 %41, i32* %6
%42 = load i32, i32* %6
%43 = icmp sge i32 %42, 0
br i1 %43, label %44, label %47
44: ; preds = %2
%45 = load i32, i32* %6
%46 = call i32 @close(i32 %45)
store i64 0, i64* %3
br label %136
47: ; preds = %2
%48 = load i8*, i8** %4
%49 = load i32, i32* @O_RDONLY
%50 = call i32 (i8*, i32, ...) @open(i8* %48, i32 %49)
store i32 %50, i32* %6
%51 = load i32, i32* %6
%52 = icmp slt i32 %51, 0
br i1 %52, label %53, label %56
53: ; preds = %47
%54 = load i32, i32* %6
%55 = sext i32 %54 to i64
store i64 %55, i64* %3
br label %136
56: ; preds = %47
%57 = load i32, i32* %6
%58 = load i32, i32* @SEEK_END
%59 = call i32 @lseek(i32 %57, i32 0, i32 %58)
store i32 %59, i32* %8
%60 = load i32, i32* %6
%61 = call i32 @close(i32 %60)
%62 = load i32, i32* %8
%63 = icmp sle i32 %62, 0
br i1 %63, label %64, label %67
64: ; preds = %56
%65 = load i32, i32* %8
%66 = sext i32 %65 to i64
store i64 %66, i64* %3
br label %136
67: ; preds = %56
%68 = load i8*, i8** %9
%69 = getelementptr inbounds i8, i8* %68, i64 1
%70 = call i8* @strchr_local(i8* %69, i8 signext 47)
store i8* %70, i8** %11
br label %71
71: ; preds = %74, %67
%72 = load i8*, i8** %11
%73 = icmp ne i8* %72, null
br i1 %73, label %74, label %82
74: ; preds = %71
%75 = load i8*, i8** %11
store i8 0, i8* %75
%76 = load i8*, i8** %9
%77 = call i32 @mkdir(i8* %76, i32 511)
%78 = load i8*, i8** %11
store i8 47, i8* %78
%79 = load i8*, i8** %11
%80 = getelementptr inbounds i8, i8* %79, i64 1
%81 = call i8* @strchr_local(i8* %80, i8 signext 47)
store i8* %81, i8** %11
br label %71
82: ; preds = %71
%83 = load i8*, i8** %4
%84 = load i32, i32* @O_RDONLY
%85 = call i32 (i8*, i32, ...) @open(i8* %83, i32 %84)
store i32 %85, i32* %7
%86 = load i32, i32* %7
%87 = icmp slt i32 %86, 0
br i1 %87, label %88, label %91
88: ; preds = %82
%89 = load i32, i32* %7
%90 = sext i32 %89 to i64
store i64 %90, i64* %3
br label %136
91: ; preds = %82
%92 = load i8*, i8** %9
%93 = load i32, i32* @O_WRONLY
%94 = load i32, i32* @O_CREAT
%95 = or i32 %93, %94
%96 = call i32 (i8*, i32, ...) @open(i8* %92, i32 %95, i32 511)
store i32 %96, i32* %6
%97 = load i32, i32* %6
%98 = icmp slt i32 %97, 0
br i1 %98, label %99, label %102
99: ; preds = %91
%100 = load i32, i32* %6
%101 = sext i32 %100 to i64
store i64 %101, i64* %3
br label %136
102: ; preds = %91
%103 = load i32, i32* %8
store i32 %103, i32* %13
br label %104
104: ; preds = %113, %102
%105 = load i32, i32* %13
%106 = icmp ne i32 %105, 0
br i1 %106, label %107, label %131
107: ; preds = %104
%108 = load i32, i32* %13
%109 = icmp slt i32 %108, 1024
br i1 %109, label %110, label %112
110: ; preds = %107
%111 = load i32, i32* %13
br label %113
112: ; preds = %107
br label %113
113: ; preds = %112, %110
%114 = phi i32 [ %111, %110 ], [ 1024, %112 ]
store i32 %114, i32* %15
%115 = load i32, i32* %7
%116 = getelementptr inbounds [1024 x i8], [1024 x i8]* %14, i64 0, i64 0
%117 = load i32, i32* %15
%118 = call i32 @read(i32 %115, i8* %116, i32 %117)
store i32 %118, i32* %12
%119 = load i32, i32* %12
%120 = load i32, i32* %15
%121 = icmp eq i32 %119, %120
%122 = zext i1 %121 to i32
%123 = call i32 @assert(i32 %122)
%124 = load i32, i32* %6
%125 = getelementptr inbounds [1024 x i8], [1024 x i8]* %14, i64 0, i64 0
%126 = load i32, i32* %15
%127 = call i32 @write(i32 %124, i8* %125, i32 %126)
store i32 %127, i32* %12
%128 = load i32, i32* %15
%129 = load i32, i32* %13
%130 = sub nsw i32 %129, %128
store i32 %130, i32* %13
br label %104
131: ; preds = %104
%132 = load i32, i32* %6
%133 = call i32 @close(i32 %132)
%134 = load i32, i32* %7
%135 = call i32 @close(i32 %134)
store i64 0, i64* %3
br label %136
136: ; preds = %131, %99, %88, %64, %53, %44
%137 = load i64, i64* %3
ret i64 %137
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@SEEK_END = global i32 0
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
define i64 @copy_file(i8* %0, i8* %1) {
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%5 = tail call i32 @strlen(i8* %1)
%6 = tail call i32 @strlen(i8* %0)
%7 = add nsw i32 %6, %5
%8 = add nsw i32 %7, 1
%9 = tail call i8* @alloca(i32 %8)
%10 = tail call i32 @strcpy(i8* %9, i8* %1)
%11 = tail call i32 @strlen(i8* %1)
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds i8, i8* %9, i64 %12
%14 = tail call i32 @strcpy(i8* %13, i8* %0)
%15 = sext i32 %7 to i64
%16 = getelementptr inbounds i8, i8* %9, i64 %15
store i8 0, i8* %16
%17 = load i32, i32* @O_RDONLY
%18 = tail call i32 (i8*, i32, ...) @open(i8* %9, i32 %17)
%19 = icmp sgt i32 %18, -1
br i1 %19, label %20, label %22
20: ; preds = %2
%21 = tail call i32 @close(i32 %18)
br label %73
22: ; preds = %2
%23 = load i32, i32* @O_RDONLY
%24 = tail call i32 (i8*, i32, ...) @open(i8* %0, i32 %23)
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %22
%27 = sext i32 %24 to i64
br label %73
28: ; preds = %22
%29 = load i32, i32* @SEEK_END
%30 = tail call i32 @lseek(i32 %24, i32 0, i32 %29)
%31 = tail call i32 @close(i32 %24)
%32 = icmp slt i32 %30, 1
br i1 %32, label %33, label %35
33: ; preds = %28
%34 = sext i32 %30 to i64
br label %73
35: ; preds = %28
%36 = getelementptr inbounds i8, i8* %9, i64 1
%37 = tail call i8* @strchr_local(i8* %36, i8 signext 47)
%38 = icmp eq i8* %37, null
br i1 %38, label %45, label %39
39: ; preds = %35, %39
%40 = phi i8* [ %43, %39 ], [ %37, %35 ]
store i8 0, i8* %40
%41 = tail call i32 @mkdir(i8* %9, i32 511)
store i8 47, i8* %40
%42 = getelementptr inbounds i8, i8* %40, i64 1
%43 = tail call i8* @strchr_local(i8* %42, i8 signext 47)
%44 = icmp eq i8* %43, null
br i1 %44, label %45, label %39
45: ; preds = %39, %35
%46 = load i32, i32* @O_RDONLY
%47 = tail call i32 (i8*, i32, ...) @open(i8* %0, i32 %46)
%48 = icmp slt i32 %47, 0
br i1 %48, label %49, label %51
49: ; preds = %45
%50 = sext i32 %47 to i64
br label %73
51: ; preds = %45
%52 = load i32, i32* @O_WRONLY
%53 = load i32, i32* @O_CREAT
%54 = or i32 %53, %52
%55 = tail call i32 (i8*, i32, ...) @open(i8* %9, i32 %54, i32 511)
%56 = icmp slt i32 %55, 0
br i1 %56, label %57, label %59
57: ; preds = %51
%58 = sext i32 %55 to i64
br label %73
59: ; preds = %51, %59
%60 = phi i32 [ %68, %59 ], [ %30, %51 ]
%61 = icmp slt i32 %60, 1024
%62 = select i1 %61, i32 %60, i32 1024
%63 = call i32 @read(i32 %47, i8* %4, i32 %62)
%64 = icmp eq i32 %63, %62
%65 = zext i1 %64 to i32
%66 = tail call i32 @assert(i32 %65)
%67 = call i32 @write(i32 %55, i8* %4, i32 %62)
%68 = sub nsw i32 %60, %62
%69 = icmp eq i32 %68, 0
br i1 %69, label %70, label %59
70: ; preds = %59
%71 = tail call i32 @close(i32 %55)
%72 = tail call i32 @close(i32 %47)
br label %73
73: ; preds = %70, %57, %49, %33, %26, %20
%74 = phi i64 [ 0, %20 ], [ %27, %26 ], [ %34, %33 ], [ %50, %49 ], [ %58, %57 ], [ 0, %70 ]
ret i64 %74
}