#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;

/* Variables and functions */
scalar_t__ ENOENT;
int O_CREAT;
int O_RDWR;
scalar_t__ errno;
int fork();
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int remove(char *);
int /*<<< orphan*/ sleep(int);
int strlen(char *);
int /*<<< orphan*/ wait(int *);
int write(int, char *, int);
void read_file() {
  pid_t pid;
  int fd;
  int i = 1;
  char *s1 = "hello";
  char *s2 = "world";
  char *s3 = "cccccc";
  if (remove("test.txt") == -1) {
    if (errno == ENOENT) {
      printf("remove ok\n");
    } else {
      perror("remove");
      return;
    }
  }
  if ((fd = open("test.txt", O_RDWR | O_CREAT, 0644)) == -1) {
    perror("parent open");
    return;
  }
  if (write(fd, s1, strlen(s1)) == -1) {
    perror("parent write");
    return;
  }
  if (write(fd, s3, strlen(s3)) == -1) {
    perror("parent write");
    return;
  }
  if ((pid = fork()) == -1) {
    perror("fork");
    return;
  } else if (pid == 0) {
    i = 2;
    printf("in child process\n");
    printf("i = %d\n", i);
    if (write(fd, s2, strlen(s2)) == -1) {
      perror("child write");
      return;
    }
  } else {
    sleep(1);
    printf("in parent process\n");
    printf("i = %d \n", i);
    if (write(fd, s3, strlen(s3) == -1)) {
      perror("parent write");
      return;
    }
  }
  wait(&pid);
  printf("over...\n");
}


