#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;
};
struct iovec {
  char *iov_base;
  int iov_len;
};
struct cmsghdr {
  int cmsg_len;
  int /*<<< orphan*/ cmsg_type;
  int /*<<< orphan*/ cmsg_level;
};
typedef scalar_t__ malloc;

/* Variables and functions */
int /*<<< orphan*/ CMSG_DATA(struct cmsghdr *);
struct cmsghdr *CMSG_FIRSTHDR(struct msghdr *);
int CMSG_LEN(int);
int /*<<< orphan*/ LOG_DEBUG;
int /*<<< orphan*/ SCM_RIGHTS;
int /*<<< orphan*/ SOL_SOCKET;
int dump_data_to_bytestream(char **, int, char const *, int, char const *, int);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/, void *, int);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, int);
int sbus_pack_message(struct msghdr *p_message, struct iovec *p_msg_iov,
                      const int *p_files, int n_files,
                      const char *str_files_metadata, int n_files_metadata_len,
                      const char *str_msg_data, int n_msg_len) {
  int n_status = 0;
  syslog(LOG_DEBUG, "sbus_pack_message: Got message with %d files", n_files);
  char *p_bytestream = NULL;
  int n_bytestream_len =
      dump_data_to_bytestream(&p_bytestream, n_files, str_files_metadata,
                              n_files_metadata_len, str_msg_data, n_msg_len);
  if (n_bytestream_len > 0) {
    int n_files_block_len = n_files * sizeof(int);
    int n_cbuf_size = CMSG_LEN(n_files_block_len);
    char *cmsg_buf = (char *)(malloc)(n_cbuf_size);
    p_msg_iov->iov_base = p_bytestream;
    p_msg_iov->iov_len = n_bytestream_len;
    p_message->msg_iov = p_msg_iov;
    p_message->msg_iovlen = 1;
    p_message->msg_control = cmsg_buf;
    p_message->msg_controllen = n_cbuf_size;
    struct cmsghdr *cmsg = CMSG_FIRSTHDR(p_message);
    cmsg->cmsg_level = SOL_SOCKET;
    cmsg->cmsg_type = SCM_RIGHTS;
    cmsg->cmsg_len = p_message->msg_controllen;
    memcpy(CMSG_DATA(cmsg), (void *)p_files, n_files * sizeof(int));
  } else
    n_status = -1;
  return n_status;
}


