#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 */
scalar_t__ EINTR;
int WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
int WTERMSIG(int);
scalar_t__ errno;
int /*<<< orphan*/ execl(char *, char *, char *, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, char *, int);
int /*<<< orphan*/ sprintf(char *, char *, int);
int vfork();
int waitpid(int, int *, int /*<<< orphan*/);
void do_kill(int pid, int sig) {
  int status;
  int killer;
  int ret;
  killer = vfork();
  if (killer == -1) {
    perror("killer/vfork");
    exit(1);
  }
  if (killer == 0) {
    char sigbuf[20];
    char pidbuf[20];
    sprintf(sigbuf, "-%d", sig);
    sprintf(pidbuf, "%d", pid);
    execl("/bin/kill", "kill", sigbuf, pidbuf, NULL);
    perror("exec failed");
    exit(1);
  }
  do
    ret = waitpid(killer, &status, 0);
  while (ret == -1 && errno == EINTR);
  if (ret != killer) {
    perror("kill/waitpid");
    exit(1);
  }
  if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
    printf("kill %d failed status=%s %d\n", killer,
           WIFEXITED(status) ? "exit" : "signal",
           WIFEXITED(status) ? WEXITSTATUS(status) : WTERMSIG(status));
    exit(1);
  }
}


x86 assembly (clang, -O0):
<do_kill>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
call   1080 <vfork@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    11d6 <do_kill+0x36>
lea    0xe39(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
cmpl   $0x0,-0x10(%rbp)
jne    123c <do_kill+0x9c>
lea    -0x30(%rbp),%rdi
mov    -0x8(%rbp),%edx
lea    0xe1f(%rip),%rsi
call   10a0 <sprintf@plt>
lea    -0x50(%rbp),%rdi
mov    -0x4(%rbp),%edx
lea    0xe0d(%rip),%rsi
call   10a0 <sprintf@plt>
lea    -0x30(%rbp),%rdx
lea    -0x50(%rbp),%rcx
lea    0xdfc(%rip),%rdi
lea    0xdfa(%rip),%rsi
xor    %eax,%eax
mov    %eax,%r8d
call   10c0 <execl@plt>
lea    0xdee(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
jmp    1241 <do_kill+0xa1>
mov    -0x10(%rbp),%edi
lea    -0xc(%rbp),%rsi
xor    %edx,%edx
call   1060 <waitpid@plt>
mov    %eax,-0x14(%rbp)
xor    %eax,%eax
cmpl   $0xffffffff,-0x14(%rbp)
mov    %al,-0x51(%rbp)
jne    127b <do_kill+0xdb>
mov    0x2d78(%rip),%rax
mov    (%rax),%rax
mov    0x2d66(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x51(%rbp)
mov    -0x51(%rbp),%al
test   $0x1,%al
jne    1241 <do_kill+0xa1>
mov    -0x14(%rbp),%eax
cmp    -0x10(%rbp),%eax
je     12a8 <do_kill+0x108>
lea    0xd8e(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0xc(%rbp),%edi
call   1090 <WIFEXITED@plt>
cmp    $0x0,%rax
je     12cb <do_kill+0x12b>
mov    -0xc(%rbp),%edi
call   1050 <WEXITSTATUS@plt>
cmp    $0x0,%eax
je     1343 <do_kill+0x1a3>
mov    -0x10(%rbp),%eax
mov    %eax,-0x64(%rbp)
mov    -0xc(%rbp),%edi
call   1090 <WIFEXITED@plt>
mov    %rax,%rdx
lea    0xd73(%rip),%rax
lea    0xd67(%rip),%rcx
cmp    $0x0,%rdx
cmovne %rcx,%rax
mov    %rax,-0x60(%rbp)
mov    -0xc(%rbp),%edi
call   1090 <WIFEXITED@plt>
cmp    $0x0,%rax
je     1318 <do_kill+0x178>
mov    -0xc(%rbp),%edi
call   1050 <WEXITSTATUS@plt>
mov    %eax,-0x68(%rbp)
jmp    1323 <do_kill+0x183>
mov    -0xc(%rbp),%edi
call   1040 <WTERMSIG@plt>
mov    %eax,-0x68(%rbp)
mov    -0x60(%rbp),%rdx
mov    -0x64(%rbp),%esi
mov    -0x68(%rbp),%ecx
lea    0xd00(%rip),%rdi
call   1030 <printf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_kill>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %esi,%ebp
mov    %edi,%ebx
call   1080 <vfork@plt>
cmp    $0xffffffff,%eax
je     125f <do_kill+0xbf>
mov    %eax,%r14d
test   %eax,%eax
je     1268 <do_kill+0xc8>
mov    %r14d,%r15d
mov    %rsp,%r12
xor    %ebx,%ebx
mov    0x2e06(%rip),%r13
mov    0x2df7(%rip),%rbp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r15d,%edi
mov    %r12,%rsi
mov    %ebx,%edx
call   1060 <waitpid@plt>
cmp    $0xffffffff,%eax
jne    120c <do_kill+0x6c>
mov    0x0(%rbp),%rcx
cmp    %rcx,0x0(%r13)
je     11f0 <do_kill+0x50>
cmp    %r14d,%eax
jne    12b9 <do_kill+0x119>
mov    (%rsp),%edi
call   1090 <WIFEXITED@plt>
test   %rax,%rax
je     123d <do_kill+0x9d>
mov    (%rsp),%edi
call   1050 <WEXITSTATUS@plt>
test   %eax,%eax
jne    123d <do_kill+0x9d>
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%rsp),%edi
call   1090 <WIFEXITED@plt>
mov    %rax,%rbp
mov    (%rsp),%edi
call   1090 <WIFEXITED@plt>
mov    (%rsp),%edi
test   %rax,%rax
jne    12cf <do_kill+0x12f>
call   1040 <WTERMSIG@plt>
jmp    12d4 <do_kill+0x134>
lea    0xd9a(%rip),%rdi
jmp    12c0 <do_kill+0x120>
lea    0xd9e(%rip),%rsi
mov    %rsp,%r14
mov    %r14,%rdi
mov    %ebp,%edx
call   10a0 <sprintf@plt>
lea    0xd8b(%rip),%rsi
lea    0x20(%rsp),%rbp
mov    %rbp,%rdi
mov    %ebx,%edx
call   10a0 <sprintf@plt>
lea    0xd78(%rip),%rdi
lea    0xd76(%rip),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r14,%rdx
mov    %rbp,%rcx
call   10c0 <execl@plt>
lea    0xd64(%rip),%rdi
jmp    12c0 <do_kill+0x120>
lea    0xd67(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
call   1050 <WEXITSTATUS@plt>
mov    %eax,%ecx
test   %rbp,%rbp
lea    0xd76(%rip),%rax
lea    0xd6a(%rip),%rdx
cmove  %rax,%rdx
lea    0xd42(%rip),%rdi
mov    %r15d,%esi
call   1030 <printf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [13 x i8] c"killer/vfork\00"
@.str.1 = [4 x i8] c"-%d\00"
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [10 x i8] c"/bin/kill\00"
@.str.4 = [5 x i8] c"kill\00"
@.str.5 = [12 x i8] c"exec failed\00"
@errno = global i64 0
@EINTR = global i64 0
@.str.6 = [13 x i8] c"kill/waitpid\00"
@.str.7 = [29 x i8] c"kill %d failed status=%s %d\0A\00"
@.str.8 = [5 x i8] c"exit\00"
@.str.9 = [7 x i8] c"signal\00"
define void @do_kill(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca [20 x i8]
%9 = alloca [20 x i8]
store i32 %0, i32* %3
store i32 %1, i32* %4
%10 = call i32 @vfork()
store i32 %10, i32* %6
%11 = load i32, i32* %6
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %2
%17 = load i32, i32* %6
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %31
19: ; preds = %16
%20 = getelementptr inbounds [20 x i8], [20 x i8]* %8, i64 0, i64 0
%21 = load i32, i32* %4
%22 = call i32 @sprintf(i8* %20, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %21)
%23 = getelementptr inbounds [20 x i8], [20 x i8]* %9, i64 0, i64 0
%24 = load i32, i32* %3
%25 = call i32 @sprintf(i8* %23, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %24)
%26 = getelementptr inbounds [20 x i8], [20 x i8]* %8, i64 0, i64 0
%27 = getelementptr inbounds [20 x i8], [20 x i8]* %9, i64 0, i64 0
%28 = call i32 @execl(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i8* %26, i8* %27, i32* null)
%29 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0))
%30 = call i32 @exit(i32 1)
unreachable
31: ; preds = %16
br label %32
32: ; preds = %42, %31
%33 = load i32, i32* %6
%34 = call i32 @waitpid(i32 %33, i32* %5, i32 0)
store i32 %34, i32* %7
br label %35
35: ; preds = %32
%36 = load i32, i32* %7
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %42
38: ; preds = %35
%39 = load i64, i64* @errno
%40 = load i64, i64* @EINTR
%41 = icmp eq i64 %39, %40
br label %42
42: ; preds = %38, %35
%43 = phi i1 [ false, %35 ], [ %41, %38 ]
br i1 %43, label %32, label %44
44: ; preds = %42
%45 = load i32, i32* %7
%46 = load i32, i32* %6
%47 = icmp ne i32 %45, %46
br i1 %47, label %48, label %51
48: ; preds = %44
%49 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0))
%50 = call i32 @exit(i32 1)
unreachable
51: ; preds = %44
%52 = load i32, i32* %5
%53 = call i64 @WIFEXITED(i32 %52)
%54 = icmp ne i64 %53, 0
br i1 %54, label %55, label %59
55: ; preds = %51
%56 = load i32, i32* %5
%57 = call i32 @WEXITSTATUS(i32 %56)
%58 = icmp ne i32 %57, 0
br i1 %58, label %59, label %79
59: ; preds = %55, %51
%60 = load i32, i32* %6
%61 = load i32, i32* %5
%62 = call i64 @WIFEXITED(i32 %61)
%63 = icmp ne i64 %62, 0
%64 = zext i1 %63 to i64
%65 = select i1 %63, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.8, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.9, i64 0, i64 0)
%66 = load i32, i32* %5
%67 = call i64 @WIFEXITED(i32 %66)
%68 = icmp ne i64 %67, 0
br i1 %68, label %69, label %72
69: ; preds = %59
%70 = load i32, i32* %5
%71 = call i32 @WEXITSTATUS(i32 %70)
br label %75
72: ; preds = %59
%73 = load i32, i32* %5
%74 = call i32 @WTERMSIG(i32 %73)
br label %75
75: ; preds = %72, %69
%76 = phi i32 [ %71, %69 ], [ %74, %72 ]
%77 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.7, i64 0, i64 0), i32 %60, i8* %65, i32 %76)
%78 = call i32 @exit(i32 1)
unreachable
79: ; preds = %55
ret void
}

