#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 */
struct input_event {
  scalar_t__ type;
  int code;
  int value;
};
typedef int pid_t;

/* Variables and functions */
#define ABS_PRESSURE 130
#define ABS_X 129
#define ABS_Y 128
scalar_t__ EV_SYN;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int fork();
unsigned char getchar();
int getpid();
int /*<<< orphan*/ kill(int, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int read(int, struct input_event *, int);
scalar_t__ waitkeyin();
int main(void) {
  struct input_event event;
  int fd, ret;
  int x, y, pressure;
  unsigned char ch;
  pid_t pid;
  if ((pid = fork()) < 0) {
    printf("fork child process error\n");
    return -1;
  } else if (pid == 0) {
    printf("This is child process:%d\n", getpid());
    printf("fork return value in child process is %d\n", pid);
    fd = open("/dev/input/ts0", O_RDONLY);
    if (fd <= 0) {
      printf("Can not open dev file:%s\n", "/dev/input/ts0");
      return -1;
    }
    while (1) {
      ret = read(fd, &event, sizeof(struct input_event));
      if (ret > 0) {
        if (event.type == EV_SYN) {
          printf("Get sync: x(%d), y(%d), pressure(%d)\n", x, y, pressure);
          continue;
        }
        switch (event.code) {
        case ABS_X:
          x = event.value;
          break;
        case ABS_Y:
          y = event.value;
          break;
        case ABS_PRESSURE:
          pressure = event.value;
          break;
        }
      }
    }
  } else {
    printf("This is parent process:%d\n", getpid());
    printf("Fork return value in parent process is %d\n", pid);
    printf("If you want to exit touch test,please key in 'Q' or 'q'\n ");
    if (waitkeyin()) {
      ch = getchar();
      if (ch == 'Q' || ch == 'q') {
        kill(pid, 9);
        close(fd);
      }
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %eax,-0x34(%rbp)
cmp    $0x0,%eax
jge    11cc <main+0x3c>
lea    0xe47(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    138c <main+0x1fc>
cmpl   $0x0,-0x34(%rbp)
jne    12f6 <main+0x166>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
lea    0xe34(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x34(%rbp),%esi
lea    0xe3d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2ddb(%rip),%rax
mov    (%rax),%esi
lea    0xe50(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jg     1241 <main+0xb1>
lea    0xe46(%rip),%rdi
lea    0xe30(%rip),%rsi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    138c <main+0x1fc>
jmp    1246 <main+0xb6>
mov    -0x1c(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x10,%edx
call   1070 <read@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jle    12f1 <main+0x161>
mov    -0x18(%rbp),%rax
mov    0x2d81(%rip),%rcx
cmp    (%rcx),%rax
jne    1294 <main+0x104>
mov    -0x24(%rbp),%esi
mov    -0x28(%rbp),%edx
mov    -0x2c(%rbp),%ecx
lea    0xdff(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1246 <main+0xb6>
mov    -0x10(%rbp),%eax
mov    %eax,-0x38(%rbp)
sub    $0x80,%eax
je     12db <main+0x14b>
jmp    12aa <main+0x11a>
mov    -0x38(%rbp),%eax
sub    $0x81,%eax
je     12d0 <main+0x140>
jmp    12bd <main+0x12d>
mov    -0x38(%rbp),%eax
sub    $0x82,%eax
je     12e6 <main+0x156>
jmp    12ec <main+0x15c>
mov    -0xc(%rbp),%eax
mov    %eax,-0x24(%rbp)
jmp    12ec <main+0x15c>
mov    -0xc(%rbp),%eax
mov    %eax,-0x28(%rbp)
jmp    12ec <main+0x15c>
mov    -0xc(%rbp),%eax
mov    %eax,-0x2c(%rbp)
jmp    12f1 <main+0x161>
jmp    1246 <main+0xb6>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
lea    0xda7(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x34(%rbp),%esi
lea    0xdb1(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xdce(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x0,%al
call   1030 <waitkeyin@plt>
cmp    $0x0,%rax
je     137b <main+0x1eb>
mov    $0x0,%al
call   1080 <getchar@plt>
mov    %al,-0x2d(%rbp)
movzbl -0x2d(%rbp),%eax
cmp    $0x51,%eax
je     1361 <main+0x1d1>
movzbl -0x2d(%rbp),%eax
cmp    $0x71,%eax
jne    1376 <main+0x1e6>
mov    -0x34(%rbp),%edi
mov    $0x9,%esi
call   1090 <kill@plt>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
jmp    137b <main+0x1eb>
jmp    1380 <main+0x1f0>
jmp    1385 <main+0x1f5>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
xor    %eax,%eax
call   10c0 <fork@plt>
test   %eax,%eax
js     122f <main+0x8f>
mov    %eax,%ebp
xor    %ebx,%ebx
xor    %eax,%eax
call   1050 <getpid@plt>
test   %ebp,%ebp
je     1240 <main+0xa0>
lea    0xec4(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xecf(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xeea(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
xor    %eax,%eax
call   1030 <waitkeyin@plt>
test   %rax,%rax
je     1309 <main+0x169>
xor    %ebx,%ebx
xor    %eax,%eax
call   1090 <getchar@plt>
and    $0xdf,%al
cmp    $0x51,%al
jne    1309 <main+0x169>
mov    %ebp,%edi
mov    $0x9,%esi
call   10a0 <kill@plt>
call   1070 <close@plt>
jmp    1309 <main+0x169>
lea    0xedd(%rip),%rdi
call   1040 <puts@plt>
jmp    1304 <main+0x164>
lea    0xdb9(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xdc3(%rip),%rdi
xor    %esi,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2d79(%rip),%rax
mov    (%rax),%esi
lea    0xdd4(%rip),%rdi
call   10b0 <open@plt>
test   %eax,%eax
jle    12ef <main+0x14f>
mov    %eax,%r12d
lea    0x8(%rsp),%r14
mov    0x2d68(%rip),%r15
jmp    12a5 <main+0x105>
nopw   0x0(%rax,%rax,1)
lea    0xdd6(%rip),%rdi
mov    %ebx,%esi
mov    %r13d,%edx
mov    %ebp,%ecx
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r12d,%edi
mov    %r14,%rsi
mov    $0x10,%edx
call   1080 <read@plt>
test   %eax,%eax
jle    12a5 <main+0x105>
mov    0x8(%rsp),%rax
cmp    (%r15),%rax
je     1290 <main+0xf0>
mov    0x10(%rsp),%eax
cmp    $0x80,%eax
je     12e2 <main+0x142>
cmp    $0x82,%eax
je     12e9 <main+0x149>
cmp    $0x81,%eax
jne    12a5 <main+0x105>
mov    0x14(%rsp),%ebx
jmp    12a5 <main+0x105>
mov    0x14(%rsp),%r13d
jmp    12a5 <main+0x105>
mov    0x14(%rsp),%ebp
jmp    12a5 <main+0x105>
lea    0xd5d(%rip),%rdi
lea    0xd47(%rip),%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.input_event = type { i64, i32, i32 }
@.str = [26 x i8] c"fork child process error\0A\00"
@.str.1 = [26 x i8] c"This is child process:%d\0A\00"
@.str.2 = [42 x i8] c"fork return value in child process is %d\0A\00"
@.str.3 = [15 x i8] c"/dev/input/ts0\00"
@O_RDONLY = global i32 0
@.str.4 = [26 x i8] c"Can not open dev file:%s\0A\00"
@EV_SYN = global i64 0
@.str.5 = [38 x i8] c"Get sync: x(%d), y(%d), pressure(%d)\0A\00"
@.str.6 = [27 x i8] c"This is parent process:%d\0A\00"
@.str.7 = [43 x i8] c"Fork return value in parent process is %d\0A\00"
@.str.8 = [58 x i8] c"If you want to exit touch test,please key in 'Q' or 'q'\0A \00"
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.input_event
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8
%9 = alloca i32
store i32 0, i32* %1
%10 = call i32 (...) @fork()
store i32 %10, i32* %9
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %0
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %1
br label %84
14: ; preds = %0
%15 = load i32, i32* %9
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %58
17: ; preds = %14
%18 = call i32 (...) @getpid()
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i32 %18)
%20 = load i32, i32* %9
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0), i32 %20)
%22 = load i32, i32* @O_RDONLY
%23 = call i32 @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i32 %22)
store i32 %23, i32* %3
%24 = load i32, i32* %3
%25 = icmp sle i32 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %17
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %1
br label %84
28: ; preds = %17
br label %29
29: ; preds = %28, %39, %57
%30 = load i32, i32* %3
%31 = call i32 @read(i32 %30, %struct.input_event* %2, i32 16)
store i32 %31, i32* %4
%32 = load i32, i32* %4
%33 = icmp sgt i32 %32, 0
br i1 %33, label %34, label %57
34: ; preds = %29
%35 = getelementptr inbounds %struct.input_event, %struct.input_event* %2, i32 0, i32 0
%36 = load i64, i64* %35
%37 = load i64, i64* @EV_SYN
%38 = icmp eq i64 %36, %37
br i1 %38, label %39, label %44
39: ; preds = %34
%40 = load i32, i32* %5
%41 = load i32, i32* %6
%42 = load i32, i32* %7
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.5, i64 0, i64 0), i32 %40, i32 %41, i32 %42)
br label %29
44: ; preds = %34
%45 = getelementptr inbounds %struct.input_event, %struct.input_event* %2, i32 0, i32 1
%46 = load i32, i32* %45
switch i32 %46, label %56 [
i32 129, label %47
i32 128, label %50
i32 130, label %53
]
47: ; preds = %44
%48 = getelementptr inbounds %struct.input_event, %struct.input_event* %2, i32 0, i32 2
%49 = load i32, i32* %48
store i32 %49, i32* %5
br label %56
50: ; preds = %44
%51 = getelementptr inbounds %struct.input_event, %struct.input_event* %2, i32 0, i32 2
%52 = load i32, i32* %51
store i32 %52, i32* %6
br label %56
53: ; preds = %44
%54 = getelementptr inbounds %struct.input_event, %struct.input_event* %2, i32 0, i32 2
%55 = load i32, i32* %54
store i32 %55, i32* %7
br label %56
56: ; preds = %44, %53, %50, %47
br label %57
57: ; preds = %56, %29
br label %29
58: ; preds = %14
%59 = call i32 (...) @getpid()
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.6, i64 0, i64 0), i32 %59)
%61 = load i32, i32* %9
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.7, i64 0, i64 0), i32 %61)
%63 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.8, i64 0, i64 0))
%64 = call i64 (...) @waitkeyin()
%65 = icmp ne i64 %64, 0
br i1 %65, label %66, label %81
66: ; preds = %58
%67 = call zeroext i8 (...) @getchar()
store i8 %67, i8* %8
%68 = load i8, i8* %8
%69 = zext i8 %68 to i32
%70 = icmp eq i32 %69, 81
br i1 %70, label %75, label %71
71: ; preds = %66
%72 = load i8, i8* %8
%73 = zext i8 %72 to i32
%74 = icmp eq i32 %73, 113
br i1 %74, label %75, label %80
75: ; preds = %71, %66
%76 = load i32, i32* %9
%77 = call i32 @kill(i32 %76, i32 9)
%78 = load i32, i32* %3
%79 = call i32 @close(i32 %78)
br label %80
80: ; preds = %75, %71
br label %81
81: ; preds = %80, %58
br label %82
82: ; preds = %81
br label %83
83: ; preds = %82
store i32 0, i32* %1
br label %84
84: ; preds = %83, %26, %12
%85 = load i32, i32* %1
ret i32 %85
}

LLVM IR (-O2):
%struct.input_event = type { i64, i32, i32 }
@.str.1 = [26 x i8] c"This is child process:%d\0A\00"
@.str.2 = [42 x i8] c"fork return value in child process is %d\0A\00"
@.str.3 = [15 x i8] c"/dev/input/ts0\00"
@O_RDONLY = global i32 0
@.str.4 = [26 x i8] c"Can not open dev file:%s\0A\00"
@EV_SYN = global i64 0
@.str.5 = [38 x i8] c"Get sync: x(%d), y(%d), pressure(%d)\0A\00"
@.str.6 = [27 x i8] c"This is parent process:%d\0A\00"
@.str.7 = [43 x i8] c"Fork return value in parent process is %d\0A\00"
@.str.8 = [58 x i8] c"If you want to exit touch test,please key in 'Q' or 'q'\0A \00"
@str = [25 x i8] c"fork child process error\00"
define i32 @main() {
%1 = alloca %struct.input_event
%2 = bitcast %struct.input_event* %1 to i8*
%3 = tail call i32 (...) @fork()
%4 = icmp slt i32 %3, 0
br i1 %4, label %5, label %7
5: ; preds = %0
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str, i64 0, i64 0))
br label %64
7: ; preds = %0
%8 = icmp eq i32 %3, 0
%9 = tail call i32 (...) @getpid()
br i1 %8, label %10, label %51
10: ; preds = %7
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i32 %9)
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0), i32 0)
%13 = load i32, i32* @O_RDONLY
%14 = tail call i32 @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i32 %13)
%15 = icmp slt i32 %14, 1
br i1 %15, label %20, label %16
16: ; preds = %10
%17 = getelementptr inbounds %struct.input_event, %struct.input_event* %1, i64 0, i32 0
%18 = getelementptr inbounds %struct.input_event, %struct.input_event* %1, i64 0, i32 1
%19 = getelementptr inbounds %struct.input_event, %struct.input_event* %1, i64 0, i32 2
br label %34
20: ; preds = %10
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
br label %64
22: ; preds = %47, %26
%23 = load i64, i64* %17
%24 = load i64, i64* @EV_SYN
%25 = icmp eq i64 %23, %24
br i1 %25, label %26, label %30
26: ; preds = %22
%27 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @.str.5, i64 0, i64 0), i32 %35, i32 %41, i32 %46)
%28 = call i32 @read(i32 %14, %struct.input_event* %1, i32 16)
%29 = icmp sgt i32 %28, 0
br i1 %29, label %22, label %50
30: ; preds = %22
%31 = load i32, i32* %18
switch i32 %31, label %50 [
i32 129, label %32
i32 128, label %38
i32 130, label %43
]
32: ; preds = %30
%33 = load i32, i32* %19
br label %34
34: ; preds = %32, %16
%35 = phi i32 [ %33, %32 ], [ undef, %16 ]
%36 = phi i32 [ %41, %32 ], [ undef, %16 ]
%37 = phi i32 [ %46, %32 ], [ undef, %16 ]
br label %40
38: ; preds = %30
%39 = load i32, i32* %19
br label %40
40: ; preds = %34, %38
%41 = phi i32 [ %36, %34 ], [ %39, %38 ]
%42 = phi i32 [ %37, %34 ], [ %46, %38 ]
br label %45
43: ; preds = %30
%44 = load i32, i32* %19
br label %45
45: ; preds = %40, %43
%46 = phi i32 [ %42, %40 ], [ %44, %43 ]
br label %47
47: ; preds = %50, %45
%48 = call i32 @read(i32 %14, %struct.input_event* %1, i32 16)
%49 = icmp sgt i32 %48, 0
br i1 %49, label %22, label %50
50: ; preds = %26, %47, %30
br label %47
51: ; preds = %7
%52 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.6, i64 0, i64 0), i32 %9)
%53 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @.str.7, i64 0, i64 0), i32 %3)
%54 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([58 x i8], [58 x i8]* @.str.8, i64 0, i64 0))
%55 = tail call i64 (...) @waitkeyin()
%56 = icmp eq i64 %55, 0
br i1 %56, label %64, label %57
57: ; preds = %51
%58 = tail call zeroext i8 (...) @getchar()
%59 = and i8 %58, -33
%60 = icmp eq i8 %59, 81
br i1 %60, label %61, label %64
61: ; preds = %57
%62 = tail call i32 @kill(i32 %3, i32 9)
%63 = tail call i32 @close(i32 undef)
br label %64
64: ; preds = %51, %57, %61, %20, %5
%65 = phi i32 [ -1, %5 ], [ -1, %20 ], [ 0, %61 ], [ 0, %57 ], [ 0, %51 ]
ret i32 %65
}