#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 {
  char *msg_control;
  int msg_controllen;
  int /*<<< orphan*/ member_0;
};
struct cmsghdr {
  int cmsg_len;
  int /*<<< orphan*/ cmsg_type;
  int /*<<< orphan*/ cmsg_level;
};
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
scalar_t__ 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*/ perror(char *);
scalar_t__ sendmsg(int, struct msghdr *, int /*<<< orphan*/);
int main(int argc, char **argv) {
  struct msghdr msg = {0};
  struct cmsghdr *cmsg;
  char buf[CMSG_SPACE(sizeof(int))];
  int *fdptr;
  msg.msg_control = buf;
  msg.msg_controllen = sizeof(buf);
  cmsg = CMSG_FIRSTHDR(&msg);
  cmsg->cmsg_level = SOL_SOCKET;
  cmsg->cmsg_type = SCM_RIGHTS;
  cmsg->cmsg_len = CMSG_LEN(sizeof(int));
  fdptr = (int *)CMSG_DATA(cmsg);
  fdptr[0] = 0;
  msg.msg_controllen = cmsg->cmsg_len;
  if (sendmsg(1, &msg, 0) < 0)
    perror("sendmsg");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
xorps  %xmm0,%xmm0
movaps %xmm0,-0x20(%rbp)
mov    $0x4,%edi
call   1060 <CMSG_SPACE@plt>
mov    %eax,%eax
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,%rsp
mov    %rax,-0x38(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %eax,-0x18(%rbp)
lea    -0x20(%rbp),%rdi
call   1080 <CMSG_FIRSTHDR@plt>
mov    %rax,-0x28(%rbp)
mov    0x2e25(%rip),%rax
mov    (%rax),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    0x2e05(%rip),%rax
mov    (%rax),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    $0x4,%edi
call   1050 <CMSG_LEN@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%rdi
call   1040 <CMSG_DATA@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x28(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    $0x1,%edi
lea    -0x20(%rbp),%rsi
xor    %edx,%edx
call   1030 <sendmsg@plt>
cmp    $0x0,%rax
jge    1234 <main+0xd4>
lea    0xdd1(%rip),%rdi
call   1070 <perror@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
movq   $0x0,-0x20(%rbp)
mov    $0x4,%edi
call   1060 <CMSG_SPACE@plt>
mov    %rsp,%r15
mov    %eax,%ecx
mov    %rsp,%rdx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
sub    %rcx,%rdx
mov    %rdx,%rsp
mov    %rdx,-0x28(%rbp)
mov    %eax,-0x20(%rbp)
lea    -0x28(%rbp),%r14
mov    %r14,%rdi
call   1080 <CMSG_FIRSTHDR@plt>
mov    %rax,%rbx
mov    0x2e36(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rbx)
mov    0x2e1a(%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>
movl   $0x0,(%rax)
mov    (%rbx),%eax
mov    %eax,-0x20(%rbp)
mov    $0x1,%edi
mov    %r14,%rsi
xor    %edx,%edx
call   1030 <sendmsg@plt>
test   %rax,%rax
js     1206 <main+0xa6>
mov    %r15,%rsp
xor    %eax,%eax
lea    -0x18(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdf3(%rip),%rdi
call   1070 <perror@plt>
jmp    11f6 <main+0x96>

LLVM IR (-O0):
%struct.msghdr = type { i8*, i32, i32 }
%struct.cmsghdr = type { i32, i32, i32 }
@SOL_SOCKET = global i32 0
@SCM_RIGHTS = global i32 0
@.str = [8 x i8] c"sendmsg\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.msghdr
%7 = alloca %struct.cmsghdr*
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = bitcast %struct.msghdr* %6 to i8*
%12 = call i32 @CMSG_SPACE(i32 4)
%13 = zext i32 %12 to i64
%14 = call i8* @llvm.stacksave()
store i8* %14, i8** %8
%15 = alloca i8, i64 %13
store i64 %13, i64* %9
%16 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %6, i32 0, i32 0
store i8* %15, i8** %16
%17 = trunc i64 %13 to i32
%18 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %6, i32 0, i32 1
store i32 %17, i32* %18
%19 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %6)
store %struct.cmsghdr* %19, %struct.cmsghdr** %7
%20 = load i32, i32* @SOL_SOCKET
%21 = load %struct.cmsghdr*, %struct.cmsghdr** %7
%22 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %21, i32 0, i32 2
store i32 %20, i32* %22
%23 = load i32, i32* @SCM_RIGHTS
%24 = load %struct.cmsghdr*, %struct.cmsghdr** %7
%25 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %24, i32 0, i32 1
store i32 %23, i32* %25
%26 = call i32 @CMSG_LEN(i32 4)
%27 = load %struct.cmsghdr*, %struct.cmsghdr** %7
%28 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %27, i32 0, i32 0
store i32 %26, i32* %28
%29 = load %struct.cmsghdr*, %struct.cmsghdr** %7
%30 = call i64 @CMSG_DATA(%struct.cmsghdr* %29)
%31 = inttoptr i64 %30 to i32*
store i32* %31, i32** %10
%32 = load i32*, i32** %10
%33 = getelementptr inbounds i32, i32* %32, i64 0
store i32 0, i32* %33
%34 = load %struct.cmsghdr*, %struct.cmsghdr** %7
%35 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %34, i32 0, i32 0
%36 = load i32, i32* %35
%37 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %6, i32 0, i32 1
store i32 %36, i32* %37
%38 = call i64 @sendmsg(i32 1, %struct.msghdr* %6, i32 0)
%39 = icmp slt i64 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %2
%41 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
br label %42
42: ; preds = %40, %2
store i32 0, i32* %3
%43 = load i8*, i8** %8
%44 = load i32, i32* %3
ret i32 %44
}

LLVM IR (-O2):
%struct.msghdr = type { i8*, i32, i32 }
%struct.cmsghdr = type { i32, i32, i32 }
@SOL_SOCKET = global i32 0
@SCM_RIGHTS = global i32 0
@.str = [8 x i8] c"sendmsg\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.msghdr
%4 = bitcast %struct.msghdr* %3 to i8*
%5 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %3, i64 0, i32 1
%6 = bitcast i32* %5 to i64*
store i64 0, i64* %6
%7 = tail call i32 @CMSG_SPACE(i32 4)
%8 = zext i32 %7 to i64
%9 = tail call i8* @llvm.stacksave()
%10 = alloca i8, i64 %8
%11 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %3, i64 0, i32 0
store i8* %10, i8** %11
%12 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %3, i64 0, i32 1
store i32 %7, i32* %12
%13 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %3)
%14 = load i32, i32* @SOL_SOCKET
%15 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %13, i64 0, i32 2
store i32 %14, i32* %15
%16 = load i32, i32* @SCM_RIGHTS
%17 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %13, i64 0, i32 1
store i32 %16, i32* %17
%18 = call i32 @CMSG_LEN(i32 4)
%19 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %13, i64 0, i32 0
store i32 %18, i32* %19
%20 = call i64 @CMSG_DATA(%struct.cmsghdr* %13)
%21 = inttoptr i64 %20 to i32*
store i32 0, i32* %21
%22 = load i32, i32* %19
store i32 %22, i32* %12
%23 = call i64 @sendmsg(i32 1, %struct.msghdr* %3, i32 0)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %2
%26 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
br label %27
27: ; preds = %25, %2
ret i32 0
}