#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 */

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int fork();
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ read(int, int *, int);
int /*<<< orphan*/ sleep(int);
int main() {
  int pid, n;
  int fdi;
  printf("Shared file pointer\n");
  fdi = open("e12in.bin", O_RDONLY);
  pid = fork();
  if (pid == 0) {
    printf("Child running ...\n");
    while (read(fdi, &n, sizeof(int))) {
      printf("Child reading %d\n", n);
      sleep(1);
    }
  } else {
    printf("Parent running ...\n");
    while (read(fdi, &n, sizeof(int))) {
      printf("Parent reading %d\n", n);
      sleep(1);
    }
  }
  close(fdi);
  return (0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe8a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2e64(%rip),%rax
mov    (%rax),%esi
lea    0xe88(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x10(%rbp)
mov    $0x0,%al
call   1080 <fork@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jne    11f7 <main+0x97>
lea    0xe6f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x10(%rbp),%edi
lea    -0xc(%rbp),%rsi
mov    $0x4,%edx
call   1050 <read@plt>
cmp    $0x0,%rax
je     11f2 <main+0x92>
mov    -0xc(%rbp),%esi
lea    0xe56(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
jmp    11b7 <main+0x57>
jmp    1245 <main+0xe5>
lea    0xe46(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x10(%rbp),%edi
lea    -0xc(%rbp),%rsi
mov    $0x4,%edx
call   1050 <read@plt>
cmp    $0x0,%rax
je     1240 <main+0xe0>
mov    -0xc(%rbp),%esi
lea    0xe2e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
jmp    1205 <main+0xa5>
jmp    1245 <main+0xe5>
mov    -0x10(%rbp),%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
lea    0xeaf(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e5c(%rip),%rax
mov    (%rax),%esi
lea    0xe6b(%rip),%rdi
call   1070 <open@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1090 <fork@plt>
test   %eax,%eax
je     120e <main+0x9e>
lea    0xe95(%rip),%rdi
call   1030 <puts@plt>
lea    0xc(%rsp),%rsi
mov    %ebx,%edi
mov    $0x4,%edx
call   1060 <read@plt>
test   %rax,%rax
je     126c <main+0xfc>
lea    0xe48(%rip),%r14
lea    0xc(%rsp),%r15
nopl   0x0(%rax)
mov    0xc(%rsp),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x4,%edx
call   1060 <read@plt>
test   %rax,%rax
jne    11e0 <main+0x70>
jmp    126c <main+0xfc>
lea    0xe41(%rip),%rdi
call   1030 <puts@plt>
lea    0xc(%rsp),%rsi
mov    %ebx,%edi
mov    $0x4,%edx
call   1060 <read@plt>
test   %rax,%rax
je     126c <main+0xfc>
lea    0xdd3(%rip),%r14
lea    0xc(%rsp),%r15
nopl   0x0(%rax)
mov    0xc(%rsp),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x4,%edx
call   1060 <read@plt>
test   %rax,%rax
jne    1240 <main+0xd0>
mov    %ebx,%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [21 x i8] c"Shared file pointer\0A\00"
@.str.1 = [10 x i8] c"e12in.bin\00"
@O_RDONLY = global i32 0
@.str.2 = [19 x i8] c"Child running ...\0A\00"
@.str.3 = [18 x i8] c"Child reading %d\0A\00"
@.str.4 = [20 x i8] c"Parent running ...\0A\00"
@.str.5 = [19 x i8] c"Parent reading %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 0, i32* %1
%5 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%6 = load i32, i32* @O_RDONLY
%7 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %6)
store i32 %7, i32* %4
%8 = call i32 (...) @fork()
store i32 %8, i32* %2
%9 = load i32, i32* %2
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %22
11: ; preds = %0
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
br label %13
13: ; preds = %17, %11
%14 = load i32, i32* %4
%15 = call i64 @read(i32 %14, i32* %3, i32 4)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %13
%18 = load i32, i32* %3
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i32 %18)
%20 = call i32 @sleep(i32 1)
br label %13
21: ; preds = %13
br label %33
22: ; preds = %0
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0))
br label %24
24: ; preds = %28, %22
%25 = load i32, i32* %4
%26 = call i64 @read(i32 %25, i32* %3, i32 4)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %32
28: ; preds = %24
%29 = load i32, i32* %3
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0), i32 %29)
%31 = call i32 @sleep(i32 1)
br label %24
32: ; preds = %24
br label %33
33: ; preds = %32, %21
%34 = load i32, i32* %4
%35 = call i32 @close(i32 %34)
ret i32 0
}

LLVM IR (-O2):
@.str.1 = [10 x i8] c"e12in.bin\00"
@O_RDONLY = global i32 0
@.str.3 = [18 x i8] c"Child reading %d\0A\00"
@.str.5 = [19 x i8] c"Parent reading %d\0A\00"
@str = [20 x i8] c"Shared file pointer\00"
@str.6 = [19 x i8] c"Parent running ...\00"
@str.7 = [18 x i8] c"Child running ...\00"
define i32 @main() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str, i64 0, i64 0))
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %4)
%6 = tail call i32 (...) @fork()
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %18
8: ; preds = %0
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.7, i64 0, i64 0))
%10 = call i64 @read(i32 %5, i32* %1, i32 4)
%11 = icmp eq i64 %10, 0
br i1 %11, label %28, label %12
12: ; preds = %8, %12
%13 = load i32, i32* %1
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i32 %13)
%15 = tail call i32 @sleep(i32 1)
%16 = call i64 @read(i32 %5, i32* %1, i32 4)
%17 = icmp eq i64 %16, 0
br i1 %17, label %28, label %12
18: ; preds = %0
%19 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.6, i64 0, i64 0))
%20 = call i64 @read(i32 %5, i32* %1, i32 4)
%21 = icmp eq i64 %20, 0
br i1 %21, label %28, label %22
22: ; preds = %18, %22
%23 = load i32, i32* %1
%24 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0), i32 %23)
%25 = tail call i32 @sleep(i32 1)
%26 = call i64 @read(i32 %5, i32* %1, i32 4)
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %22
28: ; preds = %22, %12, %18, %8
%29 = tail call i32 @close(i32 %5)
ret i32 0
}