#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 */
scalar_t__ EAGAIN;
scalar_t__ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int getpid();
int /*<<< orphan*/ pause();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int main(int argc, char **argv) {
  FILE *f;
  int newpid;
  if (argc != 2) {
    fprintf(stderr, "Usage: %s /cgroup/.../tasks\n", argv[0]);
    return 1;
  }
  f = fopen(argv[1], "a");
  if (!f) {
    perror("fopen failed");
    return 1;
  }
  fprintf(f, "%i\n", getpid());
  fclose(f);
  newpid = fork();
  if (newpid == -1 && errno == EAGAIN)
    return 2;
  if (newpid == -1) {
    perror("fork() failed");
    return 1;
  }
  if (newpid == 0)
    pause();
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11bb <main+0x4b>
mov    0x2e49(%rip),%rax
movslq (%rax),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe58(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1296 <main+0x126>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe53(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11f6 <main+0x86>
lea    0xe3a(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1296 <main+0x126>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x28(%rbp),%rdi
mov    %eax,%edx
lea    0xe1a(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    125c <main+0xec>
mov    0x2d9b(%rip),%rax
mov    (%rax),%rax
mov    0x2d81(%rip),%rcx
cmp    (%rcx),%rax
jne    125c <main+0xec>
movl   $0x2,-0x4(%rbp)
jmp    1296 <main+0x126>
cmpl   $0xffffffff,-0x1c(%rbp)
jne    127e <main+0x10e>
lea    0xdc3(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1296 <main+0x126>
cmpl   $0x0,-0x1c(%rbp)
jne    128f <main+0x11f>
mov    $0x0,%al
call   1060 <pause@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
cmp    $0x2,%edi
jne    11ec <main+0x7c>
mov    0x8(%rsi),%rdi
lea    0xe9a(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     1220 <main+0xb0>
mov    %rax,%rbx
xor    %ebp,%ebp
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe88(%rip),%rsi
mov    %rbx,%rdi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
call   1090 <fork@plt>
test   %eax,%eax
je     120e <main+0x9e>
cmp    $0xffffffff,%eax
jne    1217 <main+0xa7>
mov    0x2e09(%rip),%rax
mov    (%rax),%rax
mov    0x2def(%rip),%rcx
mov    $0x2,%ebp
cmp    (%rcx),%rax
je     1217 <main+0xa7>
lea    0xe46(%rip),%rdi
jmp    1227 <main+0xb7>
mov    0x2ded(%rip),%rax
movslq (%rax),%rdi
mov    (%rsi),%rdx
lea    0xe00(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%ebp
jmp    1217 <main+0xa7>
xor    %ebp,%ebp
xor    %eax,%eax
call   1060 <pause@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xdf8(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%ebp
jmp    1217 <main+0xa7>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [29 x i8] c"Usage: %s /cgroup/.../tasks\0A\00"
@.str.1 = [2 x i8] c"a\00"
@.str.2 = [13 x i8] c"fopen failed\00"
@.str.3 = [4 x i8] c"%i\0A\00"
@errno = global i64 0
@EAGAIN = global i64 0
@.str.4 = [14 x i8] c"fork() failed\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* %4
%9 = icmp ne i32 %8, 2
br i1 %9, label %10, label %18
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = sext i32 %11 to i64
%13 = inttoptr i64 %12 to i32*
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 0
%16 = load i8*, i8** %15
%17 = call i32 (i32*, i8*, ...) @fprintf(i32* %13, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %16)
store i32 1, i32* %3
br label %52
18: ; preds = %2
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 1
%21 = load i8*, i8** %20
%22 = call i32* @fopen(i8* %21, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %22, i32** %6
%23 = load i32*, i32** %6
%24 = icmp ne i32* %23, null
br i1 %24, label %27, label %25
25: ; preds = %18
%26 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %52
27: ; preds = %18
%28 = load i32*, i32** %6
%29 = call i32 (...) @getpid()
%30 = call i32 (i32*, i8*, ...) @fprintf(i32* %28, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %29)
%31 = load i32*, i32** %6
%32 = call i32 @fclose(i32* %31)
%33 = call i32 (...) @fork()
store i32 %33, i32* %7
%34 = load i32, i32* %7
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %41
36: ; preds = %27
%37 = load i64, i64* @errno
%38 = load i64, i64* @EAGAIN
%39 = icmp eq i64 %37, %38
br i1 %39, label %40, label %41
40: ; preds = %36
store i32 2, i32* %3
br label %52
41: ; preds = %36, %27
%42 = load i32, i32* %7
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %46
44: ; preds = %41
%45 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %3
br label %52
46: ; preds = %41
%47 = load i32, i32* %7
%48 = icmp eq i32 %47, 0
br i1 %48, label %49, label %51
49: ; preds = %46
%50 = call i32 (...) @pause()
br label %51
51: ; preds = %49, %46
store i32 0, i32* %3
br label %52
52: ; preds = %51, %44, %40, %25, %10
%53 = load i32, i32* %3
ret i32 %53
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [29 x i8] c"Usage: %s /cgroup/.../tasks\0A\00"
@.str.1 = [2 x i8] c"a\00"
@.str.2 = [13 x i8] c"fopen failed\00"
@.str.3 = [4 x i8] c"%i\0A\00"
@errno = global i64 0
@EAGAIN = global i64 0
@.str.4 = [14 x i8] c"fork() failed\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 2
br i1 %3, label %10, label %4
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = sext i32 %5 to i64
%7 = inttoptr i64 %6 to i32*
%8 = load i8*, i8** %1
%9 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %7, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %8)
br label %30
10: ; preds = %2
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = tail call i32* @fopen(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %17
15: ; preds = %10
%16 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %30
17: ; preds = %10
%18 = tail call i32 (...) @getpid()
%19 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %13, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %18)
%20 = tail call i32 @fclose(i32* %13)
%21 = tail call i32 (...) @fork()
switch i32 %21, label %30 [
i32 -1, label %22
i32 0, label %28
]
22: ; preds = %17
%23 = load i64, i64* @errno
%24 = load i64, i64* @EAGAIN
%25 = icmp eq i64 %23, %24
br i1 %25, label %30, label %26
26: ; preds = %22
%27 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
br label %30
28: ; preds = %17
%29 = tail call i32 (...) @pause()
br label %30
30: ; preds = %17, %28, %22, %26, %15, %4
%31 = phi i32 [ 1, %4 ], [ 1, %26 ], [ 1, %15 ], [ 2, %22 ], [ 0, %28 ], [ 0, %17 ]
ret i32 %31
}