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

/* Variables and functions */
scalar_t__ EINTR;
int /*<<< orphan*/ SIGKILL;
int /*<<< orphan*/ SIGTERM;
int /*<<< orphan*/ alarm(unsigned int);
scalar_t__ errno;
int kill(int /*<<< orphan*/, int /*<<< orphan*/);
int waitpid(int /*<<< orphan*/, int *, int /*<<< orphan*/);
int kill_child(pid_t pid, unsigned int timeout) {
  if (kill(pid, SIGTERM) == -1)
    return -1;
  alarm(timeout);
  int status = 0;
  if (waitpid(pid, &status, 0) == -1) {
    if (errno == EINTR)
      return kill(pid, SIGKILL);
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<kill_child>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2ea0(%rip),%rax
mov    (%rax),%esi
call   1040 <kill@plt>
cmp    $0xffffffff,%eax
jne    1164 <kill_child+0x34>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d0 <kill_child+0xa0>
mov    -0xc(%rbp),%edi
call   1030 <alarm@plt>
movl   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
xor    %edx,%edx
call   1050 <waitpid@plt>
cmp    $0xffffffff,%eax
jne    11c9 <kill_child+0x99>
mov    0x2e47(%rip),%rax
mov    (%rax),%rax
mov    0x2e35(%rip),%rcx
cmp    (%rcx),%rax
jne    11bd <kill_child+0x8d>
mov    -0x8(%rbp),%edi
mov    0x2e1a(%rip),%rax
mov    (%rax),%esi
call   1040 <kill@plt>
mov    %eax,-0x4(%rbp)
jmp    11d0 <kill_child+0xa0>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d0 <kill_child+0xa0>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<kill_child>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %edi,%ebx
mov    0x2eaa(%rip),%rax
mov    (%rax),%esi
call   1040 <kill@plt>
cmp    $0xffffffff,%eax
je     119d <kill_child+0x6d>
mov    %ebp,%edi
call   1030 <alarm@plt>
movl   $0x0,0x4(%rsp)
lea    0x4(%rsp),%rsi
xor    %ebp,%ebp
mov    %ebx,%edi
xor    %edx,%edx
call   1050 <waitpid@plt>
cmp    $0xffffffff,%eax
jne    11a2 <kill_child+0x72>
mov    0x2e63(%rip),%rax
mov    (%rax),%rax
mov    0x2e51(%rip),%rcx
mov    $0xffffffff,%ebp
cmp    (%rcx),%rax
jne    11a2 <kill_child+0x72>
mov    0x2e38(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1040 <kill@plt>
mov    %eax,%ebp
jmp    11a2 <kill_child+0x72>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@SIGTERM = global i32 0
@errno = global i64 0
@EINTR = global i64 0
@SIGKILL = global i32 0
define i32 @kill_child(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%7 = load i32, i32* %4
%8 = load i32, i32* @SIGTERM
%9 = call i32 @kill(i32 %7, i32 %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %12
11: ; preds = %2
store i32 -1, i32* %3
br label %28
12: ; preds = %2
%13 = load i32, i32* %5
%14 = call i32 @alarm(i32 %13)
store i32 0, i32* %6
%15 = load i32, i32* %4
%16 = call i32 @waitpid(i32 %15, i32* %6, i32 0)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %27
18: ; preds = %12
%19 = load i64, i64* @errno
%20 = load i64, i64* @EINTR
%21 = icmp eq i64 %19, %20
br i1 %21, label %22, label %26
22: ; preds = %18
%23 = load i32, i32* %4
%24 = load i32, i32* @SIGKILL
%25 = call i32 @kill(i32 %23, i32 %24)
store i32 %25, i32* %3
br label %28
26: ; preds = %18
store i32 -1, i32* %3
br label %28
27: ; preds = %12
store i32 0, i32* %3
br label %28
28: ; preds = %27, %26, %22, %11
%29 = load i32, i32* %3
ret i32 %29
}

LLVM IR (-O2):
@SIGTERM = global i32 0
@errno = global i64 0
@EINTR = global i64 0
@SIGKILL = global i32 0
define i32 @kill_child(i32 %0, i32 %1) {
%3 = alloca i32
%4 = load i32, i32* @SIGTERM
%5 = tail call i32 @kill(i32 %0, i32 %4)
%6 = icmp eq i32 %5, -1
br i1 %6, label %21, label %7
7: ; preds = %2
%8 = tail call i32 @alarm(i32 %1)
%9 = bitcast i32* %3 to i8*
store i32 0, i32* %3
%10 = call i32 @waitpid(i32 %0, i32* %3, i32 0)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %19
12: ; preds = %7
%13 = load i64, i64* @errno
%14 = load i64, i64* @EINTR
%15 = icmp eq i64 %13, %14
br i1 %15, label %16, label %19
16: ; preds = %12
%17 = load i32, i32* @SIGKILL
%18 = call i32 @kill(i32 %0, i32 %17)
br label %19
19: ; preds = %7, %12, %16
%20 = phi i32 [ %18, %16 ], [ -1, %12 ], [ 0, %7 ]
br label %21
21: ; preds = %2, %19
%22 = phi i32 [ %20, %19 ], [ -1, %2 ]
ret i32 %22
}