#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*/ SIGHUP;
int errno;
char getchar();
int getpid();
scalar_t__ kill(int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, int, char);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ usleep(int);
scalar_t__ waitpid(int, int *, int /*<<< orphan*/);
int in_parent(int cpid) {
  while (1) {
    if (kill(cpid, 0) == 0) {
      break;
    }
    usleep(100000);
  }
  sleep(10);
  int err = kill(cpid, SIGHUP);
  if (0 != err) {
    printf("kill pid [%d] faild, errno = [%d]\n", cpid, err);
    return -1;
  }
  int st;
  errno = 0;
  if (waitpid(cpid, &st, 0) < 0) {
    printf("waitpid for [%d] faild, errno = [%d]\n", cpid, errno);
    return -1;
  }
  int i = 0;
  for (i = 0; i < 10; i++) {
    char c = getchar();
    printf("parent [%d], c = [%c]\n", getpid(), c);
  }
  return 0;
}


x86 assembly (clang, -O0):
<in_parent>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
xor    %esi,%esi
call   1060 <kill@plt>
cmp    $0x0,%rax
jne    1194 <in_parent+0x24>
jmp    11a3 <in_parent+0x33>
mov    $0x186a0,%edi
call   1090 <usleep@plt>
jmp    117b <in_parent+0xb>
mov    $0xa,%edi
call   1080 <sleep@plt>
mov    -0x8(%rbp),%edi
mov    0x2e39(%rip),%rax
mov    (%rax),%esi
call   1060 <kill@plt>
mov    %eax,-0xc(%rbp)
xor    %eax,%eax
cmp    -0xc(%rbp),%eax
je     11ed <in_parent+0x7d>
mov    -0x8(%rbp),%esi
mov    -0xc(%rbp),%eax
lea    0xe27(%rip),%rdi
movsbl %al,%edx
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1289 <in_parent+0x119>
mov    0x2de4(%rip),%rax
movl   $0x0,(%rax)
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
xor    %edx,%edx
call   1070 <waitpid@plt>
cmp    $0x0,%rax
jge    1239 <in_parent+0xc9>
mov    -0x8(%rbp),%esi
mov    0x2dbc(%rip),%rax
mov    (%rax),%eax
lea    0xdfe(%rip),%rdi
movsbl %al,%edx
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1289 <in_parent+0x119>
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0xa,-0x14(%rbp)
jge    1282 <in_parent+0x112>
mov    $0x0,%al
call   1050 <getchar@plt>
mov    %al,-0x15(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xdde(%rip),%rdi
movsbl -0x15(%rbp),%edx
call   1040 <printf@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1247 <in_parent+0xd7>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<in_parent>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%ebp
nopl   0x0(%rax)
xor    %esi,%esi
call   1060 <kill@plt>
test   %rax,%rax
je     119a <in_parent+0x2a>
mov    $0x186a0,%edi
call   1090 <usleep@plt>
mov    %ebp,%edi
jmp    1180 <in_parent+0x10>
mov    $0xa,%edi
call   1080 <sleep@plt>
mov    0x2e45(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1060 <kill@plt>
test   %eax,%eax
je     11d4 <in_parent+0x64>
movsbl %al,%edx
lea    0xe3e(%rip),%rdi
mov    %ebp,%esi
call   1040 <printf@plt>
mov    $0xffffffff,%r15d
jmp    1324 <in_parent+0x1b4>
mov    0x2dfd(%rip),%rbx
movl   $0x0,(%rbx)
lea    0x4(%rsp),%rsi
xor    %r15d,%r15d
mov    %ebp,%edi
xor    %edx,%edx
call   1070 <waitpid@plt>
test   %rax,%rax
js     1332 <in_parent+0x1c2>
xor    %eax,%eax
call   1050 <getchar@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1030 <getpid@plt>
movsbl %bl,%edx
lea    0xe34(%rip),%r14
mov    %r14,%rdi
mov    %eax,%esi
call   1040 <printf@plt>
xor    %eax,%eax
call   1050 <getchar@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1030 <getpid@plt>
movsbl %bl,%edx
mov    %r14,%rdi
mov    %eax,%esi
call   1040 <printf@plt>
xor    %eax,%eax
call   1050 <getchar@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1030 <getpid@plt>
movsbl %bl,%edx
mov    %r14,%rdi
mov    %eax,%esi
call   1040 <printf@plt>
xor    %eax,%eax
call   1050 <getchar@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1030 <getpid@plt>
movsbl %bl,%edx
mov    %r14,%rdi
mov    %eax,%esi
call   1040 <printf@plt>
xor    %eax,%eax
call   1050 <getchar@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1030 <getpid@plt>
movsbl %bl,%edx
mov    %r14,%rdi
mov    %eax,%esi
call   1040 <printf@plt>
xor    %eax,%eax
call   1050 <getchar@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1030 <getpid@plt>
movsbl %bl,%edx
mov    %r14,%rdi
mov    %eax,%esi
call   1040 <printf@plt>
xor    %eax,%eax
call   1050 <getchar@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1030 <getpid@plt>
movsbl %bl,%edx
mov    %r14,%rdi
mov    %eax,%esi
call   1040 <printf@plt>
xor    %eax,%eax
call   1050 <getchar@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1030 <getpid@plt>
movsbl %bl,%edx
mov    %r14,%rdi
mov    %eax,%esi
call   1040 <printf@plt>
xor    %eax,%eax
call   1050 <getchar@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1030 <getpid@plt>
movsbl %bl,%edx
mov    %r14,%rdi
mov    %eax,%esi
call   1040 <printf@plt>
xor    %eax,%eax
call   1050 <getchar@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1030 <getpid@plt>
movsbl %bl,%edx
mov    %r14,%rdi
mov    %eax,%esi
call   1040 <printf@plt>
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
movsbl (%rbx),%edx
lea    0xce7(%rip),%rdi
jmp    11c2 <in_parent+0x52>

LLVM IR (-O0):
@SIGHUP = global i32 0
@.str = [35 x i8] c"kill pid [%d] faild, errno = [%d]\0A\00"
@errno = global i32 0
@.str.1 = [38 x i8] c"waitpid for [%d] faild, errno = [%d]\0A\00"
@.str.2 = [23 x i8] c"parent [%d], c = [%c]\0A\00"
define i32 @in_parent(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8
store i32 %0, i32* %3
br label %8
8: ; preds = %1, %13
%9 = load i32, i32* %3
%10 = call i64 @kill(i32 %9, i32 0)
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %8
br label %15
13: ; preds = %8
%14 = call i32 @usleep(i32 100000)
br label %8
15: ; preds = %12
%16 = call i32 @sleep(i32 10)
%17 = load i32, i32* %3
%18 = load i32, i32* @SIGHUP
%19 = call i64 @kill(i32 %17, i32 %18)
%20 = trunc i64 %19 to i32
store i32 %20, i32* %4
%21 = load i32, i32* %4
%22 = icmp ne i32 0, %21
br i1 %22, label %23, label %28
23: ; preds = %15
%24 = load i32, i32* %3
%25 = load i32, i32* %4
%26 = trunc i32 %25 to i8
%27 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i32 %24, i8 signext %26)
store i32 -1, i32* %2
br label %50
28: ; preds = %15
store i32 0, i32* @errno
%29 = load i32, i32* %3
%30 = call i64 @waitpid(i32 %29, i32* %5, i32 0)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %37
32: ; preds = %28
%33 = load i32, i32* %3
%34 = load i32, i32* @errno
%35 = trunc i32 %34 to i8
%36 = call i32 @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i32 %33, i8 signext %35)
store i32 -1, i32* %2
br label %50
37: ; preds = %28
store i32 0, i32* %6
store i32 0, i32* %6
br label %38
38: ; preds = %46, %37
%39 = load i32, i32* %6
%40 = icmp slt i32 %39, 10
br i1 %40, label %41, label %49
41: ; preds = %38
%42 = call signext i8 (...) @getchar()
store i8 %42, i8* %7
%43 = call i32 (...) @getpid()
%44 = load i8, i8* %7
%45 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i32 %43, i8 signext %44)
br label %46
46: ; preds = %41
%47 = load i32, i32* %6
%48 = add nsw i32 %47, 1
store i32 %48, i32* %6
br label %38
49: ; preds = %38
store i32 0, i32* %2
br label %50
50: ; preds = %49, %32, %23
%51 = load i32, i32* %2
ret i32 %51
}

