#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 {
  unsigned char *msg_control;
  int msg_controllen;
  int msg_iovlen;
  struct iovec *msg_iov;
};
struct iovec {
  int *iov_base;
  int iov_len;
};
struct cmsghdr {
  int /*<<< orphan*/ cmsg_len;
  int /*<<< orphan*/ cmsg_type;
  int /*<<< orphan*/ cmsg_level;
};
typedef int /*<<< orphan*/ msgbuf;
typedef int /*<<< orphan*/ msg;

/* Variables and functions */
scalar_t__ CMSG_DATA(struct cmsghdr *);
struct cmsghdr *CMSG_FIRSTHDR(struct msghdr *);
int /*<<< orphan*/ CMSG_LEN(size_t);
int CMSG_SPACE(size_t);
int /*<<< orphan*/ SCM_RIGHTS;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ memcpy(int *, int const *, size_t);
int /*<<< orphan*/ memset(struct msghdr *, int /*<<< orphan*/, int);
int sendmsg(int, struct msghdr *, int /*<<< orphan*/);
int sock_op_put_fds(int sock_fd, int fds_num, const int *fds) {
  struct msghdr msg;
  unsigned char msgbuf[512];
  size_t arrsize;
  struct cmsghdr *pmsg;
  struct iovec send_vec[1];
  int *fd2;
  int val = 0;
  memset(&msg, 0, sizeof(msg));
  msg.msg_control = msgbuf;
  msg.msg_controllen = sizeof(msgbuf);
  arrsize = sizeof(int) * fds_num;
  pmsg = CMSG_FIRSTHDR(&msg);
  fd2 = (int *)CMSG_DATA(pmsg);
  memcpy(fd2, fds, arrsize);
  pmsg->cmsg_level = SOL_SOCKET;
  pmsg->cmsg_type = SCM_RIGHTS;
  pmsg->cmsg_len = CMSG_LEN(arrsize);
  msg.msg_controllen = CMSG_SPACE(arrsize);
  send_vec[0].iov_base = &val;
  send_vec[0].iov_len = 4;
  msg.msg_iov = send_vec;
  msg.msg_iovlen = 1;
  if (sendmsg(sock_fd, &msg, 0) != 4)
    return -1;
  return 0;
}


