#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*/ tmpBuf;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ fread(unsigned char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(unsigned char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int main(int argc, char **argv) {
  FILE *keyFP;
  FILE *randFP;
  unsigned char tmpBuf[32 + 16];
  if (keyFP = fopen("/etc/cduck.key", "r")) {
    fclose(keyFP);
    fprintf(stderr, "Refusing to overwrite existing /etc/cduck.key\n");
    return 1;
  }
  keyFP = fopen("/etc/cduck.key", "w");
  if (!keyFP) {
    fprintf(stderr, "Failed to open /etc/cduck.key for reading\n");
    return 1;
  }
  randFP = fopen("/dev/random", "r");
  if (!randFP) {
    fclose(keyFP);
    fprintf(stderr, "Failed to open /dev/random for reading\n");
    return 1;
  }
  fread(tmpBuf, sizeof(tmpBuf), 1, randFP);
  fwrite(tmpBuf, sizeof(tmpBuf), 1, keyFP);
  fclose(keyFP);
  fclose(randFP);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe93(%rip),%rdi
lea    0xe9b(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     11b1 <main+0x61>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
lea    0xe71(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1287 <main+0x137>
lea    0xe48(%rip),%rdi
lea    0xe81(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11f4 <main+0xa4>
mov    0x2e06(%rip),%rax
mov    (%rax),%edi
lea    0xe5f(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1287 <main+0x137>
lea    0xe72(%rip),%rdi
lea    0xe0d(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1240 <main+0xf0>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    0x2dba(%rip),%rax
mov    (%rax),%edi
lea    0xe4a(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1287 <main+0x137>
lea    -0x50(%rbp),%rdi
mov    -0x20(%rbp),%rcx
mov    $0x30,%esi
mov    $0x1,%edx
call   1030 <fread@plt>
lea    -0x50(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x30,%esi
mov    $0x1,%edx
call   1070 <fwrite@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x30,%rsp
lea    0xea0(%rip),%rdi
lea    0xea8(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     119d <main+0x4d>
mov    %rax,%rdi
call   1040 <fclose@plt>
mov    0x2e60(%rip),%rax
mov    (%rax),%edi
lea    0xe88(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe5c(%rip),%rdi
lea    0xe95(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     1217 <main+0xc7>
mov    %rax,%rbx
lea    0xeae(%rip),%rdi
lea    0xe49(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     122c <main+0xdc>
mov    %rax,%r14
mov    %rsp,%r15
mov    %r15,%rdi
mov    $0x30,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1030 <fread@plt>
mov    %r15,%rdi
mov    $0x30,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1070 <fwrite@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %r14,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
jmp    1193 <main+0x43>
mov    0x2dc2(%rip),%rax
mov    (%rax),%edi
lea    0xe1b(%rip),%rsi
jmp    1189 <main+0x39>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    0x2da5(%rip),%rax
mov    (%rax),%edi
lea    0xe35(%rip),%rsi
jmp    1189 <main+0x39>

LLVM IR (-O0):
@.str = [15 x i8] c"/etc/cduck.key\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [47 x i8] c"Refusing to overwrite existing /etc/cduck.key\0A\00"
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [43 x i8] c"Failed to open /etc/cduck.key for reading\0A\00"
@.str.5 = [12 x i8] c"/dev/random\00"
@.str.6 = [40 x i8] c"Failed to open /dev/random for reading\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca [48 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = call i32* @fopen(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %9, i32** %6
%10 = icmp ne i32* %9, null
br i1 %10, label %11, label %16
11: ; preds = %2
%12 = load i32*, i32** %6
%13 = call i32 @fclose(i32* %12)
%14 = load i32, i32* @stderr
%15 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %43
16: ; preds = %2
%17 = call i32* @fopen(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %17, i32** %6
%18 = load i32*, i32** %6
%19 = icmp ne i32* %18, null
br i1 %19, label %23, label %20
20: ; preds = %16
%21 = load i32, i32* @stderr
%22 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %3
br label %43
23: ; preds = %16
%24 = call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %24, i32** %7
%25 = load i32*, i32** %7
%26 = icmp ne i32* %25, null
br i1 %26, label %32, label %27
27: ; preds = %23
%28 = load i32*, i32** %6
%29 = call i32 @fclose(i32* %28)
%30 = load i32, i32* @stderr
%31 = call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.6, i64 0, i64 0))
store i32 1, i32* %3
br label %43
32: ; preds = %23
%33 = getelementptr inbounds [48 x i8], [48 x i8]* %8, i64 0, i64 0
%34 = load i32*, i32** %7
%35 = call i32 @fread(i8* %33, i32 48, i32 1, i32* %34)
%36 = getelementptr inbounds [48 x i8], [48 x i8]* %8, i64 0, i64 0
%37 = load i32*, i32** %6
%38 = call i32 @fwrite(i8* %36, i32 48, i32 1, i32* %37)
%39 = load i32*, i32** %6
%40 = call i32 @fclose(i32* %39)
%41 = load i32*, i32** %7
%42 = call i32 @fclose(i32* %41)
store i32 0, i32* %3
br label %43
43: ; preds = %32, %27, %20, %11
%44 = load i32, i32* %3
ret i32 %44
}

LLVM IR (-O2):
@.str = [15 x i8] c"/etc/cduck.key\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [47 x i8] c"Refusing to overwrite existing /etc/cduck.key\0A\00"
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [43 x i8] c"Failed to open /etc/cduck.key for reading\0A\00"
@.str.5 = [12 x i8] c"/dev/random\00"
@.str.6 = [40 x i8] c"Failed to open /dev/random for reading\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [48 x i8]
%4 = getelementptr inbounds [48 x i8], [48 x i8]* %3, i64 0, i64 0
%5 = tail call i32* @fopen(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %11, label %7
7: ; preds = %2
%8 = tail call i32 @fclose(i32* %5)
%9 = load i32, i32* @stderr
%10 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0))
br label %29
11: ; preds = %2
%12 = tail call i32* @fopen(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %17
14: ; preds = %11
%15 = load i32, i32* @stderr
%16 = tail call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.4, i64 0, i64 0))
br label %29
17: ; preds = %11
%18 = tail call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %24
20: ; preds = %17
%21 = tail call i32 @fclose(i32* %12)
%22 = load i32, i32* @stderr
%23 = tail call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.6, i64 0, i64 0))
br label %29
24: ; preds = %17
%25 = call i32 @fread(i8* %4, i32 48, i32 1, i32* %18)
%26 = call i32 @fwrite(i8* %4, i32 48, i32 1, i32* %12)
%27 = tail call i32 @fclose(i32* %12)
%28 = tail call i32 @fclose(i32* %18)
br label %29
29: ; preds = %24, %20, %14, %7
%30 = phi i32 [ 1, %7 ], [ 0, %24 ], [ 1, %20 ], [ 1, %14 ]
ret i32 %30
}