#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 msghdr {
  int member_3;
  char *member_4;
  int member_5;
  int /*<<< orphan*/ 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 {
  int /*<<< orphan*/ cmsg_len;
  int /*<<< orphan*/ cmsg_type;
  int /*<<< orphan*/ cmsg_level;
};

/* Variables and functions */
int /*<<< orphan*/ CMSG_DATA(struct cmsghdr *);
struct cmsghdr *CMSG_FIRSTHDR(struct msghdr *);
int /*<<< orphan*/ CMSG_LEN(size_t);
int CMSG_SPACE(size_t);
int /*<<< orphan*/ SCM_RIGHTS;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ memcpy(int /*<<< orphan*/, int *, size_t);
int /*<<< orphan*/ perror(char *);
size_t sendmsg(int, struct msghdr *, int /*<<< orphan*/);
int sendUpdate(int fd, long posn, size_t posn_len, int sendfd) {
  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};
  iov[0].iov_base = (void *)(&posn);
  iov[0].iov_len = posn_len;
  cmsg = CMSG_FIRSTHDR(&msg);
  cmsg->cmsg_level = SOL_SOCKET;
  cmsg->cmsg_type = SCM_RIGHTS;
  cmsg->cmsg_len = CMSG_LEN(msg_size);
  msg.msg_controllen = cmsg->cmsg_len;
  memcpy((CMSG_DATA(cmsg)), &sendfd, msg_size);
  len = sendmsg(fd, &msg, 0);
  if (len == (size_t)(-1)) {
    perror("sendmsg()");
    return -1;
  }
  return (len == posn_len);
}


