#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 S_IRUSR;
int S_IWUSR;
int S_IXUSR;
int /*<<< orphan*/ clearerr(int /*<<< orphan*/ *);
int /*<<< orphan*/ fchmod(int /*<<< orphan*/, int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int fread(char *, int, int, int /*<<< orphan*/ *);
int fwrite(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *pyi_open_target(char const *, char const *);
int pyi_copy_file(const char *src, const char *dst, const char *filename) {
  FILE *in = fopen(src, "rb");
  FILE *out = pyi_open_target(dst, filename);
  char buf[4096];
  int error = 0;
  if (in == NULL || out == NULL) {
    return -1;
  }
  while (!feof(in)) {
    if (fread(buf, 4096, 1, in) == -1) {
      if (ferror(in)) {
        clearerr(in);
        error = -1;
        break;
      }
    } else {
      int rc = fwrite(buf, 4096, 1, out);
      if (rc <= 0 || ferror(out)) {
        clearerr(out);
        error = -1;
        break;
      }
    }
  }
  fchmod(fileno(out), S_IRUSR | S_IWUSR | S_IXUSR);
  fclose(in);
  fclose(out);
  return error;
}


x86 assembly (clang, -O0):
<pyi_copy_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1040,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe3e(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1090 <pyi_open_target@plt>
mov    %rax,-0x30(%rbp)
movl   $0x0,-0x1034(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11fc <pyi_copy_file+0x5c>
cmpq   $0x0,-0x30(%rbp)
jne    1208 <pyi_copy_file+0x68>
movl   $0xffffffff,-0x4(%rbp)
jmp    1326 <pyi_copy_file+0x186>
jmp    120d <pyi_copy_file+0x6d>
mov    -0x28(%rbp),%rdi
call   1060 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    122b <pyi_copy_file+0x8b>
jmp    12e0 <pyi_copy_file+0x140>
lea    -0x1030(%rbp),%rdi
mov    -0x28(%rbp),%rcx
mov    $0x1000,%esi
mov    $0x1,%edx
call   1040 <fread@plt>
cmp    $0xffffffff,%eax
jne    127e <pyi_copy_file+0xde>
mov    -0x28(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     1279 <pyi_copy_file+0xd9>
mov    -0x28(%rbp),%rdi
call   1070 <clearerr@plt>
movl   $0xffffffff,-0x1034(%rbp)
ff ff ff
jmp    12e0 <pyi_copy_file+0x140>
jmp    12db <pyi_copy_file+0x13b>
lea    -0x1030(%rbp),%rdi
mov    -0x30(%rbp),%rcx
mov    $0x1000,%esi
mov    $0x1,%edx
call   10c0 <fwrite@plt>
mov    %eax,-0x1038(%rbp)
cmpl   $0x0,-0x1038(%rbp)
jle    12be <pyi_copy_file+0x11e>
mov    -0x30(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     12d6 <pyi_copy_file+0x136>
mov    -0x30(%rbp),%rdi
call   1070 <clearerr@plt>
movl   $0xffffffff,-0x1034(%rbp)
ff ff ff
jmp    12e0 <pyi_copy_file+0x140>
jmp    12db <pyi_copy_file+0x13b>
jmp    120d <pyi_copy_file+0x6d>
mov    -0x30(%rbp),%rdi
call   1080 <fileno@plt>
mov    %eax,%edi
mov    0x2cde(%rip),%rax
mov    (%rax),%esi
mov    0x2ce5(%rip),%rax
or     (%rax),%esi
mov    0x2ce4(%rip),%rax
or     (%rax),%esi
call   10a0 <fchmod@plt>
mov    -0x28(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x30(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x1034(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pyi_copy_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x1008,%rsp
mov    %rdx,%r14
mov    %rsi,%rbp
lea    0xe46(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,%rbx
mov    %rbp,%rdi
mov    %r14,%rsi
call   1090 <pyi_open_target@plt>
mov    $0xffffffff,%r15d
test   %rbx,%rbx
je     12a6 <pyi_copy_file+0x106>
mov    %rax,%r14
test   %rax,%rax
je     12a6 <pyi_copy_file+0x106>
mov    %rbx,%rdi
call   1060 <feof@plt>
xor    %r15d,%r15d
test   %eax,%eax
jne    126c <pyi_copy_file+0xcc>
mov    %rsp,%rbp
jmp    1219 <pyi_copy_file+0x79>
nopl   0x0(%rax)
mov    %rbx,%rdi
call   1030 <ferror@plt>
test   %rax,%rax
jne    125e <pyi_copy_file+0xbe>
mov    %rbx,%rdi
call   1060 <feof@plt>
test   %eax,%eax
jne    126c <pyi_copy_file+0xcc>
mov    %rbp,%rdi
mov    $0x1000,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1040 <fread@plt>
cmp    $0xffffffff,%eax
je     1200 <pyi_copy_file+0x60>
mov    %rbp,%rdi
mov    $0x1000,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   10c0 <fwrite@plt>
mov    %r14,%rdi
test   %eax,%eax
jle    1261 <pyi_copy_file+0xc1>
call   1030 <ferror@plt>
test   %rax,%rax
je     120d <pyi_copy_file+0x6d>
mov    %r14,%rdi
jmp    1261 <pyi_copy_file+0xc1>
mov    %rbx,%rdi
call   1070 <clearerr@plt>
mov    $0xffffffff,%r15d
mov    %r14,%rdi
call   1080 <fileno@plt>
mov    0x2d55(%rip),%rcx
mov    0x2d5e(%rip),%rdx
mov    (%rdx),%esi
or     (%rcx),%esi
mov    0x2d5b(%rip),%rcx
or     (%rcx),%esi
mov    %eax,%edi
call   10a0 <fchmod@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    %r14,%rdi
call   1050 <fclose@plt>
mov    %r15d,%eax
add    $0x1008,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IXUSR = global i32 0
define i32 @pyi_copy_file(i8* %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca [4096 x i8]
%11 = alloca i32
%12 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
%13 = load i8*, i8** %5
%14 = call i32* @fopen(i8* %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %14, i32** %8
%15 = load i8*, i8** %6
%16 = load i8*, i8** %7
%17 = call i32* @pyi_open_target(i8* %15, i8* %16)
store i32* %17, i32** %9
store i32 0, i32* %11
%18 = load i32*, i32** %8
%19 = icmp eq i32* %18, null
br i1 %19, label %23, label %20
20: ; preds = %3
%21 = load i32*, i32** %9
%22 = icmp eq i32* %21, null
br i1 %22, label %23, label %24
23: ; preds = %20, %3
store i32 -1, i32* %4
br label %72
24: ; preds = %20
br label %25
25: ; preds = %57, %24
%26 = load i32*, i32** %8
%27 = call i32 @feof(i32* %26)
%28 = icmp ne i32 %27, 0
%29 = xor i1 %28, true
br i1 %29, label %30, label %58
30: ; preds = %25
%31 = getelementptr inbounds [4096 x i8], [4096 x i8]* %10, i64 0, i64 0
%32 = load i32*, i32** %8
%33 = call i32 @fread(i8* %31, i32 4096, i32 1, i32* %32)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %43
35: ; preds = %30
%36 = load i32*, i32** %8
%37 = call i64 @ferror(i32* %36)
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %42
39: ; preds = %35
%40 = load i32*, i32** %8
%41 = call i32 @clearerr(i32* %40)
store i32 -1, i32* %11
br label %58
42: ; preds = %35
br label %57
43: ; preds = %30
%44 = getelementptr inbounds [4096 x i8], [4096 x i8]* %10, i64 0, i64 0
%45 = load i32*, i32** %9
%46 = call i32 @fwrite(i8* %44, i32 4096, i32 1, i32* %45)
store i32 %46, i32* %12
%47 = load i32, i32* %12
%48 = icmp sle i32 %47, 0
br i1 %48, label %53, label %49
49: ; preds = %43
%50 = load i32*, i32** %9
%51 = call i64 @ferror(i32* %50)
%52 = icmp ne i64 %51, 0
br i1 %52, label %53, label %56
53: ; preds = %49, %43
%54 = load i32*, i32** %9
%55 = call i32 @clearerr(i32* %54)
store i32 -1, i32* %11
br label %58
56: ; preds = %49
br label %57
57: ; preds = %56, %42
br label %25
58: ; preds = %53, %39, %25
%59 = load i32*, i32** %9
%60 = call i32 @fileno(i32* %59)
%61 = load i32, i32* @S_IRUSR
%62 = load i32, i32* @S_IWUSR
%63 = or i32 %61, %62
%64 = load i32, i32* @S_IXUSR
%65 = or i32 %63, %64
%66 = call i32 @fchmod(i32 %60, i32 %65)
%67 = load i32*, i32** %8
%68 = call i32 @fclose(i32* %67)
%69 = load i32*, i32** %9
%70 = call i32 @fclose(i32* %69)
%71 = load i32, i32* %11
store i32 %71, i32* %4
br label %72
72: ; preds = %58, %23
%73 = load i32, i32* %4
ret i32 %73
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IXUSR = global i32 0
define i32 @pyi_copy_file(i8* nocapture %0, i8* %1, i8* %2) {
%4 = alloca [4096 x i8]
%5 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%6 = tail call i32* @pyi_open_target(i8* %1, i8* %2)
%7 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%8 = icmp eq i32* %5, null
%9 = icmp eq i32* %6, null
%10 = select i1 %8, i1 true, i1 %9
br i1 %10, label %43, label %11
11: ; preds = %3
%12 = tail call i32 @feof(i32* %5)
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %32
14: ; preds = %11, %26
%15 = call i32 @fread(i8* %7, i32 4096, i32 1, i32* %5)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %20
17: ; preds = %14
%18 = tail call i64 @ferror(i32* %5)
%19 = icmp eq i64 %18, 0
br i1 %19, label %26, label %29
20: ; preds = %14
%21 = call i32 @fwrite(i8* %7, i32 4096, i32 1, i32* %6)
%22 = icmp slt i32 %21, 1
br i1 %22, label %29, label %23
23: ; preds = %20
%24 = tail call i64 @ferror(i32* %6)
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %23, %17
%27 = tail call i32 @feof(i32* %5)
%28 = icmp eq i32 %27, 0
br i1 %28, label %14, label %32
29: ; preds = %23, %20, %17
%30 = phi i32* [ %5, %17 ], [ %6, %20 ], [ %6, %23 ]
%31 = tail call i32 @clearerr(i32* %30)
br label %32
32: ; preds = %26, %29, %11
%33 = phi i32 [ 0, %11 ], [ -1, %29 ], [ 0, %26 ]
%34 = tail call i32 @fileno(i32* %6)
%35 = load i32, i32* @S_IRUSR
%36 = load i32, i32* @S_IWUSR
%37 = or i32 %36, %35
%38 = load i32, i32* @S_IXUSR
%39 = or i32 %37, %38
%40 = tail call i32 @fchmod(i32 %34, i32 %39)
%41 = tail call i32 @fclose(i32* %5)
%42 = tail call i32 @fclose(i32* %6)
br label %43
43: ; preds = %3, %32
%44 = phi i32 [ %33, %32 ], [ -1, %3 ]
ret i32 %44
}