x86 assembly (clang, -O0):
<sock_op_put_fds>:
push   %rbp
mov    %rsp,%rbp
sub    $0x260,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x25c(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1050 <memset@plt>
lea    -0x230(%rbp),%rax
mov    %rax,-0x30(%rbp)
movl   $0x200,-0x28(%rbp)
movslq -0xc(%rbp),%rax
shl    $0x2,%rax
mov    %rax,-0x238(%rbp)
lea    -0x30(%rbp),%rdi
call   1090 <CMSG_FIRSTHDR@plt>
mov    %rax,-0x240(%rbp)
mov    -0x240(%rbp),%rdi
call   1040 <CMSG_DATA@plt>
mov    %rax,-0x258(%rbp)
mov    -0x258(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x238(%rbp),%rdx
call   1080 <memcpy@plt>
mov    0x2de7(%rip),%rax
mov    (%rax),%ecx
mov    -0x240(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    0x2dc4(%rip),%rax
mov    (%rax),%ecx
mov    -0x240(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x238(%rbp),%rdi
call   1060 <CMSG_LEN@plt>
mov    %eax,%ecx
mov    -0x240(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x238(%rbp),%rdi
call   1070 <CMSG_SPACE@plt>
mov    %eax,-0x28(%rbp)
lea    -0x25c(%rbp),%rax
mov    %rax,-0x250(%rbp)
movl   $0x4,-0x248(%rbp)
lea    -0x250(%rbp),%rax
mov    %rax,-0x20(%rbp)
movl   $0x1,-0x24(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
xor    %edx,%edx
call   1030 <sendmsg@plt>
cmp    $0x4,%eax
je     1293 <sock_op_put_fds+0x123>
movl   $0xffffffff,-0x4(%rbp)
jmp    129a <sock_op_put_fds+0x12a>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x260,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sock_op_put_fds>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x230,%rsp
mov    %rdx,%r12
mov    %esi,%ebp
mov    %edi,%r14d
movl   $0x0,0x4(%rsp)
lea    0x8(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1050 <memset@plt>
lea    0x30(%rsp),%rax
mov    %rax,0x8(%rsp)
movl   $0x200,0x10(%rsp)
movslq %ebp,%rbp
shl    $0x2,%rbp
mov    %r15,%rdi
call   1090 <CMSG_FIRSTHDR@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1040 <CMSG_DATA@plt>
mov    %rax,%rdi
mov    %r12,%rsi
mov    %rbp,%rdx
call   1080 <memcpy@plt>
mov    0x2e04(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rbx)
mov    0x2de8(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rbx)
mov    %rbp,%rdi
call   1060 <CMSG_LEN@plt>
mov    %eax,(%rbx)
mov    %rbp,%rdi
call   1070 <CMSG_SPACE@plt>
mov    %eax,0x10(%rsp)
lea    0x4(%rsp),%rax
mov    %rax,0x20(%rsp)
movl   $0x4,0x28(%rsp)
lea    0x20(%rsp),%rax
mov    %rax,0x18(%rsp)
movl   $0x1,0x14(%rsp)
mov    %r14d,%edi
mov    %r15,%rsi
xor    %edx,%edx
call   1030 <sendmsg@plt>
xor    %ecx,%ecx
cmp    $0x4,%eax
setne  %cl
neg    %ecx
mov    %ecx,%eax
add    $0x230,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.msghdr = type { i8*, i32, i32, %struct.iovec* }
%struct.iovec = type { i32*, i32 }
%struct.cmsghdr = type { i32, i32, i32 }
@SOL_SOCKET = global i32 0
@SCM_RIGHTS = global i32 0
define i32 @sock_op_put_fds(i32 %0, i32 %1, i32* %2) {
%4 = alloca %struct.msghdr
%5 = alloca [512 x i8]
%6 = alloca [1 x %struct.iovec]
%7 = alloca i32
%8 = bitcast %struct.msghdr* %4 to i8*
%9 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%10 = bitcast [1 x %struct.iovec]* %6 to i8*
%11 = bitcast i32* %7 to i8*
store i32 0, i32* %7
%12 = call i32 @memset(%struct.msghdr* %4, i32 0, i32 24)
%13 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %4, i64 0, i32 0
store i8* %9, i8** %13
%14 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %4, i64 0, i32 1
store i32 512, i32* %14
%15 = sext i32 %1 to i64
%16 = shl nsw i64 %15, 2
%17 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %4)
%18 = call i64 @CMSG_DATA(%struct.cmsghdr* %17)
%19 = inttoptr i64 %18 to i32*
%20 = call i32 @memcpy(i32* %19, i32* %2, i64 %16)
%21 = load i32, i32* @SOL_SOCKET
%22 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %17, i64 0, i32 2
store i32 %21, i32* %22
%23 = load i32, i32* @SCM_RIGHTS
%24 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %17, i64 0, i32 1
store i32 %23, i32* %24
%25 = call i32 @CMSG_LEN(i64 %16)
%26 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %17, i64 0, i32 0
store i32 %25, i32* %26
%27 = call i32 @CMSG_SPACE(i64 %16)
store i32 %27, i32* %14
%28 = getelementptr inbounds [1 x %struct.iovec], [1 x %struct.iovec]* %6, i64 0, i64 0
%29 = getelementptr inbounds [1 x %struct.iovec], [1 x %struct.iovec]* %6, i64 0, i64 0, i32 0
store i32* %7, i32** %29
%30 = getelementptr inbounds [1 x %struct.iovec], [1 x %struct.iovec]* %6, i64 0, i64 0, i32 1
store i32 4, i32* %30
%31 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %4, i64 0, i32 3
store %struct.iovec* %28, %struct.iovec** %31
%32 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %4, i64 0, i32 2
store i32 1, i32* %32
%33 = call i32 @sendmsg(i32 %0, %struct.msghdr* %4, i32 0)
%34 = icmp ne i32 %33, 4
%35 = sext i1 %34 to i32
ret i32 %35
}