#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 dummy;
};
struct cmsghdr {
  scalar_t__ cmsg_type;
  scalar_t__ cmsg_level;
  int cmsg_len;
};

/* Variables and functions */
scalar_t__ CMSG_DATA(struct cmsghdr *);
struct cmsghdr *CMSG_FIRSTHDR(struct msghdr *);
int CMSG_LEN(int /*<<< orphan*/);
int /*<<< orphan*/ LOG_ERR;
scalar_t__ SCM_RIGHTS;
scalar_t__ SOL_SOCKET;
int /*<<< orphan*/ errno;
scalar_t__ malloc(int);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, int, ...);
int sbus_extract_files(struct msghdr *p_msg, int n_files, int **pp_files) {
  int n_status = 0;
  struct cmsghdr *cmsg = CMSG_FIRSTHDR(p_msg);
  if (NULL == cmsg) {
    syslog(LOG_ERR, "sbus_extract_files: NULL cmsg. Error is %s",
           strerror(errno));
    n_status = -1;
  }
  if (0 != n_status || SCM_RIGHTS != cmsg->cmsg_type) {
    syslog(LOG_ERR, "sbus_extract_files: cmsg with wrong type. Type is %d",
           cmsg->cmsg_type);
    n_status = -1;
  }
  if (0 != n_status || SOL_SOCKET != cmsg->cmsg_level) {
    syslog(LOG_ERR, "sbus_extract_files: cmsg with wrong level. Level is %d",
           cmsg->cmsg_level);
    n_status = -1;
  }
  int n_actual_num = (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int);
  if (0 != n_status || n_actual_num != n_files) {
    syslog(LOG_ERR,
           "sbus_extract_files:  Incompatible number of descriptors"
           " in message. expected %d, found %d",
           n_files, n_actual_num);
    n_status = -1;
  }
  int i;
  *pp_files = (int *)malloc(n_files * sizeof(int));
  for (i = 0; i < n_files; ++i) {
    (*pp_files)[i] = ((int *)CMSG_DATA(cmsg))[i];
  }
  return n_status;
}