LLVM IR (-O2):
@.str = [13 x i8] c"killer/vfork\00"
@.str.1 = [4 x i8] c"-%d\00"
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [10 x i8] c"/bin/kill\00"
@.str.4 = [5 x i8] c"kill\00"
@.str.5 = [12 x i8] c"exec failed\00"
@errno = global i64 0
@EINTR = global i64 0
@.str.6 = [13 x i8] c"kill/waitpid\00"
@.str.7 = [29 x i8] c"kill %d failed status=%s %d\0A\00"
@.str.8 = [5 x i8] c"exit\00"
@.str.9 = [7 x i8] c"signal\00"
define void @do_kill(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca [20 x i8]
%5 = alloca [20 x i8]
%6 = bitcast i32* %3 to i8*
%7 = call i32 @vfork()
switch i32 %7, label %19 [
i32 -1, label %8
i32 0, label %11
]
8: ; preds = %2
%9 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%10 = call i32 @exit(i32 1)
unreachable
11: ; preds = %2
%12 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
%13 = getelementptr inbounds [20 x i8], [20 x i8]* %5, i64 0, i64 0
%14 = call i32 @sprintf(i8* %12, i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %1)
%15 = call i32 @sprintf(i8* %13, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %0)
%16 = call i32 @execl(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i8* %12, i8* %13, i32* null)
%17 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %2, %19
%20 = call i32 @waitpid(i32 %7, i32* %3, i32 0)
%21 = icmp eq i32 %20, -1
%22 = load i64, i64* @errno
%23 = load i64, i64* @EINTR
%24 = icmp eq i64 %22, %23
%25 = select i1 %21, i1 %24, i1 false
br i1 %25, label %19, label %26
26: ; preds = %19
%27 = icmp eq i32 %20, %7
br i1 %27, label %31, label %28
28: ; preds = %26
%29 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0))
%30 = call i32 @exit(i32 1)
unreachable
31: ; preds = %26
%32 = load i32, i32* %3
%33 = call i64 @WIFEXITED(i32 %32)
%34 = icmp eq i64 %33, 0
br i1 %34, label %39, label %35
35: ; preds = %31
%36 = load i32, i32* %3
%37 = call i32 @WEXITSTATUS(i32 %36)
%38 = icmp eq i32 %37, 0
br i1 %38, label %56, label %39
39: ; preds = %35, %31
%40 = load i32, i32* %3
%41 = call i64 @WIFEXITED(i32 %40)
%42 = load i32, i32* %3
%43 = call i64 @WIFEXITED(i32 %42)
%44 = icmp eq i64 %43, 0
%45 = load i32, i32* %3
br i1 %44, label %48, label %46
46: ; preds = %39
%47 = call i32 @WEXITSTATUS(i32 %45)
br label %50
48: ; preds = %39
%49 = call i32 @WTERMSIG(i32 %45)
br label %50
50: ; preds = %48, %46
%51 = phi i32 [ %47, %46 ], [ %49, %48 ]
%52 = icmp eq i64 %41, 0
%53 = select i1 %52, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.9, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.8, i64 0, i64 0)
%54 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.7, i64 0, i64 0), i32 %7, i8* %53, i32 %51)
%55 = call i32 @exit(i32 1)
unreachable
56: ; preds = %35
ret void
}