x86 assembly (clang, -O0):
<sendUpdate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
movq   $0x4,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
call   1060 <CMSG_SPACE@plt>
mov    %eax,%eax
mov    %rsp,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,%rsp
mov    %rax,-0x40(%rbp)
movl   $0x0,-0x88(%rbp)
movq   $0x0,-0x80(%rbp)
lea    -0x50(%rbp),%rdx
mov    %edx,-0x78(%rbp)
movl   $0x1,-0x74(%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    -0x88(%rbp),%rdi
call   1090 <CMSG_FIRSTHDR@plt>
mov    %rax,-0x28(%rbp)
mov    0x2dd1(%rip),%rax
mov    (%rax),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    0x2db1(%rip),%rax
mov    (%rax),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x30(%rbp),%rdi
call   1050 <CMSG_LEN@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x74(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <CMSG_DATA@plt>
mov    %eax,%edi
mov    -0x30(%rbp),%rdx
lea    -0x1c(%rbp),%rsi
call   1070 <memcpy@plt>
mov    -0x8(%rbp),%edi
lea    -0x88(%rbp),%rsi
xor    %edx,%edx
call   1030 <sendmsg@plt>
mov    %rax,-0x58(%rbp)
cmpq   $0xffffffffffffffff,-0x58(%rbp)
jne    12a4 <sendUpdate+0x134>
lea    0xd77(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x8c(%rbp)
jmp    12c1 <sendUpdate+0x151>
mov    -0x58(%rbp),%rax
cmp    -0x18(%rbp),%rax
sete   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x8c(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sendUpdate>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x50,%rsp
mov    %rdx,%r12
mov    %edi,%r14d
mov    %rsi,-0x30(%rbp)
mov    %ecx,-0x24(%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,-0x70(%rbp)
movq   $0x0,-0x68(%rbp)
lea    -0x40(%rbp),%rdx
mov    %edx,-0x60(%rbp)
movl   $0x1,-0x5c(%rbp)
mov    %ecx,-0x58(%rbp)
mov    %rax,-0x50(%rbp)
movq   $0x0,-0x48(%rbp)
lea    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    %r12,-0x40(%rbp)
lea    -0x70(%rbp),%r15
mov    %r15,%rdi
call   1090 <CMSG_FIRSTHDR@plt>
mov    %rax,%rbx
mov    0x2df1(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rbx)
mov    0x2dd5(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rbx)
mov    $0x4,%edi
call   1050 <CMSG_LEN@plt>
mov    %eax,(%rbx)
mov    %eax,-0x5c(%rbp)
mov    %rbx,%rdi
call   1040 <CMSG_DATA@plt>
lea    -0x24(%rbp),%rsi
mov    $0x4,%edx
mov    %eax,%edi
call   1070 <memcpy@plt>
mov    %r14d,%edi
mov    %r15,%rsi
xor    %edx,%edx
call   1030 <sendmsg@plt>
cmp    $0xffffffffffffffff,%rax
je     125a <sendUpdate+0xea>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %r12,%rcx
sete   %al
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd9f(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%eax
jmp    124d <sendUpdate+0xdd>

LLVM IR (-O0):
%struct.cmsghdr = type { i32, i32, i32 }
%struct.iovec = type { i64, i8* }
%struct.msghdr = type { i32, i8*, i32, i32, i32, %struct.iovec*, i32, i32 }
@SOL_SOCKET = global i32 0
@SCM_RIGHTS = global i32 0
@.str = [10 x i8] c"sendmsg()\00"
define i32 @sendUpdate(i32 %0, i64 %1, i64 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i64
%8 = alloca i64
%9 = alloca i32
%10 = alloca %struct.cmsghdr*
%11 = alloca i64
%12 = alloca i8*
%13 = alloca i64
%14 = alloca [1 x %struct.iovec]
%15 = alloca i64
%16 = alloca %struct.msghdr
%17 = alloca i32
store i32 %0, i32* %6
store i64 %1, i64* %7
store i64 %2, i64* %8
store i32 %3, i32* %9
store i64 4, i64* %11
%18 = load i64, i64* %11
%19 = call i32 @CMSG_SPACE(i64 %18)
%20 = zext i32 %19 to i64
%21 = call i8* @llvm.stacksave()
store i8* %21, i8** %12
%22 = alloca i8, i64 %20
store i64 %20, i64* %13
%23 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %16, i32 0, i32 0
store i32 0, i32* %23
%24 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %16, i32 0, i32 1
store i8* null, i8** %24
%25 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %16, i32 0, i32 2
%26 = getelementptr inbounds [1 x %struct.iovec], [1 x %struct.iovec]* %14, i64 0, i64 0
%27 = ptrtoint %struct.iovec* %26 to i32
store i32 %27, i32* %25
%28 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %16, i32 0, i32 3
store i32 1, i32* %28
%29 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %16, i32 0, i32 4
%30 = ptrtoint i8* %22 to i32
store i32 %30, i32* %29
%31 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %16, 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* %16, i32 0, i32 6
store i32 0, i32* %33
%34 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %16, i32 0, i32 7
store i32 0, i32* %34
%35 = bitcast i64* %7 to i8*
%36 = getelementptr inbounds [1 x %struct.iovec], [1 x %struct.iovec]* %14, 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* %8
%39 = getelementptr inbounds [1 x %struct.iovec], [1 x %struct.iovec]* %14, 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* %16)
store %struct.cmsghdr* %41, %struct.cmsghdr** %10
%42 = load i32, i32* @SOL_SOCKET
%43 = load %struct.cmsghdr*, %struct.cmsghdr** %10
%44 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %43, i32 0, i32 2
store i32 %42, i32* %44
%45 = load i32, i32* @SCM_RIGHTS
%46 = load %struct.cmsghdr*, %struct.cmsghdr** %10
%47 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %46, i32 0, i32 1
store i32 %45, i32* %47
%48 = load i64, i64* %11
%49 = call i32 @CMSG_LEN(i64 %48)
%50 = load %struct.cmsghdr*, %struct.cmsghdr** %10
%51 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %50, i32 0, i32 0
store i32 %49, i32* %51
%52 = load %struct.cmsghdr*, %struct.cmsghdr** %10
%53 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %52, i32 0, i32 0
%54 = load i32, i32* %53
%55 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %16, i32 0, i32 3
store i32 %54, i32* %55
%56 = load %struct.cmsghdr*, %struct.cmsghdr** %10
%57 = call i32 @CMSG_DATA(%struct.cmsghdr* %56)
%58 = load i64, i64* %11
%59 = call i32 @memcpy(i32 %57, i32* %9, i64 %58)
%60 = load i32, i32* %6
%61 = call i64 @sendmsg(i32 %60, %struct.msghdr* %16, i32 0)
store i64 %61, i64* %15
%62 = load i64, i64* %15
%63 = icmp eq i64 %62, -1
br i1 %63, label %64, label %66
64: ; preds = %4
%65 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %5
store i32 1, i32* %17
br label %71
66: ; preds = %4
%67 = load i64, i64* %15
%68 = load i64, i64* %8
%69 = icmp eq i64 %67, %68
%70 = zext i1 %69 to i32
store i32 %70, i32* %5
store i32 1, i32* %17
br label %71
71: ; preds = %66, %64
%72 = load i8*, i8** %12
%73 = load i32, i32* %5
ret i32 %73
}

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