#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*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ free(unsigned char *);
unsigned long fwrite(unsigned char *, int, unsigned long, int /*<<< orphan*/ *);
int /*<<< orphan*/ read_file(char *, unsigned char **, unsigned long *);
int /*<<< orphan*/ stderr;
void descramble(char *src, char *dst) {
  unsigned char *ptr = NULL;
  unsigned long sz = 0;
  FILE *fh;
  read_file(src, &ptr, &sz);
  fh = fopen(dst, "wb");
  if (fh == NULL) {
    fprintf(stderr, "Can't open \"%s\".\n", dst);
    exit(1);
  }
  if (fwrite(ptr, 1, sz, fh) != sz) {
    fprintf(stderr, "Write error.\n");
    exit(1);
  }
  fclose(fh);
  free(ptr);
}


x86 assembly (clang, -O0):
<descramble>:
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    -0x18(%rbp),%rsi
lea    -0x20(%rbp),%rdx
call   1060 <read_file@plt>
mov    -0x10(%rbp),%rdi
lea    0xe54(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11e5 <descramble+0x75>
mov    0x2e19(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe2f(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   1090 <fwrite@plt>
cmp    -0x20(%rbp),%rax
je     1226 <descramble+0xb6>
mov    0x2dd4(%rip),%rax
mov    (%rax),%edi
lea    0xe00(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<descramble>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
movq   $0x0,0x10(%rsp)
movq   $0x0,0x8(%rsp)
lea    0x10(%rsp),%rsi
lea    0x8(%rsp),%rdx
call   1060 <read_file@plt>
lea    0xe5e(%rip),%rsi
mov    %r14,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     11ea <descramble+0x7a>
mov    %rax,%rbx
mov    0x10(%rsp),%rdi
mov    0x8(%rsp),%rdx
mov    $0x1,%esi
mov    %rax,%rcx
call   1090 <fwrite@plt>
cmp    0x8(%rsp),%rax
jne    120e <descramble+0x9e>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    0x10(%rsp),%rdi
call   1030 <free@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2def(%rip),%rax
mov    (%rax),%edi
lea    0xe09(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    0x2dcb(%rip),%rax
mov    (%rax),%edi
lea    0xdf7(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"wb\00"
@stderr = global i32 0
@.str.1 = [18 x i8] c"Can't open \22%s\22.\0A\00"
@.str.2 = [14 x i8] c"Write error.\0A\00"
define void @descramble(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 @read_file(i8* %8, i8** %5, i64* %6)
%10 = load i8*, i8** %4
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %7
%12 = load i32*, i32** %7
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %19
14: ; preds = %2
%15 = load i32, i32* @stderr
%16 = load i8*, i8** %4
%17 = call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %16)
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %2
%20 = load i8*, i8** %5
%21 = load i64, i64* %6
%22 = load i32*, i32** %7
%23 = call i64 @fwrite(i8* %20, i32 1, i64 %21, i32* %22)
%24 = load i64, i64* %6
%25 = icmp ne i64 %23, %24
br i1 %25, label %26, label %30
26: ; preds = %19
%27 = load i32, i32* @stderr
%28 = call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %19
%31 = load i32*, i32** %7
%32 = call i32 @fclose(i32* %31)
%33 = load i8*, i8** %5
%34 = call i32 @free(i8* %33)
ret void
}

LLVM IR (-O2):
@.str = [3 x i8] c"wb\00"
@stderr = global i32 0
@.str.1 = [18 x i8] c"Can't open \22%s\22.\0A\00"
@.str.2 = [14 x i8] c"Write error.\0A\00"
define void @descramble(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = bitcast i8** %3 to i8*
store i8* null, i8** %3
%6 = bitcast i64* %4 to i8*
store i64 0, i64* %4
%7 = call i32 @read_file(i8* %0, i8** %3, i64* %4)
%8 = call i32* @fopen(i8* %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %14
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = call i32 (i32, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %1)
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %2
%15 = load i8*, i8** %3
%16 = load i64, i64* %4
%17 = call i64 @fwrite(i8* %15, i32 1, i64 %16, i32* %8)
%18 = load i64, i64* %4
%19 = icmp eq i64 %17, %18
br i1 %19, label %24, label %20
20: ; preds = %14
%21 = load i32, i32* @stderr
%22 = call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %14
%25 = call i32 @fclose(i32* %8)
%26 = load i8*, i8** %3
%27 = call i32 @free(i8* %26)
ret void
}