#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*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
unsigned long fread(unsigned char *, int, unsigned long, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(unsigned char *);
scalar_t__ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/);
unsigned long ftell(int /*<<< orphan*/ *);
unsigned char *malloc(unsigned long);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ write_file(char *, unsigned char *, unsigned long);
void scramble(char *src, char *dst) {
  unsigned char *ptr = NULL;
  unsigned long sz = 0;
  FILE *fh;
  fh = fopen(src, "rb");
  if (fh == NULL) {
    fprintf(stderr, "Can't open \"%s\".\n", src);
    exit(1);
  }
  if (fseek(fh, 0, SEEK_END) < 0) {
    fprintf(stderr, "Seek error.\n");
    exit(1);
  }
  sz = ftell(fh);
  ptr = malloc(sz);
  if (ptr == NULL) {
    fprintf(stderr, "Out of memory.\n");
    exit(1);
  }
  if (fseek(fh, 0, SEEK_SET) < 0) {
    fprintf(stderr, "Seek error.\n");
    exit(1);
  }
  if (fread(ptr, 1, sz, fh) != sz) {
    fprintf(stderr, "Read error.\n");
    exit(1);
  }
  fclose(fh);
  write_file(dst, ptr, sz);
  free(ptr);
}


x86 assembly (clang, -O0):
<scramble>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe35(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1204 <scramble+0x64>
mov    0x2df2(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe10(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x28(%rbp),%rdi
mov    0x2dd9(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10a0 <fseek@plt>
cmp    $0x0,%rax
jge    1243 <scramble+0xa3>
mov    0x2daf(%rip),%rax
mov    (%rax),%edi
lea    0xde3(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x28(%rbp),%rdi
call   1080 <ftell@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1090 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1289 <scramble+0xe9>
mov    0x2d69(%rip),%rax
mov    (%rax),%edi
lea    0xdaa(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x28(%rbp),%rdi
mov    0x2d3c(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10a0 <fseek@plt>
cmp    $0x0,%rax
jge    12c8 <scramble+0x128>
mov    0x2d2a(%rip),%rax
mov    (%rax),%edi
lea    0xd5e(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
cmp    -0x20(%rbp),%rax
je     1309 <scramble+0x169>
mov    0x2ce9(%rip),%rax
mov    (%rax),%edi
lea    0xd3a(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x28(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1060 <write_file@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<scramble>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
lea    0xe4b(%rip),%rsi
call   10b0 <fopen@plt>
test   %rax,%rax
je     1268 <scramble+0xc8>
mov    %rax,%rbx
mov    0x2e1b(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   10a0 <fseek@plt>
test   %rax,%rax
js     1256 <scramble+0xb6>
mov    %rbx,%rdi
call   1080 <ftell@plt>
mov    %rax,%r15
mov    %rax,%rdi
call   1090 <malloc@plt>
test   %rax,%rax
je     128c <scramble+0xec>
mov    %rax,%r12
mov    0x2dcc(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   10a0 <fseek@plt>
test   %rax,%rax
js     1256 <scramble+0xb6>
mov    %r12,%rdi
mov    $0x1,%esi
mov    %r15,%rdx
mov    %rbx,%rcx
call   1040 <fread@plt>
cmp    %r15,%rax
jne    129e <scramble+0xfe>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    %r14,%rdi
mov    %r12,%rsi
mov    %r15,%rdx
call   1060 <write_file@plt>
mov    %r12,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1030 <free@plt>
mov    0x2d7b(%rip),%rax
mov    (%rax),%edi
lea    0xdaf(%rip),%rsi
jmp    12ae <scramble+0x10e>
mov    0x2d69(%rip),%rax
mov    (%rax),%edi
lea    0xd8b(%rip),%rsi
mov    %r15,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    0x2d45(%rip),%rax
mov    (%rax),%edi
lea    0xd86(%rip),%rsi
jmp    12ae <scramble+0x10e>
mov    0x2d33(%rip),%rax
mov    (%rax),%edi
lea    0xd84(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@stderr = global i32 0
@.str.1 = [18 x i8] c"Can't open \22%s\22.\0A\00"
@SEEK_END = global i32 0
@.str.2 = [13 x i8] c"Seek error.\0A\00"
@.str.3 = [16 x i8] c"Out of memory.\0A\00"
@SEEK_SET = global i32 0
@.str.4 = [13 x i8] c"Read error.\0A\00"
define void @scramble(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32*
store i8* %0, i8** %3
store i8* %1, i8** %4
store i8* null, i8** %5
store i64 0, i64* %6
%8 = load i8*, i8** %3
%9 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %9, i32** %7
%10 = load i32*, i32** %7
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %17
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = load i8*, i8** %3
%15 = call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %14)
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %2
%18 = load i32*, i32** %7
%19 = load i32, i32* @SEEK_END
%20 = call i64 @fseek(i32* %18, i32 0, i32 %19)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %26
22: ; preds = %17
%23 = load i32, i32* @stderr
%24 = call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %17
%27 = load i32*, i32** %7
%28 = call i64 @ftell(i32* %27)
store i64 %28, i64* %6
%29 = load i64, i64* %6
%30 = call i8* @malloc(i64 %29)
store i8* %30, i8** %5
%31 = load i8*, i8** %5
%32 = icmp eq i8* %31, null
br i1 %32, label %33, label %37
33: ; preds = %26
%34 = load i32, i32* @stderr
%35 = call i32 (i32, i8*, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
%36 = call i32 @exit(i32 1)
unreachable
37: ; preds = %26
%38 = load i32*, i32** %7
%39 = load i32, i32* @SEEK_SET
%40 = call i64 @fseek(i32* %38, i32 0, i32 %39)
%41 = icmp slt i64 %40, 0
br i1 %41, label %42, label %46
42: ; preds = %37
%43 = load i32, i32* @stderr
%44 = call i32 (i32, i8*, ...) @fprintf(i32 %43, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%45 = call i32 @exit(i32 1)
unreachable
46: ; preds = %37
%47 = load i8*, i8** %5
%48 = load i64, i64* %6
%49 = load i32*, i32** %7
%50 = call i64 @fread(i8* %47, i32 1, i64 %48, i32* %49)
%51 = load i64, i64* %6
%52 = icmp ne i64 %50, %51
br i1 %52, label %53, label %57
53: ; preds = %46
%54 = load i32, i32* @stderr
%55 = call i32 (i32, i8*, ...) @fprintf(i32 %54, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%56 = call i32 @exit(i32 1)
unreachable
57: ; preds = %46
%58 = load i32*, i32** %7
%59 = call i32 @fclose(i32* %58)
%60 = load i8*, i8** %4
%61 = load i8*, i8** %5
%62 = load i64, i64* %6
%63 = call i32 @write_file(i8* %60, i8* %61, i64 %62)
%64 = load i8*, i8** %5
%65 = call i32 @free(i8* %64)
ret void
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@stderr = global i32 0
@.str.1 = [18 x i8] c"Can't open \22%s\22.\0A\00"
@SEEK_END = global i32 0
@.str.2 = [13 x i8] c"Seek error.\0A\00"
@.str.3 = [16 x i8] c"Out of memory.\0A\00"
@SEEK_SET = global i32 0
@.str.4 = [13 x i8] c"Read error.\0A\00"
define void @scramble(i8* %0, i8* %1) {
%3 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %9
5: ; preds = %2
%6 = load i32, i32* @stderr
%7 = tail call i32 (i32, i8*, ...) @fprintf(i32 %6, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %2
%10 = load i32, i32* @SEEK_END
%11 = tail call i64 @fseek(i32* %3, i32 0, i32 %10)
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %17
13: ; preds = %9
%14 = load i32, i32* @stderr
%15 = tail call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%16 = tail call i32 @exit(i32 1)
unreachable
17: ; preds = %9
%18 = tail call i64 @ftell(i32* %3)
%19 = tail call i8* @malloc(i64 %18)
%20 = icmp eq i8* %19, null
br i1 %20, label %21, label %25
21: ; preds = %17
%22 = load i32, i32* @stderr
%23 = tail call i32 (i32, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
%24 = tail call i32 @exit(i32 1)
unreachable
25: ; preds = %17
%26 = load i32, i32* @SEEK_SET
%27 = tail call i64 @fseek(i32* %3, i32 0, i32 %26)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %25
%30 = load i32, i32* @stderr
%31 = tail call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%32 = tail call i32 @exit(i32 1)
unreachable
33: ; preds = %25
%34 = tail call i64 @fread(i8* %19, i32 1, i64 %18, i32* %3)
%35 = icmp eq i64 %34, %18
br i1 %35, label %40, label %36
36: ; preds = %33
%37 = load i32, i32* @stderr
%38 = tail call i32 (i32, i8*, ...) @fprintf(i32 %37, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%39 = tail call i32 @exit(i32 1)
unreachable
40: ; preds = %33
%41 = tail call i32 @fclose(i32* %3)
%42 = tail call i32 @write_file(i8* %1, i8* %19, i64 %18)
%43 = tail call i32 @free(i8* %19)
ret void
}