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

/* Variables and functions */
scalar_t__ CMSG_DATA(struct cmsghdr *);
int CMSG_SPACE(int);
scalar_t__ EFAULT;
int /*<<< orphan*/ MSG_WAITALL;
scalar_t__ errno;
scalar_t__ 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;
  }
  total = recvmsg(fd, &msg, MSG_WAITALL);
  if (total != 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,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    $0x4,%edi
call   1040 <CMSG_SPACE@plt>
mov    %eax,%eax
mov    %rax,-0xb0(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x78(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0xa8(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x80(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x70(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
lea    -0x70(%rbp),%rax
mov    %rax,-0x40(%rbp)
movl   $0x1,-0x60(%rbp)
movq   $0x0,-0x48(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11df <ipc_read_fixed_size+0xaf>
mov    -0xb0(%rbp),%rax
mov    -0xa8(%rbp),%rcx
mov    %rcx,-0x58(%rbp)
mov    %eax,-0x50(%rbp)
jmp    11ee <ipc_read_fixed_size+0xbe>
movq   $0x0,-0x58(%rbp)
movl   $0x0,-0x50(%rbp)
mov    -0xc(%rbp),%edi
mov    0x2dd0(%rip),%rax
mov    (%rax),%edx
lea    -0x60(%rbp),%rsi
call   1050 <recvmsg@plt>
mov    %rax,-0x88(%rbp)
mov    -0x88(%rbp),%rax
cmp    -0x20(%rbp),%rax
je     124e <ipc_read_fixed_size+0x11e>
mov    0x2dd6(%rip),%rax
mov    (%rax),%rcx
mov    0x2dac(%rip),%rax
mov    %rcx,(%rax)
xor    %eax,%eax
mov    0x2da0(%rip),%rcx
sub    (%rcx),%rax
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x9c(%rbp)
jmp    129e <ipc_read_fixed_size+0x16e>
cmpq   $0x0,-0x28(%rbp)
je     1289 <ipc_read_fixed_size+0x159>
mov    -0xa8(%rbp),%rax
mov    %rax,-0x90(%rbp)
mov    -0x90(%rbp),%rdi
call   1030 <CMSG_DATA@plt>
mov    %rax,-0x98(%rbp)
mov    -0x98(%rbp),%rax
mov    (%rax),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x88(%rbp),%rax
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x9c(%rbp)
mov    -0x78(%rbp),%rax
mov    %rax,%rsp
mov    -0x8(%rbp),%rax
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    %rdx,%r12
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    %r12,-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    %r12,%rax
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    %r12,%rax
je     1209 <ipc_read_fixed_size+0xd9>
mov    0x2dff(%rip),%rax
mov    (%rax),%r12
mov    0x2dd5(%rip),%rax
mov    %r12,(%rax)
neg    %r12
mov    -0x30(%rbp),%rsp
mov    %r12,%rax
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 { i64, i8* }
%struct.cmsghdr = type { i32 }
@MSG_WAITALL = global i32 0
@EFAULT = global i64 0
@errno = global i64 0
define i64 @ipc_read_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.msghdr
%11 = alloca %struct.iovec
%12 = alloca i8*
%13 = alloca i64
%14 = alloca i64
%15 = alloca %struct.cmsghdr*
%16 = alloca i32*
%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** %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 i64, i64* %8
%25 = getelementptr inbounds %struct.iovec, %struct.iovec* %11, i32 0, i32 0
store i64 %24, i64* %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 i64 @recvmsg(i32 %41, %struct.msghdr* %10, i32 %42)
store i64 %43, i64* %14
%44 = load i64, i64* %14
%45 = load i64, i64* %8
%46 = icmp ne i64 %44, %45
br i1 %46, label %47, label %51
47: ; preds = %40
%48 = load i64, i64* @EFAULT
store i64 %48, i64* @errno
%49 = load i64, i64* @errno
%50 = sub nsw i64 0, %49
store i64 %50, i64* %5
store i32 1, i32* %17
br label %64
51: ; preds = %40
%52 = load i32*, i32** %9
%53 = icmp ne i32* %52, null
br i1 %53, label %54, label %62
54: ; preds = %51
%55 = bitcast i8* %21 to %struct.cmsghdr*
store %struct.cmsghdr* %55, %struct.cmsghdr** %15
%56 = load %struct.cmsghdr*, %struct.cmsghdr** %15
%57 = call i64 @CMSG_DATA(%struct.cmsghdr* %56)
%58 = inttoptr i64 %57 to i32*
store i32* %58, i32** %16
%59 = load i32*, i32** %16
%60 = load i32, i32* %59
%61 = load i32*, i32** %9
store i32 %60, i32* %61
br label %62
62: ; preds = %54, %51
%63 = load i64, i64* %14
store i64 %63, i64* %5
store i32 1, i32* %17
br label %64
64: ; preds = %62, %47
%65 = load i8*, i8** %12
%66 = load i64, i64* %5
ret i64 %66
}

LLVM IR (-O2):
%struct.msghdr = type { i32, i8*, i32, i64, %struct.iovec*, i64, i32* }
%struct.iovec = type { i64, i8* }
%struct.cmsghdr = type { i32 }
@MSG_WAITALL = global i32 0
@EFAULT = global i64 0
@errno = global i64 0
define i64 @ipc_read_fixed_size(i32 %0, i8* %1, i64 %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 i64 %2, i64* %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 i64 @recvmsg(i32 %0, %struct.msghdr* %5, i32 %24)
%26 = icmp eq i64 %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 i64 @recvmsg(i32 %0, %struct.msghdr* %5, i32 %29)
%31 = icmp eq i64 %30, %2
br i1 %31, label %35, label %32
32: ; preds = %27, %22
%33 = load i64, i64* @EFAULT
store i64 %33, i64* @errno
%34 = sub nsw i64 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 i64 [ %34, %32 ], [ %2, %35 ], [ %2, %22 ]
ret i64 %41
}