#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct msghdr {
  scalar_t__ msg_controllen;
  TYPE_1__ *msg_iov;
};
struct cmsghdr {
  int /*<<< orphan*/ cmsg_len;
  int /*<<< orphan*/ cmsg_type;
  int /*<<< orphan*/ cmsg_level;
};
struct TYPE_2__ {
  size_t iov_len;
  void *iov_base;
};

/* Variables and functions */
int /*<<< orphan*/ CMSG_DATA(struct cmsghdr *);
struct cmsghdr *CMSG_FIRSTHDR(struct msghdr *);
int /*<<< orphan*/ CMSG_LEN(int);
scalar_t__ CMSG_SPACE(int);
int /*<<< orphan*/ SCM_RIGHTS;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ memcpy(int /*<<< orphan*/, int *, int);
struct msghdr *so_fdmsg_(struct msghdr *msg, const void *p, size_t n, int fd) {
  msg->msg_iov->iov_base = (void *)p;
  msg->msg_iov->iov_len = n;
  msg->msg_controllen = (((msg->msg_controllen) < (CMSG_SPACE(sizeof(int))))
                             ? (msg->msg_controllen)
                             : (CMSG_SPACE(sizeof(int))));
  struct cmsghdr *cmsg;
  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);
  return msg;
}


