#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*/ filename;

/* Variables and functions */
int PATH_MAX;
int /*<<< orphan*/ SIGTERM;
int /*<<< orphan*/ dio_append(char *, int);
int dio_read(char *);
int fork();
char *getenv(char *);
int /*<<< orphan*/ kill(int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
int /*<<< orphan*/ truncate(char *, int /*<<< orphan*/);
int main(int argc, char **argv) {
  char filename[PATH_MAX];
  int pid[8];
  int num_children = 1;
  int i;
  snprintf(filename, sizeof(filename), "%s/aiodio/file",
           getenv("TMP") ? getenv("TMP") : "/tmp");
  for (i = 0; i < num_children; i++) {
    if ((pid[i] = fork()) == 0) {
      return dio_read(filename);
    } else if (pid[i] < 0) {
      perror("fork error");
      break;
    } else {
      continue;
    }
  }
  for (i = 0; i < 100; i++) {
    dio_append(filename, 0);
    truncate(filename, 0);
    dio_append("junkfile", 0xaa);
    truncate("junkfile", 0);
  }
  for (i = 0; i < num_children; i++) {
    kill(pid[i], SIGTERM);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e53(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x58(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
movl   $0x1,-0x44(%rbp)
mov    %eax,-0x50(%rbp)
lea    0xe3c(%rip),%rdi
call   1030 <getenv@plt>
cmp    $0x0,%rax
je     11f7 <main+0x77>
lea    0xe26(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x60(%rbp)
jmp    1207 <main+0x87>
lea    0xe15(%rip),%rax
mov    %rax,-0x60(%rbp)
jmp    1207 <main+0x87>
mov    -0x50(%rbp),%esi
mov    -0x58(%rbp),%rdi
mov    -0x60(%rbp),%rcx
lea    0xde7(%rip),%rdx
call   1040 <snprintf@plt>
movl   $0x0,-0x48(%rbp)
mov    -0x48(%rbp),%eax
cmp    -0x44(%rbp),%eax
jge    1294 <main+0x114>
mov    $0x0,%al
call   10a0 <fork@plt>
movslq -0x48(%rbp),%rcx
mov    %eax,-0x40(%rbp,%rcx,4)
cmp    $0x0,%eax
jne    1261 <main+0xe1>
mov    -0x58(%rbp),%rdi
call   1080 <dio_read@plt>
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x4c(%rbp)
jmp    132d <main+0x1ad>
movslq -0x48(%rbp),%rax
cmpl   $0x0,-0x40(%rbp,%rax,4)
jge    1281 <main+0x101>
lea    0xda1(%rip),%rdi
call   1090 <perror@plt>
jmp    1294 <main+0x114>
jmp    1286 <main+0x106>
mov    -0x48(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x48(%rbp)
jmp    1225 <main+0xa5>
movl   $0x0,-0x48(%rbp)
cmpl   $0x64,-0x48(%rbp)
jge    12e8 <main+0x168>
mov    -0x58(%rbp),%rdi
xor    %esi,%esi
call   1050 <dio_append@plt>
mov    -0x58(%rbp),%rdi
xor    %esi,%esi
call   1070 <truncate@plt>
lea    0xd61(%rip),%rdi
mov    $0xaa,%esi
call   1050 <dio_append@plt>
lea    0xd50(%rip),%rdi
xor    %esi,%esi
call   1070 <truncate@plt>
mov    -0x48(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x48(%rbp)
jmp    129b <main+0x11b>
movl   $0x0,-0x48(%rbp)
mov    -0x48(%rbp),%eax
cmp    -0x44(%rbp),%eax
jge    131f <main+0x19f>
movslq -0x48(%rbp),%rax
mov    -0x40(%rbp,%rax,4),%edi
mov    0x2cd6(%rip),%rax
mov    (%rax),%esi
call   1060 <kill@plt>
mov    -0x48(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x48(%rbp)
jmp    12ef <main+0x16f>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x4c(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    0x2e5e(%rip),%rax
mov    (%rax),%r14d
mov    %rsp,%r12
mov    %r14,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r12
mov    %r12,%rsp
lea    0xe5f(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
lea    0xe54(%rip),%rcx
cmovne %rax,%rcx
lea    0xe36(%rip),%rdx
mov    %r12,%rdi
mov    %r14d,%esi
call   1040 <snprintf@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %eax,%eax
je     1245 <main+0xc5>
mov    %eax,%r14d
js     125a <main+0xda>
mov    $0x64,%ebx
lea    0xe32(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r12,%rdi
xor    %esi,%esi
call   1050 <dio_append@plt>
mov    %r12,%rdi
xor    %esi,%esi
call   1070 <truncate@plt>
mov    %r15,%rdi
mov    $0xaa,%esi
call   1050 <dio_append@plt>
mov    %r15,%rdi
xor    %esi,%esi
call   1070 <truncate@plt>
add    $0xffffffff,%ebx
jne    1200 <main+0x80>
mov    0x2da9(%rip),%rax
mov    (%rax),%esi
mov    %r14d,%edi
call   1060 <kill@plt>
xor    %eax,%eax
jmp    124d <main+0xcd>
mov    %r12,%rdi
call   1080 <dio_read@plt>
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdb7(%rip),%rdi
call   1090 <perror@plt>
jmp    11e5 <main+0x65>

LLVM IR (-O0):
@PATH_MAX = global i32 0
@.str = [15 x i8] c"%s/aiodio/file\00"
@.str.1 = [4 x i8] c"TMP\00"
@.str.2 = [5 x i8] c"/tmp\00"
@.str.3 = [11 x i8] c"fork error\00"
@.str.4 = [9 x i8] c"junkfile\00"
@SIGTERM = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i64
%8 = alloca [8 x i32]
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* @PATH_MAX
%13 = zext i32 %12 to i64
%14 = call i8* @llvm.stacksave()
store i8* %14, i8** %6
%15 = alloca i8, i64 %13
store i64 %13, i64* %7
store i32 1, i32* %9
%16 = trunc i64 %13 to i32
%17 = call i8* @getenv(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%18 = icmp ne i8* %17, null
br i1 %18, label %19, label %21
19: ; preds = %2
%20 = call i8* @getenv(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
br label %22
21: ; preds = %2
br label %22
22: ; preds = %21, %19
%23 = phi i8* [ %20, %19 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), %21 ]
%24 = call i32 @snprintf(i8* %15, i32 %16, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %23)
store i32 0, i32* %10
br label %25
25: ; preds = %46, %22
%26 = load i32, i32* %10
%27 = load i32, i32* %9
%28 = icmp slt i32 %26, %27
br i1 %28, label %29, label %49
29: ; preds = %25
%30 = call i32 (...) @fork()
%31 = load i32, i32* %10
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds [8 x i32], [8 x i32]* %8, i64 0, i64 %32
store i32 %30, i32* %33
%34 = icmp eq i32 %30, 0
br i1 %34, label %35, label %37
35: ; preds = %29
%36 = call i32 @dio_read(i8* %15)
store i32 %36, i32* %3
store i32 1, i32* %11
br label %77
37: ; preds = %29
%38 = load i32, i32* %10
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds [8 x i32], [8 x i32]* %8, i64 0, i64 %39
%41 = load i32, i32* %40
%42 = icmp slt i32 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %37
%44 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
br label %49
45: ; preds = %37
br label %46
46: ; preds = %45
%47 = load i32, i32* %10
%48 = add nsw i32 %47, 1
store i32 %48, i32* %10
br label %25
49: ; preds = %43, %25
store i32 0, i32* %10
br label %50
50: ; preds = %58, %49
%51 = load i32, i32* %10
%52 = icmp slt i32 %51, 100
br i1 %52, label %53, label %61
53: ; preds = %50
%54 = call i32 @dio_append(i8* %15, i32 0)
%55 = call i32 @truncate(i8* %15, i32 0)
%56 = call i32 @dio_append(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i32 170)
%57 = call i32 @truncate(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i32 0)
br label %58
58: ; preds = %53
%59 = load i32, i32* %10
%60 = add nsw i32 %59, 1
store i32 %60, i32* %10
br label %50
61: ; preds = %50
store i32 0, i32* %10
br label %62
62: ; preds = %73, %61
%63 = load i32, i32* %10
%64 = load i32, i32* %9
%65 = icmp slt i32 %63, %64
br i1 %65, label %66, label %76
66: ; preds = %62
%67 = load i32, i32* %10
%68 = sext i32 %67 to i64
%69 = getelementptr inbounds [8 x i32], [8 x i32]* %8, i64 0, i64 %68
%70 = load i32, i32* %69
%71 = load i32, i32* @SIGTERM
%72 = call i32 @kill(i32 %70, i32 %71)
br label %73
73: ; preds = %66
%74 = load i32, i32* %10
%75 = add nsw i32 %74, 1
store i32 %75, i32* %10
br label %62
76: ; preds = %62
store i32 0, i32* %3
store i32 1, i32* %11
br label %77
77: ; preds = %76, %35
%78 = load i8*, i8** %6
%79 = load i32, i32* %3
ret i32 %79
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [15 x i8] c"%s/aiodio/file\00"
@.str.1 = [4 x i8] c"TMP\00"
@.str.2 = [5 x i8] c"/tmp\00"
@.str.3 = [11 x i8] c"fork error\00"
@.str.4 = [9 x i8] c"junkfile\00"
@SIGTERM = global i32 0
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = load i32, i32* @PATH_MAX
%4 = zext i32 %3 to i64
%5 = alloca i8, i64 %4
%6 = tail call i8* @getenv(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%7 = icmp eq i8* %6, null
%8 = select i1 %7, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* %6
%9 = call i32 @snprintf(i8* %5, i32 %3, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %8)
%10 = call i32 (...) @fork()
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 @dio_read(i8* %5)
br label %30
14: ; preds = %2
%15 = icmp slt i32 %10, 0
br i1 %15, label %16, label %18
16: ; preds = %14
%17 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
br label %18
18: ; preds = %14, %16
br label %19
19: ; preds = %18, %19
%20 = phi i32 [ %25, %19 ], [ 0, %18 ]
%21 = call i32 @dio_append(i8* %5, i32 0)
%22 = call i32 @truncate(i8* %5, i32 0)
%23 = call i32 @dio_append(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i32 170)
%24 = call i32 @truncate(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i32 0)
%25 = add nuw nsw i32 %20, 1
%26 = icmp eq i32 %25, 100
br i1 %26, label %27, label %19
27: ; preds = %19
%28 = load i32, i32* @SIGTERM
%29 = call i32 @kill(i32 %10, i32 %28)
br label %30
30: ; preds = %27, %12
%31 = phi i32 [ %13, %12 ], [ 0, %27 ]
ret i32 %31
}