#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 {
  char *iov_base;
  int iov_len;
};
struct cmsghdr {
  int cmsg_len;
  int /*<<< orphan*/ cmsg_type;
  int /*<<< orphan*/ cmsg_level;
};
typedef int /*<<< orphan*/ fd;
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*/ err_syserr(char *);
int /*<<< orphan*/ memset(char *, char, int);
scalar_t__ sendmsg(int, struct msghdr *, int /*<<< orphan*/);
void wyslij(int socket, int fd) {
  struct msghdr msg = {0};
  char buf[CMSG_SPACE(sizeof(fd))];
  memset(buf, '\0', sizeof(buf));
  struct iovec io = {.iov_base = "ABC", .iov_len = 3};
  msg.msg_iov = &io;
  msg.msg_iovlen = 1;
  msg.msg_control = buf;
  msg.msg_controllen = sizeof(buf);
  struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msg);
  cmsg->cmsg_level = SOL_SOCKET;
  cmsg->cmsg_type = SCM_RIGHTS;
  cmsg->cmsg_len = CMSG_LEN(sizeof(fd));
  *((int *)CMSG_DATA(cmsg)) = fd;
  msg.msg_controllen = cmsg->cmsg_len;
  if (sendmsg(socket, &msg, 0) < 0)
    err_syserr("Failed to send message\n");
}


x86 assembly (clang, -O0):
<wyslij>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
xorps  %xmm0,%xmm0
movaps %xmm0,-0x20(%rbp)
movaps %xmm0,-0x30(%rbp)
movq   $0x0,-0x10(%rbp)
mov    $0x4,%edi
call   1080 <CMSG_SPACE@plt>
mov    %eax,%eax
mov    %rax,-0x68(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x70(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x40(%rbp)
mov    %eax,%edx
xor    %esi,%esi
call   1050 <memset@plt>
mov    -0x70(%rbp),%rcx
mov    -0x68(%rbp),%rax
mov    0x2c24(%rip),%rdx
mov    %rdx,-0x50(%rbp)
mov    0x2c21(%rip),%rdx
mov    %rdx,-0x48(%rbp)
lea    -0x50(%rbp),%rdx
mov    %rdx,-0x18(%rbp)
movl   $0x1,-0x30(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %eax,-0x20(%rbp)
lea    -0x30(%rbp),%rdi
call   1090 <CMSG_FIRSTHDR@plt>
mov    %rax,-0x58(%rbp)
mov    0x2dd3(%rip),%rax
mov    (%rax),%ecx
mov    -0x58(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    0x2db3(%rip),%rax
mov    (%rax),%ecx
mov    -0x58(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    $0x4,%edi
call   1070 <CMSG_LEN@plt>
mov    %eax,%ecx
mov    -0x58(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x8(%rbp),%eax
mov    %eax,-0x5c(%rbp)
mov    -0x58(%rbp),%rdi
call   1040 <CMSG_DATA@plt>
mov    -0x5c(%rbp),%ecx
mov    %ecx,(%rax)
mov    -0x58(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x30(%rbp),%rsi
xor    %edx,%edx
call   1030 <sendmsg@plt>
cmp    $0x0,%rax
jge    1281 <wyslij+0x111>
lea    0xd88(%rip),%rdi
call   1060 <err_syserr@plt>
mov    -0x38(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wyslij>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %esi,%r15d
mov    %edi,%r14d
xorps  %xmm0,%xmm0
movaps %xmm0,-0x40(%rbp)
movaps %xmm0,-0x50(%rbp)
movq   $0x0,-0x30(%rbp)
mov    $0x4,%edi
call   1080 <CMSG_SPACE@plt>
mov    %eax,%r12d
mov    %rsp,%r13
mov    %eax,%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %rbx,%rdi
xor    %esi,%esi
mov    %r12d,%edx
call   1050 <memset@plt>
movups 0x2c2f(%rip),%xmm0
movaps %xmm0,-0x60(%rbp)
lea    -0x60(%rbp),%rax
mov    %rax,-0x38(%rbp)
movl   $0x1,-0x50(%rbp)
mov    %rbx,-0x48(%rbp)
mov    %r12d,-0x40(%rbp)
lea    -0x50(%rbp),%r12
mov    %r12,%rdi
call   1090 <CMSG_FIRSTHDR@plt>
mov    %rax,%rbx
mov    0x2de6(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rbx)
mov    0x2dca(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rbx)
mov    $0x4,%edi
call   1070 <CMSG_LEN@plt>
mov    %eax,(%rbx)
mov    %rbx,%rdi
call   1040 <CMSG_DATA@plt>
mov    %r15d,(%rax)
mov    (%rbx),%eax
mov    %eax,-0x40(%rbp)
mov    %r14d,%edi
mov    %r12,%rsi
xor    %edx,%edx
call   1030 <sendmsg@plt>
test   %rax,%rax
jns    124d <wyslij+0xdd>
lea    0xdbc(%rip),%rdi
call   1060 <err_syserr@plt>
mov    %r13,%rsp
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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