x86 assembly (clang, -O0):
<so_fdmsg_>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0x10(%rbp),%rcx
mov    -0x8(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rcx,0x8(%rax)
mov    -0x18(%rbp),%rcx
mov    -0x8(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
mov    $0x4,%edi
call   1050 <CMSG_SPACE@plt>
mov    %rax,%rcx
mov    -0x30(%rbp),%rax
cmp    %rcx,%rax
jge    11bb <so_fdmsg_+0x6b>
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x38(%rbp)
jmp    11c9 <so_fdmsg_+0x79>
mov    $0x4,%edi
call   1050 <CMSG_SPACE@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rdi
call   1070 <CMSG_FIRSTHDR@plt>
mov    %rax,-0x28(%rbp)
mov    0x2e00(%rip),%rax
mov    (%rax),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    0x2de0(%rip),%rax
mov    (%rax),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    $0x4,%edi
call   1040 <CMSG_LEN@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%rdi
call   1030 <CMSG_DATA@plt>
mov    %eax,%edi
lea    -0x1c(%rbp),%rsi
mov    $0x4,%edx
call   1060 <memcpy@plt>
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<so_fdmsg_>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    %ecx,0x4(%rsp)
mov    0x8(%rdi),%rax
mov    %rsi,0x8(%rax)
mov    %rdx,(%rax)
mov    (%rdi),%rbx
mov    $0x4,%edi
call   1050 <CMSG_SPACE@plt>
cmp    %rax,%rbx
jge    117d <so_fdmsg_+0x2d>
mov    (%r14),%rax
jmp    1187 <so_fdmsg_+0x37>
mov    $0x4,%edi
call   1050 <CMSG_SPACE@plt>
mov    %rax,(%r14)
mov    %r14,%rdi
call   1070 <CMSG_FIRSTHDR@plt>
mov    %rax,%rbx
mov    0x2e4c(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rbx)
mov    0x2e30(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rbx)
mov    $0x4,%edi
call   1040 <CMSG_LEN@plt>
mov    %eax,(%rbx)
mov    %rbx,%rdi
call   1030 <CMSG_DATA@plt>
lea    0x4(%rsp),%rsi
mov    %eax,%edi
mov    $0x4,%edx
call   1060 <memcpy@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.msghdr = type { i64, %struct.TYPE_2__* }
%struct.TYPE_2__ = type { i64, i8* }
%struct.cmsghdr = type { i32, i32, i32 }
@SOL_SOCKET = global i32 0
@SCM_RIGHTS = global i32 0
define %struct.msghdr* @so_fdmsg_(%struct.msghdr* %0, i8* %1, i64 %2, i32 %3) {
%5 = alloca %struct.msghdr*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca %struct.cmsghdr*
store %struct.msghdr* %0, %struct.msghdr** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i32 %3, i32* %8
%10 = load i8*, i8** %6
%11 = load %struct.msghdr*, %struct.msghdr** %5
%12 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %11, i32 0, i32 1
%13 = load %struct.TYPE_2__*, %struct.TYPE_2__** %12
%14 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %13, i32 0, i32 1
store i8* %10, i8** %14
%15 = load i64, i64* %7
%16 = load %struct.msghdr*, %struct.msghdr** %5
%17 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %16, i32 0, i32 1
%18 = load %struct.TYPE_2__*, %struct.TYPE_2__** %17
%19 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %18, i32 0, i32 0
store i64 %15, i64* %19
%20 = load %struct.msghdr*, %struct.msghdr** %5
%21 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %20, i32 0, i32 0
%22 = load i64, i64* %21
%23 = call i64 @CMSG_SPACE(i32 4)
%24 = icmp slt i64 %22, %23
br i1 %24, label %25, label %29
25: ; preds = %4
%26 = load %struct.msghdr*, %struct.msghdr** %5
%27 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %26, i32 0, i32 0
%28 = load i64, i64* %27
br label %31
29: ; preds = %4
%30 = call i64 @CMSG_SPACE(i32 4)
br label %31
31: ; preds = %29, %25
%32 = phi i64 [ %28, %25 ], [ %30, %29 ]
%33 = load %struct.msghdr*, %struct.msghdr** %5
%34 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %33, i32 0, i32 0
store i64 %32, i64* %34
%35 = load %struct.msghdr*, %struct.msghdr** %5
%36 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %35)
store %struct.cmsghdr* %36, %struct.cmsghdr** %9
%37 = load i32, i32* @SOL_SOCKET
%38 = load %struct.cmsghdr*, %struct.cmsghdr** %9
%39 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %38, i32 0, i32 2
store i32 %37, i32* %39
%40 = load i32, i32* @SCM_RIGHTS
%41 = load %struct.cmsghdr*, %struct.cmsghdr** %9
%42 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %41, i32 0, i32 1
store i32 %40, i32* %42
%43 = call i32 @CMSG_LEN(i32 4)
%44 = load %struct.cmsghdr*, %struct.cmsghdr** %9
%45 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %44, i32 0, i32 0
store i32 %43, i32* %45
%46 = load %struct.cmsghdr*, %struct.cmsghdr** %9
%47 = call i32 @CMSG_DATA(%struct.cmsghdr* %46)
%48 = call i32 @memcpy(i32 %47, i32* %8, i32 4)
%49 = load %struct.msghdr*, %struct.msghdr** %5
ret %struct.msghdr* %49
}

LLVM IR (-O2):
%struct.msghdr = type { i64, %struct.TYPE_2__* }
%struct.TYPE_2__ = type { i64, i8* }
%struct.cmsghdr = type { i32, i32, i32 }
@SOL_SOCKET = global i32 0
@SCM_RIGHTS = global i32 0
define %struct.msghdr* @so_fdmsg_(%struct.msghdr* returned %0, i8* %1, i64 %2, i32 %3) {
%5 = alloca i32
store i32 %3, i32* %5
%6 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %0, i64 0, i32 1
%7 = load %struct.TYPE_2__*, %struct.TYPE_2__** %6
%8 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %7, i64 0, i32 1
store i8* %1, i8** %8
%9 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %7, i64 0, i32 0
store i64 %2, i64* %9
%10 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %0, i64 0, i32 0
%11 = load i64, i64* %10
%12 = tail call i64 @CMSG_SPACE(i32 4)
%13 = icmp slt i64 %11, %12
br i1 %13, label %14, label %16
14: ; preds = %4
%15 = load i64, i64* %10
br label %18
16: ; preds = %4
%17 = tail call i64 @CMSG_SPACE(i32 4)
br label %18
18: ; preds = %16, %14
%19 = phi i64 [ %15, %14 ], [ %17, %16 ]
store i64 %19, i64* %10
%20 = tail call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %0)
%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 = tail call i32 @CMSG_LEN(i32 4)
%26 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %20, i64 0, i32 0
store i32 %25, i32* %26
%27 = tail call i32 @CMSG_DATA(%struct.cmsghdr* %20)
%28 = call i32 @memcpy(i32 %27, i32* %5, i32 4)
ret %struct.msghdr* %0
}