x86 assembly (clang, -O0):
<sbus_pack_message>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    0x18(%rbp),%eax
mov    0x10(%rbp),%rax
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8,-0x28(%rbp)
mov    %r9d,-0x2c(%rbp)
movl   $0x0,-0x30(%rbp)
mov    0x2e3c(%rip),%rax
mov    (%rax),%edi
mov    -0x1c(%rbp),%edx
lea    0xe60(%rip),%rsi
call   1060 <syslog@plt>
movq   $0x0,-0x38(%rbp)
mov    -0x1c(%rbp),%esi
mov    -0x28(%rbp),%rdx
mov    -0x2c(%rbp),%ecx
mov    0x10(%rbp),%r8
mov    0x18(%rbp),%r9d
lea    -0x38(%rbp),%rdi
call   1070 <dump_data_to_bytestream@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jle    1291 <sbus_pack_message+0x131>
movslq -0x1c(%rbp),%rax
shl    $0x2,%rax
mov    %eax,-0x40(%rbp)
mov    -0x40(%rbp),%edi
call   1040 <CMSG_LEN@plt>
mov    %eax,-0x44(%rbp)
movslq -0x44(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x38(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x3c(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    -0x10(%rbp),%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,0x18(%rax)
mov    -0x8(%rbp),%rax
movl   $0x1,(%rax)
mov    -0x50(%rbp),%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,0x8(%rax)
mov    -0x44(%rbp),%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,0x10(%rax)
mov    -0x8(%rbp),%rdi
call   1080 <CMSG_FIRSTHDR@plt>
mov    %rax,-0x58(%rbp)
mov    0x2da0(%rip),%rax
mov    (%rax),%ecx
mov    -0x58(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    0x2d80(%rip),%rax
mov    (%rax),%ecx
mov    -0x58(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x8(%rbp),%rax
mov    0x10(%rax),%ecx
mov    -0x58(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x58(%rbp),%rdi
call   1030 <CMSG_DATA@plt>
mov    %eax,%edi
mov    -0x18(%rbp),%rsi
movslq -0x1c(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edx
call   1050 <memcpy@plt>
jmp    1298 <sbus_pack_message+0x138>
movl   $0xffffffff,-0x30(%rbp)
mov    -0x30(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sbus_pack_message>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r9d,%r13d
mov    %r8,%rbp
mov    %ecx,%r12d
mov    %rdx,0x10(%rsp)
mov    %rsi,%r15
mov    %rdi,%rbx
mov    0x50(%rsp),%r14
mov    0x2e42(%rip),%rax
mov    (%rax),%edi
lea    0xe69(%rip),%rsi
mov    %ecx,%edx
call   1060 <syslog@plt>
movq   $0x0,0x8(%rsp)
lea    0x8(%rsp),%rdi
mov    %r12d,%esi
mov    %rbp,%rdx
mov    %r13d,%ecx
mov    %r14,%r8
mov    0x58(%rsp),%r9d
call   1070 <dump_data_to_bytestream@plt>
test   %eax,%eax
jle    1233 <sbus_pack_message+0xd3>
mov    %eax,%ebp
shl    $0x2,%r12d
mov    %r12d,%edi
call   1040 <CMSG_LEN@plt>
cltq
mov    0x8(%rsp),%rcx
mov    %rcx,(%r15)
mov    %ebp,0x8(%r15)
mov    %r15,0x18(%rbx)
movl   $0x1,(%rbx)
mov    %rax,0x8(%rbx)
mov    %eax,0x10(%rbx)
mov    %rbx,%rdi
call   1080 <CMSG_FIRSTHDR@plt>
mov    0x2de6(%rip),%rcx
mov    (%rcx),%ecx
mov    %ecx,0x8(%rax)
mov    0x2dca(%rip),%rcx
mov    (%rcx),%ecx
mov    %ecx,0x4(%rax)
mov    0x10(%rbx),%ecx
mov    %ecx,(%rax)
mov    %rax,%rdi
call   1030 <CMSG_DATA@plt>
mov    %eax,%edi
mov    0x10(%rsp),%rsi
mov    %r12d,%edx
call   1050 <memcpy@plt>
xor    %eax,%eax
jmp    1238 <sbus_pack_message+0xd8>
mov    $0xffffffff,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.msghdr = type { i32, i8*, i32, %struct.iovec* }
%struct.iovec = type { i8*, i32 }
%struct.cmsghdr = type { i32, i32, i32 }
@LOG_DEBUG = global i32 0
@.str = [45 x i8] c"sbus_pack_message: Got message with %d files\00"
@SOL_SOCKET = global i32 0
@SCM_RIGHTS = global i32 0
define i32 @sbus_pack_message(%struct.msghdr* %0, %struct.iovec* %1, i32* %2, i32 %3, i8* %4, i32 %5, i8* %6, i32 %7) {
%9 = alloca %struct.msghdr*
%10 = alloca %struct.iovec*
%11 = alloca i32*
%12 = alloca i32
%13 = alloca i8*
%14 = alloca i32
%15 = alloca i8*
%16 = alloca i32
%17 = alloca i32
%18 = alloca i8*
%19 = alloca i32
%20 = alloca i32
%21 = alloca i32
%22 = alloca i8*
%23 = alloca %struct.cmsghdr*
store %struct.msghdr* %0, %struct.msghdr** %9
store %struct.iovec* %1, %struct.iovec** %10
store i32* %2, i32** %11
store i32 %3, i32* %12
store i8* %4, i8** %13
store i32 %5, i32* %14
store i8* %6, i8** %15
store i32 %7, i32* %16
store i32 0, i32* %17
%24 = load i32, i32* @LOG_DEBUG
%25 = load i32, i32* %12
%26 = call i32 @syslog(i32 %24, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0), i32 %25)
store i8* null, i8** %18
%27 = load i32, i32* %12
%28 = load i8*, i8** %13
%29 = load i32, i32* %14
%30 = load i8*, i8** %15
%31 = load i32, i32* %16
%32 = call i32 @dump_data_to_bytestream(i8** %18, i32 %27, i8* %28, i32 %29, i8* %30, i32 %31)
store i32 %32, i32* %19
%33 = load i32, i32* %19
%34 = icmp sgt i32 %33, 0
br i1 %34, label %35, label %84
35: ; preds = %8
%36 = load i32, i32* %12
%37 = sext i32 %36 to i64
%38 = mul i64 %37, 4
%39 = trunc i64 %38 to i32
store i32 %39, i32* %20
%40 = load i32, i32* %20
%41 = call i32 @CMSG_LEN(i32 %40)
store i32 %41, i32* %21
%42 = load i32, i32* %21
%43 = sext i32 %42 to i64
%44 = inttoptr i64 %43 to i8*
store i8* %44, i8** %22
%45 = load i8*, i8** %18
%46 = load %struct.iovec*, %struct.iovec** %10
%47 = getelementptr inbounds %struct.iovec, %struct.iovec* %46, i32 0, i32 0
store i8* %45, i8** %47
%48 = load i32, i32* %19
%49 = load %struct.iovec*, %struct.iovec** %10
%50 = getelementptr inbounds %struct.iovec, %struct.iovec* %49, i32 0, i32 1
store i32 %48, i32* %50
%51 = load %struct.iovec*, %struct.iovec** %10
%52 = load %struct.msghdr*, %struct.msghdr** %9
%53 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %52, i32 0, i32 3
store %struct.iovec* %51, %struct.iovec** %53
%54 = load %struct.msghdr*, %struct.msghdr** %9
%55 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %54, i32 0, i32 0
store i32 1, i32* %55
%56 = load i8*, i8** %22
%57 = load %struct.msghdr*, %struct.msghdr** %9
%58 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %57, i32 0, i32 1
store i8* %56, i8** %58
%59 = load i32, i32* %21
%60 = load %struct.msghdr*, %struct.msghdr** %9
%61 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %60, i32 0, i32 2
store i32 %59, i32* %61
%62 = load %struct.msghdr*, %struct.msghdr** %9
%63 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %62)
store %struct.cmsghdr* %63, %struct.cmsghdr** %23
%64 = load i32, i32* @SOL_SOCKET
%65 = load %struct.cmsghdr*, %struct.cmsghdr** %23
%66 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %65, i32 0, i32 2
store i32 %64, i32* %66
%67 = load i32, i32* @SCM_RIGHTS
%68 = load %struct.cmsghdr*, %struct.cmsghdr** %23
%69 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %68, i32 0, i32 1
store i32 %67, i32* %69
%70 = load %struct.msghdr*, %struct.msghdr** %9
%71 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %70, i32 0, i32 2
%72 = load i32, i32* %71
%73 = load %struct.cmsghdr*, %struct.cmsghdr** %23
%74 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %73, i32 0, i32 0
store i32 %72, i32* %74
%75 = load %struct.cmsghdr*, %struct.cmsghdr** %23
%76 = call i32 @CMSG_DATA(%struct.cmsghdr* %75)
%77 = load i32*, i32** %11
%78 = bitcast i32* %77 to i8*
%79 = load i32, i32* %12
%80 = sext i32 %79 to i64
%81 = mul i64 %80, 4
%82 = trunc i64 %81 to i32
%83 = call i32 @memcpy(i32 %76, i8* %78, i32 %82)
br label %85
84: ; preds = %8
store i32 -1, i32* %17
br label %85
85: ; preds = %84, %35
%86 = load i32, i32* %17
ret i32 %86
}

LLVM IR (-O2):
%struct.msghdr = type { i32, i8*, i32, %struct.iovec* }
%struct.iovec = type { i8*, i32 }
%struct.cmsghdr = type { i32, i32, i32 }
@LOG_DEBUG = global i32 0
@.str = [45 x i8] c"sbus_pack_message: Got message with %d files\00"
@SOL_SOCKET = global i32 0
@SCM_RIGHTS = global i32 0
define i32 @sbus_pack_message(%struct.msghdr* %0, %struct.iovec* %1, i32* %2, i32 %3, i8* %4, i32 %5, i8* %6, i32 %7) {
%9 = alloca i8*
%10 = load i32, i32* @LOG_DEBUG
%11 = tail call i32 @syslog(i32 %10, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0), i32 %3)
%12 = bitcast i8** %9 to i8*
store i8* null, i8** %9
%13 = call i32 @dump_data_to_bytestream(i8** %9, i32 %3, i8* %4, i32 %5, i8* %6, i32 %7)
%14 = icmp sgt i32 %13, 0
br i1 %14, label %15, label %37
15: ; preds = %8
%16 = shl i32 %3, 2
%17 = call i32 @CMSG_LEN(i32 %16)
%18 = sext i32 %17 to i64
%19 = inttoptr i64 %18 to i8*
%20 = load i8*, i8** %9
%21 = getelementptr inbounds %struct.iovec, %struct.iovec* %1, i64 0, i32 0
store i8* %20, i8** %21
%22 = getelementptr inbounds %struct.iovec, %struct.iovec* %1, i64 0, i32 1
store i32 %13, i32* %22
%23 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %0, i64 0, i32 3
store %struct.iovec* %1, %struct.iovec** %23
%24 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %0, i64 0, i32 0
store i32 1, i32* %24
%25 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %0, i64 0, i32 1
store i8* %19, i8** %25
%26 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %0, i64 0, i32 2
store i32 %17, i32* %26
%27 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %0)
%28 = load i32, i32* @SOL_SOCKET
%29 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %27, i64 0, i32 2
store i32 %28, i32* %29
%30 = load i32, i32* @SCM_RIGHTS
%31 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %27, i64 0, i32 1
store i32 %30, i32* %31
%32 = load i32, i32* %26
%33 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %27, i64 0, i32 0
store i32 %32, i32* %33
%34 = call i32 @CMSG_DATA(%struct.cmsghdr* %27)
%35 = bitcast i32* %2 to i8*
%36 = call i32 @memcpy(i32 %34, i8* %35, i32 %16)
br label %37
37: ; preds = %8, %15
%38 = phi i32 [ 0, %15 ], [ -1, %8 ]
ret i32 %38
}