#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(int);
size_t fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fork();
size_t fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
size_t fwrite(char *, int, int, int /*<<< orphan*/ *);
char *malloc(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
int main() {
  pid_t p;
  size_t err;
  p = fork();
  if (p == -1) {
    perror("Unable to create child");
    exit(-1);
  }
  if (p == 0) {
    FILE *pfile = fopen("shared.txt", "r+");
    if (pfile == NULL) {
      perror("Child couldn't open file");
      exit(-1);
    }
    char *read;
    read = malloc(sizeof("hola mundo"));
    err = fread(read, sizeof(*read), 1, pfile);
    if (err == -1) {
      perror("Couldn't read the file");
      exit(-1);
    }
    printf("%s\n", read);
    free(read);
    err = fclose(pfile);
    if (err != 0) {
      perror("Couldn't close the file");
      exit(-1);
    }
  } else {
    FILE *pfile = fopen("shared.txt", "w+");
    if (pfile == NULL) {
      perror("Parent couldn't open file");
      exit(-1);
    }
    err = fwrite("hola mundo", sizeof("hola mundo"), 1, pfile);
    if (err == -1) {
      perror("Couldn't write in the file");
      exit(-1);
    }
    printf("done\n");
    err = fclose(pfile);
    if (err != 0) {
      perror("Couldn't close the file");
      exit(-1);
    }
    sleep(2);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11e9 <main+0x39>
lea    0xe26(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
cmpl   $0x0,-0x8(%rbp)
jne    12c0 <main+0x110>
lea    0xe1d(%rip),%rdi
lea    0xe21(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    122b <main+0x7b>
lea    0xe09(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
mov    $0xb,%edi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x1,%edx
mov    %edx,%esi
call   1040 <fread@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0xffffffffffffffff,-0x10(%rbp)
jne    1272 <main+0xc2>
lea    0xddb(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
mov    -0x20(%rbp),%rsi
lea    0xdd8(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     12bb <main+0x10b>
lea    0xdad(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
jmp    137d <main+0x1cd>
lea    0xd50(%rip),%rdi
lea    0xda3(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    12f8 <main+0x148>
lea    0xd8b(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
mov    -0x28(%rbp),%rcx
lea    0xd8b(%rip),%rdi
mov    $0xb,%esi
mov    $0x1,%edx
call   10b0 <fwrite@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0xffffffffffffffff,-0x10(%rbp)
jne    1337 <main+0x187>
lea    0xd71(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
lea    0xd76(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x28(%rbp),%rdi
call   1050 <fclose@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1373 <main+0x1c3>
lea    0xcf5(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
mov    $0x2,%edi
call   10c0 <sleep@plt>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
xor    %eax,%eax
call   10d0 <fork@plt>
test   %eax,%eax
je     122f <main+0x7f>
cmp    $0xffffffff,%eax
je     12a2 <main+0xf2>
lea    0xe48(%rip),%rdi
lea    0xe97(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     12ab <main+0xfb>
mov    %rax,%rbx
lea    0xe9c(%rip),%rdi
mov    $0xb,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   10b0 <fwrite@plt>
cmp    $0xffffffffffffffff,%rax
je     12b4 <main+0x104>
lea    0xe9f(%rip),%rdi
call   1040 <puts@plt>
mov    %rbx,%rdi
call   1060 <fclose@plt>
test   %rax,%rax
jne    1299 <main+0xe9>
mov    $0x2,%edi
call   10c0 <sleep@plt>
jmp    128f <main+0xdf>
lea    0xde1(%rip),%rdi
lea    0xde5(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     12bd <main+0x10d>
mov    %rax,%r14
mov    $0xb,%edi
call   1070 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   1050 <fread@plt>
cmp    $0xffffffffffffffff,%rax
je     12c6 <main+0x116>
mov    %rbx,%rdi
call   1040 <puts@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r14,%rdi
call   1060 <fclose@plt>
test   %rax,%rax
jne    1299 <main+0xe9>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xdb5(%rip),%rdi
jmp    12cd <main+0x11d>
lea    0xd57(%rip),%rdi
jmp    12cd <main+0x11d>
lea    0xdbe(%rip),%rdi
jmp    12cd <main+0x11d>
lea    0xdda(%rip),%rdi
jmp    12cd <main+0x11d>
lea    0xd61(%rip),%rdi
jmp    12cd <main+0x11d>
lea    0xd71(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [23 x i8] c"Unable to create child\00"
@.str.1 = [11 x i8] c"shared.txt\00"
@.str.2 = [3 x i8] c"r+\00"
@.str.3 = [25 x i8] c"Child couldn't open file\00"
@.str.4 = [23 x i8] c"Couldn't read the file\00"
@.str.5 = [4 x i8] c"%s\0A\00"
@.str.6 = [24 x i8] c"Couldn't close the file\00"
@.str.7 = [3 x i8] c"w+\00"
@.str.8 = [26 x i8] c"Parent couldn't open file\00"
@.str.9 = [11 x i8] c"hola mundo\00"
@.str.10 = [27 x i8] c"Couldn't write in the file\00"
@.str.11 = [6 x i8] c"done\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i64
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i32*
store i32 0, i32* %1
%7 = call i32 (...) @fork()
store i32 %7, i32* %2
%8 = load i32, i32* %2
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %13
10: ; preds = %0
%11 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%12 = call i32 @exit(i32 -1)
unreachable
13: ; preds = %0
%14 = load i32, i32* %2
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %46
16: ; preds = %13
%17 = call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %17, i32** %4
%18 = load i32*, i32** %4
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %23
20: ; preds = %16
%21 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0))
%22 = call i32 @exit(i32 -1)
unreachable
23: ; preds = %16
%24 = call i8* @malloc(i32 11)
store i8* %24, i8** %5
%25 = load i8*, i8** %5
%26 = load i32*, i32** %4
%27 = call i64 @fread(i8* %25, i32 1, i32 1, i32* %26)
store i64 %27, i64* %3
%28 = load i64, i64* %3
%29 = icmp eq i64 %28, -1
br i1 %29, label %30, label %33
30: ; preds = %23
%31 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0))
%32 = call i32 @exit(i32 -1)
unreachable
33: ; preds = %23
%34 = load i8*, i8** %5
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i8* %34)
%36 = load i8*, i8** %5
%37 = call i32 @free(i8* %36)
%38 = load i32*, i32** %4
%39 = call i64 @fclose(i32* %38)
store i64 %39, i64* %3
%40 = load i64, i64* %3
%41 = icmp ne i64 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %33
%43 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0))
%44 = call i32 @exit(i32 -1)
unreachable
45: ; preds = %33
br label %72
46: ; preds = %13
%47 = call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0))
store i32* %47, i32** %6
%48 = load i32*, i32** %6
%49 = icmp eq i32* %48, null
br i1 %49, label %50, label %53
50: ; preds = %46
%51 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.8, i64 0, i64 0))
%52 = call i32 @exit(i32 -1)
unreachable
53: ; preds = %46
%54 = load i32*, i32** %6
%55 = call i64 @fwrite(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.9, i64 0, i64 0), i32 11, i32 1, i32* %54)
store i64 %55, i64* %3
%56 = load i64, i64* %3
%57 = icmp eq i64 %56, -1
br i1 %57, label %58, label %61
58: ; preds = %53
%59 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.10, i64 0, i64 0))
%60 = call i32 @exit(i32 -1)
unreachable
61: ; preds = %53
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.11, i64 0, i64 0))
%63 = load i32*, i32** %6
%64 = call i64 @fclose(i32* %63)
store i64 %64, i64* %3
%65 = load i64, i64* %3
%66 = icmp ne i64 %65, 0
br i1 %66, label %67, label %70
67: ; preds = %61
%68 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0))
%69 = call i32 @exit(i32 -1)
unreachable
70: ; preds = %61
%71 = call i32 @sleep(i32 2)
br label %72
72: ; preds = %70, %45
%73 = load i32, i32* %1
ret i32 %73
}