x86 assembly (clang, -O0):
<read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x1,-0xc(%rbp)
lea    0xe5a(%rip),%rax
mov    %rax,-0x18(%rbp)
lea    0xe55(%rip),%rax
mov    %rax,-0x20(%rbp)
lea    0xe50(%rip),%rax
mov    %rax,-0x28(%rbp)
lea    0xe4c(%rip),%rdi
call   1030 <remove@plt>
cmp    $0xffffffff,%eax
jne    1218 <read_file+0x88>
mov    0x2df4(%rip),%rax
mov    (%rax),%rax
mov    0x2dda(%rip),%rcx
cmp    (%rcx),%rax
jne    1202 <read_file+0x72>
lea    0xe26(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    1213 <read_file+0x83>
lea    0xe1e(%rip),%rdi
call   1080 <perror@plt>
jmp    1400 <read_file+0x270>
jmp    1218 <read_file+0x88>
mov    0x2dc1(%rip),%rax
mov    (%rax),%esi
mov    0x2dd0(%rip),%rax
or     (%rax),%esi
lea    0xde2(%rip),%rdi
mov    $0x1a4,%edx
call   1070 <open@plt>
mov    %eax,-0x8(%rbp)
cmp    $0xffffffff,%eax
jne    1258 <read_file+0xc8>
lea    0xde0(%rip),%rdi
call   1080 <perror@plt>
jmp    1400 <read_file+0x270>
mov    -0x8(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    %eax,%edx
call   1040 <write@plt>
cmp    $0xffffffff,%eax
jne    1297 <read_file+0x107>
lea    0xdad(%rip),%rdi
call   1080 <perror@plt>
jmp    1400 <read_file+0x270>
mov    -0x8(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x28(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x44(%rbp),%edi
mov    -0x40(%rbp),%rsi
mov    %eax,%edx
call   1040 <write@plt>
cmp    $0xffffffff,%eax
jne    12d6 <read_file+0x146>
lea    0xd6e(%rip),%rdi
call   1080 <perror@plt>
jmp    1400 <read_file+0x270>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %eax,-0x4(%rbp)
cmp    $0xffffffff,%eax
jne    12fa <read_file+0x16a>
lea    0xd57(%rip),%rdi
call   1080 <perror@plt>
jmp    1400 <read_file+0x270>
cmpl   $0x0,-0x4(%rbp)
jne    136e <read_file+0x1de>
movl   $0x2,-0xc(%rbp)
lea    0xd3a(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0xc(%rbp),%esi
lea    0xd3b(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x54(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x54(%rbp),%edi
mov    -0x50(%rbp),%rsi
mov    %eax,%edx
call   1040 <write@plt>
cmp    $0xffffffff,%eax
jne    1369 <read_file+0x1d9>
lea    0xd07(%rip),%rdi
call   1080 <perror@plt>
jmp    1400 <read_file+0x270>
jmp    13e4 <read_file+0x254>
mov    $0x1,%edi
call   1090 <sleep@plt>
lea    0xcf3(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0xc(%rbp),%esi
lea    0xcf5(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x64(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x28(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x64(%rbp),%edi
mov    -0x60(%rbp),%rsi
cmp    $0xffffffff,%eax
sete   %al
and    $0x1,%al
movzbl %al,%edx
call   1040 <write@plt>
cmp    $0x0,%eax
je     13df <read_file+0x24f>
lea    0xc65(%rip),%rdi
call   1080 <perror@plt>
jmp    1400 <read_file+0x270>
jmp    13e4 <read_file+0x254>
jmp    13e9 <read_file+0x259>
lea    -0x4(%rbp),%rdi
call   10a0 <wait@plt>
lea    0xc95(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file>:
push   %rbp
push   %rbx
push   %rax
lea    0xe69(%rip),%rdi
call   1030 <remove@plt>
cmp    $0xffffffff,%eax
jne    11da <read_file+0x3a>
mov    0x2e15(%rip),%rax
mov    (%rax),%rax
mov    0x2dfb(%rip),%rcx
cmp    (%rcx),%rax
jne    1332 <read_file+0x192>
lea    0xeb4(%rip),%rdi
call   1040 <puts@plt>
mov    0x2dff(%rip),%rax
mov    0x2e10(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe20(%rip),%rdi
mov    $0x1a4,%edx
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     1317 <read_file+0x177>
mov    %eax,%ebp
lea    0xdf1(%rip),%rbx
mov    %rbx,%rdi
call   1060 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1050 <write@plt>
cmp    $0xffffffff,%eax
je     12b2 <read_file+0x112>
lea    0xdd9(%rip),%rbx
mov    %rbx,%rdi
call   1060 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1050 <write@plt>
cmp    $0xffffffff,%eax
je     12b2 <read_file+0x112>
xor    %eax,%eax
call   10c0 <fork@plt>
mov    %eax,0x4(%rsp)
test   %eax,%eax
je     12bb <read_file+0x11b>
cmp    $0xffffffff,%eax
je     1320 <read_file+0x180>
mov    $0x1,%edi
call   10a0 <sleep@plt>
lea    0xde9(%rip),%rdi
call   1040 <puts@plt>
lea    0xdd4(%rip),%rdi
mov    $0x1,%esi
xor    %eax,%eax
call   1070 <printf@plt>
lea    0xd78(%rip),%rbx
mov    %rbx,%rdi
call   1060 <strlen@plt>
xor    %edx,%edx
cmp    $0xffffffff,%eax
sete   %dl
mov    %ebp,%edi
mov    %rbx,%rsi
call   1050 <write@plt>
test   %eax,%eax
je     12fa <read_file+0x15a>
lea    0xd76(%rip),%rdi
jmp    1339 <read_file+0x199>
lea    0xdb6(%rip),%rdi
call   1040 <puts@plt>
lea    0xd73(%rip),%rdi
mov    $0x2,%esi
xor    %eax,%eax
call   1070 <printf@plt>
lea    0xd25(%rip),%rbx
mov    %rbx,%rdi
call   1060 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1050 <write@plt>
cmp    $0xffffffff,%eax
je     1329 <read_file+0x189>
lea    0x4(%rsp),%rdi
call   10b0 <wait@plt>
lea    0xd65(%rip),%rdi
call   1040 <puts@plt>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xd05(%rip),%rdi
jmp    1339 <read_file+0x199>
lea    0xd15(%rip),%rdi
jmp    1339 <read_file+0x199>
lea    0xd19(%rip),%rdi
jmp    1339 <read_file+0x199>
lea    0xce3(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1090 <perror@plt>

LLVM IR (-O0):
@.str = [6 x i8] c"hello\00"
@.str.1 = [6 x i8] c"world\00"
@.str.2 = [7 x i8] c"cccccc\00"
@.str.3 = [9 x i8] c"test.txt\00"
@errno = global i64 0
@ENOENT = global i64 0
@.str.4 = [11 x i8] c"remove ok\0A\00"
@.str.5 = [7 x i8] c"remove\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str.6 = [12 x i8] c"parent open\00"
@.str.7 = [13 x i8] c"parent write\00"
@.str.8 = [5 x i8] c"fork\00"
@.str.9 = [18 x i8] c"in child process\0A\00"
@.str.10 = [8 x i8] c"i = %d\0A\00"
@.str.11 = [12 x i8] c"child write\00"
@.str.12 = [19 x i8] c"in parent process\0A\00"
@.str.13 = [9 x i8] c"i = %d \0A\00"
@.str.14 = [9 x i8] c"over...\0A\00"
define void @read_file() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
store i32 1, i32* %3
store i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8** %4
store i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8** %5
store i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8** %6
%7 = call i32 @remove(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %18
9: ; preds = %0
%10 = load i64, i64* @errno
%11 = load i64, i64* @ENOENT
%12 = icmp eq i64 %10, %11
br i1 %12, label %13, label %15
13: ; preds = %9
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
br label %17
15: ; preds = %9
%16 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
br label %85
17: ; preds = %13
br label %18
18: ; preds = %17, %0
%19 = load i32, i32* @O_RDWR
%20 = load i32, i32* @O_CREAT
%21 = or i32 %19, %20
%22 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 %21, i32 420)
store i32 %22, i32* %2
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %26
24: ; preds = %18
%25 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.6, i64 0, i64 0))
br label %85
26: ; preds = %18
%27 = load i32, i32* %2
%28 = load i8*, i8** %4
%29 = load i8*, i8** %4
%30 = call i32 @strlen(i8* %29)
%31 = call i32 @write(i32 %27, i8* %28, i32 %30)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %35
33: ; preds = %26
%34 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.7, i64 0, i64 0))
br label %85
35: ; preds = %26
%36 = load i32, i32* %2
%37 = load i8*, i8** %6
%38 = load i8*, i8** %6
%39 = call i32 @strlen(i8* %38)
%40 = call i32 @write(i32 %36, i8* %37, i32 %39)
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %44
42: ; preds = %35
%43 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.7, i64 0, i64 0))
br label %85
44: ; preds = %35
%45 = call i32 (...) @fork()
store i32 %45, i32* %1
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %49
47: ; preds = %44
%48 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.8, i64 0, i64 0))
br label %85
49: ; preds = %44
%50 = load i32, i32* %1
%51 = icmp eq i32 %50, 0
br i1 %51, label %52, label %65
52: ; preds = %49
store i32 2, i32* %3
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.9, i64 0, i64 0))
%54 = load i32, i32* %3
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.10, i64 0, i64 0), i32 %54)
%56 = load i32, i32* %2
%57 = load i8*, i8** %5
%58 = load i8*, i8** %5
%59 = call i32 @strlen(i8* %58)
%60 = call i32 @write(i32 %56, i8* %57, i32 %59)
%61 = icmp eq i32 %60, -1
br i1 %61, label %62, label %64
62: ; preds = %52
%63 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.11, i64 0, i64 0))
br label %85
64: ; preds = %52
br label %81
65: ; preds = %49
%66 = call i32 @sleep(i32 1)
%67 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.12, i64 0, i64 0))
%68 = load i32, i32* %3
%69 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.13, i64 0, i64 0), i32 %68)
%70 = load i32, i32* %2
%71 = load i8*, i8** %6
%72 = load i8*, i8** %6
%73 = call i32 @strlen(i8* %72)
%74 = icmp eq i32 %73, -1
%75 = zext i1 %74 to i32
%76 = call i32 @write(i32 %70, i8* %71, i32 %75)
%77 = icmp ne i32 %76, 0
br i1 %77, label %78, label %80
78: ; preds = %65
%79 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.7, i64 0, i64 0))
br label %85
80: ; preds = %65
br label %81
81: ; preds = %80, %64
br label %82
82: ; preds = %81
%83 = call i32 @wait(i32* %1)
%84 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.14, i64 0, i64 0))
br label %85
85: ; preds = %82, %78, %62, %47, %42, %33, %24, %15
ret void
}

