#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 dummy;
};
typedef int /*<<< orphan*/ ssize_t;
typedef int /*<<< orphan*/ cmsgbuf;

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


x86 assembly (clang, -O0):
<ipc_read_fixed_size>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    $0x4,%edi
call   1040 <CMSG_SPACE@plt>
mov    %eax,%eax
mov    %rax,-0xa8(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x70(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0xa0(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x78(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x68(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
lea    -0x68(%rbp),%rax
mov    %rax,-0x38(%rbp)
movl   $0x1,-0x58(%rbp)
movq   $0x0,-0x40(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11dc <ipc_read_fixed_size+0xac>
mov    -0xa8(%rbp),%rax
mov    -0xa0(%rbp),%rcx
mov    %rcx,-0x50(%rbp)
mov    %eax,-0x48(%rbp)
jmp    11eb <ipc_read_fixed_size+0xbb>
movq   $0x0,-0x50(%rbp)
movl   $0x0,-0x48(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2dd3(%rip),%rax
mov    (%rax),%edx
lea    -0x58(%rbp),%rsi
call   1050 <recvmsg@plt>
mov    %eax,-0x7c(%rbp)
cmp    -0x14(%rbp),%eax
je     123b <ipc_read_fixed_size+0x10b>
mov    0x2de5(%rip),%rax
mov    (%rax),%ecx
mov    0x2dbc(%rip),%rax
mov    %ecx,(%rax)
xor    %eax,%eax
mov    0x2db1(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x94(%rbp)
jmp    1286 <ipc_read_fixed_size+0x156>
cmpq   $0x0,-0x20(%rbp)
je     1276 <ipc_read_fixed_size+0x146>
mov    -0xa0(%rbp),%rax
mov    %rax,-0x88(%rbp)
mov    -0x88(%rbp),%rdi
call   1030 <CMSG_DATA@plt>
mov    %rax,-0x90(%rbp)
mov    -0x90(%rbp),%rax
mov    (%rax),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x7c(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x94(%rbp)
mov    -0x70(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ipc_read_fixed_size>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x58,%rsp
mov    %rcx,%r14
mov    %edx,%r12d
mov    %rsi,%r13
mov    %edi,%r15d
mov    $0x4,%edi
call   1040 <CMSG_SPACE@plt>
mov    %rsp,-0x30(%rbp)
mov    %eax,%ecx
mov    %rsp,%rbx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
sub    %rcx,%rbx
mov    %rbx,%rsp
mov    %r13,-0x38(%rbp)
mov    %r12d,-0x40(%rbp)
xorps  %xmm0,%xmm0
movups %xmm0,-0x50(%rbp)
lea    -0x40(%rbp),%rcx
mov    %rcx,-0x58(%rbp)
movl   $0x1,-0x78(%rbp)
movq   $0x0,-0x60(%rbp)
test   %r14,%r14
je     11c9 <ipc_read_fixed_size+0x99>
mov    %rbx,-0x70(%rbp)
mov    %eax,-0x68(%rbp)
mov    0x2e21(%rip),%rax
mov    (%rax),%edx
lea    -0x78(%rbp),%rsi
mov    %r15d,%edi
call   1050 <recvmsg@plt>
cmp    %r12d,%eax
jne    11f2 <ipc_read_fixed_size+0xc2>
mov    %rbx,%rdi
call   1030 <CMSG_DATA@plt>
mov    (%rax),%eax
mov    %eax,(%r14)
jmp    1209 <ipc_read_fixed_size+0xd9>
movq   $0x0,-0x70(%rbp)
movl   $0x0,-0x68(%rbp)
mov    0x2de9(%rip),%rax
mov    (%rax),%edx
lea    -0x78(%rbp),%rsi
mov    %r15d,%edi
call   1050 <recvmsg@plt>
cmp    %r12d,%eax
je     1209 <ipc_read_fixed_size+0xd9>
mov    0x2dff(%rip),%rax
mov    (%rax),%r12d
mov    0x2dd5(%rip),%rax
mov    %r12d,(%rax)
neg    %r12d
mov    -0x30(%rbp),%rsp
mov    %r12d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.msghdr = type { i32, i8*, i32, i64, %struct.iovec*, i64, i32* }
%struct.iovec = type { i32, i8* }
%struct.cmsghdr = type { i32 }
@MSG_WAITALL = global i32 0
@EFAULT = global i32 0
@errno = global i32 0
define i32 @ipc_read_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.msghdr
%11 = alloca %struct.iovec
%12 = alloca i8*
%13 = alloca i64
%14 = alloca i32
%15 = alloca %struct.cmsghdr*
%16 = alloca i32*
%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** %12
%21 = alloca i8, i64 %19
store i64 %19, i64* %13
%22 = load i8*, i8** %7
%23 = getelementptr inbounds %struct.iovec, %struct.iovec* %11, i32 0, i32 1
store i8* %22, i8** %23
%24 = load i32, i32* %8
%25 = getelementptr inbounds %struct.iovec, %struct.iovec* %11, i32 0, i32 0
store i32 %24, i32* %25
%26 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %10, i32 0, i32 6
store i32* null, i32** %26
%27 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %10, i32 0, i32 5
store i64 0, i64* %27
%28 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %10, i32 0, i32 4
store %struct.iovec* %11, %struct.iovec** %28
%29 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %10, i32 0, i32 0
store i32 1, i32* %29
%30 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %10, i32 0, i32 3
store i64 0, i64* %30
%31 = load i32*, i32** %9
%32 = icmp ne i32* %31, null
br i1 %32, label %33, label %37
33: ; preds = %4
%34 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %10, i32 0, i32 1
store i8* %21, i8** %34
%35 = trunc i64 %19 to i32
%36 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %10, i32 0, i32 2
store i32 %35, i32* %36
br label %40
37: ; preds = %4
%38 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %10, i32 0, i32 1
store i8* null, i8** %38
%39 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %10, i32 0, i32 2
store i32 0, i32* %39
br label %40
40: ; preds = %37, %33
%41 = load i32, i32* %6
%42 = load i32, i32* @MSG_WAITALL
%43 = call i32 @recvmsg(i32 %41, %struct.msghdr* %10, i32 %42)
store i32 %43, i32* %14
%44 = load i32, i32* %8
%45 = icmp ne i32 %43, %44
br i1 %45, label %46, label %50
46: ; preds = %40
%47 = load i32, i32* @EFAULT
store i32 %47, i32* @errno
%48 = load i32, i32* @errno
%49 = sub nsw i32 0, %48
store i32 %49, i32* %5
store i32 1, i32* %17
br label %63
50: ; preds = %40
%51 = load i32*, i32** %9
%52 = icmp ne i32* %51, null
br i1 %52, label %53, label %61
53: ; preds = %50
%54 = bitcast i8* %21 to %struct.cmsghdr*
store %struct.cmsghdr* %54, %struct.cmsghdr** %15
%55 = load %struct.cmsghdr*, %struct.cmsghdr** %15
%56 = call i64 @CMSG_DATA(%struct.cmsghdr* %55)
%57 = inttoptr i64 %56 to i32*
store i32* %57, i32** %16
%58 = load i32*, i32** %16
%59 = load i32, i32* %58
%60 = load i32*, i32** %9
store i32 %59, i32* %60
br label %61
61: ; preds = %53, %50
%62 = load i32, i32* %14
store i32 %62, i32* %5
store i32 1, i32* %17
br label %63
63: ; preds = %61, %46
%64 = load i8*, i8** %12
%65 = load i32, i32* %5
ret i32 %65
}

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