#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 msg_iovlen;
  char *msg_control;
  int msg_controllen;
  scalar_t__ msg_flags;
  struct iovec *msg_iov;
  scalar_t__ msg_namelen;
  int /*<<< orphan*/ *msg_name;
};
struct iovec {
  scalar_t__ iov_len;
  void *iov_base;
};
struct cmsghdr {
  int /*<<< orphan*/ cmsg_type;
  int /*<<< orphan*/ cmsg_level;
  int /*<<< orphan*/ cmsg_len;
};
typedef scalar_t__ ssize_t;
typedef int /*<<< orphan*/ msg;
typedef int /*<<< orphan*/ cmsgbuf;

/* Variables and functions */
scalar_t__ CMSG_DATA(struct cmsghdr *);
int /*<<< orphan*/ CMSG_LEN(int);
int CMSG_SPACE(int);
scalar_t__ EFAULT;
int /*<<< orphan*/ MSG_NOSIGNAL;
int /*<<< orphan*/ SCM_RIGHTS;
int /*<<< orphan*/ SOL_SOCKET;
scalar_t__ errno;
int /*<<< orphan*/ memset(struct msghdr *, int /*<<< orphan*/, int);
scalar_t__ sendmsg(int, struct msghdr *, int /*<<< orphan*/);
ssize_t ipc_write_fixed_size(int fd, void *buf, ssize_t remain, int ram_fd) {
  struct iovec iov;
  char cmsgbuf[CMSG_SPACE(sizeof(int))];
  struct cmsghdr *cmsg = (struct cmsghdr *)cmsgbuf;
  ssize_t total;
  int *p;
  struct msghdr msg;
  iov.iov_base = buf;
  iov.iov_len = remain;
  cmsg->cmsg_len = CMSG_LEN(sizeof(int));
  cmsg->cmsg_level = SOL_SOCKET;
  cmsg->cmsg_type = SCM_RIGHTS;
  p = (int *)CMSG_DATA(cmsg);
  *p = ram_fd;
  memset(&msg, 0, sizeof(msg));
  msg.msg_name = NULL;
  msg.msg_namelen = 0;
  msg.msg_iov = &iov;
  msg.msg_iovlen = 1;
  msg.msg_flags = 0;
  if (ram_fd >= 0) {
    msg.msg_control = cmsgbuf;
    msg.msg_controllen = sizeof(cmsgbuf);
  } else {
    msg.msg_control = NULL;
    msg.msg_controllen = 0;
  }
  total = sendmsg(fd, &msg, MSG_NOSIGNAL);
  if (total != remain) {
    errno = EFAULT;
    return -errno;
  }
  return total;
}


