#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;
  struct iovec *msg_iov;
  int /*<<< orphan*/ member_0;
};
struct iovec {
  size_t iov_len;
  void *iov_base;
};
struct cmsghdr {
  scalar_t__ cmsg_level;
  scalar_t__ cmsg_type;
};
typedef int /*<<< orphan*/ control;

/* Variables and functions */
int /*<<< orphan*/ CMSG_DATA(struct cmsghdr *);
struct cmsghdr *CMSG_FIRSTHDR(struct msghdr *);
struct cmsghdr *CMSG_NXTHDR(struct msghdr *, struct cmsghdr *);
int CMSG_SPACE(int);
scalar_t__ SCM_RIGHTS;
scalar_t__ SOL_SOCKET;
int /*<<< orphan*/ memcpy(int *, int /*<<< orphan*/, int);
int recvmsg(int, struct msghdr *, int /*<<< orphan*/);
int cras_recv_with_fd(int sockfd, void *buf, size_t len, int *fd) {
  struct msghdr msg = {0};
  struct iovec iov;
  struct cmsghdr *cmsg;
  char control[CMSG_SPACE(sizeof(int))];
  int rc;
  *fd = -1;
  msg.msg_iov = &iov;
  msg.msg_iovlen = 1;
  iov.iov_base = buf;
  iov.iov_len = len;
  msg.msg_control = control;
  msg.msg_controllen = sizeof(control);
  rc = recvmsg(sockfd, &msg, 0);
  if (rc < 0)
    return rc;
  for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
       cmsg = CMSG_NXTHDR(&msg, cmsg)) {
    if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) {
      memcpy(fd, CMSG_DATA(cmsg), sizeof(*fd));
      break;
    }
  }
  return rc;
}