x86 assembly (clang, -O0):
<sbus_extract_files>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
call   1070 <CMSG_FIRSTHDR@plt>
mov    %rax,-0x28(%rbp)
xor    %eax,%eax
cmp    -0x28(%rbp),%rax
jne    11c7 <sbus_extract_files+0x67>
mov    0x2e26(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    0x2e32(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x34(%rbp),%edi
mov    %eax,%edx
lea    0xe47(%rip),%rsi
mov    $0x0,%al
call   1060 <syslog@plt>
movl   $0xffffffff,-0x1c(%rbp)
xor    %eax,%eax
cmp    -0x1c(%rbp),%eax
jne    11e9 <sbus_extract_files+0x89>
mov    0x2df7(%rip),%rax
mov    (%rax),%rax
mov    -0x28(%rbp),%rcx
cmp    (%rcx),%rax
je     1210 <sbus_extract_files+0xb0>
mov    0x2dd0(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
mov    %eax,%edx
lea    0xe29(%rip),%rsi
mov    $0x0,%al
call   1060 <syslog@plt>
movl   $0xffffffff,-0x1c(%rbp)
xor    %eax,%eax
cmp    -0x1c(%rbp),%eax
jne    1233 <sbus_extract_files+0xd3>
mov    0x2dc6(%rip),%rax
mov    (%rax),%rax
mov    -0x28(%rbp),%rcx
cmp    0x8(%rcx),%rax
je     125b <sbus_extract_files+0xfb>
mov    0x2d86(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%rax
mov    0x8(%rax),%rax
mov    %eax,%edx
lea    0xe13(%rip),%rsi
mov    $0x0,%al
call   1060 <syslog@plt>
movl   $0xffffffff,-0x1c(%rbp)
mov    -0x28(%rbp),%rax
mov    0x10(%rax),%eax
mov    %eax,-0x38(%rbp)
xor    %edi,%edi
call   1040 <CMSG_LEN@plt>
mov    %eax,%ecx
mov    -0x38(%rbp),%eax
sub    %ecx,%eax
cltq
shr    $0x2,%rax
mov    %eax,-0x2c(%rbp)
xor    %eax,%eax
cmp    -0x1c(%rbp),%eax
jne    1293 <sbus_extract_files+0x133>
mov    -0x2c(%rbp),%eax
cmp    -0xc(%rbp),%eax
je     12b7 <sbus_extract_files+0x157>
mov    0x2d26(%rip),%rax
mov    (%rax),%edi
mov    -0xc(%rbp),%edx
mov    -0x2c(%rbp),%ecx
lea    0xdee(%rip),%rsi
mov    $0x0,%al
call   1060 <syslog@plt>
movl   $0xffffffff,-0x1c(%rbp)
movslq -0xc(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    130f <sbus_extract_files+0x1af>
mov    -0x28(%rbp),%rdi
call   1030 <CMSG_DATA@plt>
movslq -0x30(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
movslq -0x30(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    12d7 <sbus_extract_files+0x177>
mov    -0x1c(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sbus_extract_files>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %esi,%ebx
call   1070 <CMSG_FIRSTHDR@plt>
mov    %rax,%r15
test   %rax,%rax
je     11a1 <sbus_extract_files+0x41>
mov    0x2e4c(%rip),%rax
mov    (%r15),%rdx
cmp    %rdx,(%rax)
jne    11ca <sbus_extract_files+0x6a>
mov    0x2e55(%rip),%rax
mov    0x8(%r15),%rdx
mov    $0x1,%r13b
cmp    %rdx,(%rax)
jne    11e5 <sbus_extract_files+0x85>
jmp    11ff <sbus_extract_files+0x9f>
mov    0x2e18(%rip),%rax
mov    (%rax),%ebp
mov    0x2e27(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe41(%rip),%rsi
mov    %ebp,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1060 <syslog@plt>
mov    0x2def(%rip),%rax
mov    (%rax),%edi
lea    0xe51(%rip),%rsi
xor    %eax,%eax
call   1060 <syslog@plt>
mov    0x8(%r15),%rdx
mov    0x2dd4(%rip),%rax
mov    (%rax),%edi
lea    0xe6b(%rip),%rsi
xor    %r13d,%r13d
xor    %eax,%eax
call   1060 <syslog@plt>
mov    0x10(%r15),%ebp
xor    %r12d,%r12d
xor    %edi,%edi
call   1040 <CMSG_LEN@plt>
sub    %eax,%ebp
sar    $0x2,%ebp
test   %r13b,%r13b
je     121b <sbus_extract_files+0xbb>
cmp    %ebx,%ebp
je     123c <sbus_extract_files+0xdc>
mov    0x2d9e(%rip),%rax
mov    (%rax),%edi
lea    0xe6c(%rip),%rsi
mov    %ebx,%edx
mov    %ebp,%ecx
xor    %eax,%eax
call   1060 <syslog@plt>
mov    $0xffffffff,%r12d
lea    0x0(,%rbx,4),%edi
call   1050 <malloc@plt>
mov    %rax,(%r14)
test   %ebx,%ebx
jle    127a <sbus_extract_files+0x11a>
mov    %ebx,%ebp
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r15,%rdi
call   1030 <CMSG_DATA@plt>
mov    (%rax,%rbx,4),%eax
mov    (%r14),%rcx
mov    %eax,(%rcx,%rbx,4)
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    1260 <sbus_extract_files+0x100>
mov    %r12d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.msghdr = type { i32 }
%struct.cmsghdr = type { i64, i64, i32 }
@LOG_ERR = global i32 0
@.str = [43 x i8] c"sbus_extract_files: NULL cmsg. Error is %s\00"
@errno = global i32 0
@SCM_RIGHTS = global i64 0
@.str.1 = [53 x i8] c"sbus_extract_files: cmsg with wrong type. Type is %d\00"
@SOL_SOCKET = global i64 0
@.str.2 = [55 x i8] c"sbus_extract_files: cmsg with wrong level. Level is %d\00"
@.str.3 = [90 x i8] c"sbus_extract_files: Incompatible number of descriptors in message. expected %d, found %d\00"
define i32 @sbus_extract_files(%struct.msghdr* %0, i32 %1, i32** %2) {
%4 = alloca %struct.msghdr*
%5 = alloca i32
%6 = alloca i32**
%7 = alloca i32
%8 = alloca %struct.cmsghdr*
%9 = alloca i32
%10 = alloca i32
store %struct.msghdr* %0, %struct.msghdr** %4
store i32 %1, i32* %5
store i32** %2, i32*** %6
store i32 0, i32* %7
%11 = load %struct.msghdr*, %struct.msghdr** %4
%12 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %11)
store %struct.cmsghdr* %12, %struct.cmsghdr** %8
%13 = load %struct.cmsghdr*, %struct.cmsghdr** %8
%14 = icmp eq %struct.cmsghdr* null, %13
br i1 %14, label %15, label %20
15: ; preds = %3
%16 = load i32, i32* @LOG_ERR
%17 = load i32, i32* @errno
%18 = call i32 @strerror(i32 %17)
%19 = call i32 (i32, i8*, i32, ...) @syslog(i32 %16, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0), i32 %18)
store i32 -1, i32* %7
br label %20
20: ; preds = %15, %3
%21 = load i32, i32* %7
%22 = icmp ne i32 0, %21
br i1 %22, label %29, label %23
23: ; preds = %20
%24 = load i64, i64* @SCM_RIGHTS
%25 = load %struct.cmsghdr*, %struct.cmsghdr** %8
%26 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %25, i32 0, i32 0
%27 = load i64, i64* %26
%28 = icmp ne i64 %24, %27
br i1 %28, label %29, label %36
29: ; preds = %23, %20
%30 = load i32, i32* @LOG_ERR
%31 = load %struct.cmsghdr*, %struct.cmsghdr** %8
%32 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %31, i32 0, i32 0
%33 = load i64, i64* %32
%34 = trunc i64 %33 to i32
%35 = call i32 (i32, i8*, i32, ...) @syslog(i32 %30, i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.1, i64 0, i64 0), i32 %34)
store i32 -1, i32* %7
br label %36
36: ; preds = %29, %23
%37 = load i32, i32* %7
%38 = icmp ne i32 0, %37
br i1 %38, label %45, label %39
39: ; preds = %36
%40 = load i64, i64* @SOL_SOCKET
%41 = load %struct.cmsghdr*, %struct.cmsghdr** %8
%42 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %41, i32 0, i32 1
%43 = load i64, i64* %42
%44 = icmp ne i64 %40, %43
br i1 %44, label %45, label %52
45: ; preds = %39, %36
%46 = load i32, i32* @LOG_ERR
%47 = load %struct.cmsghdr*, %struct.cmsghdr** %8
%48 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %47, i32 0, i32 1
%49 = load i64, i64* %48
%50 = trunc i64 %49 to i32
%51 = call i32 (i32, i8*, i32, ...) @syslog(i32 %46, i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.2, i64 0, i64 0), i32 %50)
store i32 -1, i32* %7
br label %52
52: ; preds = %45, %39
%53 = load %struct.cmsghdr*, %struct.cmsghdr** %8
%54 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %53, i32 0, i32 2
%55 = load i32, i32* %54
%56 = call i32 @CMSG_LEN(i32 0)
%57 = sub nsw i32 %55, %56
%58 = sext i32 %57 to i64
%59 = udiv i64 %58, 4
%60 = trunc i64 %59 to i32
store i32 %60, i32* %9
%61 = load i32, i32* %7
%62 = icmp ne i32 0, %61
br i1 %62, label %67, label %63
63: ; preds = %52
%64 = load i32, i32* %9
%65 = load i32, i32* %5
%66 = icmp ne i32 %64, %65
br i1 %66, label %67, label %72
67: ; preds = %63, %52
%68 = load i32, i32* @LOG_ERR
%69 = load i32, i32* %5
%70 = load i32, i32* %9
%71 = call i32 (i32, i8*, i32, ...) @syslog(i32 %68, i8* getelementptr inbounds ([90 x i8], [90 x i8]* @.str.3, i64 0, i64 0), i32 %69, i32 %70)
store i32 -1, i32* %7
br label %72
72: ; preds = %67, %63
%73 = load i32, i32* %5
%74 = sext i32 %73 to i64
%75 = mul i64 %74, 4
%76 = trunc i64 %75 to i32
%77 = call i64 @malloc(i32 %76)
%78 = inttoptr i64 %77 to i32*
%79 = load i32**, i32*** %6
store i32* %78, i32** %79
store i32 0, i32* %10
br label %80
80: ; preds = %97, %72
%81 = load i32, i32* %10
%82 = load i32, i32* %5
%83 = icmp slt i32 %81, %82
br i1 %83, label %84, label %100
84: ; preds = %80
%85 = load %struct.cmsghdr*, %struct.cmsghdr** %8
%86 = call i64 @CMSG_DATA(%struct.cmsghdr* %85)
%87 = inttoptr i64 %86 to i32*
%88 = load i32, i32* %10
%89 = sext i32 %88 to i64
%90 = getelementptr inbounds i32, i32* %87, i64 %89
%91 = load i32, i32* %90
%92 = load i32**, i32*** %6
%93 = load i32*, i32** %92
%94 = load i32, i32* %10
%95 = sext i32 %94 to i64
%96 = getelementptr inbounds i32, i32* %93, i64 %95
store i32 %91, i32* %96
br label %97
97: ; preds = %84
%98 = load i32, i32* %10
%99 = add nsw i32 %98, 1
store i32 %99, i32* %10
br label %80
100: ; preds = %80
%101 = load i32, i32* %7
ret i32 %101
}

LLVM IR (-O2):
%struct.msghdr = type { i32 }
%struct.cmsghdr = type { i64, i64, i32 }
@LOG_ERR = global i32 0
@.str = [43 x i8] c"sbus_extract_files: NULL cmsg. Error is %s\00"
@errno = global i32 0
@SCM_RIGHTS = global i64 0
@.str.1 = [53 x i8] c"sbus_extract_files: cmsg with wrong type. Type is %d\00"
@SOL_SOCKET = global i64 0
@.str.2 = [55 x i8] c"sbus_extract_files: cmsg with wrong level. Level is %d\00"
@.str.3 = [90 x i8] c"sbus_extract_files: Incompatible number of descriptors in message. expected %d, found %d\00"
define i32 @sbus_extract_files(%struct.msghdr* %0, i32 %1, i32** nocapture %2) {
%4 = tail call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %0)
%5 = icmp eq %struct.cmsghdr* %4, null
br i1 %5, label %6, label %11
6: ; preds = %3
%7 = load i32, i32* @LOG_ERR
%8 = load i32, i32* @errno
%9 = tail call i32 @strerror(i32 %8)
%10 = tail call i32 (i32, i8*, i32, ...) @syslog(i32 %7, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0), i32 %9)
br label %16
11: ; preds = %3
%12 = load i64, i64* @SCM_RIGHTS
%13 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %4, i64 0, i32 0
%14 = load i64, i64* %13
%15 = icmp eq i64 %12, %14
br i1 %15, label %23, label %16
16: ; preds = %6, %11
%17 = phi i64 [ undef, %6 ], [ %14, %11 ]
%18 = load i32, i32* @LOG_ERR
%19 = trunc i64 %17 to i32
%20 = tail call i32 (i32, i8*, i32, ...) @syslog(i32 %18, i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.1, i64 0, i64 0), i32 %19)
%21 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %4, i64 0, i32 1
%22 = load i64, i64* %21
br label %28
23: ; preds = %11
%24 = load i64, i64* @SOL_SOCKET
%25 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %4, i64 0, i32 1
%26 = load i64, i64* %25
%27 = icmp eq i64 %24, %26
br i1 %27, label %33, label %28
28: ; preds = %16, %23
%29 = phi i64 [ %22, %16 ], [ %26, %23 ]
%30 = load i32, i32* @LOG_ERR
%31 = trunc i64 %29 to i32
%32 = tail call i32 (i32, i8*, i32, ...) @syslog(i32 %30, i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.2, i64 0, i64 0), i32 %31)
br label %33
33: ; preds = %28, %23
%34 = phi i1 [ false, %28 ], [ true, %23 ]
%35 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %4, i64 0, i32 2
%36 = load i32, i32* %35
%37 = tail call i32 @CMSG_LEN(i32 0)
%38 = sub nsw i32 %36, %37
%39 = ashr i32 %38, 2
%40 = icmp eq i32 %39, %1
%41 = select i1 %34, i1 %40, i1 false
br i1 %41, label %45, label %42
42: ; preds = %33
%43 = load i32, i32* @LOG_ERR
%44 = tail call i32 (i32, i8*, i32, ...) @syslog(i32 %43, i8* getelementptr inbounds ([90 x i8], [90 x i8]* @.str.3, i64 0, i64 0), i32 %1, i32 %39)
br label %45
45: ; preds = %33, %42
%46 = phi i32 [ -1, %42 ], [ 0, %33 ]
%47 = shl i32 %1, 2
%48 = tail call i64 @malloc(i32 %47)
%49 = inttoptr i64 %48 to i32*
store i32* %49, i32** %2
%50 = icmp sgt i32 %1, 0
br i1 %50, label %51, label %63
51: ; preds = %45
%52 = zext i32 %1 to i64
br label %53
53: ; preds = %51, %53
%54 = phi i64 [ 0, %51 ], [ %61, %53 ]
%55 = tail call i64 @CMSG_DATA(%struct.cmsghdr* %4)
%56 = inttoptr i64 %55 to i32*
%57 = getelementptr inbounds i32, i32* %56, i64 %54
%58 = load i32, i32* %57
%59 = load i32*, i32** %2
%60 = getelementptr inbounds i32, i32* %59, i64 %54
store i32 %58, i32* %60
%61 = add nuw nsw i64 %54, 1
%62 = icmp eq i64 %61, %52
br i1 %62, label %63, label %53
63: ; preds = %53, %45
ret i32 %46
}