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

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int atoi(char *);
int errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int getpid();
int getppid();
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ *stderr;
int /*<<< orphan*/ wait(int /*<<< orphan*/);
int main(int argc, char **argv) {
  pid_t pid;
  int i, chain;
  FILE *fileName = fopen("lab3.log", "w+");
  if (!fileName) {
    fprintf(stderr, "Error : Could not open file.\n");
    exit(EXIT_FAILURE);
  }
  if (argc != 2) {
    fprintf(stderr, "Not enough command line arguments\n");
    exit(EXIT_FAILURE);
  }
  chain = atoi(argv[1]);
  printf("Creating chain of %d processes.\n", chain);
  for (i = 1; i <= chain; i++) {
    fflush(fileName);
    if ((pid = fork()) == -1) {
      fprintf(stderr, "Can't fork, error %d\n", errno);
      exit(EXIT_FAILURE);
    }
    if (pid == 0)
      fprintf(fileName, "PID %d Starting\n", getppid());
    else {
      wait(0);
      fprintf(fileName, "PID %d Ending\n", getpid());
      exit(0);
    }
  }
  fclose(fileName);
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe33(%rip),%rdi
lea    0xe35(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    120e <main+0x5e>
mov    0x2df1(%rip),%rax
mov    (%rax),%rdi
lea    0xe13(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2dc9(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
cmpl   $0x2,-0x8(%rbp)
je     123e <main+0x8e>
mov    0x2dc1(%rip),%rax
mov    (%rax),%rdi
lea    0xe01(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2d99(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1090 <atoi@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%esi
lea    0xdf5(%rip),%rdi
call   1050 <printf@plt>
movl   $0x1,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jg     1331 <main+0x181>
mov    -0x28(%rbp),%rdi
call   1070 <fflush@plt>
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    12bb <main+0x10b>
mov    0x2d4d(%rip),%rax
mov    (%rax),%rdi
mov    0x2d3b(%rip),%rax
mov    (%rax),%edx
lea    0xdc8(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2d1c(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
cmpl   $0x0,-0x14(%rbp)
jne    12ed <main+0x13d>
mov    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    $0x0,%al
call   10a0 <getppid@plt>
mov    -0x30(%rbp),%rdi
mov    %eax,%edx
lea    0xda3(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
jmp    131e <main+0x16e>
xor    %edi,%edi
call   10c0 <wait@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x38(%rbp),%rdi
mov    %eax,%edx
lea    0xd85(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
xor    %edi,%edi
call   10b0 <exit@plt>
jmp    1323 <main+0x173>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1264 <main+0xb4>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
mov    0x2c87(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %edi,%ebp
lea    0xe30(%rip),%rdi
lea    0xe32(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
jne    11fc <main+0x3c>
mov    0x2df8(%rip),%rax
mov    (%rax),%rcx
lea    0xe1a(%rip),%rdi
mov    $0x1d,%esi
jmp    128a <main+0xca>
cmp    $0x2,%ebp
jne    1274 <main+0xb4>
mov    %rax,%rbx
mov    0x8(%r14),%rdi
call   1090 <atoi@plt>
mov    %eax,%ebp
lea    0xe37(%rip),%rdi
mov    %eax,%esi
call   1050 <printf@plt>
test   %ebp,%ebp
jle    125e <main+0x9e>
lea    0xe5c(%rip),%r14
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1070 <fflush@plt>
xor    %eax,%eax
call   10e0 <fork@plt>
test   %eax,%eax
jne    12a2 <main+0xe2>
xor    %eax,%eax
call   10a0 <getppid@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1060 <fprintf@plt>
add    $0xffffffff,%ebp
jne    1230 <main+0x70>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    0x2d5b(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    0x2d65(%rip),%rax
mov    (%rax),%rcx
lea    0xda5(%rip),%rdi
mov    $0x22,%esi
mov    $0x1,%edx
call   10c0 <fwrite@plt>
mov    0x2d35(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
cmp    $0xffffffff,%eax
jne    12ca <main+0x10a>
mov    0x2d32(%rip),%rax
mov    (%rax),%rdi
mov    0x2d20(%rip),%rax
mov    (%rax),%edx
lea    0xdad(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    1294 <main+0xd4>
xor    %edi,%edi
call   10d0 <wait@plt>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xdb6(%rip),%rsi
mov    %rbx,%rdi
mov    %eax,%edx
xor    %eax,%eax
call   1060 <fprintf@plt>
xor    %edi,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [9 x i8] c"lab3.log\00"
@.str.1 = [3 x i8] c"w+\00"
@stderr = global i32* null
@.str.2 = [30 x i8] c"Error : Could not open file.\0A\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [35 x i8] c"Not enough command line arguments\0A\00"
@.str.4 = [33 x i8] c"Creating chain of %d processes.\0A\00"
@.str.5 = [22 x i8] c"Can't fork, error %d\0A\00"
@errno = global i32 0
@.str.6 = [17 x i8] c"PID %d Starting\0A\00"
@.str.7 = [15 x i8] c"PID %d Ending\0A\00"
@EXIT_SUCCESS = global i32 0
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 i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %10, i32** %9
%11 = load i32*, i32** %9
%12 = icmp ne i32* %11, null
br i1 %12, label %18, label %13
13: ; preds = %2
%14 = load i32*, i32** @stderr
%15 = call i32 (i32*, i8*, ...) @fprintf(i32* %14, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
%16 = load i32, i32* @EXIT_FAILURE
%17 = call i32 @exit(i32 %16)
unreachable
18: ; preds = %2
%19 = load i32, i32* %4
%20 = icmp ne i32 %19, 2
br i1 %20, label %21, label %26
21: ; preds = %18
%22 = load i32*, i32** @stderr
%23 = call i32 (i32*, i8*, ...) @fprintf(i32* %22, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0))
%24 = load i32, i32* @EXIT_FAILURE
%25 = call i32 @exit(i32 %24)
unreachable
26: ; preds = %18
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 1
%29 = load i8*, i8** %28
%30 = call i32 @atoi(i8* %29)
store i32 %30, i32* %8
%31 = load i32, i32* %8
%32 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0), i32 %31)
store i32 1, i32* %7
br label %33
33: ; preds = %62, %26
%34 = load i32, i32* %7
%35 = load i32, i32* %8
%36 = icmp sle i32 %34, %35
br i1 %36, label %37, label %65
37: ; preds = %33
%38 = load i32*, i32** %9
%39 = call i32 @fflush(i32* %38)
%40 = call i32 (...) @fork()
store i32 %40, i32* %6
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %48
42: ; preds = %37
%43 = load i32*, i32** @stderr
%44 = load i32, i32* @errno
%45 = call i32 (i32*, i8*, ...) @fprintf(i32* %43, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0), i32 %44)
%46 = load i32, i32* @EXIT_FAILURE
%47 = call i32 @exit(i32 %46)
unreachable
48: ; preds = %37
%49 = load i32, i32* %6
%50 = icmp eq i32 %49, 0
br i1 %50, label %51, label %55
51: ; preds = %48
%52 = load i32*, i32** %9
%53 = call i32 (...) @getppid()
%54 = call i32 (i32*, i8*, ...) @fprintf(i32* %52, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.6, i64 0, i64 0), i32 %53)
br label %61
55: ; preds = %48
%56 = call i32 @wait(i32 0)
%57 = load i32*, i32** %9
%58 = call i32 (...) @getpid()
%59 = call i32 (i32*, i8*, ...) @fprintf(i32* %57, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.7, i64 0, i64 0), i32 %58)
%60 = call i32 @exit(i32 0)
unreachable
61: ; preds = %51
br label %62
62: ; preds = %61
%63 = load i32, i32* %7
%64 = add nsw i32 %63, 1
store i32 %64, i32* %7
br label %33
65: ; preds = %33
%66 = load i32*, i32** %9
%67 = call i32 @fclose(i32* %66)
%68 = load i32, i32* @EXIT_SUCCESS
%69 = call i32 @exit(i32 %68)
unreachable
}

LLVM IR (-O2):
@.str = [9 x i8] c"lab3.log\00"
@.str.1 = [3 x i8] c"w+\00"
@stderr = global i32* null
@.str.2 = [30 x i8] c"Error : Could not open file.\0A\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [35 x i8] c"Not enough command line arguments\0A\00"
@.str.4 = [33 x i8] c"Creating chain of %d processes.\0A\00"
@.str.5 = [22 x i8] c"Can't fork, error %d\0A\00"
@errno = global i32 0
@.str.6 = [17 x i8] c"PID %d Starting\0A\00"
@.str.7 = [15 x i8] c"PID %d Ending\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %10
5: ; preds = %2
%6 = load i32*, i32** @stderr
%7 = tail call i64 @fwrite(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i64 29, i64 1, i32* %6)
%8 = load i32, i32* @EXIT_FAILURE
%9 = tail call i32 @exit(i32 %8)
unreachable
10: ; preds = %2
%11 = icmp eq i32 %0, 2
br i1 %11, label %17, label %12
12: ; preds = %10
%13 = load i32*, i32** @stderr
%14 = tail call i64 @fwrite(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0), i64 34, i64 1, i32* %13)
%15 = load i32, i32* @EXIT_FAILURE
%16 = tail call i32 @exit(i32 %15)
unreachable
17: ; preds = %10
%18 = getelementptr inbounds i8*, i8** %1, i64 1
%19 = load i8*, i8** %18
%20 = tail call i32 @atoi(i8* %19)
%21 = tail call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0), i32 %20)
%22 = icmp slt i32 %20, 1
br i1 %22, label %43, label %23
23: ; preds = %17, %33
%24 = phi i32 [ %36, %33 ], [ 1, %17 ]
%25 = tail call i32 @fflush(i32* %3)
%26 = tail call i32 (...) @fork()
switch i32 %26, label %38 [
i32 -1, label %27
i32 0, label %33
]
27: ; preds = %23
%28 = load i32*, i32** @stderr
%29 = load i32, i32* @errno
%30 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %28, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0), i32 %29)
%31 = load i32, i32* @EXIT_FAILURE
%32 = tail call i32 @exit(i32 %31)
unreachable
33: ; preds = %23
%34 = tail call i32 (...) @getppid()
%35 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %3, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.6, i64 0, i64 0), i32 %34)
%36 = add nuw i32 %24, 1
%37 = icmp eq i32 %24, %20
br i1 %37, label %43, label %23
38: ; preds = %23
%39 = tail call i32 @wait(i32 0)
%40 = tail call i32 (...) @getpid()
%41 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %3, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.7, i64 0, i64 0), i32 %40)
%42 = tail call i32 @exit(i32 0)
unreachable
43: ; preds = %33, %17
%44 = tail call i32 @fclose(i32* %3)
%45 = load i32, i32* @EXIT_SUCCESS
%46 = tail call i32 @exit(i32 %45)
unreachable
}