#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 fork();
char *getenv(char *);
int /*<<< orphan*/ kill(int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int read_eof(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
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");
  printf("Begin dio_append test...\n");
  for (i = 0; i < num_children; i++) {
    if ((pid[i] = fork()) == 0) {
      return read_eof(filename);
    } else if (pid[i] < 0) {
      perror("fork error");
      break;
    } else {
      continue;
    }
  }
  dio_append(filename);
  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   1060 <snprintf@plt>
lea    0xdf3(%rip),%rdi
call   1050 <printf@plt>
movl   $0x0,-0x48(%rbp)
mov    -0x48(%rbp),%eax
cmp    -0x44(%rbp),%eax
jge    12a0 <main+0x120>
mov    $0x0,%al
call   10a0 <fork@plt>
movslq -0x48(%rbp),%rcx
mov    %eax,-0x40(%rbp,%rcx,4)
cmp    $0x0,%eax
jne    126d <main+0xed>
mov    -0x58(%rbp),%rdi
call   1040 <read_eof@plt>
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x4c(%rbp)
jmp    12ee <main+0x16e>
movslq -0x48(%rbp),%rax
cmpl   $0x0,-0x40(%rbp,%rax,4)
jge    128d <main+0x10d>
lea    0xdaf(%rip),%rdi
call   1090 <perror@plt>
jmp    12a0 <main+0x120>
jmp    1292 <main+0x112>
mov    -0x48(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x48(%rbp)
jmp    1231 <main+0xb1>
mov    -0x58(%rbp),%rdi
call   1070 <dio_append@plt>
movl   $0x0,-0x48(%rbp)
mov    -0x48(%rbp),%eax
cmp    -0x44(%rbp),%eax
jge    12e0 <main+0x160>
movslq -0x48(%rbp),%rax
mov    -0x40(%rbp,%rax,4),%edi
mov    0x2d15(%rip),%rax
mov    (%rax),%esi
call   1080 <kill@plt>
mov    -0x48(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x48(%rbp)
jmp    12b0 <main+0x130>
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   %r14
push   %rbx
mov    0x2e62(%rip),%rax
mov    (%rax),%r14d
mov    %rsp,%rbx
mov    %r14,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xe63(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
lea    0xe58(%rip),%rcx
cmovne %rax,%rcx
lea    0xe3a(%rip),%rdx
mov    %rbx,%rdi
mov    %r14d,%esi
call   1060 <snprintf@plt>
lea    0xe4b(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %eax,%eax
je     120a <main+0x8a>
mov    %eax,%r14d
js     121b <main+0x9b>
mov    %rbx,%rdi
call   1070 <dio_append@plt>
mov    0x2de4(%rip),%rax
mov    (%rax),%esi
mov    %r14d,%edi
call   1080 <kill@plt>
xor    %eax,%eax
jmp    1212 <main+0x92>
mov    %rbx,%rdi
call   1050 <read_eof@plt>
lea    -0x10(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdf6(%rip),%rdi
call   1090 <perror@plt>
jmp    11ed <main+0x6d>

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 = [26 x i8] c"Begin dio_append test...\0A\00"
@.str.4 = [11 x i8] c"fork error\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)
%25 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %10
br label %26
26: ; preds = %47, %22
%27 = load i32, i32* %10
%28 = load i32, i32* %9
%29 = icmp slt i32 %27, %28
br i1 %29, label %30, label %50
30: ; preds = %26
%31 = call i32 (...) @fork()
%32 = load i32, i32* %10
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds [8 x i32], [8 x i32]* %8, i64 0, i64 %33
store i32 %31, i32* %34
%35 = icmp eq i32 %31, 0
br i1 %35, label %36, label %38
36: ; preds = %30
%37 = call i32 @read_eof(i8* %15)
store i32 %37, i32* %3
store i32 1, i32* %11
br label %67
38: ; preds = %30
%39 = load i32, i32* %10
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds [8 x i32], [8 x i32]* %8, i64 0, i64 %40
%42 = load i32, i32* %41
%43 = icmp slt i32 %42, 0
br i1 %43, label %44, label %46
44: ; preds = %38
%45 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
br label %50
46: ; preds = %38
br label %47
47: ; preds = %46
%48 = load i32, i32* %10
%49 = add nsw i32 %48, 1
store i32 %49, i32* %10
br label %26
50: ; preds = %44, %26
%51 = call i32 @dio_append(i8* %15)
store i32 0, i32* %10
br label %52
52: ; preds = %63, %50
%53 = load i32, i32* %10
%54 = load i32, i32* %9
%55 = icmp slt i32 %53, %54
br i1 %55, label %56, label %66
56: ; preds = %52
%57 = load i32, i32* %10
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds [8 x i32], [8 x i32]* %8, i64 0, i64 %58
%60 = load i32, i32* %59
%61 = load i32, i32* @SIGTERM
%62 = call i32 @kill(i32 %60, i32 %61)
br label %63
63: ; preds = %56
%64 = load i32, i32* %10
%65 = add nsw i32 %64, 1
store i32 %65, i32* %10
br label %52
66: ; preds = %52
store i32 0, i32* %3
store i32 1, i32* %11
br label %67
67: ; preds = %66, %36
%68 = load i8*, i8** %6
%69 = load i32, i32* %3
ret i32 %69
}

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.4 = [11 x i8] c"fork error\00"
@SIGTERM = global i32 0
@str = [25 x i8] c"Begin dio_append test...\00"
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 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str, i64 0, i64 0))
%11 = call i32 (...) @fork()
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i32 @read_eof(i8* %5)
br label %23
15: ; preds = %2
%16 = icmp slt i32 %11, 0
br i1 %16, label %17, label %19
17: ; preds = %15
%18 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
br label %19
19: ; preds = %15, %17
%20 = call i32 @dio_append(i8* %5)
%21 = load i32, i32* @SIGTERM
%22 = call i32 @kill(i32 %11, i32 %21)
br label %23
23: ; preds = %19, %13
%24 = phi i32 [ %14, %13 ], [ 0, %19 ]
ret i32 %24
}