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

/* Variables and functions */
int O_CREAT;
int O_RDONLY;
scalar_t__ close(int);
int /*<<< orphan*/ execv(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, char *);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
scalar_t__ read(int, char *, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ unlink(char *);
int main(int argc, char *argv[]) {
  int fd, cnt, res;
  pid_t p;
  char buf[4096];
  ssize_t ret;
  FILE *f;
  fd = open("secret_number", O_RDONLY | O_CREAT);
  if (fd < 0) {
    perror("open");
    exit(1);
  }
  unlink("secret_number");
  p = fork();
  if (p < 0) {
    perror("fork");
    exit(1);
  }
  if (p == 0)
    execv("riddle", NULL);
  sleep(5);
  ret = read(fd, buf, 4096);
  if (ret < 0) {
    perror("write");
    exit(1);
  }
  f = fopen("out.txt", "w");
  fprintf(f, "%s\n", buf);
  if (close(fd) < 0) {
    perror("close");
    exit(1);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1040,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e10(%rip),%rax
mov    (%rax),%esi
mov    0x2e1f(%rip),%rax
or     (%rax),%esi
lea    0xe1e(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    120a <main+0x5a>
lea    0xe13(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    0xdef(%rip),%rdi
call   1030 <unlink@plt>
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jge    1242 <main+0x92>
lea    0xde0(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
cmpq   $0x0,-0x28(%rbp)
jne    125d <main+0xad>
lea    0xdc4(%rip),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   10c0 <execv@plt>
mov    $0x5,%edi
call   10b0 <sleep@plt>
mov    -0x14(%rbp),%edi
lea    -0x1030(%rbp),%rsi
mov    $0x1000,%edx
call   1050 <read@plt>
mov    %rax,-0x1038(%rbp)
cmpq   $0x0,-0x1038(%rbp)
jge    12a6 <main+0xf6>
lea    0xd88(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    0xd78(%rip),%rdi
lea    0xd79(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x1040(%rbp)
mov    -0x1040(%rbp),%rdi
lea    -0x1030(%rbp),%rdx
lea    0xd5a(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
cmp    $0x0,%rax
jge    1302 <main+0x152>
lea    0xd40(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
xor    %eax,%eax
add    $0x1040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x1000,%rsp
mov    0x2e21(%rip),%rax
mov    0x2e32(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe2f(%rip),%rdi
call   1070 <open@plt>
test   %eax,%eax
js     1262 <main+0xb2>
mov    %eax,%ebx
lea    0xe19(%rip),%rdi
call   1030 <unlink@plt>
xor    %eax,%eax
call   10d0 <fork@plt>
test   %rax,%rax
js     126b <main+0xbb>
jne    1208 <main+0x58>
lea    0xe17(%rip),%rdi
xor    %esi,%esi
call   10c0 <execv@plt>
mov    $0x5,%edi
call   10b0 <sleep@plt>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x1000,%edx
call   1050 <read@plt>
test   %rax,%rax
js     1274 <main+0xc4>
lea    0xdf8(%rip),%rdi
lea    0xdf9(%rip),%rsi
call   1080 <fopen@plt>
lea    0xdef(%rip),%rsi
mov    %rsp,%rdx
mov    %rax,%rdi
call   1060 <fprintf@plt>
mov    %ebx,%edi
call   1040 <close@plt>
test   %rax,%rax
js     127d <main+0xcd>
xor    %eax,%eax
add    $0x1000,%rsp
pop    %rbx
ret
lea    0xda5(%rip),%rdi
jmp    1284 <main+0xd4>
lea    0xda1(%rip),%rdi
jmp    1284 <main+0xd4>
lea    0xda4(%rip),%rdi
jmp    1284 <main+0xd4>
lea    0xdaf(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [14 x i8] c"secret_number\00"
@O_RDONLY = global i32 0
@O_CREAT = global i32 0
@.str.1 = [5 x i8] c"open\00"
@.str.2 = [5 x i8] c"fork\00"
@.str.3 = [7 x i8] c"riddle\00"
@.str.4 = [6 x i8] c"write\00"
@.str.5 = [8 x i8] c"out.txt\00"
@.str.6 = [2 x i8] c"w\00"
@.str.7 = [4 x i8] c"%s\0A\00"
@.str.8 = [6 x i8] c"close\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 i32
%9 = alloca i64
%10 = alloca [4096 x i8]
%11 = alloca i64
%12 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i32, i32* @O_RDONLY
%14 = load i32, i32* @O_CREAT
%15 = or i32 %13, %14
%16 = call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %15)
store i32 %16, i32* %6
%17 = load i32, i32* %6
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %2
%20 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %2
%23 = call i32 @unlink(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%24 = call i64 (...) @fork()
store i64 %24, i64* %9
%25 = load i64, i64* %9
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %22
%28 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %22
%31 = load i64, i64* %9
%32 = icmp eq i64 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %30
%34 = call i32 @execv(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32* null)
br label %35
35: ; preds = %33, %30
%36 = call i32 @sleep(i32 5)
%37 = load i32, i32* %6
%38 = getelementptr inbounds [4096 x i8], [4096 x i8]* %10, i64 0, i64 0
%39 = call i64 @read(i32 %37, i8* %38, i32 4096)
store i64 %39, i64* %11
%40 = load i64, i64* %11
%41 = icmp slt i64 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %35
%43 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%44 = call i32 @exit(i32 1)
unreachable
45: ; preds = %35
%46 = call i32* @fopen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
store i32* %46, i32** %12
%47 = load i32*, i32** %12
%48 = getelementptr inbounds [4096 x i8], [4096 x i8]* %10, i64 0, i64 0
%49 = call i32 @fprintf(i32* %47, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.7, i64 0, i64 0), i8* %48)
%50 = load i32, i32* %6
%51 = call i64 @close(i32 %50)
%52 = icmp slt i64 %51, 0
br i1 %52, label %53, label %56
53: ; preds = %45
%54 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.8, i64 0, i64 0))
%55 = call i32 @exit(i32 1)
unreachable
56: ; preds = %45
ret i32 0
}

LLVM IR (-O2):
@.str = [14 x i8] c"secret_number\00"
@O_RDONLY = global i32 0
@O_CREAT = global i32 0
@.str.1 = [5 x i8] c"open\00"
@.str.2 = [5 x i8] c"fork\00"
@.str.3 = [7 x i8] c"riddle\00"
@.str.4 = [6 x i8] c"write\00"
@.str.5 = [8 x i8] c"out.txt\00"
@.str.6 = [2 x i8] c"w\00"
@.str.7 = [4 x i8] c"%s\0A\00"
@.str.8 = [6 x i8] c"close\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [4096 x i8]
%4 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%5 = load i32, i32* @O_RDONLY
%6 = load i32, i32* @O_CREAT
%7 = or i32 %6, %5
%8 = tail call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %2
%14 = tail call i32 @unlink(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%15 = tail call i64 (...) @fork()
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %13
%18 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%19 = tail call i32 @exit(i32 1)
unreachable
20: ; preds = %13
%21 = icmp eq i64 %15, 0
br i1 %21, label %22, label %24
22: ; preds = %20
%23 = tail call i32 @execv(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32* null)
br label %24
24: ; preds = %22, %20
%25 = tail call i32 @sleep(i32 5)
%26 = call i64 @read(i32 %8, i8* %4, i32 4096)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %24
%29 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%30 = tail call i32 @exit(i32 1)
unreachable
31: ; preds = %24
%32 = tail call i32* @fopen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
%33 = call i32 @fprintf(i32* %32, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.7, i64 0, i64 0), i8* %4)
%34 = call i64 @close(i32 %8)
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %31
%37 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.8, i64 0, i64 0))
%38 = call i32 @exit(i32 1)
unreachable
39: ; preds = %31
ret i32 0
}