#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 {
  int cmsg_len;
  int /*<<< orphan*/ cmsg_type;
  int /*<<< orphan*/ cmsg_level;
};
typedef int /*<<< orphan*/ fd;
typedef int /*<<< orphan*/ control;

/* Variables and functions */
int /*<<< orphan*/ CMSG_DATA(struct cmsghdr *);
struct cmsghdr *CMSG_FIRSTHDR(struct msghdr *);
int CMSG_LEN(int);
int CMSG_SPACE(int);
int /*<<< orphan*/ SCM_RIGHTS;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ memcpy(int /*<<< orphan*/, int *, int);
int sendmsg(int, struct msghdr *, int /*<<< orphan*/);
int cras_send_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))];
  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);
  cmsg = CMSG_FIRSTHDR(&msg);
  cmsg->cmsg_level = SOL_SOCKET;
  cmsg->cmsg_type = SCM_RIGHTS;
  cmsg->cmsg_len = CMSG_LEN(sizeof(int));
  memcpy(CMSG_DATA(cmsg), &fd, sizeof(fd));
  msg.msg_controllen = cmsg->cmsg_len;
  return sendmsg(sockfd, &msg, 0);
}


x86 assembly (clang, -O0):
<cras_send_with_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
xorps  %xmm0,%xmm0
movaps %xmm0,-0x40(%rbp)
movaps %xmm0,-0x50(%rbp)
movq   $0x0,-0x30(%rbp)
mov    $0x4,%edi
call   1060 <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)
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)
lea    -0x50(%rbp),%rdi
call   1080 <CMSG_FIRSTHDR@plt>
mov    %rax,-0x68(%rbp)
mov    0x2df7(%rip),%rax
mov    (%rax),%ecx
mov    -0x68(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    0x2dd7(%rip),%rax
mov    (%rax),%ecx
mov    -0x68(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    $0x4,%edi
call   1050 <CMSG_LEN@plt>
mov    %eax,%ecx
mov    -0x68(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x68(%rbp),%rdi
call   1040 <CMSG_DATA@plt>
mov    %eax,%edi
lea    -0x1c(%rbp),%rsi
mov    $0x4,%edx
call   1070 <memcpy@plt>
mov    -0x68(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x50(%rbp),%rsi
xor    %edx,%edx
call   1030 <sendmsg@plt>
mov    -0x70(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cras_send_with_fd>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x40,%rsp
mov    %rdx,%r15
mov    %rsi,%rbx
mov    %edi,%r14d
mov    %ecx,-0x24(%rbp)
xorps  %xmm0,%xmm0
movaps %xmm0,-0x40(%rbp)
movaps %xmm0,-0x50(%rbp)
movq   $0x0,-0x30(%rbp)
mov    $0x4,%edi
call   1060 <CMSG_SPACE@plt>
mov    %rsp,%r12
mov    %eax,%ecx
mov    %rsp,%rdx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
sub    %rcx,%rdx
mov    %rdx,%rsp
lea    -0x60(%rbp),%rcx
mov    %rcx,-0x38(%rbp)
movl   $0x1,-0x50(%rbp)
mov    %rbx,-0x58(%rbp)
mov    %r15,-0x60(%rbp)
mov    %rdx,-0x48(%rbp)
mov    %eax,-0x40(%rbp)
lea    -0x50(%rbp),%r15
mov    %r15,%rdi
call   1080 <CMSG_FIRSTHDR@plt>
mov    %rax,%rbx
mov    0x2e06(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rbx)
mov    0x2dea(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rbx)
mov    $0x4,%edi
call   1050 <CMSG_LEN@plt>
mov    %eax,(%rbx)
mov    %rbx,%rdi
call   1040 <CMSG_DATA@plt>
lea    -0x24(%rbp),%rsi
mov    %eax,%edi
mov    $0x4,%edx
call   1070 <memcpy@plt>
mov    (%rbx),%eax
mov    %eax,-0x40(%rbp)
mov    %r14d,%edi
mov    %r15,%rsi
xor    %edx,%edx
call   1030 <sendmsg@plt>
mov    %r12,%rsp
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
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 { i32, i32, i32 }
@SOL_SOCKET = global i32 0
@SCM_RIGHTS = global i32 0
define i32 @cras_send_with_fd(i32 %0, i8* %1, i64 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca %struct.msghdr
%10 = alloca %struct.iovec
%11 = alloca %struct.cmsghdr*
%12 = alloca i8*
%13 = alloca i64
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i32 %3, i32* %8
%14 = bitcast %struct.msghdr* %9 to i8*
%15 = call i32 @CMSG_SPACE(i32 4)
%16 = zext i32 %15 to i64
%17 = call i8* @llvm.stacksave()
store i8* %17, i8** %12
%18 = alloca i8, i64 %16
store i64 %16, i64* %13
%19 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %9, i32 0, i32 3
store %struct.iovec* %10, %struct.iovec** %19
%20 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %9, i32 0, i32 0
store i32 1, i32* %20
%21 = load i8*, i8** %6
%22 = getelementptr inbounds %struct.iovec, %struct.iovec* %10, i32 0, i32 1
store i8* %21, i8** %22
%23 = load i64, i64* %7
%24 = getelementptr inbounds %struct.iovec, %struct.iovec* %10, i32 0, i32 0
store i64 %23, i64* %24
%25 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %9, i32 0, i32 1
store i8* %18, i8** %25
%26 = trunc i64 %16 to i32
%27 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %9, i32 0, i32 2
store i32 %26, i32* %27
%28 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %9)
store %struct.cmsghdr* %28, %struct.cmsghdr** %11
%29 = load i32, i32* @SOL_SOCKET
%30 = load %struct.cmsghdr*, %struct.cmsghdr** %11
%31 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %30, i32 0, i32 2
store i32 %29, i32* %31
%32 = load i32, i32* @SCM_RIGHTS
%33 = load %struct.cmsghdr*, %struct.cmsghdr** %11
%34 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %33, i32 0, i32 1
store i32 %32, i32* %34
%35 = call i32 @CMSG_LEN(i32 4)
%36 = load %struct.cmsghdr*, %struct.cmsghdr** %11
%37 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %36, i32 0, i32 0
store i32 %35, i32* %37
%38 = load %struct.cmsghdr*, %struct.cmsghdr** %11
%39 = call i32 @CMSG_DATA(%struct.cmsghdr* %38)
%40 = call i32 @memcpy(i32 %39, i32* %8, i32 4)
%41 = load %struct.cmsghdr*, %struct.cmsghdr** %11
%42 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %41, i32 0, i32 0
%43 = load i32, i32* %42
%44 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %9, i32 0, i32 2
store i32 %43, i32* %44
%45 = load i32, i32* %5
%46 = call i32 @sendmsg(i32 %45, %struct.msghdr* %9, i32 0)
%47 = load i8*, i8** %12
ret i32 %46
}

LLVM IR (-O2):
%struct.msghdr = type { i32, i8*, i32, %struct.iovec*, i32 }
%struct.iovec = type { i64, i8* }
%struct.cmsghdr = type { i32, i32, i32 }
@SOL_SOCKET = global i32 0
@SCM_RIGHTS = global i32 0
define i32 @cras_send_with_fd(i32 %0, i8* %1, i64 %2, i32 %3) {
%5 = alloca i32
%6 = alloca %struct.msghdr
%7 = alloca %struct.iovec
store i32 %3, i32* %5
%8 = bitcast %struct.msghdr* %6 to i8*
%9 = bitcast %struct.iovec* %7 to i8*
%10 = tail call i32 @CMSG_SPACE(i32 4)
%11 = zext i32 %10 to i64
%12 = tail call i8* @llvm.stacksave()
%13 = alloca i8, i64 %11
%14 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %6, i64 0, i32 3
store %struct.iovec* %7, %struct.iovec** %14
%15 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %6, i64 0, i32 0
store i32 1, i32* %15
%16 = getelementptr inbounds %struct.iovec, %struct.iovec* %7, i64 0, i32 1
store i8* %1, i8** %16
%17 = getelementptr inbounds %struct.iovec, %struct.iovec* %7, i64 0, i32 0
store i64 %2, i64* %17
%18 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %6, i64 0, i32 1
store i8* %13, i8** %18
%19 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %6, i64 0, i32 2
store i32 %10, i32* %19
%20 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %6)
%21 = load i32, i32* @SOL_SOCKET
%22 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %20, i64 0, i32 2
store i32 %21, i32* %22
%23 = load i32, i32* @SCM_RIGHTS
%24 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %20, i64 0, i32 1
store i32 %23, i32* %24
%25 = call i32 @CMSG_LEN(i32 4)
%26 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %20, i64 0, i32 0
store i32 %25, i32* %26
%27 = call i32 @CMSG_DATA(%struct.cmsghdr* %20)
%28 = call i32 @memcpy(i32 %27, i32* %5, i32 4)
%29 = load i32, i32* %26
store i32 %29, i32* %19
%30 = call i32 @sendmsg(i32 %0, %struct.msghdr* %6, i32 0)
ret i32 %30
}