LLVM IR (-O2):
@.str = [23 x i8] c"Unable to create child\00"
@.str.1 = [11 x i8] c"shared.txt\00"
@.str.2 = [3 x i8] c"r+\00"
@.str.3 = [25 x i8] c"Child couldn't open file\00"
@.str.4 = [23 x i8] c"Couldn't read the file\00"
@.str.6 = [24 x i8] c"Couldn't close the file\00"
@.str.7 = [3 x i8] c"w+\00"
@.str.8 = [26 x i8] c"Parent couldn't open file\00"
@.str.9 = [11 x i8] c"hola mundo\00"
@.str.10 = [27 x i8] c"Couldn't write in the file\00"
@str = [5 x i8] c"done\00"
define i32 @main() {
%1 = tail call i32 (...) @fork()
switch i32 %1, label %26 [
i32 -1, label %2
i32 0, label %5
]
2: ; preds = %0
%3 = tail call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%4 = tail call i32 @exit(i32 -1)
unreachable
5: ; preds = %0
%6 = tail call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %11
8: ; preds = %5
%9 = tail call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0))
%10 = tail call i32 @exit(i32 -1)
unreachable
11: ; preds = %5
%12 = tail call dereferenceable_or_null(11) i8* @malloc(i32 11)
%13 = tail call i64 @fread(i8* %12, i32 1, i32 1, i32* %6)
%14 = icmp eq i64 %13, -1
br i1 %14, label %15, label %18
15: ; preds = %11
%16 = tail call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0))
%17 = tail call i32 @exit(i32 -1)
unreachable
18: ; preds = %11
%19 = tail call i32 @puts(i8* dereferenceable(1) %12)
%20 = tail call i32 @free(i8* %12)
%21 = tail call i64 @fclose(i32* %6)
%22 = icmp eq i64 %21, 0
br i1 %22, label %47, label %23
23: ; preds = %18
%24 = tail call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0))
%25 = tail call i32 @exit(i32 -1)
unreachable
26: ; preds = %0
%27 = tail call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0))
%28 = icmp eq i32* %27, null
br i1 %28, label %29, label %32
29: ; preds = %26
%30 = tail call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.8, i64 0, i64 0))
%31 = tail call i32 @exit(i32 -1)
unreachable
32: ; preds = %26
%33 = tail call i64 @fwrite(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.9, i64 0, i64 0), i32 11, i32 1, i32* %27)
%34 = icmp eq i64 %33, -1
br i1 %34, label %35, label %38
35: ; preds = %32
%36 = tail call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.10, i64 0, i64 0))
%37 = tail call i32 @exit(i32 -1)
unreachable
38: ; preds = %32
%39 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @str, i64 0, i64 0))
%40 = tail call i64 @fclose(i32* %27)
%41 = icmp eq i64 %40, 0
br i1 %41, label %45, label %42
42: ; preds = %38
%43 = tail call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0))
%44 = tail call i32 @exit(i32 -1)
unreachable
45: ; preds = %38
%46 = tail call i32 @sleep(i32 2)
br label %47
47: ; preds = %18, %45
ret i32 0
}