LLVM IR (-O2):
@.str = [6 x i8] c"hello\00"
@.str.1 = [6 x i8] c"world\00"
@.str.2 = [7 x i8] c"cccccc\00"
@.str.3 = [9 x i8] c"test.txt\00"
@errno = global i64 0
@ENOENT = global i64 0
@.str.5 = [7 x i8] c"remove\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str.6 = [12 x i8] c"parent open\00"
@.str.7 = [13 x i8] c"parent write\00"
@.str.8 = [5 x i8] c"fork\00"
@.str.10 = [8 x i8] c"i = %d\0A\00"
@.str.11 = [12 x i8] c"child write\00"
@.str.13 = [9 x i8] c"i = %d \0A\00"
@str = [18 x i8] c"in parent process\00"
@str.15 = [8 x i8] c"over...\00"
@str.16 = [17 x i8] c"in child process\00"
@str.17 = [10 x i8] c"remove ok\00"
define void @read_file() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = tail call i32 @remove(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %13
5: ; preds = %0
%6 = load i64, i64* @errno
%7 = load i64, i64* @ENOENT
%8 = icmp eq i64 %6, %7
br i1 %8, label %9, label %11
9: ; preds = %5
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str.17, i64 0, i64 0))
br label %13
11: ; preds = %5
%12 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
br label %59
13: ; preds = %9, %0
%14 = load i32, i32* @O_RDWR
%15 = load i32, i32* @O_CREAT
%16 = or i32 %15, %14
%17 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 %16, i32 420)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %21
19: ; preds = %13
%20 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.6, i64 0, i64 0))
br label %59
21: ; preds = %13
%22 = tail call i32 @strlen(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%23 = tail call i32 @write(i32 %17, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %22)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %27
25: ; preds = %21
%26 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.7, i64 0, i64 0))
br label %59
27: ; preds = %21
%28 = tail call i32 @strlen(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%29 = tail call i32 @write(i32 %17, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 %28)
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %33
31: ; preds = %27
%32 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.7, i64 0, i64 0))
br label %59
33: ; preds = %27
%34 = tail call i32 (...) @fork()
store i32 %34, i32* %1
switch i32 %34, label %45 [
i32 -1, label %35
i32 0, label %37
]
35: ; preds = %33
%36 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.8, i64 0, i64 0))
br label %59
37: ; preds = %33
%38 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str.16, i64 0, i64 0))
%39 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.10, i64 0, i64 0), i32 2)
%40 = tail call i32 @strlen(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%41 = tail call i32 @write(i32 %17, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 %40)
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %56
43: ; preds = %37
%44 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.11, i64 0, i64 0))
br label %59
45: ; preds = %33
%46 = tail call i32 @sleep(i32 1)
%47 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
%48 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.13, i64 0, i64 0), i32 1)
%49 = tail call i32 @strlen(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%50 = icmp eq i32 %49, -1
%51 = zext i1 %50 to i32
%52 = tail call i32 @write(i32 %17, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 %51)
%53 = icmp eq i32 %52, 0
br i1 %53, label %56, label %54
54: ; preds = %45
%55 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.7, i64 0, i64 0))
br label %59
56: ; preds = %37, %45
%57 = call i32 @wait(i32* %1)
%58 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @str.15, i64 0, i64 0))
br label %59
59: ; preds = %56, %54, %43, %35, %31, %25, %19, %11
ret void
}