#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 size_t ssize_t;
typedef int pid_t;
typedef int mode_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ O_RDONLY;
int S_IRGRP;
int S_IRUSR;
int S_IWGRP;
int S_IWUSR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int getpid();
int open(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
size_t read(int, char *, int const);
void parent(pid_t childPid) {
  int fd;
  char *path;
  mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP;
  printf("I am parent\n");
  printf("parent pid: %d, child pid: %d\n", getpid(), childPid);
  const int data_max_length = 30;
  char data[data_max_length + 1];
  ssize_t data_length;
  path = "new.txt";
  fd = open(path, O_RDONLY, mode);
  if (fd == -1) {
    perror("File open problem");
    exit(EXIT_FAILURE);
  }
  while ((data_length = read(fd, data, data_max_length)) > 0) {
    data[data_length] = '\0';
    printf("Datum je: %s\n", data);
  }
  close(fd);
}


x86 assembly (clang, -O0):
<parent>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
mov    0x2e36(%rip),%rax
mov    (%rax),%eax
mov    0x2e4d(%rip),%rcx
or     (%rcx),%eax
mov    0x2e34(%rip),%rcx
or     (%rcx),%eax
mov    0x2e43(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x14(%rbp)
lea    0xe57(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
mov    -0x4(%rbp),%edx
lea    0xe4a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1e,-0x18(%rbp)
mov    %rsp,%rax
mov    %rax,-0x20(%rbp)
lea    0xe4d(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2dfa(%rip),%rax
mov    (%rax),%esi
mov    -0x14(%rbp),%edx
call   1070 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    121f <parent+0xaf>
lea    0xe28(%rip),%rdi
call   1080 <perror@plt>
mov    0x2da8(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
jmp    1224 <parent+0xb4>
mov    -0x8(%rbp),%edi
lea    -0x50(%rbp),%rsi
mov    $0x1e,%edx
call   1060 <read@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jbe    1263 <parent+0xf3>
mov    -0x28(%rbp),%rax
movb   $0x0,-0x50(%rbp,%rax,1)
lea    0xdf3(%rip),%rdi
lea    -0x50(%rbp),%rsi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1224 <parent+0xb4>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parent>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    0x2e29(%rip),%rax
mov    0x2e42(%rip),%rcx
mov    (%rcx),%ebx
or     (%rax),%ebx
mov    0x2e27(%rip),%rax
or     (%rax),%ebx
mov    %edi,%ebp
mov    0x2e34(%rip),%rax
or     (%rax),%ebx
lea    0xe92(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe38(%rip),%rdi
mov    %eax,%esi
mov    %ebp,%edx
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2e0e(%rip),%rax
mov    (%rax),%esi
lea    0xe3c(%rip),%rdi
mov    %ebx,%edx
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     1245 <parent+0xc5>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x1e,%edx
call   1070 <read@plt>
test   %rax,%rax
je     1235 <parent+0xb5>
lea    0xe2d(%rip),%r14
mov    %rsp,%rbx
nop
movb   $0x0,(%rsp,%rax,1)
mov    %r14,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1e,%edx
call   1070 <read@plt>
test   %rax,%rax
jne    1210 <parent+0x90>
mov    %ebp,%edi
call   1060 <close@plt>
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xddb(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d68(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IWGRP = global i32 0
@.str = [13 x i8] c"I am parent\0A\00"
@.str.1 = [31 x i8] c"parent pid: %d, child pid: %d\0A\00"
@.str.2 = [8 x i8] c"new.txt\00"
@O_RDONLY = global i32 0
@.str.3 = [18 x i8] c"File open problem\00"
@EXIT_FAILURE = global i32 0
@.str.4 = [14 x i8] c"Datum je: %s\0A\00"
define void @parent(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
store i32 %0, i32* %2
%9 = load i32, i32* @S_IRUSR
%10 = load i32, i32* @S_IWUSR
%11 = or i32 %9, %10
%12 = load i32, i32* @S_IRGRP
%13 = or i32 %11, %12
%14 = load i32, i32* @S_IWGRP
%15 = or i32 %13, %14
store i32 %15, i32* %5
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%17 = call i32 (...) @getpid()
%18 = load i32, i32* %2
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i32 %17, i32 %18)
store i32 30, i32* %6
%20 = call i8* @llvm.stacksave()
store i8* %20, i8** %7
%21 = alloca i8, i64 31
store i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8** %4
%22 = load i8*, i8** %4
%23 = load i32, i32* @O_RDONLY
%24 = load i32, i32* %5
%25 = call i32 @open(i8* %22, i32 %23, i32 %24)
store i32 %25, i32* %3
%26 = load i32, i32* %3
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %32
28: ; preds = %1
%29 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
%30 = load i32, i32* @EXIT_FAILURE
%31 = call i32 @exit(i32 %30)
unreachable
32: ; preds = %1
br label %33
33: ; preds = %37, %32
%34 = load i32, i32* %3
%35 = call i64 @read(i32 %34, i8* %21, i32 30)
store i64 %35, i64* %8
%36 = icmp ugt i64 %35, 0
br i1 %36, label %37, label %41
37: ; preds = %33
%38 = load i64, i64* %8
%39 = getelementptr inbounds i8, i8* %21, i64 %38
store i8 0, i8* %39
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i8* %21)
br label %33
41: ; preds = %33
%42 = load i32, i32* %3
%43 = call i32 @close(i32 %42)
%44 = load i8*, i8** %7
ret void
}

LLVM IR (-O2):
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IWGRP = global i32 0
@.str.1 = [31 x i8] c"parent pid: %d, child pid: %d\0A\00"
@.str.2 = [8 x i8] c"new.txt\00"
@O_RDONLY = global i32 0
@.str.3 = [18 x i8] c"File open problem\00"
@EXIT_FAILURE = global i32 0
@.str.4 = [14 x i8] c"Datum je: %s\0A\00"
@str = [12 x i8] c"I am parent\00"
define void @parent(i32 %0) {
%2 = load i32, i32* @S_IRUSR
%3 = load i32, i32* @S_IWUSR
%4 = or i32 %3, %2
%5 = load i32, i32* @S_IRGRP
%6 = or i32 %4, %5
%7 = load i32, i32* @S_IWGRP
%8 = or i32 %6, %7
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
%10 = tail call i32 (...) @getpid()
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i32 %10, i32 %0)
%12 = alloca [31 x i8]
%13 = getelementptr inbounds [31 x i8], [31 x i8]* %12, i64 0, i64 0
%14 = load i32, i32* @O_RDONLY
%15 = tail call i32 @open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i32 %14, i32 %8)
%16 = icmp eq i32 %15, -1
br i1 %16, label %20, label %17
17: ; preds = %1
%18 = call i64 @read(i32 %15, i8* %13, i32 30)
%19 = icmp eq i64 %18, 0
br i1 %19, label %30, label %24
20: ; preds = %1
%21 = tail call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
%22 = load i32, i32* @EXIT_FAILURE
%23 = tail call i32 @exit(i32 %22)
unreachable
24: ; preds = %17, %24
%25 = phi i64 [ %28, %24 ], [ %18, %17 ]
%26 = getelementptr inbounds [31 x i8], [31 x i8]* %12, i64 0, i64 %25
store i8 0, i8* %26
%27 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i8* %13)
%28 = call i64 @read(i32 %15, i8* %13, i32 30)
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %24
30: ; preds = %24, %17
%31 = call i32 @close(i32 %15)
ret void
}