#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 pollfd {
  int fd;
  int events;
  scalar_t__ revents;
};
struct msghdr {
  int member_3;
  char *member_4;
  int member_5;
  scalar_t__ msg_controllen;
  int /*<<< orphan*/ member_6;
  struct iovec *member_2;
  int /*<<< orphan*/ member_1;
  int /*<<< orphan*/ member_0;
};
struct iovec {
  size_t iov_len;
  void *iov_base;
};
struct cmsghdr {
  scalar_t__ cmsg_len;
  int /*<<< orphan*/ cmsg_level;
};

/* Variables and functions */
struct cmsghdr *CMSG_FIRSTHDR(struct msghdr *);
int CMSG_SPACE(size_t);
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ perror(char *);
int poll(struct pollfd *, int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
size_t sendmsg(int, struct msghdr *, int /*<<< orphan*/);
int sendKill(int fd, long const posn, size_t posn_len) {
  struct cmsghdr *cmsg;
  size_t msg_size = sizeof(int);
  char control[CMSG_SPACE(msg_size)];
  struct iovec iov[1];
  size_t len;
  struct msghdr msg = {0, 0, iov, 1, control, sizeof control, 0};
  struct pollfd mypollfd;
  int rv;
  iov[0].iov_base = (void *)&posn;
  iov[0].iov_len = posn_len;
  cmsg = CMSG_FIRSTHDR(&msg);
  cmsg->cmsg_level = SOL_SOCKET;
  cmsg->cmsg_len = 0;
  msg.msg_controllen = cmsg->cmsg_len;
  printf("Killing posn %ld\n", posn);
  mypollfd.fd = fd;
  mypollfd.events = 0x2000;
  mypollfd.revents = 0;
  rv = poll(&mypollfd, 1, 0);
  printf("rv is %d\n", rv);
  if (rv == 0) {
    len = sendmsg(fd, &msg, 0);
    if (len == (size_t)(-1)) {
      perror("sendmsg()");
      return -1;
    }
    return (len == posn_len);
  } else {
    printf("already dead\n");
    return 0;
  }
}


x86 assembly (clang, -O0):
<sendKill>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x4,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1050 <CMSG_SPACE@plt>
mov    %eax,%eax
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,%rsp
mov    %rax,-0x38(%rbp)
movl   $0x0,-0x90(%rbp)
movq   $0x0,-0x88(%rbp)
lea    -0x50(%rbp),%rdx
mov    %edx,-0x80(%rbp)
movq   $0x1,-0x78(%rbp)
mov    %ecx,-0x70(%rbp)
mov    %rax,-0x68(%rbp)
movl   $0x0,-0x60(%rbp)
movl   $0x0,-0x5c(%rbp)
lea    -0x10(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x50(%rbp)
lea    -0x90(%rbp),%rdi
call   1080 <CMSG_FIRSTHDR@plt>
mov    %rax,-0x20(%rbp)
mov    0x2de0(%rip),%rax
mov    (%rax),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    -0x20(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x78(%rbp)
mov    -0x10(%rbp),%rsi
lea    0xdce(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0xa0(%rbp)
movl   $0x2000,-0x9c(%rbp)
movq   $0x0,-0x98(%rbp)
lea    -0xa0(%rbp),%rdi
mov    $0x1,%esi
xor    %edx,%edx
call   1060 <poll@plt>
mov    %eax,-0xa4(%rbp)
mov    -0xa4(%rbp),%esi
lea    0xd95(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpl   $0x0,-0xa4(%rbp)
jne    12f5 <sendKill+0x195>
mov    -0x8(%rbp),%edi
lea    -0x90(%rbp),%rsi
xor    %edx,%edx
call   1030 <sendmsg@plt>
mov    %rax,-0x58(%rbp)
cmpq   $0xffffffffffffffff,-0x58(%rbp)
jne    12d3 <sendKill+0x173>
lea    0xd64(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0xa8(%rbp)
jmp    1314 <sendKill+0x1b4>
mov    -0x58(%rbp),%rax
cmp    -0x18(%rbp),%rax
sete   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0xa8(%rbp)
jmp    1314 <sendKill+0x1b4>
lea    0xd2a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0xa8(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sendKill>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x70,%rsp
mov    %rdx,%r14
mov    %edi,%r15d
mov    %rsi,-0x28(%rbp)
mov    $0x4,%edi
call   1060 <CMSG_SPACE@plt>
mov    %eax,%eax
mov    %rsp,%rcx
lea    0xf(%rax),%rdx
and    $0xfffffffffffffff0,%rdx
sub    %rdx,%rcx
mov    %rcx,%rsp
movl   $0x0,-0x88(%rbp)
movq   $0x0,-0x80(%rbp)
lea    -0x50(%rbp),%rdx
mov    %edx,-0x78(%rbp)
movq   $0x1,-0x70(%rbp)
mov    %ecx,-0x68(%rbp)
mov    %rax,-0x60(%rbp)
movq   $0x0,-0x58(%rbp)
lea    -0x28(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    %r14,-0x50(%rbp)
lea    -0x88(%rbp),%rdi
call   1090 <CMSG_FIRSTHDR@plt>
mov    0x2df3(%rip),%rcx
mov    (%rcx),%ecx
mov    %ecx,0x8(%rax)
movq   $0x0,(%rax)
movq   $0x0,-0x70(%rbp)
mov    -0x28(%rbp),%rsi
lea    0xdec(%rip),%rdi
xor    %r12d,%r12d
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r15d,-0x38(%rbp)
movl   $0x2000,-0x34(%rbp)
movq   $0x0,-0x30(%rbp)
lea    -0x38(%rbp),%rdi
mov    $0x1,%esi
xor    %edx,%edx
call   1070 <poll@plt>
mov    %eax,%ebx
lea    0xdc8(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
test   %ebx,%ebx
je     1265 <sendKill+0xf5>
lea    0xdc8(%rip),%rdi
call   1040 <puts@plt>
jmp    1286 <sendKill+0x116>
lea    -0x88(%rbp),%rsi
mov    %r15d,%edi
xor    %edx,%edx
call   1030 <sendmsg@plt>
cmp    $0xffffffffffffffff,%rax
je     1296 <sendKill+0x126>
xor    %r12d,%r12d
cmp    %r14,%rax
sete   %r12b
mov    %r12d,%eax
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd7f(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%r12d
jmp    1286 <sendKill+0x116>

LLVM IR (-O0):
%struct.cmsghdr = type { i64, i32 }
%struct.iovec = type { i64, i8* }
%struct.msghdr = type { i32, i8*, i32, i64, i32, %struct.iovec*, i32, i32 }
%struct.pollfd = type { i32, i32, i64 }
@SOL_SOCKET = global i32 0
@.str = [18 x i8] c"Killing posn %ld\0A\00"
@.str.1 = [10 x i8] c"rv is %d\0A\00"
@.str.2 = [10 x i8] c"sendmsg()\00"
@.str.3 = [14 x i8] c"already dead\0A\00"
define i32 @sendKill(i32 %0, i64 %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i64
%7 = alloca i64
%8 = alloca %struct.cmsghdr*
%9 = alloca i64
%10 = alloca i8*
%11 = alloca i64
%12 = alloca [1 x %struct.iovec]
%13 = alloca i64
%14 = alloca %struct.msghdr
%15 = alloca %struct.pollfd
%16 = alloca i32
%17 = alloca i32
store i32 %0, i32* %5
store i64 %1, i64* %6
store i64 %2, i64* %7
store i64 4, i64* %9
%18 = load i64, i64* %9
%19 = call i32 @CMSG_SPACE(i64 %18)
%20 = zext i32 %19 to i64
%21 = call i8* @llvm.stacksave()
store i8* %21, i8** %10
%22 = alloca i8, i64 %20
store i64 %20, i64* %11
%23 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %14, i32 0, i32 0
store i32 0, i32* %23
%24 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %14, i32 0, i32 1
store i8* null, i8** %24
%25 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %14, i32 0, i32 2
%26 = getelementptr inbounds [1 x %struct.iovec], [1 x %struct.iovec]* %12, i64 0, i64 0
%27 = ptrtoint %struct.iovec* %26 to i32
store i32 %27, i32* %25
%28 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %14, i32 0, i32 3
store i64 1, i64* %28
%29 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %14, i32 0, i32 4
%30 = ptrtoint i8* %22 to i32
store i32 %30, i32* %29
%31 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %14, i32 0, i32 5
%32 = inttoptr i64 %20 to %struct.iovec*
store %struct.iovec* %32, %struct.iovec** %31
%33 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %14, i32 0, i32 6
store i32 0, i32* %33
%34 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %14, i32 0, i32 7
store i32 0, i32* %34
%35 = bitcast i64* %6 to i8*
%36 = getelementptr inbounds [1 x %struct.iovec], [1 x %struct.iovec]* %12, i64 0, i64 0
%37 = getelementptr inbounds %struct.iovec, %struct.iovec* %36, i32 0, i32 1
store i8* %35, i8** %37
%38 = load i64, i64* %7
%39 = getelementptr inbounds [1 x %struct.iovec], [1 x %struct.iovec]* %12, i64 0, i64 0
%40 = getelementptr inbounds %struct.iovec, %struct.iovec* %39, i32 0, i32 0
store i64 %38, i64* %40
%41 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %14)
store %struct.cmsghdr* %41, %struct.cmsghdr** %8
%42 = load i32, i32* @SOL_SOCKET
%43 = load %struct.cmsghdr*, %struct.cmsghdr** %8
%44 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %43, i32 0, i32 1
store i32 %42, i32* %44
%45 = load %struct.cmsghdr*, %struct.cmsghdr** %8
%46 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %45, i32 0, i32 0
store i64 0, i64* %46
%47 = load %struct.cmsghdr*, %struct.cmsghdr** %8
%48 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %47, i32 0, i32 0
%49 = load i64, i64* %48
%50 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %14, i32 0, i32 3
store i64 %49, i64* %50
%51 = load i64, i64* %6
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i64 %51)
%53 = load i32, i32* %5
%54 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %15, i32 0, i32 0
store i32 %53, i32* %54
%55 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %15, i32 0, i32 1
store i32 8192, i32* %55
%56 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %15, i32 0, i32 2
store i64 0, i64* %56
%57 = call i32 @poll(%struct.pollfd* %15, i32 1, i32 0)
store i32 %57, i32* %16
%58 = load i32, i32* %16
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %58)
%60 = load i32, i32* %16
%61 = icmp eq i32 %60, 0
br i1 %61, label %62, label %74
62: ; preds = %3
%63 = load i32, i32* %5
%64 = call i64 @sendmsg(i32 %63, %struct.msghdr* %14, i32 0)
store i64 %64, i64* %13
%65 = load i64, i64* %13
%66 = icmp eq i64 %65, -1
br i1 %66, label %67, label %69
67: ; preds = %62
%68 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %4
store i32 1, i32* %17
br label %76
69: ; preds = %62
%70 = load i64, i64* %13
%71 = load i64, i64* %7
%72 = icmp eq i64 %70, %71
%73 = zext i1 %72 to i32
store i32 %73, i32* %4
store i32 1, i32* %17
br label %76
74: ; preds = %3
%75 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %4
store i32 1, i32* %17
br label %76
76: ; preds = %74, %69, %67
%77 = load i8*, i8** %10
%78 = load i32, i32* %4
ret i32 %78
}

LLVM IR (-O2):
%struct.iovec = type { i64, i8* }
%struct.msghdr = type { i32, i8*, i32, i64, i32, %struct.iovec*, i32, i32 }
%struct.pollfd = type { i32, i32, i64 }
%struct.cmsghdr = type { i64, i32 }
@SOL_SOCKET = global i32 0
@.str = [18 x i8] c"Killing posn %ld\0A\00"
@.str.1 = [10 x i8] c"rv is %d\0A\00"
@.str.2 = [10 x i8] c"sendmsg()\00"
@str = [13 x i8] c"already dead\00"
define i32 @sendKill(i32 %0, i64 %1, i64 %2) {
%4 = alloca i64
%5 = alloca [1 x %struct.iovec]
%6 = alloca %struct.msghdr
%7 = alloca %struct.pollfd
store i64 %1, i64* %4
%8 = tail call i32 @CMSG_SPACE(i64 4)
%9 = zext i32 %8 to i64
%10 = alloca i8, i64 %9
%11 = bitcast [1 x %struct.iovec]* %5 to i8*
%12 = bitcast %struct.msghdr* %6 to i8*
%13 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %6, i64 0, i32 0
store i32 0, i32* %13
%14 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %6, i64 0, i32 1
store i8* null, i8** %14
%15 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %6, i64 0, i32 2
%16 = ptrtoint [1 x %struct.iovec]* %5 to i64
%17 = trunc i64 %16 to i32
store i32 %17, i32* %15
%18 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %6, i64 0, i32 3
store i64 1, i64* %18
%19 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %6, i64 0, i32 4
%20 = ptrtoint i8* %10 to i64
%21 = trunc i64 %20 to i32
store i32 %21, i32* %19
%22 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %6, i64 0, i32 5
%23 = inttoptr i64 %9 to %struct.iovec*
store %struct.iovec* %23, %struct.iovec** %22
%24 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %6, i64 0, i32 6
store i32 0, i32* %24
%25 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %6, i64 0, i32 7
store i32 0, i32* %25
%26 = bitcast %struct.pollfd* %7 to i8*
%27 = getelementptr inbounds [1 x %struct.iovec], [1 x %struct.iovec]* %5, i64 0, i64 0, i32 1
%28 = bitcast i8** %27 to i64**
store i64* %4, i64** %28
%29 = getelementptr inbounds [1 x %struct.iovec], [1 x %struct.iovec]* %5, i64 0, i64 0, i32 0
store i64 %2, i64* %29
%30 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %6)
%31 = load i32, i32* @SOL_SOCKET
%32 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %30, i64 0, i32 1
store i32 %31, i32* %32
%33 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %30, i64 0, i32 0
store i64 0, i64* %33
store i64 0, i64* %18
%34 = load i64, i64* %4
%35 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i64 %34)
%36 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %7, i64 0, i32 0
store i32 %0, i32* %36
%37 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %7, i64 0, i32 1
store i32 8192, i32* %37
%38 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %7, i64 0, i32 2
store i64 0, i64* %38
%39 = call i32 @poll(%struct.pollfd* %7, i32 1, i32 0)
%40 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %39)
%41 = icmp eq i32 %39, 0
br i1 %41, label %42, label %50
42: ; preds = %3
%43 = call i64 @sendmsg(i32 %0, %struct.msghdr* %6, i32 0)
%44 = icmp eq i64 %43, -1
br i1 %44, label %45, label %47
45: ; preds = %42
%46 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
br label %52
47: ; preds = %42
%48 = icmp eq i64 %43, %2
%49 = zext i1 %48 to i32
br label %52
50: ; preds = %3
%51 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
br label %52
52: ; preds = %50, %47, %45
%53 = phi i32 [ -1, %45 ], [ %49, %47 ], [ 0, %50 ]
ret i32 %53
}