LLVM IR (-O2):
@SIGHUP = global i32 0
@.str = [35 x i8] c"kill pid [%d] faild, errno = [%d]\0A\00"
@errno = global i32 0
@.str.1 = [38 x i8] c"waitpid for [%d] faild, errno = [%d]\0A\00"
@.str.2 = [23 x i8] c"parent [%d], c = [%c]\0A\00"
define i32 @in_parent(i32 %0) {
%2 = alloca i32
%3 = tail call i64 @kill(i32 %0, i32 0)
%4 = icmp eq i64 %3, 0
br i1 %4, label %9, label %5
5: ; preds = %1, %5
%6 = tail call i32 @usleep(i32 100000)
%7 = tail call i64 @kill(i32 %0, i32 0)
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %5
9: ; preds = %5, %1
%10 = tail call i32 @sleep(i32 10)
%11 = load i32, i32* @SIGHUP
%12 = tail call i64 @kill(i32 %0, i32 %11)
%13 = trunc i64 %12 to i32
%14 = icmp eq i32 %13, 0
br i1 %14, label %18, label %15
15: ; preds = %9
%16 = trunc i64 %12 to i8
%17 = tail call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i32 %0, i8 signext %16)
br label %59
18: ; preds = %9
%19 = bitcast i32* %2 to i8*
store i32 0, i32* @errno
%20 = call i64 @waitpid(i32 %0, i32* %2, i32 0)
%21 = icmp slt i64 %20, 0
br i1 %21, label %53, label %22
22: ; preds = %18
%23 = call signext i8 (...) @getchar()
%24 = call i32 (...) @getpid()
%25 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i32 %24, i8 signext %23)
%26 = call signext i8 (...) @getchar()
%27 = call i32 (...) @getpid()
%28 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i32 %27, i8 signext %26)
%29 = call signext i8 (...) @getchar()
%30 = call i32 (...) @getpid()
%31 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i32 %30, i8 signext %29)
%32 = call signext i8 (...) @getchar()
%33 = call i32 (...) @getpid()
%34 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i32 %33, i8 signext %32)
%35 = call signext i8 (...) @getchar()
%36 = call i32 (...) @getpid()
%37 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i32 %36, i8 signext %35)
%38 = call signext i8 (...) @getchar()
%39 = call i32 (...) @getpid()
%40 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i32 %39, i8 signext %38)
%41 = call signext i8 (...) @getchar()
%42 = call i32 (...) @getpid()
%43 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i32 %42, i8 signext %41)
%44 = call signext i8 (...) @getchar()
%45 = call i32 (...) @getpid()
%46 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i32 %45, i8 signext %44)
%47 = call signext i8 (...) @getchar()
%48 = call i32 (...) @getpid()
%49 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i32 %48, i8 signext %47)
%50 = call signext i8 (...) @getchar()
%51 = call i32 (...) @getpid()
%52 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i32 %51, i8 signext %50)
br label %57
53: ; preds = %18
%54 = load i32, i32* @errno
%55 = trunc i32 %54 to i8
%56 = call i32 @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i32 %0, i8 signext %55)
br label %57
57: ; preds = %22, %53
%58 = phi i32 [ -1, %53 ], [ 0, %22 ]
br label %59
59: ; preds = %57, %15
%60 = phi i32 [ -1, %15 ], [ %58, %57 ]
ret i32 %60
}