#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 {
  int /*<<< orphan*/ iov_len;
  void *iov_base;
};
struct cmsghdr {
  int /*<<< orphan*/ cmsg_type;
  int /*<<< orphan*/ cmsg_level;
  int /*<<< orphan*/ cmsg_len;
};
typedef int /*<<< orphan*/ 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);
int /*<<< orphan*/ EFAULT;
int /*<<< orphan*/ MSG_NOSIGNAL;
int /*<<< orphan*/ SCM_RIGHTS;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ memset(struct msghdr *, int /*<<< orphan*/, int);
int /*<<< orphan*/ 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;
  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;
  struct msghdr msg;
  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;
  }
  if ((total = sendmsg(fd, &msg, MSG_NOSIGNAL)) != remain) {
    errno = EFAULT;
    return -errno;
  }
  return total;
}


x86 assembly (clang, -O0):
<ipc_write_fixed_size>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    $0x4,%edi
call   1070 <CMSG_SPACE@plt>
mov    %eax,%eax
mov    %eax,%ecx
mov    %rcx,-0xa0(%rbp)
mov    %rsp,%rax
mov    %rax,-0x30(%rbp)
mov    %rcx,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rax
sub    %rdx,%rax
mov    %rax,-0x98(%rbp)
mov    %rax,%rsp
mov    %rcx,-0x38(%rbp)
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    $0x4,%edi
call   1060 <CMSG_LEN@plt>
mov    %eax,%ecx
mov    -0x40(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    0x2e11(%rip),%rax
mov    (%rax),%ecx
mov    -0x40(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    0x2de9(%rip),%rax
mov    (%rax),%ecx
mov    -0x40(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x40(%rbp),%rdi
call   1040 <CMSG_DATA@plt>
mov    %rax,-0x50(%rbp)
mov    -0x18(%rbp),%ecx
mov    -0x50(%rbp),%rax
mov    %ecx,(%rax)
lea    -0x88(%rbp),%rdi
xor    %esi,%esi
mov    $0x38,%edx
call   1050 <memset@plt>
movq   $0x0,-0x58(%rbp)
movq   $0x0,-0x60(%rbp)
lea    -0x28(%rbp),%rax
mov    %rax,-0x68(%rbp)
movl   $0x1,-0x88(%rbp)
movq   $0x0,-0x70(%rbp)
cmpl   $0x0,-0x18(%rbp)
jl     125e <ipc_write_fixed_size+0x10e>
mov    -0xa0(%rbp),%rax
mov    -0x98(%rbp),%rcx
mov    %rcx,-0x80(%rbp)
mov    %eax,-0x78(%rbp)
jmp    126d <ipc_write_fixed_size+0x11d>
movq   $0x0,-0x80(%rbp)
movl   $0x0,-0x78(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2d49(%rip),%rax
mov    (%rax),%edx
lea    -0x88(%rbp),%rsi
call   1030 <sendmsg@plt>
mov    %eax,-0x44(%rbp)
cmp    -0x14(%rbp),%eax
je     12c0 <ipc_write_fixed_size+0x170>
mov    0x2d60(%rip),%rax
mov    (%rax),%ecx
mov    0x2d2f(%rip),%rax
mov    %ecx,(%rax)
xor    %eax,%eax
mov    0x2d24(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x8c(%rbp)
jmp    12d0 <ipc_write_fixed_size+0x180>
mov    -0x44(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x8c(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
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    %edx,%r13d
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    %r13d,-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    %r13d,%eax
je     1253 <ipc_write_fixed_size+0x103>
mov    0x2db5(%rip),%rax
mov    (%rax),%r13d
mov    0x2d83(%rip),%rax
mov    %r13d,(%rax)
neg    %r13d
mov    -0x40(%rbp),%rsp
mov    %r13d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.iovec = type { i32, 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 i32 0
@errno = global i32 0
define i32 @ipc_write_fixed_size(i32 %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.iovec
%11 = alloca i8*
%12 = alloca i64
%13 = alloca %struct.cmsghdr*
%14 = alloca i32
%15 = alloca i32*
%16 = alloca %struct.msghdr
%17 = alloca i32
store i32 %0, i32* %6
store i8* %1, i8** %7
store i32 %2, i32* %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 i32, i32* %8
%26 = getelementptr inbounds %struct.iovec, %struct.iovec* %10, i32 0, i32 0
store i32 %25, i32* %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 i32 @sendmsg(i32 %57, %struct.msghdr* %16, i32 %58)
store i32 %59, i32* %14
%60 = load i32, i32* %8
%61 = icmp ne i32 %59, %60
br i1 %61, label %62, label %66
62: ; preds = %56
%63 = load i32, i32* @EFAULT
store i32 %63, i32* @errno
%64 = load i32, i32* @errno
%65 = sub nsw i32 0, %64
store i32 %65, i32* %5
store i32 1, i32* %17
br label %68
66: ; preds = %56
%67 = load i32, i32* %14
store i32 %67, i32* %5
store i32 1, i32* %17
br label %68
68: ; preds = %66, %62
%69 = load i8*, i8** %11
%70 = load i32, i32* %5
ret i32 %70
}

LLVM IR (-O2):
%struct.iovec = type { i32, 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 i32 0
@errno = global i32 0
define i32 @ipc_write_fixed_size(i32 %0, i8* %1, i32 %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 = getelementptr inbounds %struct.iovec, %struct.iovec* %5, i64 0, i32 1
store i8* %1, i8** %13
%14 = getelementptr inbounds %struct.iovec, %struct.iovec* %5, i64 0, i32 0
store i32 %2, i32* %14
%15 = tail call i32 @CMSG_LEN(i32 4)
%16 = getelementptr inbounds i8, i8* %11, i64 8
%17 = bitcast i8* %16 to i32*
store i32 %15, i32* %17
%18 = load i32, i32* @SOL_SOCKET
%19 = getelementptr inbounds i8, i8* %11, i64 4
%20 = bitcast i8* %19 to i32*
store i32 %18, i32* %20
%21 = load i32, i32* @SCM_RIGHTS
%22 = bitcast i8* %11 to i32*
store i32 %21, i32* %22
%23 = call i64 @CMSG_DATA(%struct.cmsghdr* %12)
%24 = inttoptr i64 %23 to i32*
store i32 %3, i32* %24
%25 = bitcast %struct.msghdr* %6 to i8*
%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 i32 @sendmsg(i32 %0, %struct.msghdr* %6, i32 %37)
%39 = icmp eq i32 %38, %2
br i1 %39, label %43, label %40
40: ; preds = %4
%41 = load i32, i32* @EFAULT
store i32 %41, i32* @errno
%42 = sub nsw i32 0, %41
br label %43
43: ; preds = %4, %40
%44 = phi i32 [ %42, %40 ], [ %2, %4 ]
ret i32 %44
}