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

/* Variables and functions */
scalar_t__ EOF;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fork();
scalar_t__ fscanf(int /*<<< orphan*/ *, char *, int *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ setbuf(int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ *stdout;
int main() {
  int pid, n;
  FILE *fp;
  printf("Shared file pointer, but problem with fscanf\n");
  setbuf(stdout, 0);
  fp = fopen("e12in.txt", "r");
  setbuf(fp, 0);
  pid = fork();
  if (pid == 0) {
    printf("Child running ...\n");
    while (fscanf(fp, "%d", &n) != EOF) {
      printf("Child reading %d\n", n);
      sleep(1);
    }
  } else {
    printf("Parent running ...\n");
    while (fscanf(fp, "%d", &n) != EOF) {
      printf("Parent reading %d\n", n);
      sleep(1);
    }
  }
  fclose(fp);
  return (0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe7a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2e44(%rip),%rax
mov    (%rax),%rdi
xor    %esi,%esi
call   1040 <setbuf@plt>
lea    0xe89(%rip),%rdi
lea    0xe8c(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
xor    %esi,%esi
call   1040 <setbuf@plt>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jne    122b <main+0xbb>
lea    0xe5f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%rdi
lea    0xe60(%rip),%rsi
lea    -0xc(%rbp),%rdx
call   1060 <fscanf@plt>
mov    0x2deb(%rip),%rcx
cmp    (%rcx),%rax
je     1226 <main+0xb6>
mov    -0xc(%rbp),%esi
lea    0xe40(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
jmp    11e2 <main+0x72>
jmp    1282 <main+0x112>
lea    0xe30(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%rdi
lea    0xe09(%rip),%rsi
lea    -0xc(%rbp),%rdx
call   1060 <fscanf@plt>
mov    0x2d94(%rip),%rcx
cmp    (%rcx),%rax
je     127d <main+0x10d>
mov    -0xc(%rbp),%esi
lea    0xe0f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
jmp    1239 <main+0xc9>
jmp    1282 <main+0x112>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
lea    0xea0(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e38(%rip),%rax
mov    (%rax),%rdi
xor    %esi,%esi
call   1050 <setbuf@plt>
lea    0xe4f(%rip),%rdi
lea    0xe52(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,%rbx
mov    %rax,%rdi
xor    %esi,%esi
call   1050 <setbuf@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %eax,%eax
je     124e <main+0xce>
lea    0xe85(%rip),%rdi
call   1030 <puts@plt>
lea    0xe24(%rip),%rsi
lea    0xc(%rsp),%rdx
mov    %rbx,%rdi
call   1070 <fscanf@plt>
mov    0x2dec(%rip),%r13
cmp    0x0(%r13),%rax
je     12bc <main+0x13c>
lea    0xe14(%rip),%r14
lea    0xdf8(%rip),%r15
lea    0xc(%rsp),%r12
nopl   0x0(%rax)
mov    0xc(%rsp),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
call   1070 <fscanf@plt>
cmp    0x0(%r13),%rax
jne    1220 <main+0xa0>
jmp    12bc <main+0x13c>
lea    0xe1f(%rip),%rdi
call   1030 <puts@plt>
lea    0xdab(%rip),%rsi
lea    0xc(%rsp),%rdx
mov    %rbx,%rdi
call   1070 <fscanf@plt>
mov    0x2d73(%rip),%r13
cmp    0x0(%r13),%rax
je     12bc <main+0x13c>
lea    0xd8d(%rip),%r14
lea    0xd83(%rip),%r15
lea    0xc(%rsp),%r12
xchg   %ax,%ax
mov    0xc(%rsp),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
call   1070 <fscanf@plt>
cmp    0x0(%r13),%rax
jne    1290 <main+0x110>
mov    %rbx,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [46 x i8] c"Shared file pointer, but problem with fscanf\0A\00"
@stdout = global i32* null
@.str.1 = [10 x i8] c"e12in.txt\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [19 x i8] c"Child running ...\0A\00"
@.str.4 = [3 x i8] c"%d\00"
@EOF = global i64 0
@.str.5 = [18 x i8] c"Child reading %d\0A\00"
@.str.6 = [20 x i8] c"Parent running ...\0A\00"
@.str.7 = [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 ([46 x i8], [46 x i8]* @.str, i64 0, i64 0))
%6 = load i32*, i32** @stdout
%7 = call i32 @setbuf(i32* %6, i32 0)
%8 = call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %8, i32** %4
%9 = load i32*, i32** %4
%10 = call i32 @setbuf(i32* %9, i32 0)
%11 = call i32 (...) @fork()
store i32 %11, i32* %2
%12 = load i32, i32* %2
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %26
14: ; preds = %0
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
br label %16
16: ; preds = %21, %14
%17 = load i32*, i32** %4
%18 = call i64 @fscanf(i32* %17, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32* %3)
%19 = load i64, i64* @EOF
%20 = icmp ne i64 %18, %19
br i1 %20, label %21, label %25
21: ; preds = %16
%22 = load i32, i32* %3
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0), i32 %22)
%24 = call i32 @sleep(i32 1)
br label %16
25: ; preds = %16
br label %38
26: ; preds = %0
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.6, i64 0, i64 0))
br label %28
28: ; preds = %33, %26
%29 = load i32*, i32** %4
%30 = call i64 @fscanf(i32* %29, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32* %3)
%31 = load i64, i64* @EOF
%32 = icmp ne i64 %30, %31
br i1 %32, label %33, label %37
33: ; preds = %28
%34 = load i32, i32* %3
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.7, i64 0, i64 0), i32 %34)
%36 = call i32 @sleep(i32 1)
br label %28
37: ; preds = %28
br label %38
38: ; preds = %37, %25
%39 = load i32*, i32** %4
%40 = call i32 @fclose(i32* %39)
ret i32 0
}

LLVM IR (-O2):
@stdout = global i32* null
@.str.1 = [10 x i8] c"e12in.txt\00"
@.str.2 = [2 x i8] c"r\00"
@.str.4 = [3 x i8] c"%d\00"
@EOF = global i64 0
@.str.5 = [18 x i8] c"Child reading %d\0A\00"
@.str.7 = [19 x i8] c"Parent reading %d\0A\00"
@str = [45 x i8] c"Shared file pointer, but problem with fscanf\00"
@str.8 = [19 x i8] c"Parent running ...\00"
@str.9 = [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 ([45 x i8], [45 x i8]* @str, i64 0, i64 0))
%4 = load i32*, i32** @stdout
%5 = tail call i32 @setbuf(i32* %4, i32 0)
%6 = tail call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%7 = tail call i32 @setbuf(i32* %6, i32 0)
%8 = tail call i32 (...) @fork()
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %22
10: ; preds = %0
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.9, i64 0, i64 0))
%12 = call i64 @fscanf(i32* %6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32* %1)
%13 = load i64, i64* @EOF
%14 = icmp eq i64 %12, %13
br i1 %14, label %34, label %15
15: ; preds = %10, %15
%16 = load i32, i32* %1
%17 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0), i32 %16)
%18 = call i32 @sleep(i32 1)
%19 = call i64 @fscanf(i32* %6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32* %1)
%20 = load i64, i64* @EOF
%21 = icmp eq i64 %19, %20
br i1 %21, label %34, label %15
22: ; preds = %0
%23 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.8, i64 0, i64 0))
%24 = call i64 @fscanf(i32* %6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32* %1)
%25 = load i64, i64* @EOF
%26 = icmp eq i64 %24, %25
br i1 %26, label %34, label %27
27: ; preds = %22, %27
%28 = load i32, i32* %1
%29 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.7, i64 0, i64 0), i32 %28)
%30 = call i32 @sleep(i32 1)
%31 = call i64 @fscanf(i32* %6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32* %1)
%32 = load i64, i64* @EOF
%33 = icmp eq i64 %31, %32
br i1 %33, label %34, label %27
34: ; preds = %27, %15, %22, %10
%35 = call i32 @fclose(i32* %6)
ret i32 0
}