#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_level;
  scalar_t__ cmsg_type;
  int cmsg_len;
};

/* Variables and functions */
scalar_t__ CMSG_DATA(struct cmsghdr *);
struct cmsghdr *CMSG_FIRSTHDR(struct msghdr *);
int CMSG_LEN(int /*<<< orphan*/);
struct cmsghdr *CMSG_NXTHDR(struct msghdr *, struct cmsghdr *);
scalar_t__ SCM_RIGHTS;
scalar_t__ SOL_SOCKET;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ stderr;
void read_ancillary_fds(struct msghdr *msg) {
  struct cmsghdr *cmsg;
  for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
    if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) {
      int i, fd, num_fd = (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int);
      for (i = 0; i < num_fd; i++) {
        fd = ((int *)CMSG_DATA(cmsg))[i];
        fprintf(stderr, "  received ancillary file descriptor %d\n", fd);
      }
    }
  }
}


x86 assembly (clang, -O0):
<read_ancillary_fds>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1070 <CMSG_FIRSTHDR@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1230 <read_ancillary_fds+0xe0>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    0x2e66(%rip),%rcx
cmp    (%rcx),%rax
jne    1215 <read_ancillary_fds+0xc5>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    0x2e36(%rip),%rcx
cmp    (%rcx),%rax
jne    1215 <read_ancillary_fds+0xc5>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%eax
mov    %eax,-0x20(%rbp)
xor    %edi,%edi
call   1040 <CMSG_LEN@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%eax
sub    %ecx,%eax
cltq
shr    $0x2,%rax
mov    %eax,-0x1c(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    1210 <read_ancillary_fds+0xc0>
mov    -0x10(%rbp),%rdi
call   1030 <CMSG_DATA@plt>
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x18(%rbp)
mov    0x2de7(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%edx
lea    0xe03(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11cb <read_ancillary_fds+0x7b>
jmp    1215 <read_ancillary_fds+0xc5>
jmp    121a <read_ancillary_fds+0xca>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1060 <CMSG_NXTHDR@plt>
mov    %rax,-0x10(%rbp)
jmp    1169 <read_ancillary_fds+0x19>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_ancillary_fds>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
call   1070 <CMSG_FIRSTHDR@plt>
test   %rax,%rax
je     11f2 <read_ancillary_fds+0xa2>
mov    %rax,%rbx
mov    0x2e62(%rip),%rbp
lea    0xe83(%rip),%r15
jmp    1193 <read_ancillary_fds+0x43>
nop
mov    %r14,%rdi
mov    %rbx,%rsi
call   1060 <CMSG_NXTHDR@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11f2 <read_ancillary_fds+0xa2>
mov    (%rbx),%rax
mov    0x2e4b(%rip),%rcx
cmp    (%rcx),%rax
jne    1180 <read_ancillary_fds+0x30>
mov    0x8(%rbx),%rax
mov    0x2e23(%rip),%rcx
cmp    (%rcx),%rax
jne    1180 <read_ancillary_fds+0x30>
mov    0x10(%rbx),%r13d
xor    %edi,%edi
call   1040 <CMSG_LEN@plt>
sub    %eax,%r13d
cmp    $0x4,%r13d
jl     1180 <read_ancillary_fds+0x30>
shr    $0x2,%r13d
xor    %r12d,%r12d
nopl   (%rax)
mov    %rbx,%rdi
call   1030 <CMSG_DATA@plt>
mov    (%rax,%r12,4),%edx
mov    0x0(%rbp),%edi
mov    %r15,%rsi
call   1050 <fprintf@plt>
add    $0x1,%r12
cmp    %r12,%r13
jne    11d0 <read_ancillary_fds+0x80>
jmp    1180 <read_ancillary_fds+0x30>
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 }
@SOL_SOCKET = global i64 0
@SCM_RIGHTS = global i64 0
@stderr = global i32 0
@.str = [41 x i8] c" received ancillary file descriptor %d\0A\00"
define void @read_ancillary_fds(%struct.msghdr* %0) {
%2 = alloca %struct.msghdr*
%3 = alloca %struct.cmsghdr*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store %struct.msghdr* %0, %struct.msghdr** %2
%7 = load %struct.msghdr*, %struct.msghdr** %2
%8 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %7)
store %struct.cmsghdr* %8, %struct.cmsghdr** %3
br label %9
9: ; preds = %53, %1
%10 = load %struct.cmsghdr*, %struct.cmsghdr** %3
%11 = icmp ne %struct.cmsghdr* %10, null
br i1 %11, label %12, label %57
12: ; preds = %9
%13 = load %struct.cmsghdr*, %struct.cmsghdr** %3
%14 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %13, i32 0, i32 0
%15 = load i64, i64* %14
%16 = load i64, i64* @SOL_SOCKET
%17 = icmp eq i64 %15, %16
br i1 %17, label %18, label %52
18: ; preds = %12
%19 = load %struct.cmsghdr*, %struct.cmsghdr** %3
%20 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %19, i32 0, i32 1
%21 = load i64, i64* %20
%22 = load i64, i64* @SCM_RIGHTS
%23 = icmp eq i64 %21, %22
br i1 %23, label %24, label %52
24: ; preds = %18
%25 = load %struct.cmsghdr*, %struct.cmsghdr** %3
%26 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %25, i32 0, i32 2
%27 = load i32, i32* %26
%28 = call i32 @CMSG_LEN(i32 0)
%29 = sub nsw i32 %27, %28
%30 = sext i32 %29 to i64
%31 = udiv i64 %30, 4
%32 = trunc i64 %31 to i32
store i32 %32, i32* %6
store i32 0, i32* %4
br label %33
33: ; preds = %48, %24
%34 = load i32, i32* %4
%35 = load i32, i32* %6
%36 = icmp slt i32 %34, %35
br i1 %36, label %37, label %51
37: ; preds = %33
%38 = load %struct.cmsghdr*, %struct.cmsghdr** %3
%39 = call i64 @CMSG_DATA(%struct.cmsghdr* %38)
%40 = inttoptr i64 %39 to i32*
%41 = load i32, i32* %4
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i32, i32* %40, i64 %42
%44 = load i32, i32* %43
store i32 %44, i32* %5
%45 = load i32, i32* @stderr
%46 = load i32, i32* %5
%47 = call i32 @fprintf(i32 %45, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i32 %46)
br label %48
48: ; preds = %37
%49 = load i32, i32* %4
%50 = add nsw i32 %49, 1
store i32 %50, i32* %4
br label %33
51: ; preds = %33
br label %52
52: ; preds = %51, %18, %12
br label %53
53: ; preds = %52
%54 = load %struct.msghdr*, %struct.msghdr** %2
%55 = load %struct.cmsghdr*, %struct.cmsghdr** %3
%56 = call %struct.cmsghdr* @CMSG_NXTHDR(%struct.msghdr* %54, %struct.cmsghdr* %55)
store %struct.cmsghdr* %56, %struct.cmsghdr** %3
br label %9
57: ; preds = %9
ret void
}

LLVM IR (-O2):
%struct.msghdr = type { i32 }
%struct.cmsghdr = type { i64, i64, i32 }
@SOL_SOCKET = global i64 0
@SCM_RIGHTS = global i64 0
@stderr = global i32 0
@.str = [41 x i8] c" received ancillary file descriptor %d\0A\00"
define void @read_ancillary_fds(%struct.msghdr* %0) {
%2 = tail call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %0)
%3 = icmp eq %struct.cmsghdr* %2, null
br i1 %3, label %37, label %4
4: ; preds = %1, %34
%5 = phi %struct.cmsghdr* [ %35, %34 ], [ %2, %1 ]
%6 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %5, i64 0, i32 0
%7 = load i64, i64* %6
%8 = load i64, i64* @SOL_SOCKET
%9 = icmp eq i64 %7, %8
br i1 %9, label %10, label %34
10: ; preds = %4
%11 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %5, i64 0, i32 1
%12 = load i64, i64* %11
%13 = load i64, i64* @SCM_RIGHTS
%14 = icmp eq i64 %12, %13
br i1 %14, label %15, label %34
15: ; preds = %10
%16 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %5, i64 0, i32 2
%17 = load i32, i32* %16
%18 = tail call i32 @CMSG_LEN(i32 0)
%19 = sub nsw i32 %17, %18
%20 = icmp sgt i32 %19, 3
br i1 %20, label %21, label %34
21: ; preds = %15
%22 = lshr i32 %19, 2
%23 = zext i32 %22 to i64
br label %24
24: ; preds = %21, %24
%25 = phi i64 [ 0, %21 ], [ %32, %24 ]
%26 = tail call i64 @CMSG_DATA(%struct.cmsghdr* %5)
%27 = inttoptr i64 %26 to i32*
%28 = getelementptr inbounds i32, i32* %27, i64 %25
%29 = load i32, i32* %28
%30 = load i32, i32* @stderr
%31 = tail call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i32 %29)
%32 = add nuw nsw i64 %25, 1
%33 = icmp eq i64 %32, %23
br i1 %33, label %34, label %24
34: ; preds = %24, %15, %4, %10
%35 = tail call %struct.cmsghdr* @CMSG_NXTHDR(%struct.msghdr* %0, %struct.cmsghdr* %5)
%36 = icmp eq %struct.cmsghdr* %35, null
br i1 %36, label %37, label %4
37: ; preds = %34, %1
ret void
}