x86 assembly (clang, -O0):
<ipc_write_fixed_size>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    $0x4,%edi
call   1070 <CMSG_SPACE@plt>
mov    %eax,%eax
mov    %eax,%ecx
mov    %rcx,-0xb0(%rbp)
mov    %rsp,%rax
mov    %rax,-0x40(%rbp)
mov    %rcx,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rax
sub    %rdx,%rax
mov    %rax,-0xa8(%rbp)
mov    %rax,%rsp
mov    %rcx,-0x48(%rbp)
mov    %rax,-0x50(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    $0x4,%edi
call   1060 <CMSG_LEN@plt>
mov    %eax,%ecx
mov    -0x50(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    0x2e0e(%rip),%rax
mov    (%rax),%ecx
mov    -0x50(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    0x2de6(%rip),%rax
mov    (%rax),%ecx
mov    -0x50(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x50(%rbp),%rdi
call   1040 <CMSG_DATA@plt>
mov    %rax,-0x60(%rbp)
mov    -0x24(%rbp),%ecx
mov    -0x60(%rbp),%rax
mov    %ecx,(%rax)
lea    -0x98(%rbp),%rdi
xor    %esi,%esi
mov    $0x38,%edx
call   1050 <memset@plt>
movq   $0x0,-0x68(%rbp)
movq   $0x0,-0x70(%rbp)
lea    -0x38(%rbp),%rax
mov    %rax,-0x78(%rbp)
movl   $0x1,-0x98(%rbp)
movq   $0x0,-0x80(%rbp)
cmpl   $0x0,-0x24(%rbp)
jl     1267 <ipc_write_fixed_size+0x117>
mov    -0xb0(%rbp),%rax
mov    -0xa8(%rbp),%rcx
mov    %rcx,-0x90(%rbp)
mov    %eax,-0x88(%rbp)
jmp    127c <ipc_write_fixed_size+0x12c>
movq   $0x0,-0x90(%rbp)
movl   $0x0,-0x88(%rbp)
mov    -0xc(%rbp),%edi
mov    0x2d3a(%rip),%rax
mov    (%rax),%edx
lea    -0x98(%rbp),%rsi
call   1030 <sendmsg@plt>
mov    %rax,-0x58(%rbp)
mov    -0x58(%rbp),%rax
cmp    -0x20(%rbp),%rax
je     12d9 <ipc_write_fixed_size+0x189>
mov    0x2d4b(%rip),%rax
mov    (%rax),%rcx
mov    0x2d19(%rip),%rax
mov    %rcx,(%rax)
xor    %eax,%eax
mov    0x2d0d(%rip),%rcx
sub    (%rcx),%rax
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x9c(%rbp)
jmp    12eb <ipc_write_fixed_size+0x19b>
mov    -0x58(%rbp),%rax
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x9c(%rbp)
mov    -0x40(%rbp),%rax
mov    %rax,%rsp
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ipc_write_fixed_size>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x68,%rsp
mov    %ecx,-0x2c(%rbp)
mov    %rdx,%r13
mov    %rsi,%r14
mov    %edi,-0x34(%rbp)
mov    $0x4,%edi
call   1070 <CMSG_SPACE@plt>
mov    %eax,-0x30(%rbp)
mov    %rsp,-0x40(%rbp)
mov    %eax,%r15d
mov    %rsp,%r12
add    $0xf,%r15
and    $0xfffffffffffffff0,%r15
mov    %r12,%rbx
sub    %r15,%rbx
mov    %rbx,%rsp
neg    %r15
mov    %r14,-0x48(%rbp)
mov    %r13,-0x50(%rbp)
mov    $0x4,%edi
call   1060 <CMSG_LEN@plt>
mov    %eax,0x8(%rbx)
mov    0x2e2c(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rbx)
mov    0x2e08(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%r12,%r15,1)
mov    %rbx,%rdi
call   1040 <CMSG_DATA@plt>
mov    -0x2c(%rbp),%r12d
mov    %r12d,(%rax)
lea    -0x88(%rbp),%r14
xor    %r15d,%r15d
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x38,%edx
call   1050 <memset@plt>
xorps  %xmm0,%xmm0
movups %xmm0,-0x60(%rbp)
lea    -0x50(%rbp),%rax
mov    %rax,-0x68(%rbp)
movl   $0x1,-0x88(%rbp)
movq   $0x0,-0x70(%rbp)
test   %r12d,%r12d
cmovs  %r15,%rbx
cmovns -0x30(%rbp),%r15d
mov    %rbx,-0x80(%rbp)
mov    %r15d,-0x78(%rbp)
mov    0x2d96(%rip),%rax
mov    (%rax),%edx
mov    -0x34(%rbp),%edi
mov    %r14,%rsi
call   1030 <sendmsg@plt>
cmp    %r13,%rax
je     1253 <ipc_write_fixed_size+0x103>
mov    0x2db5(%rip),%rax
mov    (%rax),%r13
mov    0x2d83(%rip),%rax
mov    %r13,(%rax)
neg    %r13
mov    -0x40(%rbp),%rsp
mov    %r13,%rax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.iovec = type { i64, i8* }
%struct.cmsghdr = type { i32, i32, i32 }
%struct.msghdr = type { i32, i8*, i32, i64, %struct.iovec*, i64, i32* }
@SOL_SOCKET = global i32 0
@SCM_RIGHTS = global i32 0
@MSG_NOSIGNAL = global i32 0
@EFAULT = global i64 0
@errno = global i64 0
define i64 @ipc_write_fixed_size(i32 %0, i8* %1, i64 %2, i32 %3) {
%5 = alloca i64
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
%10 = alloca %struct.iovec
%11 = alloca i8*
%12 = alloca i64
%13 = alloca %struct.cmsghdr*
%14 = alloca i64
%15 = alloca i32*
%16 = alloca %struct.msghdr
%17 = alloca i32
store i32 %0, i32* %6
store i8* %1, i8** %7
store i64 %2, i64* %8
store i32 %3, i32* %9
%18 = call i32 @CMSG_SPACE(i32 4)
%19 = zext i32 %18 to i64
%20 = call i8* @llvm.stacksave()
store i8* %20, i8** %11
%21 = alloca i8, i64 %19
store i64 %19, i64* %12
%22 = bitcast i8* %21 to %struct.cmsghdr*
store %struct.cmsghdr* %22, %struct.cmsghdr** %13
%23 = load i8*, i8** %7
%24 = getelementptr inbounds %struct.iovec, %struct.iovec* %10, i32 0, i32 1
store i8* %23, i8** %24
%25 = load i64, i64* %8
%26 = getelementptr inbounds %struct.iovec, %struct.iovec* %10, i32 0, i32 0
store i64 %25, i64* %26
%27 = call i32 @CMSG_LEN(i32 4)
%28 = load %struct.cmsghdr*, %struct.cmsghdr** %13
%29 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %28, i32 0, i32 2
store i32 %27, i32* %29
%30 = load i32, i32* @SOL_SOCKET
%31 = load %struct.cmsghdr*, %struct.cmsghdr** %13
%32 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %31, i32 0, i32 1
store i32 %30, i32* %32
%33 = load i32, i32* @SCM_RIGHTS
%34 = load %struct.cmsghdr*, %struct.cmsghdr** %13
%35 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %34, i32 0, i32 0
store i32 %33, i32* %35
%36 = load %struct.cmsghdr*, %struct.cmsghdr** %13
%37 = call i64 @CMSG_DATA(%struct.cmsghdr* %36)
%38 = inttoptr i64 %37 to i32*
store i32* %38, i32** %15
%39 = load i32, i32* %9
%40 = load i32*, i32** %15
store i32 %39, i32* %40
%41 = call i32 @memset(%struct.msghdr* %16, i32 0, i32 56)
%42 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %16, i32 0, i32 6
store i32* null, i32** %42
%43 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %16, i32 0, i32 5
store i64 0, i64* %43
%44 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %16, i32 0, i32 4
store %struct.iovec* %10, %struct.iovec** %44
%45 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %16, i32 0, i32 0
store i32 1, i32* %45
%46 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %16, i32 0, i32 3
store i64 0, i64* %46
%47 = load i32, i32* %9
%48 = icmp sge i32 %47, 0
br i1 %48, label %49, label %53
49: ; preds = %4
%50 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %16, i32 0, i32 1
store i8* %21, i8** %50
%51 = trunc i64 %19 to i32
%52 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %16, i32 0, i32 2
store i32 %51, i32* %52
br label %56
53: ; preds = %4
%54 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %16, i32 0, i32 1
store i8* null, i8** %54
%55 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %16, i32 0, i32 2
store i32 0, i32* %55
br label %56
56: ; preds = %53, %49
%57 = load i32, i32* %6
%58 = load i32, i32* @MSG_NOSIGNAL
%59 = call i64 @sendmsg(i32 %57, %struct.msghdr* %16, i32 %58)
store i64 %59, i64* %14
%60 = load i64, i64* %14
%61 = load i64, i64* %8
%62 = icmp ne i64 %60, %61
br i1 %62, label %63, label %67
63: ; preds = %56
%64 = load i64, i64* @EFAULT
store i64 %64, i64* @errno
%65 = load i64, i64* @errno
%66 = sub nsw i64 0, %65
store i64 %66, i64* %5
store i32 1, i32* %17
br label %69
67: ; preds = %56
%68 = load i64, i64* %14
store i64 %68, i64* %5
store i32 1, i32* %17
br label %69
69: ; preds = %67, %63
%70 = load i8*, i8** %11
%71 = load i64, i64* %5
ret i64 %71
}

LLVM IR (-O2):
%struct.iovec = type { i64, i8* }
%struct.msghdr = type { i32, i8*, i32, i64, %struct.iovec*, i64, i32* }
%struct.cmsghdr = type { i32, i32, i32 }
@SOL_SOCKET = global i32 0
@SCM_RIGHTS = global i32 0
@MSG_NOSIGNAL = global i32 0
@EFAULT = global i64 0
@errno = global i64 0
define i64 @ipc_write_fixed_size(i32 %0, i8* %1, i64 %2, i32 %3) {
%5 = alloca %struct.iovec
%6 = alloca %struct.msghdr
%7 = bitcast %struct.iovec* %5 to i8*
%8 = tail call i32 @CMSG_SPACE(i32 4)
%9 = zext i32 %8 to i64
%10 = tail call i8* @llvm.stacksave()
%11 = alloca i8, i64 %9
%12 = bitcast i8* %11 to %struct.cmsghdr*
%13 = bitcast %struct.msghdr* %6 to i8*
%14 = getelementptr inbounds %struct.iovec, %struct.iovec* %5, i64 0, i32 1
store i8* %1, i8** %14
%15 = getelementptr inbounds %struct.iovec, %struct.iovec* %5, i64 0, i32 0
store i64 %2, i64* %15
%16 = tail call i32 @CMSG_LEN(i32 4)
%17 = getelementptr inbounds i8, i8* %11, i64 8
%18 = bitcast i8* %17 to i32*
store i32 %16, i32* %18
%19 = load i32, i32* @SOL_SOCKET
%20 = getelementptr inbounds i8, i8* %11, i64 4
%21 = bitcast i8* %20 to i32*
store i32 %19, i32* %21
%22 = load i32, i32* @SCM_RIGHTS
%23 = bitcast i8* %11 to i32*
store i32 %22, i32* %23
%24 = call i64 @CMSG_DATA(%struct.cmsghdr* %12)
%25 = inttoptr i64 %24 to i32*
store i32 %3, i32* %25
%26 = call i32 @memset(%struct.msghdr* %6, i32 0, i32 56)
%27 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %6, i64 0, i32 5
%28 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %6, i64 0, i32 4
%29 = bitcast i64* %27 to i8*
store %struct.iovec* %5, %struct.iovec** %28
%30 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %6, i64 0, i32 0
store i32 1, i32* %30
%31 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %6, i64 0, i32 3
store i64 0, i64* %31
%32 = icmp sgt i32 %3, -1
%33 = select i1 %32, i8* %11, i8* null
%34 = select i1 %32, i32 %8, i32 0
%35 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %6, i64 0, i32 1
store i8* %33, i8** %35
%36 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %6, i64 0, i32 2
store i32 %34, i32* %36
%37 = load i32, i32* @MSG_NOSIGNAL
%38 = call i64 @sendmsg(i32 %0, %struct.msghdr* %6, i32 %37)
%39 = icmp eq i64 %38, %2
br i1 %39, label %43, label %40
40: ; preds = %4
%41 = load i64, i64* @EFAULT
store i64 %41, i64* @errno
%42 = sub nsw i64 0, %41
br label %43
43: ; preds = %4, %40
%44 = phi i64 [ %42, %40 ], [ %2, %4 ]
ret i64 %44
}