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

/* Variables and functions */
char EOF;
int EXIT_FAILURE;
int EXIT_SUCCESS;
int FD_CLOEXEC;
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int O_CREAT;
int O_RDWR;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ WNOHANG;
int /*<<< orphan*/ execl(char *, char *, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
int fcntl(int, int /*<<< orphan*/, ...);
char fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ fork();
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ sprintf(char *, char *, int);
scalar_t__ waitpid(scalar_t__, int /*<<< orphan*/ *, int /*<<< orphan*/);
int main(void) {
  pid_t pidOfChild;
  int fd;
  char str;
  int file_flags;
  FILE *fp;
  fd = open("/tmp/file.txt", O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
  sprintf(&str, "%d", fd);
  file_flags = fcntl(fd, F_GETFL);
  file_flags = file_flags | FD_CLOEXEC;
  fcntl(fd, F_SETFL, file_flags);
  pidOfChild = fork();
  if (pidOfChild < 0) {
    perror("fork error");
    exit(EXIT_FAILURE);
  } else if (pidOfChild == 0) {
    execl("./child.exe", "child.exe", &str, NULL);
    perror("execl");
    exit(EXIT_FAILURE);
  } else {
    while (waitpid(pidOfChild, NULL, WNOHANG) <= 0) {
      printf("Parent is working\n");
      sleep(1);
    }
    printf("Child terminated\n");
    fp = fopen("/tmp/file.txt", "r");
    if (fp == NULL) {
      perror("Error while opening the file");
      exit(EXIT_FAILURE);
    }
    printf("Displaying contents of file /tmp/file.txt\n");
    while ((str = fgetc(fp)) != EOF)
      printf("%c", str);
    printf("\n");
    exit(EXIT_SUCCESS);
  }
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e22(%rip),%rax
mov    (%rax),%esi
mov    0x2e01(%rip),%rax
or     (%rax),%esi
mov    0x2db0(%rip),%rax
mov    (%rax),%edx
mov    0x2dd7(%rip),%rax
or     (%rax),%edx
lea    0xe06(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edx
lea    -0x15(%rbp),%rdi
lea    0xdfe(%rip),%rsi
call   10a0 <sprintf@plt>
mov    -0x14(%rbp),%edi
mov    0x2d89(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    0x2d7b(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2d74(%rip),%rax
mov    (%rax),%esi
mov    -0x1c(%rbp),%edx
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    $0x0,%al
call   10e0 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    1280 <main+0xc0>
lea    0xda4(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d27(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
cmpq   $0x0,-0x10(%rbp)
jne    12c0 <main+0x100>
lea    0xd8a(%rip),%rdi
lea    0xd85(%rip),%rsi
lea    -0x15(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%ecx
call   10c0 <execl@plt>
lea    0xd7b(%rip),%rdi
call   1090 <perror@plt>
mov    0x2ce7(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
jmp    12c5 <main+0x105>
mov    -0x10(%rbp),%rdi
mov    0x2d08(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1060 <waitpid@plt>
cmp    $0x0,%rax
jg     1302 <main+0x142>
lea    0xd42(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   10d0 <sleep@plt>
jmp    12c5 <main+0x105>
lea    0xd38(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xce9(%rip),%rdi
lea    0xcfc(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    134c <main+0x18c>
lea    0xd1a(%rip),%rdi
call   1090 <perror@plt>
mov    0x2c5b(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
lea    0xd1d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x28(%rbp),%rdi
call   1050 <fgetc@plt>
mov    %al,-0x15(%rbp)
movsbl %al,%eax
mov    0x2c60(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
je     1392 <main+0x1d2>
movsbl -0x15(%rbp),%esi
lea    0xd15(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    135a <main+0x19a>
lea    0xcb8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2be9(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2e18(%rip),%rax
mov    0x2df9(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2da6(%rip),%rax
mov    0x2dcf(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
lea    0xe66(%rip),%rdi
call   1080 <open@plt>
mov    %eax,%ebx
lea    0xdee(%rip),%rsi
lea    0xf(%rsp),%rdi
mov    %eax,%edx
call   10b0 <sprintf@plt>
mov    0x2d83(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %eax,%eax
call   1050 <fcntl@plt>
mov    0x2d79(%rip),%rcx
or     (%rcx),%eax
mov    0x2d78(%rip),%rcx
mov    (%rcx),%esi
mov    %ebx,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <fcntl@plt>
xor    %eax,%eax
call   10f0 <fork@plt>
test   %rax,%rax
jns    1265 <main+0x95>
lea    0xda3(%rip),%rdi
jmp    12ff <main+0x12f>
jne    128a <main+0xba>
lea    0xda0(%rip),%rdi
lea    0xd9b(%rip),%rsi
lea    0xf(%rsp),%rdx
xor    %ecx,%ecx
call   10d0 <execl@plt>
lea    0xd92(%rip),%rdi
jmp    12ff <main+0x12f>
mov    %rax,%rbx
mov    0x2d44(%rip),%r15
mov    (%r15),%edx
mov    %rax,%rdi
xor    %esi,%esi
call   1070 <waitpid@plt>
test   %rax,%rax
jg     12d4 <main+0x104>
lea    0xdcb(%rip),%r14
nopl   (%rax)
mov    %r14,%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   10e0 <sleep@plt>
mov    (%r15),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <waitpid@plt>
test   %rax,%rax
jle    12b0 <main+0xe0>
lea    0xd62(%rip),%rdi
call   1040 <puts@plt>
lea    0xd83(%rip),%rdi
lea    0xd1e(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
jne    1312 <main+0x142>
lea    0xd21(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2c95(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    %rax,%rbx
lea    0xd32(%rip),%rdi
call   1040 <puts@plt>
mov    %rbx,%rdi
call   1060 <fgetc@plt>
mov    0x2ca0(%rip),%r14
cmp    (%r14),%al
je     1355 <main+0x185>
cs nopw 0x0(%rax,%rax,1)
nop
movsbl %al,%edi
call   1030 <putchar@plt>
mov    %rbx,%rdi
call   1060 <fgetc@plt>
cmp    (%r14),%al
jne    1340 <main+0x170>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    0x2c2a(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@.str = [14 x i8] c"/tmp/file.txt\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.1 = [3 x i8] c"%d\00"
@F_GETFL = global i32 0
@FD_CLOEXEC = global i32 0
@F_SETFL = global i32 0
@.str.2 = [11 x i8] c"fork error\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [12 x i8] c"./child.exe\00"
@.str.4 = [10 x i8] c"child.exe\00"
@.str.5 = [6 x i8] c"execl\00"
@WNOHANG = global i32 0
@.str.6 = [19 x i8] c"Parent is working\0A\00"
@.str.7 = [18 x i8] c"Child terminated\0A\00"
@.str.8 = [2 x i8] c"r\00"
@.str.9 = [29 x i8] c"Error while opening the file\00"
@.str.10 = [43 x i8] c"Displaying contents of file /tmp/file.txt\0A\00"
@EOF = global i8 0
@.str.11 = [3 x i8] c"%c\00"
@.str.12 = [2 x i8] c"\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i32
%4 = alloca i8
%5 = alloca i32
%6 = alloca i32*
store i32 0, i32* %1
%7 = load i32, i32* @O_CREAT
%8 = load i32, i32* @O_RDWR
%9 = or i32 %7, %8
%10 = load i32, i32* @S_IRUSR
%11 = load i32, i32* @S_IWUSR
%12 = or i32 %10, %11
%13 = call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %9, i32 %12)
store i32 %13, i32* %3
%14 = load i32, i32* %3
%15 = call i32 @sprintf(i8* %4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %14)
%16 = load i32, i32* %3
%17 = load i32, i32* @F_GETFL
%18 = call i32 (i32, i32, ...) @fcntl(i32 %16, i32 %17)
store i32 %18, i32* %5
%19 = load i32, i32* %5
%20 = load i32, i32* @FD_CLOEXEC
%21 = or i32 %19, %20
store i32 %21, i32* %5
%22 = load i32, i32* %3
%23 = load i32, i32* @F_SETFL
%24 = load i32, i32* %5
%25 = call i32 (i32, i32, ...) @fcntl(i32 %22, i32 %23, i32 %24)
%26 = call i64 (...) @fork()
store i64 %26, i64* %2
%27 = load i64, i64* %2
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %0
%30 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%31 = load i32, i32* @EXIT_FAILURE
%32 = call i32 @exit(i32 %31)
unreachable
33: ; preds = %0
%34 = load i64, i64* %2
%35 = icmp eq i64 %34, 0
br i1 %35, label %36, label %41
36: ; preds = %33
%37 = call i32 @execl(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8* %4, i32* null)
%38 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
%39 = load i32, i32* @EXIT_FAILURE
%40 = call i32 @exit(i32 %39)
unreachable
41: ; preds = %33
br label %42
42: ; preds = %47, %41
%43 = load i64, i64* %2
%44 = load i32, i32* @WNOHANG
%45 = call i64 @waitpid(i64 %43, i32* null, i32 %44)
%46 = icmp sle i64 %45, 0
br i1 %46, label %47, label %50
47: ; preds = %42
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0))
%49 = call i32 @sleep(i32 1)
br label %42
50: ; preds = %42
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.7, i64 0, i64 0))
%52 = call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.8, i64 0, i64 0))
store i32* %52, i32** %6
%53 = load i32*, i32** %6
%54 = icmp eq i32* %53, null
br i1 %54, label %55, label %59
55: ; preds = %50
%56 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.9, i64 0, i64 0))
%57 = load i32, i32* @EXIT_FAILURE
%58 = call i32 @exit(i32 %57)
unreachable
59: ; preds = %50
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.10, i64 0, i64 0))
br label %61
61: ; preds = %68, %59
%62 = load i32*, i32** %6
%63 = call signext i8 @fgetc(i32* %62)
store i8 %63, i8* %4
%64 = sext i8 %63 to i32
%65 = load i8, i8* @EOF
%66 = sext i8 %65 to i32
%67 = icmp ne i32 %64, %66
br i1 %67, label %68, label %72
68: ; preds = %61
%69 = load i8, i8* %4
%70 = sext i8 %69 to i32
%71 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.11, i64 0, i64 0), i32 %70)
br label %61
72: ; preds = %61
%73 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.12, i64 0, i64 0))
%74 = load i32, i32* @EXIT_SUCCESS
%75 = call i32 @exit(i32 %74)
unreachable
}

LLVM IR (-O2):
@.str = [14 x i8] c"/tmp/file.txt\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.1 = [3 x i8] c"%d\00"
@F_GETFL = global i32 0
@FD_CLOEXEC = global i32 0
@F_SETFL = global i32 0
@.str.2 = [11 x i8] c"fork error\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [12 x i8] c"./child.exe\00"
@.str.4 = [10 x i8] c"child.exe\00"
@.str.5 = [6 x i8] c"execl\00"
@WNOHANG = global i32 0
@.str.8 = [2 x i8] c"r\00"
@.str.9 = [29 x i8] c"Error while opening the file\00"
@EOF = global i8 0
@EXIT_SUCCESS = global i32 0
@str = [17 x i8] c"Child terminated\00"
@str.13 = [42 x i8] c"Displaying contents of file /tmp/file.txt\00"
@str.14 = [18 x i8] c"Parent is working\00"
define i32 @main() {
%1 = alloca i8
%2 = load i32, i32* @O_CREAT
%3 = load i32, i32* @O_RDWR
%4 = or i32 %3, %2
%5 = load i32, i32* @S_IRUSR
%6 = load i32, i32* @S_IWUSR
%7 = or i32 %6, %5
%8 = tail call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %4, i32 %7)
%9 = call i32 @sprintf(i8* %1, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %8)
%10 = load i32, i32* @F_GETFL
%11 = tail call i32 (i32, i32, ...) @fcntl(i32 %8, i32 %10)
%12 = load i32, i32* @FD_CLOEXEC
%13 = or i32 %12, %11
%14 = load i32, i32* @F_SETFL
%15 = tail call i32 (i32, i32, ...) @fcntl(i32 %8, i32 %14, i32 %13)
%16 = tail call i64 (...) @fork()
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %22
18: ; preds = %0
%19 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%20 = load i32, i32* @EXIT_FAILURE
%21 = tail call i32 @exit(i32 %20)
unreachable
22: ; preds = %0
%23 = icmp eq i64 %16, 0
br i1 %23, label %28, label %24
24: ; preds = %22
%25 = load i32, i32* @WNOHANG
%26 = tail call i64 @waitpid(i64 %16, i32* null, i32 %25)
%27 = icmp slt i64 %26, 1
br i1 %27, label %33, label %39
28: ; preds = %22
%29 = call i32 @execl(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8* %1, i32* null)
%30 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
%31 = load i32, i32* @EXIT_FAILURE
%32 = call i32 @exit(i32 %31)
unreachable
33: ; preds = %24, %33
%34 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.14, i64 0, i64 0))
%35 = tail call i32 @sleep(i32 1)
%36 = load i32, i32* @WNOHANG
%37 = tail call i64 @waitpid(i64 %16, i32* null, i32 %36)
%38 = icmp slt i64 %37, 1
br i1 %38, label %33, label %39
39: ; preds = %33, %24
%40 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0))
%41 = tail call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.8, i64 0, i64 0))
%42 = icmp eq i32* %41, null
br i1 %42, label %43, label %47
43: ; preds = %39
%44 = tail call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.9, i64 0, i64 0))
%45 = load i32, i32* @EXIT_FAILURE
%46 = tail call i32 @exit(i32 %45)
unreachable
47: ; preds = %39
%48 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @str.13, i64 0, i64 0))
%49 = tail call signext i8 @fgetc(i32* %41)
%50 = load i8, i8* @EOF
%51 = icmp eq i8 %49, %50
br i1 %51, label %59, label %52
52: ; preds = %47, %52
%53 = phi i8 [ %56, %52 ], [ %49, %47 ]
%54 = sext i8 %53 to i32
%55 = tail call i32 @putchar(i32 %54)
%56 = tail call signext i8 @fgetc(i32* %41)
%57 = load i8, i8* @EOF
%58 = icmp eq i8 %56, %57
br i1 %58, label %59, label %52
59: ; preds = %52, %47
%60 = tail call i32 @putchar(i32 10)
%61 = load i32, i32* @EXIT_SUCCESS
%62 = tail call i32 @exit(i32 %61)
unreachable
}