x86 assembly (clang, -O0):
<cras_recv_with_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
xorps  %xmm0,%xmm0
movaps %xmm0,-0x40(%rbp)
movaps %xmm0,-0x50(%rbp)
movq   $0x0,-0x30(%rbp)
mov    $0x4,%edi
call   1040 <CMSG_SPACE@plt>
mov    %eax,%eax
mov    %rsp,%rcx
mov    %rcx,-0x70(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,%rsp
mov    %rax,-0x78(%rbp)
mov    -0x20(%rbp),%rdx
movl   $0xffffffff,(%rdx)
lea    -0x60(%rbp),%rdx
mov    %rdx,-0x38(%rbp)
movl   $0x1,-0x50(%rbp)
mov    -0x10(%rbp),%rdx
mov    %rdx,-0x58(%rbp)
mov    -0x18(%rbp),%rdx
mov    %rdx,-0x60(%rbp)
mov    %rcx,-0x48(%rbp)
mov    %eax,-0x40(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x50(%rbp),%rsi
xor    %edx,%edx
call   1070 <recvmsg@plt>
mov    %eax,-0x7c(%rbp)
cmpl   $0x0,-0x7c(%rbp)
jge    1215 <cras_recv_with_fd+0xb5>
mov    -0x7c(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x80(%rbp)
jmp    12b0 <cras_recv_with_fd+0x150>
lea    -0x50(%rbp),%rdi
call   1080 <CMSG_FIRSTHDR@plt>
mov    %rax,-0x68(%rbp)
cmpq   $0x0,-0x68(%rbp)
je     12a3 <cras_recv_with_fd+0x143>
mov    -0x68(%rbp),%rax
mov    (%rax),%rax
mov    0x2dad(%rip),%rcx
cmp    (%rcx),%rax
jne    1288 <cras_recv_with_fd+0x128>
mov    -0x68(%rbp),%rax
mov    0x8(%rax),%rax
mov    0x2d85(%rip),%rcx
cmp    (%rcx),%rax
jne    1288 <cras_recv_with_fd+0x128>
mov    -0x20(%rbp),%rax
mov    %rax,-0x88(%rbp)
mov    -0x68(%rbp),%rdi
call   1030 <CMSG_DATA@plt>
mov    -0x88(%rbp),%rdi
mov    %eax,%esi
mov    $0x4,%edx
call   1050 <memcpy@plt>
jmp    12a3 <cras_recv_with_fd+0x143>
jmp    128d <cras_recv_with_fd+0x12d>
mov    -0x68(%rbp),%rsi
lea    -0x50(%rbp),%rdi
call   1060 <CMSG_NXTHDR@plt>
mov    %rax,-0x68(%rbp)
jmp    1222 <cras_recv_with_fd+0xc2>
mov    -0x7c(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x80(%rbp)
mov    -0x70(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cras_recv_with_fd>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %rcx,%rbx
mov    %rdx,%r12
mov    %rsi,%r14
mov    %edi,%r15d
xorps  %xmm0,%xmm0
movaps %xmm0,-0x40(%rbp)
movaps %xmm0,-0x50(%rbp)
movq   $0x0,-0x30(%rbp)
mov    $0x4,%edi
call   1040 <CMSG_SPACE@plt>
mov    %rsp,%r13
mov    %eax,%ecx
mov    %rsp,%rdx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
sub    %rcx,%rdx
mov    %rdx,%rsp
mov    %rbx,-0x58(%rbp)
movl   $0xffffffff,(%rbx)
lea    -0x68(%rbp),%rcx
mov    %rcx,-0x38(%rbp)
movl   $0x1,-0x50(%rbp)
mov    %r14,-0x60(%rbp)
mov    %r12,-0x68(%rbp)
mov    %rdx,-0x48(%rbp)
mov    %eax,-0x40(%rbp)
lea    -0x50(%rbp),%rsi
mov    %r15d,%edi
xor    %edx,%edx
call   1070 <recvmsg@plt>
mov    %eax,%r15d
test   %eax,%eax
js     124a <cras_recv_with_fd+0xea>
lea    -0x50(%rbp),%rdi
call   1080 <CMSG_FIRSTHDR@plt>
test   %rax,%rax
je     124a <cras_recv_with_fd+0xea>
mov    0x2de6(%rip),%r12
mov    0x2dcf(%rip),%r14
lea    -0x50(%rbp),%rbx
jmp    1220 <cras_recv_with_fd+0xc0>
nop
mov    %rbx,%rdi
mov    %rax,%rsi
call   1060 <CMSG_NXTHDR@plt>
test   %rax,%rax
je     124a <cras_recv_with_fd+0xea>
mov    (%rax),%rcx
cmp    (%r12),%rcx
jne    1210 <cras_recv_with_fd+0xb0>
mov    0x8(%rax),%rcx
cmp    (%r14),%rcx
jne    1210 <cras_recv_with_fd+0xb0>
mov    %rax,%rdi
call   1030 <CMSG_DATA@plt>
mov    -0x58(%rbp),%rdi
mov    %eax,%esi
mov    $0x4,%edx
call   1050 <memcpy@plt>
mov    %r13,%rsp
mov    %r15d,%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, %struct.iovec*, i32 }
%struct.iovec = type { i64, i8* }
%struct.cmsghdr = type { i64, i64 }
@SOL_SOCKET = global i64 0
@SCM_RIGHTS = global i64 0
define i32 @cras_recv_with_fd(i32 %0, i8* %1, i64 %2, i32* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32*
%10 = alloca %struct.msghdr
%11 = alloca %struct.iovec
%12 = alloca %struct.cmsghdr*
%13 = alloca i8*
%14 = alloca i64
%15 = alloca i32
%16 = alloca i32
store i32 %0, i32* %6
store i8* %1, i8** %7
store i64 %2, i64* %8
store i32* %3, i32** %9
%17 = bitcast %struct.msghdr* %10 to i8*
%18 = call i32 @CMSG_SPACE(i32 4)
%19 = zext i32 %18 to i64
%20 = call i8* @llvm.stacksave()
store i8* %20, i8** %13
%21 = alloca i8, i64 %19
store i64 %19, i64* %14
%22 = load i32*, i32** %9
store i32 -1, i32* %22
%23 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %10, i32 0, i32 3
store %struct.iovec* %11, %struct.iovec** %23
%24 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %10, i32 0, i32 0
store i32 1, i32* %24
%25 = load i8*, i8** %7
%26 = getelementptr inbounds %struct.iovec, %struct.iovec* %11, i32 0, i32 1
store i8* %25, i8** %26
%27 = load i64, i64* %8
%28 = getelementptr inbounds %struct.iovec, %struct.iovec* %11, i32 0, i32 0
store i64 %27, i64* %28
%29 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %10, i32 0, i32 1
store i8* %21, i8** %29
%30 = trunc i64 %19 to i32
%31 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %10, i32 0, i32 2
store i32 %30, i32* %31
%32 = load i32, i32* %6
%33 = call i32 @recvmsg(i32 %32, %struct.msghdr* %10, i32 0)
store i32 %33, i32* %15
%34 = load i32, i32* %15
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %4
%37 = load i32, i32* %15
store i32 %37, i32* %5
store i32 1, i32* %16
br label %66
38: ; preds = %4
%39 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %10)
store %struct.cmsghdr* %39, %struct.cmsghdr** %12
br label %40
40: ; preds = %61, %38
%41 = load %struct.cmsghdr*, %struct.cmsghdr** %12
%42 = icmp ne %struct.cmsghdr* %41, null
br i1 %42, label %43, label %64
43: ; preds = %40
%44 = load %struct.cmsghdr*, %struct.cmsghdr** %12
%45 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %44, i32 0, i32 0
%46 = load i64, i64* %45
%47 = load i64, i64* @SOL_SOCKET
%48 = icmp eq i64 %46, %47
br i1 %48, label %49, label %60
49: ; preds = %43
%50 = load %struct.cmsghdr*, %struct.cmsghdr** %12
%51 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %50, i32 0, i32 1
%52 = load i64, i64* %51
%53 = load i64, i64* @SCM_RIGHTS
%54 = icmp eq i64 %52, %53
br i1 %54, label %55, label %60
55: ; preds = %49
%56 = load i32*, i32** %9
%57 = load %struct.cmsghdr*, %struct.cmsghdr** %12
%58 = call i32 @CMSG_DATA(%struct.cmsghdr* %57)
%59 = call i32 @memcpy(i32* %56, i32 %58, i32 4)
br label %64
60: ; preds = %49, %43
br label %61
61: ; preds = %60
%62 = load %struct.cmsghdr*, %struct.cmsghdr** %12
%63 = call %struct.cmsghdr* @CMSG_NXTHDR(%struct.msghdr* %10, %struct.cmsghdr* %62)
store %struct.cmsghdr* %63, %struct.cmsghdr** %12
br label %40
64: ; preds = %55, %40
%65 = load i32, i32* %15
store i32 %65, i32* %5
store i32 1, i32* %16
br label %66
66: ; preds = %64, %36
%67 = load i8*, i8** %13
%68 = load i32, i32* %5
ret i32 %68
}

LLVM IR (-O2):
%struct.msghdr = type { i32, i8*, i32, %struct.iovec*, i32 }
%struct.iovec = type { i64, i8* }
%struct.cmsghdr = type { i64, i64 }
@SOL_SOCKET = global i64 0
@SCM_RIGHTS = global i64 0
define i32 @cras_recv_with_fd(i32 %0, i8* %1, i64 %2, i32* %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
store i32 -1, i32* %3
%13 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i64 0, i32 3
store %struct.iovec* %6, %struct.iovec** %13
%14 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i64 0, i32 0
store i32 1, i32* %14
%15 = getelementptr inbounds %struct.iovec, %struct.iovec* %6, i64 0, i32 1
store i8* %1, i8** %15
%16 = getelementptr inbounds %struct.iovec, %struct.iovec* %6, i64 0, i32 0
store i64 %2, i64* %16
%17 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i64 0, i32 1
store i8* %12, i8** %17
%18 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i64 0, i32 2
store i32 %9, i32* %18
%19 = call i32 @recvmsg(i32 %0, %struct.msghdr* %5, i32 0)
%20 = icmp slt i32 %19, 0
br i1 %20, label %41, label %21
21: ; preds = %4
%22 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %5)
%23 = icmp eq %struct.cmsghdr* %22, null
br i1 %23, label %41, label %24
24: ; preds = %21, %38
%25 = phi %struct.cmsghdr* [ %39, %38 ], [ %22, %21 ]
%26 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %25, i64 0, i32 0
%27 = load i64, i64* %26
%28 = load i64, i64* @SOL_SOCKET
%29 = icmp eq i64 %27, %28
br i1 %29, label %30, label %38
30: ; preds = %24
%31 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %25, i64 0, i32 1
%32 = load i64, i64* %31
%33 = load i64, i64* @SCM_RIGHTS
%34 = icmp eq i64 %32, %33
br i1 %34, label %35, label %38
35: ; preds = %30
%36 = call i32 @CMSG_DATA(%struct.cmsghdr* %25)
%37 = call i32 @memcpy(i32* %3, i32 %36, i32 4)
br label %41
38: ; preds = %24, %30
%39 = call %struct.cmsghdr* @CMSG_NXTHDR(%struct.msghdr* %5, %struct.cmsghdr* %25)
%40 = icmp eq %struct.cmsghdr* %39, null
br i1 %40, label %41, label %24
41: ; preds = %38, %21, %35, %4
ret i32 %19
}