#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__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ abort();
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ deleteFiles(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ gen_random(char *, int);
char *malloc(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int rand();
int /*<<< orphan*/ save(char *, int);
int /*<<< orphan*/ sprintf(char *, char *, ...);
int /*<<< orphan*/ srand(int /*<<< orphan*/);
int system(char *);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
scalar_t__ wait(int *);
int main(int argc, char *argv[]) {
  pid_t pids[1];
  int i;
  int n = 1;
  assert(argc == 2);
  srand(time(NULL));
  for (i = 0; i < n; ++i) {
    if ((pids[i] = fork()) < 0) {
      perror("fork");
      abort();
    } else if (pids[i] == 0) {
      printf("in child %d\n", i);
      int length = rand() % 4096 + 1;
      char *str = malloc(length * sizeof(char));
      gen_random(str, length - 1);
      printf("curr string: %s\n", str);
      save(str, i);
      char filepath[80];
      sprintf(filepath, "input_%d.txt", i);
      char filepathOut[80];
      sprintf(filepathOut, "output_%d.txt", i);
      char cmd[1024];
      sprintf(cmd,
              "cat %s | while read -r line; do echo -e $line; done | valgrind "
              "--log-file=/dev/null %s 2>&1 > %s",
              filepath, argv[1], filepathOut);
      printf("executing command: %s\n", cmd);
      int retValue = system(cmd);
      if (retValue != 0) {
        printf("exit with some problem");
      } else {
        deleteFiles(i);
      }
      free(str);
      exit(0);
    }
  }
  int status;
  pid_t pid;
  while (n > 0) {
    pid = wait(&status);
    printf("Child with PID %ld exited with status 0x%x.\n", (long)pid, status);
    --n;
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x4e0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x1,-0x20(%rbp)
cmpl   $0x2,-0x8(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   10a0 <assert@plt>
xor    %eax,%eax
mov    %eax,%edi
call   10b0 <time@plt>
mov    %eax,%edi
call   1090 <srand@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x20(%rbp),%eax
jge    13d3 <main+0x1c3>
mov    $0x0,%al
call   1120 <fork@plt>
movslq -0x1c(%rbp),%rcx
mov    %rax,-0x18(%rbp,%rcx,8)
cmp    $0x0,%rax
jge    1291 <main+0x81>
lea    0xd7b(%rip),%rdi
call   10e0 <perror@plt>
mov    $0x0,%al
call   1050 <abort@plt>
movslq -0x1c(%rbp),%rax
cmpq   $0x0,-0x18(%rbp,%rax,8)
jne    13bb <main+0x1ab>
mov    -0x1c(%rbp),%esi
lea    0xd5a(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    $0x0,%al
call   1130 <rand@plt>
mov    $0x1000,%ecx
cltd
idiv   %ecx
add    $0x1,%edx
mov    %edx,-0x24(%rbp)
movslq -0x24(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   10c0 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x24(%rbp),%esi
sub    $0x1,%esi
call   10d0 <gen_random@plt>
mov    -0x30(%rbp),%rsi
lea    0xd1e(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x30(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1080 <save@plt>
lea    -0x80(%rbp),%rdi
mov    -0x1c(%rbp),%edx
lea    0xd0e(%rip),%rsi
mov    $0x0,%al
call   10f0 <sprintf@plt>
lea    -0xd0(%rbp),%rdi
mov    -0x1c(%rbp),%edx
lea    0xd03(%rip),%rsi
mov    $0x0,%al
call   10f0 <sprintf@plt>
lea    -0x4d0(%rbp),%rdi
lea    -0x80(%rbp),%rdx
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rcx
lea    -0xd0(%rbp),%r8
lea    0xce9(%rip),%rsi
mov    $0x0,%al
call   10f0 <sprintf@plt>
lea    -0x4d0(%rbp),%rsi
lea    0xd35(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
lea    -0x4d0(%rbp),%rdi
call   1060 <system@plt>
mov    %eax,-0x4d4(%rbp)
cmpl   $0x0,-0x4d4(%rbp)
je     13a3 <main+0x193>
lea    0xd1f(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    13ab <main+0x19b>
mov    -0x1c(%rbp),%edi
call   1040 <deleteFiles@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
xor    %edi,%edi
call   1100 <exit@plt>
jmp    13c0 <main+0x1b0>
jmp    13c5 <main+0x1b5>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1258 <main+0x48>
jmp    13d8 <main+0x1c8>
cmpl   $0x0,-0x20(%rbp)
jle    141e <main+0x20e>
lea    -0x4d8(%rbp),%rdi
call   1110 <wait@plt>
mov    %rax,-0x4e0(%rbp)
mov    -0x4e0(%rbp),%rsi
mov    -0x4d8(%rbp),%edx
lea    0xcc4(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x20(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x20(%rbp)
jmp    13d8 <main+0x1c8>
mov    -0x4(%rbp),%eax
add    $0x4e0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x4a8,%rsp
mov    %rsi,%rbx
xor    %eax,%eax
cmp    $0x2,%edi
sete   %al
mov    %eax,%edi
call   10a0 <assert@plt>
xor    %edi,%edi
call   10b0 <time@plt>
mov    %eax,%edi
call   1090 <srand@plt>
xor    %eax,%eax
call   1120 <fork@plt>
test   %rax,%rax
js     1280 <main+0x70>
je     1293 <main+0x83>
lea    0xa0(%rsp),%rdi
call   1110 <wait@plt>
mov    0xa0(%rsp),%edx
lea    0xe67(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
xor    %eax,%eax
add    $0x4a8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd79(%rip),%rdi
call   10e0 <perror@plt>
xor    %eax,%eax
call   1050 <abort@plt>
lea    0xd6b(%rip),%rdi
xor    %esi,%esi
xor    %eax,%eax
call   1070 <printf@plt>
xor    %eax,%eax
call   1130 <rand@plt>
lea    0xfff(%rax),%ecx
test   %eax,%eax
cmovns %eax,%ecx
and    $0xfffff000,%ecx
mov    %eax,%ebp
sub    %ecx,%ebp
neg    %ecx
lea    (%rax,%rcx,1),%edi
add    $0x1,%edi
call   10c0 <malloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    %ebp,%esi
call   10d0 <gen_random@plt>
lea    0xd32(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %r14,%rdi
xor    %esi,%esi
call   1080 <save@plt>
lea    0xd28(%rip),%rsi
lea    0x50(%rsp),%r15
mov    %r15,%rdi
xor    %edx,%edx
xor    %eax,%eax
call   10f0 <sprintf@plt>
lea    0xd1d(%rip),%rsi
mov    %rsp,%rbp
mov    %rbp,%rdi
xor    %edx,%edx
xor    %eax,%eax
call   10f0 <sprintf@plt>
mov    0x8(%rbx),%rcx
lea    0xd11(%rip),%rsi
lea    0xa0(%rsp),%rbx
mov    %rbx,%rdi
mov    %r15,%rdx
mov    %rbp,%r8
xor    %eax,%eax
call   10f0 <sprintf@plt>
lea    0xd53(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %rbx,%rdi
call   1060 <system@plt>
test   %eax,%eax
jne    136b <main+0x15b>
xor    %edi,%edi
call   1040 <deleteFiles@plt>
jmp    1379 <main+0x169>
lea    0xd44(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %edi,%edi
call   1100 <exit@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"fork\00"
@.str.1 = [13 x i8] c"in child %d\0A\00"
@.str.2 = [17 x i8] c"curr string: %s\0A\00"
@.str.3 = [13 x i8] c"input_%d.txt\00"
@.str.4 = [14 x i8] c"output_%d.txt\00"
@.str.5 = [97 x i8] c"cat %s | while read -r line; do echo -e $line; done | valgrind --log-file=/dev/null %s 2>&1 > %s\00"
@.str.6 = [23 x i8] c"executing command: %s\0A\00"
@.str.7 = [23 x i8] c"exit with some problem\00"
@.str.8 = [45 x i8] c"Child with PID %ld exited with status 0x%x.\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [1 x i64]
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca [80 x i8]
%12 = alloca [80 x i8]
%13 = alloca [1024 x i8]
%14 = alloca i32
%15 = alloca i32
%16 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 1, i32* %8
%17 = load i32, i32* %4
%18 = icmp eq i32 %17, 2
%19 = zext i1 %18 to i32
%20 = call i32 @assert(i32 %19)
%21 = call i32 @time(i32* null)
%22 = call i32 @srand(i32 %21)
store i32 0, i32* %7
br label %23
23: ; preds = %92, %2
%24 = load i32, i32* %7
%25 = load i32, i32* %8
%26 = icmp slt i32 %24, %25
br i1 %26, label %27, label %95
27: ; preds = %23
%28 = call i64 (...) @fork()
%29 = load i32, i32* %7
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds [1 x i64], [1 x i64]* %6, i64 0, i64 %30
store i64 %28, i64* %31
%32 = icmp slt i64 %28, 0
br i1 %32, label %33, label %36
33: ; preds = %27
%34 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%35 = call i32 (...) @abort()
unreachable
36: ; preds = %27
%37 = load i32, i32* %7
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds [1 x i64], [1 x i64]* %6, i64 0, i64 %38
%40 = load i64, i64* %39
%41 = icmp eq i64 %40, 0
br i1 %41, label %42, label %90
42: ; preds = %36
%43 = load i32, i32* %7
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %43)
%45 = call i32 (...) @rand()
%46 = srem i32 %45, 4096
%47 = add nsw i32 %46, 1
store i32 %47, i32* %9
%48 = load i32, i32* %9
%49 = sext i32 %48 to i64
%50 = mul i64 %49, 1
%51 = trunc i64 %50 to i32
%52 = call i8* @malloc(i32 %51)
store i8* %52, i8** %10
%53 = load i8*, i8** %10
%54 = load i32, i32* %9
%55 = sub nsw i32 %54, 1
%56 = call i32 @gen_random(i8* %53, i32 %55)
%57 = load i8*, i8** %10
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i8* %57)
%59 = load i8*, i8** %10
%60 = load i32, i32* %7
%61 = call i32 @save(i8* %59, i32 %60)
%62 = getelementptr inbounds [80 x i8], [80 x i8]* %11, i64 0, i64 0
%63 = load i32, i32* %7
%64 = call i32 (i8*, i8*, ...) @sprintf(i8* %62, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i32 %63)
%65 = getelementptr inbounds [80 x i8], [80 x i8]* %12, i64 0, i64 0
%66 = load i32, i32* %7
%67 = call i32 (i8*, i8*, ...) @sprintf(i8* %65, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i32 %66)
%68 = getelementptr inbounds [1024 x i8], [1024 x i8]* %13, i64 0, i64 0
%69 = getelementptr inbounds [80 x i8], [80 x i8]* %11, i64 0, i64 0
%70 = load i8**, i8*** %5
%71 = getelementptr inbounds i8*, i8** %70, i64 1
%72 = load i8*, i8** %71
%73 = getelementptr inbounds [80 x i8], [80 x i8]* %12, i64 0, i64 0
%74 = call i32 (i8*, i8*, ...) @sprintf(i8* %68, i8* getelementptr inbounds ([97 x i8], [97 x i8]* @.str.5, i64 0, i64 0), i8* %69, i8* %72, i8* %73)
%75 = getelementptr inbounds [1024 x i8], [1024 x i8]* %13, i64 0, i64 0
%76 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.6, i64 0, i64 0), i8* %75)
%77 = getelementptr inbounds [1024 x i8], [1024 x i8]* %13, i64 0, i64 0
%78 = call i32 @system(i8* %77)
store i32 %78, i32* %14
%79 = load i32, i32* %14
%80 = icmp ne i32 %79, 0
br i1 %80, label %81, label %83
81: ; preds = %42
%82 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.7, i64 0, i64 0))
br label %86
83: ; preds = %42
%84 = load i32, i32* %7
%85 = call i32 @deleteFiles(i32 %84)
br label %86
86: ; preds = %83, %81
%87 = load i8*, i8** %10
%88 = call i32 @free(i8* %87)
%89 = call i32 @exit(i32 0)
unreachable
90: ; preds = %36
br label %91
91: ; preds = %90
br label %92
92: ; preds = %91
%93 = load i32, i32* %7
%94 = add nsw i32 %93, 1
store i32 %94, i32* %7
br label %23
95: ; preds = %23
br label %96
96: ; preds = %99, %95
%97 = load i32, i32* %8
%98 = icmp sgt i32 %97, 0
br i1 %98, label %99, label %106
99: ; preds = %96
%100 = call i64 @wait(i32* %15)
store i64 %100, i64* %16
%101 = load i64, i64* %16
%102 = load i32, i32* %15
%103 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.8, i64 0, i64 0), i64 %101, i32 %102)
%104 = load i32, i32* %8
%105 = add nsw i32 %104, -1
store i32 %105, i32* %8
br label %96
106: ; preds = %96
%107 = load i32, i32* %3
ret i32 %107
}

LLVM IR (-O2):
@.str = [5 x i8] c"fork\00"
@.str.1 = [13 x i8] c"in child %d\0A\00"
@.str.2 = [17 x i8] c"curr string: %s\0A\00"
@.str.3 = [13 x i8] c"input_%d.txt\00"
@.str.4 = [14 x i8] c"output_%d.txt\00"
@.str.5 = [97 x i8] c"cat %s | while read -r line; do echo -e $line; done | valgrind --log-file=/dev/null %s 2>&1 > %s\00"
@.str.6 = [23 x i8] c"executing command: %s\0A\00"
@.str.7 = [23 x i8] c"exit with some problem\00"
@.str.8 = [45 x i8] c"Child with PID %ld exited with status 0x%x.\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [80 x i8]
%4 = alloca [80 x i8]
%5 = alloca [1024 x i8]
%6 = alloca i32
%7 = icmp eq i32 %0, 2
%8 = zext i1 %7 to i32
%9 = tail call i32 @assert(i32 %8)
%10 = tail call i32 @time(i32* null)
%11 = tail call i32 @srand(i32 %10)
%12 = tail call i64 (...) @fork()
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%16 = tail call i32 (...) @abort()
unreachable
17: ; preds = %2
%18 = icmp eq i64 %12, 0
br i1 %18, label %19, label %46
19: ; preds = %17
%20 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 0)
%21 = tail call i32 (...) @rand()
%22 = srem i32 %21, 4096
%23 = add nsw i32 %22, 1
%24 = tail call i8* @malloc(i32 %23)
%25 = tail call i32 @gen_random(i8* %24, i32 %22)
%26 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i8* %24)
%27 = tail call i32 @save(i8* %24, i32 0)
%28 = getelementptr inbounds [80 x i8], [80 x i8]* %3, i64 0, i64 0
%29 = call i32 (i8*, i8*, ...) @sprintf(i8* %28, i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i32 0)
%30 = getelementptr inbounds [80 x i8], [80 x i8]* %4, i64 0, i64 0
%31 = call i32 (i8*, i8*, ...) @sprintf(i8* %30, i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i32 0)
%32 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%33 = getelementptr inbounds i8*, i8** %1, i64 1
%34 = load i8*, i8** %33
%35 = call i32 (i8*, i8*, ...) @sprintf(i8* %32, i8* dereferenceable(1) getelementptr inbounds ([97 x i8], [97 x i8]* @.str.5, i64 0, i64 0), i8* %28, i8* %34, i8* %30)
%36 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.6, i64 0, i64 0), i8* %32)
%37 = call i32 @system(i8* %32)
%38 = icmp eq i32 %37, 0
br i1 %38, label %41, label %39
39: ; preds = %19
%40 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.7, i64 0, i64 0))
br label %43
41: ; preds = %19
%42 = call i32 @deleteFiles(i32 0)
br label %43
43: ; preds = %41, %39
%44 = call i32 @free(i8* %24)
%45 = call i32 @exit(i32 0)
unreachable
46: ; preds = %17
%47 = bitcast i32* %6 to i8*
%48 = call i64 @wait(i32* %6)
%49 = load i32, i32* %6
%50 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([45 x i8], [45 x i8]* @.str.8, i64 0, i64 0), i64 %48, i32 %49)
ret i32 0
}