#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*/ fprintf(int /*<<< orphan*/, char *, int, char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ stderr;
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) {
    fprintf(stderr, "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    0x2d70(%rip),%rax
mov    (%rax),%rax
mov    0x2d5e(%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     1353 <do_kill+0x1b3>
mov    0x2d0e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x68(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x64(%rbp)
mov    -0xc(%rbp),%edi
call   1090 <WIFEXITED@plt>
mov    %rax,%rdx
lea    0xd67(%rip),%rax
lea    0xd5b(%rip),%rcx
cmp    $0x0,%rdx
cmovne %rcx,%rax
mov    %rax,-0x60(%rbp)
mov    -0xc(%rbp),%edi
call   1090 <WIFEXITED@plt>
cmp    $0x0,%rax
je     1324 <do_kill+0x184>
mov    -0xc(%rbp),%edi
call   1050 <WEXITSTATUS@plt>
mov    %eax,-0x6c(%rbp)
jmp    132f <do_kill+0x18f>
mov    -0xc(%rbp),%edi
call   1040 <WTERMSIG@plt>
mov    %eax,-0x6c(%rbp)
mov    -0x60(%rbp),%rcx
mov    -0x64(%rbp),%edx
mov    -0x68(%rbp),%edi
mov    -0x6c(%rbp),%r8d
lea    0xcf0(%rip),%rsi
call   1030 <fprintf@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     1269 <do_kill+0xc9>
mov    %eax,%r14d
test   %eax,%eax
je     1272 <do_kill+0xd2>
mov    %r14d,%r15d
mov    %rsp,%r12
xor    %ebx,%ebx
mov    0x2dfe(%rip),%r13
mov    0x2def(%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    12c3 <do_kill+0x123>
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    0x2d9c(%rip),%rax
mov    (%rax),%r14d
mov    (%rsp),%edi
call   1090 <WIFEXITED@plt>
mov    %rax,%rbp
mov    (%rsp),%edi
call   1090 <WIFEXITED@plt>
mov    (%rsp),%edi
test   %rax,%rax
jne    12d9 <do_kill+0x139>
call   1040 <WTERMSIG@plt>
jmp    12de <do_kill+0x13e>
lea    0xd90(%rip),%rdi
jmp    12ca <do_kill+0x12a>
lea    0xd94(%rip),%rsi
mov    %rsp,%r14
mov    %r14,%rdi
mov    %ebp,%edx
call   10a0 <sprintf@plt>
lea    0xd81(%rip),%rsi
lea    0x20(%rsp),%rbp
mov    %rbp,%rdi
mov    %ebx,%edx
call   10a0 <sprintf@plt>
lea    0xd6e(%rip),%rdi
lea    0xd6c(%rip),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r14,%rdx
mov    %rbp,%rcx
call   10c0 <execl@plt>
lea    0xd5a(%rip),%rdi
jmp    12ca <do_kill+0x12a>
lea    0xd5d(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
call   1050 <WEXITSTATUS@plt>
mov    %eax,%r8d
test   %rbp,%rbp
lea    0xd6b(%rip),%rax
lea    0xd5f(%rip),%rcx
cmove  %rax,%rcx
lea    0xd37(%rip),%rsi
mov    %r14d,%edi
mov    %r15d,%edx
call   1030 <fprintf@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"
@stderr = global i32 0
@.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 %80
59: ; preds = %55, %51
%60 = load i32, i32* @stderr
%61 = load i32, i32* %6
%62 = load i32, i32* %5
%63 = call i64 @WIFEXITED(i32 %62)
%64 = icmp ne i64 %63, 0
%65 = zext i1 %64 to i64
%66 = select i1 %64, 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)
%67 = load i32, i32* %5
%68 = call i64 @WIFEXITED(i32 %67)
%69 = icmp ne i64 %68, 0
br i1 %69, label %70, label %73
70: ; preds = %59
%71 = load i32, i32* %5
%72 = call i32 @WEXITSTATUS(i32 %71)
br label %76
73: ; preds = %59
%74 = load i32, i32* %5
%75 = call i32 @WTERMSIG(i32 %74)
br label %76
76: ; preds = %73, %70
%77 = phi i32 [ %72, %70 ], [ %75, %73 ]
%78 = call i32 @fprintf(i32 %60, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.7, i64 0, i64 0), i32 %61, i8* %66, i32 %77)
%79 = call i32 @exit(i32 1)
unreachable
80: ; 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"
@stderr = global i32 0
@.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 %57, label %39
39: ; preds = %35, %31
%40 = load i32, i32* @stderr
%41 = load i32, i32* %3
%42 = call i64 @WIFEXITED(i32 %41)
%43 = load i32, i32* %3
%44 = call i64 @WIFEXITED(i32 %43)
%45 = icmp eq i64 %44, 0
%46 = load i32, i32* %3
br i1 %45, label %49, label %47
47: ; preds = %39
%48 = call i32 @WEXITSTATUS(i32 %46)
br label %51
49: ; preds = %39
%50 = call i32 @WTERMSIG(i32 %46)
br label %51
51: ; preds = %49, %47
%52 = phi i32 [ %48, %47 ], [ %50, %49 ]
%53 = icmp eq i64 %42, 0
%54 = select i1 %53, 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)
%55 = call i32 @fprintf(i32 %40, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.7, i64 0, i64 0), i32 %7, i8* %54, i32 %52)
%56 = call i32 @exit(i32 1)
unreachable
57: ; preds = %35
ret void
}