#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 scalar_t__ pid_t;
typedef int /*<<< orphan*/ num;

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int getpid();
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ read(int, int *, int);
scalar_t__ wait(int *);
int main(void) {
  int status;
  pid_t pid;
  int fd_driver;
  if ((fd_driver = open("/dev/key_eint_race", O_RDWR)) < 0) {
    printf("file open error\n");
    exit(1);
  }
  if ((pid = fork()) < 0) {
    perror("fork:");
    exit(1);
  } else if (pid == 0) {
    int num;
    while (1) {
      read(fd_driver, &num, sizeof(num));
      printf("the num value is <son-%d>: %d\n", getpid(), num);
    }
    close(fd_driver);
  } else {
    int num;
    while (1) {
      read(fd_driver, &num, sizeof(num));
      printf("the num value is <father-%d>: %d\n", getpid(), num);
    }
    pid = wait(&status);
    close(fd_driver);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e62(%rip),%rax
mov    (%rax),%esi
lea    0xe71(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    11b8 <main+0x48>
lea    0xe6c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jge    11e3 <main+0x73>
lea    0xe50(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
cmpq   $0x0,-0x10(%rbp)
jne    1223 <main+0xb3>
jmp    11f3 <main+0x83>
mov    -0x14(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x4,%edx
call   1050 <read@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
mov    -0x18(%rbp),%edx
lea    0xe13(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11f3 <main+0x83>
jmp    1228 <main+0xb8>
mov    -0x14(%rbp),%edi
lea    -0x1c(%rbp),%rsi
mov    $0x4,%edx
call   1050 <read@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
mov    -0x1c(%rbp),%edx
lea    0xdfd(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1228 <main+0xb8>

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2e58(%rip),%rax
mov    (%rax),%esi
lea    0xe67(%rip),%rdi
call   1070 <open@plt>
test   %eax,%eax
js     1238 <main+0xb8>
mov    %eax,%ebx
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
js     124e <main+0xce>
je     11f8 <main+0x78>
lea    0x8(%rsp),%r14
lea    0xe72(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x4,%edx
call   1060 <read@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    0x8(%rsp),%edx
mov    %r15,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    11d0 <main+0x50>
lea    0xc(%rsp),%r14
lea    0xe15(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x4,%edx
call   1060 <read@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    0xc(%rsp),%edx
mov    %r15,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1210 <main+0x90>
lea    0xe1b(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    0xdbe(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [19 x i8] c"/dev/key_eint_race\00"
@O_RDWR = global i32 0
@.str.1 = [17 x i8] c"file open error\0A\00"
@.str.2 = [6 x i8] c"fork:\00"
@.str.3 = [31 x i8] c"the num value is <son-%d>: %d\0A\00"
@.str.4 = [34 x i8] c"the num value is <father-%d>: %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i64
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 0, i32* %1
%7 = load i32, i32* @O_RDWR
%8 = call i32 @open(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %7)
store i32 %8, i32* %4
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %0
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %0
%14 = call i64 (...) @fork()
store i64 %14, i64* %3
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %13
%17 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %13
%20 = load i64, i64* %3
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %29
22: ; preds = %19
br label %23
23: ; preds = %22, %23
%24 = load i32, i32* %4
%25 = call i32 @read(i32 %24, i32* %5, i32 4)
%26 = call i32 (...) @getpid()
%27 = load i32, i32* %5
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i32 %26, i32 %27)
br label %23
29: ; preds = %19
br label %30
30: ; preds = %29, %30
%31 = load i32, i32* %4
%32 = call i32 @read(i32 %31, i32* %6, i32 4)
%33 = call i32 (...) @getpid()
%34 = load i32, i32* %6
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0), i32 %33, i32 %34)
br label %30
}

LLVM IR (-O2):
@.str = [19 x i8] c"/dev/key_eint_race\00"
@O_RDWR = global i32 0
@.str.2 = [6 x i8] c"fork:\00"
@.str.3 = [31 x i8] c"the num value is <son-%d>: %d\0A\00"
@.str.4 = [34 x i8] c"the num value is <father-%d>: %d\0A\00"
@str = [16 x i8] c"file open error\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = load i32, i32* @O_RDWR
%4 = tail call i32 @open(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %9
6: ; preds = %0
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str, i64 0, i64 0))
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %0
%10 = tail call i64 (...) @fork()
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %9
%13 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%14 = tail call i32 @exit(i32 1)
unreachable
15: ; preds = %9
%16 = icmp eq i64 %10, 0
br i1 %16, label %17, label %24
17: ; preds = %15
%18 = bitcast i32* %1 to i8*
br label %19
19: ; preds = %19, %17
%20 = call i32 @read(i32 %4, i32* %1, i32 4)
%21 = tail call i32 (...) @getpid()
%22 = load i32, i32* %1
%23 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i32 %21, i32 %22)
br label %19
24: ; preds = %15
%25 = bitcast i32* %2 to i8*
br label %26
26: ; preds = %26, %24
%27 = call i32 @read(i32 %4, i32* %2, i32 4)
%28 = tail call i32 (...) @getpid()
%29 = load i32, i32* %2
%30 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0), i32 %28, i32 %29)
br label %26
}