#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 /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *const, char *);
int /*<<< orphan*/ fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, long const, int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(char *, int, int, int /*<<< orphan*/ *);
scalar_t__ malloc(int);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
long ReadPlain(const char *const srcfile, const char *const dstfile,
               const long Pointer, int length) {
  FILE *src = fopen(srcfile, "rb");
  if (src == NULL)
    return -2;
  FILE *dst = fopen(dstfile, "wb");
  if (length == 0) {
    if (Pointer == 0) {
      fseek(src, 0, SEEK_END);
      length = ftell(src);
      rewind(src);
    } else {
      fclose(src);
      fclose(dst);
      return -1;
    }
  }
  fseek(src, Pointer, SEEK_SET);
  rewind(dst);
  char *buffer = (char *)malloc(length);
  fread(buffer, length, 1, src);
  fwrite(buffer, length, 1, dst);
  free(buffer);
  fclose(src);
  fclose(dst);
  return length;
}


x86 assembly (clang, -O0):
<ReadPlain>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe4e(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11d3 <ReadPlain+0x43>
movq   $0xfffffffffffffffe,-0x8(%rbp)
ff
jmp    12c8 <ReadPlain+0x138>
mov    -0x18(%rbp),%rdi
lea    0xe25(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmpl   $0x0,-0x24(%rbp)
jne    1250 <ReadPlain+0xc0>
cmpq   $0x0,-0x20(%rbp)
jne    122c <ReadPlain+0x9c>
mov    -0x30(%rbp),%rdi
mov    0x2de1(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1090 <fseek@plt>
mov    -0x30(%rbp),%rdi
call   1070 <ftell@plt>
mov    %eax,-0x24(%rbp)
mov    -0x30(%rbp),%rdi
call   1060 <rewind@plt>
jmp    124b <ReadPlain+0xbb>
mov    -0x30(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x38(%rbp),%rdi
call   1050 <fclose@plt>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    12c8 <ReadPlain+0x138>
jmp    1250 <ReadPlain+0xc0>
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    0x2d79(%rip),%rax
mov    (%rax),%edx
call   1090 <fseek@plt>
mov    -0x38(%rbp),%rdi
call   1060 <rewind@plt>
mov    -0x24(%rbp),%edi
call   1080 <malloc@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
mov    -0x24(%rbp),%esi
mov    -0x30(%rbp),%rcx
mov    $0x1,%edx
call   1040 <fread@plt>
mov    -0x40(%rbp),%rdi
mov    -0x24(%rbp),%esi
mov    -0x38(%rbp),%rcx
mov    $0x1,%edx
call   10b0 <fwrite@plt>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x38(%rbp),%rdi
call   1050 <fclose@plt>
movslq -0x24(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ReadPlain>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %ecx,%r12d
mov    %rdx,%r15
mov    %rsi,%rbp
lea    0xe58(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     123a <ReadPlain+0xaa>
mov    %rax,%rbx
lea    0xe43(%rip),%rsi
mov    %rbp,%rdi
call   10a0 <fopen@plt>
mov    %rax,%r14
test   %r12d,%r12d
je     1243 <ReadPlain+0xb3>
mov    0x2e01(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    %r15,%rsi
call   1090 <fseek@plt>
mov    %r14,%rdi
call   1060 <rewind@plt>
mov    %r12d,%edi
call   1080 <malloc@plt>
mov    %rax,%rbp
mov    %rax,%rdi
mov    %r12d,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1040 <fread@plt>
mov    %rbp,%rdi
mov    %r12d,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   10b0 <fwrite@plt>
mov    %rbp,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    %r14,%rdi
call   1050 <fclose@plt>
movslq %r12d,%rax
jmp    125f <ReadPlain+0xcf>
mov    $0xfffffffffffffffe,%rax
jmp    125f <ReadPlain+0xcf>
test   %r15,%r15
je     1268 <ReadPlain+0xd8>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    %r14,%rdi
call   1050 <fclose@plt>
mov    $0xffffffffffffffff,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d79(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
mov    %rbx,%rdi
call   1070 <ftell@plt>
mov    %eax,%r12d
mov    %rbx,%rdi
call   1060 <rewind@plt>
jmp    11d0 <ReadPlain+0x40>

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@.str.1 = [3 x i8] c"wb\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define i64 @ReadPlain(i8* %0, i8* %1, i64 %2, i32 %3) {
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
%10 = alloca i32*
%11 = alloca i32*
%12 = alloca i8*
store i8* %0, i8** %6
store i8* %1, i8** %7
store i64 %2, i64* %8
store i32 %3, i32* %9
%13 = load i8*, i8** %6
%14 = call i32* @fopen(i8* %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %14, i32** %10
%15 = load i32*, i32** %10
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %18
17: ; preds = %4
store i64 -2, i64* %5
br label %66
18: ; preds = %4
%19 = load i8*, i8** %7
%20 = call i32* @fopen(i8* %19, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %20, i32** %11
%21 = load i32, i32* %9
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %40
23: ; preds = %18
%24 = load i64, i64* %8
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %34
26: ; preds = %23
%27 = load i32*, i32** %10
%28 = load i32, i32* @SEEK_END
%29 = call i32 @fseek(i32* %27, i64 0, i32 %28)
%30 = load i32*, i32** %10
%31 = call i32 @ftell(i32* %30)
store i32 %31, i32* %9
%32 = load i32*, i32** %10
%33 = call i32 @rewind(i32* %32)
br label %39
34: ; preds = %23
%35 = load i32*, i32** %10
%36 = call i32 @fclose(i32* %35)
%37 = load i32*, i32** %11
%38 = call i32 @fclose(i32* %37)
store i64 -1, i64* %5
br label %66
39: ; preds = %26
br label %40
40: ; preds = %39, %18
%41 = load i32*, i32** %10
%42 = load i64, i64* %8
%43 = load i32, i32* @SEEK_SET
%44 = call i32 @fseek(i32* %41, i64 %42, i32 %43)
%45 = load i32*, i32** %11
%46 = call i32 @rewind(i32* %45)
%47 = load i32, i32* %9
%48 = call i64 @malloc(i32 %47)
%49 = inttoptr i64 %48 to i8*
store i8* %49, i8** %12
%50 = load i8*, i8** %12
%51 = load i32, i32* %9
%52 = load i32*, i32** %10
%53 = call i32 @fread(i8* %50, i32 %51, i32 1, i32* %52)
%54 = load i8*, i8** %12
%55 = load i32, i32* %9
%56 = load i32*, i32** %11
%57 = call i32 @fwrite(i8* %54, i32 %55, i32 1, i32* %56)
%58 = load i8*, i8** %12
%59 = call i32 @free(i8* %58)
%60 = load i32*, i32** %10
%61 = call i32 @fclose(i32* %60)
%62 = load i32*, i32** %11
%63 = call i32 @fclose(i32* %62)
%64 = load i32, i32* %9
%65 = sext i32 %64 to i64
store i64 %65, i64* %5
br label %66
66: ; preds = %40, %34, %17
%67 = load i64, i64* %5
ret i64 %67
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@.str.1 = [3 x i8] c"wb\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define i64 @ReadPlain(i8* nocapture %0, i8* nocapture %1, i64 %2, i32 %3) {
%5 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %33, label %7
7: ; preds = %4
%8 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%9 = icmp eq i32 %3, 0
br i1 %9, label %10, label %20
10: ; preds = %7
%11 = icmp eq i64 %2, 0
br i1 %11, label %12, label %17
12: ; preds = %10
%13 = load i32, i32* @SEEK_END
%14 = tail call i32 @fseek(i32* %5, i64 0, i32 %13)
%15 = tail call i32 @ftell(i32* %5)
%16 = tail call i32 @rewind(i32* %5)
br label %20
17: ; preds = %10
%18 = tail call i32 @fclose(i32* %5)
%19 = tail call i32 @fclose(i32* %8)
br label %33
20: ; preds = %12, %7
%21 = phi i32 [ %15, %12 ], [ %3, %7 ]
%22 = load i32, i32* @SEEK_SET
%23 = tail call i32 @fseek(i32* %5, i64 %2, i32 %22)
%24 = tail call i32 @rewind(i32* %8)
%25 = tail call i64 @malloc(i32 %21)
%26 = inttoptr i64 %25 to i8*
%27 = tail call i32 @fread(i8* %26, i32 %21, i32 1, i32* %5)
%28 = tail call i32 @fwrite(i8* %26, i32 %21, i32 1, i32* %8)
%29 = tail call i32 @free(i8* %26)
%30 = tail call i32 @fclose(i32* %5)
%31 = tail call i32 @fclose(i32* %8)
%32 = sext i32 %21 to i64
br label %33
33: ; preds = %17, %20, %4
%34 = phi i64 [ -2, %4 ], [ %32, %20 ], [ -1, %17 ]
ret i64 %34
}