#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 */
typedef void *uint32_t;
struct msghdr {
  int dummy;
};
struct cmsghdr {
  scalar_t__ cmsg_len;
  void *cmsg_type;
  void *cmsg_level;
};

/* Variables and functions */
scalar_t__ CMSG_DATA(struct cmsghdr *);
struct cmsghdr *CMSG_FIRSTHDR(struct msghdr *);
scalar_t__ CMSG_LEN(void *);
struct cmsghdr *CMSG_NXTHDR(struct msghdr *, struct cmsghdr *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ memcpy(void *, void *, void *);
int /*<<< orphan*/ stderr;
uint32_t set_ancillary_attr(struct msghdr *msg, uint32_t cmsg_lvl,
                            uint32_t cmsg_type, void *cmsg_data,
                            uint32_t cmsg_len) {
  struct cmsghdr *cmsg;
  cmsg = CMSG_FIRSTHDR(msg);
  void *value_p;
  if (cmsg == NULL)
    fprintf(stderr, "ERROR: No CMSG buffer allocated! ");
  while (cmsg->cmsg_len > 0) {
    cmsg = CMSG_NXTHDR(msg, cmsg);
    if (cmsg == NULL)
      fprintf(stderr, "ERROR: Out of CMSG buffer space!");
  }
  cmsg->cmsg_level = cmsg_lvl;
  cmsg->cmsg_type = cmsg_type;
  cmsg->cmsg_len = CMSG_LEN(cmsg_len);
  value_p = (void *)CMSG_DATA(cmsg);
  memcpy(value_p, cmsg_data, cmsg_len);
  return cmsg->cmsg_len;
}


x86 assembly (clang, -O0):
<set_ancillary_attr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
call   1080 <CMSG_FIRSTHDR@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11a9 <set_ancillary_attr+0x49>
mov    0x2e45(%rip),%rax
mov    (%rax),%edi
lea    0xe5c(%rip),%rsi
call   1050 <fprintf@plt>
jmp    11ae <set_ancillary_attr+0x4e>
mov    -0x30(%rbp),%rax
cmpq   $0x0,(%rax)
jle    11f2 <set_ancillary_attr+0x92>
mov    -0x8(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1070 <CMSG_NXTHDR@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11ed <set_ancillary_attr+0x8d>
mov    0x2e01(%rip),%rax
mov    (%rax),%edi
lea    0xe3a(%rip),%rsi
call   1050 <fprintf@plt>
jmp    11ae <set_ancillary_attr+0x4e>
mov    -0x10(%rbp),%rcx
mov    -0x30(%rbp),%rax
mov    %rcx,0x10(%rax)
mov    -0x18(%rbp),%rcx
mov    -0x30(%rbp),%rax
mov    %rcx,0x8(%rax)
mov    -0x28(%rbp),%rdi
call   1040 <CMSG_LEN@plt>
mov    %rax,%rcx
mov    -0x30(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x30(%rbp),%rdi
call   1030 <CMSG_DATA@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1060 <memcpy@plt>
mov    -0x30(%rbp),%rax
mov    (%rax),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_ancillary_attr>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r8,%r15
mov    %rcx,0x10(%rsp)
mov    %rdx,0x8(%rsp)
mov    %rsi,%r13
mov    %rdi,%rbp
call   1080 <CMSG_FIRSTHDR@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1196 <set_ancillary_attr+0x36>
cmpq   $0x0,(%rbx)
jg     11b1 <set_ancillary_attr+0x51>
jmp    11f7 <set_ancillary_attr+0x97>
mov    0x2e43(%rip),%rax
mov    (%rax),%edi
lea    0xe5a(%rip),%rsi
call   1050 <fprintf@plt>
cmpq   $0x0,(%rbx)
jle    11f7 <set_ancillary_attr+0x97>
mov    0x2e28(%rip),%r12
lea    0xe63(%rip),%r14
jmp    11d6 <set_ancillary_attr+0x76>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
cmpq   $0x0,(%rbx)
jle    11f7 <set_ancillary_attr+0x97>
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1070 <CMSG_NXTHDR@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11d0 <set_ancillary_attr+0x70>
mov    (%r12),%edi
mov    %r14,%rsi
call   1050 <fprintf@plt>
jmp    11d0 <set_ancillary_attr+0x70>
mov    %r13,0x10(%rbx)
mov    0x8(%rsp),%rax
mov    %rax,0x8(%rbx)
mov    %r15,%rdi
call   1040 <CMSG_LEN@plt>
mov    %rax,(%rbx)
mov    %rbx,%rdi
call   1030 <CMSG_DATA@plt>
mov    %rax,%rdi
mov    0x10(%rsp),%rsi
mov    %r15,%rdx
call   1060 <memcpy@plt>
mov    (%rbx),%rax
add    $0x18,%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, i8*, i8* }
@stderr = global i32 0
@.str = [34 x i8] c"ERROR: No CMSG buffer allocated! \00"
@.str.1 = [33 x i8] c"ERROR: Out of CMSG buffer space!\00"
define i8* @set_ancillary_attr(%struct.msghdr* %0, i8* %1, i8* %2, i8* %3, i8* %4) {
%6 = alloca %struct.msghdr*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca %struct.cmsghdr*
%12 = alloca i8*
store %struct.msghdr* %0, %struct.msghdr** %6
store i8* %1, i8** %7
store i8* %2, i8** %8
store i8* %3, i8** %9
store i8* %4, i8** %10
%13 = load %struct.msghdr*, %struct.msghdr** %6
%14 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %13)
store %struct.cmsghdr* %14, %struct.cmsghdr** %11
%15 = load %struct.cmsghdr*, %struct.cmsghdr** %11
%16 = icmp eq %struct.cmsghdr* %15, null
br i1 %16, label %17, label %20
17: ; preds = %5
%18 = load i32, i32* @stderr
%19 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
br label %20
20: ; preds = %17, %5
br label %21
21: ; preds = %35, %20
%22 = load %struct.cmsghdr*, %struct.cmsghdr** %11
%23 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %22, i32 0, i32 0
%24 = load i64, i64* %23
%25 = icmp sgt i64 %24, 0
br i1 %25, label %26, label %36
26: ; preds = %21
%27 = load %struct.msghdr*, %struct.msghdr** %6
%28 = load %struct.cmsghdr*, %struct.cmsghdr** %11
%29 = call %struct.cmsghdr* @CMSG_NXTHDR(%struct.msghdr* %27, %struct.cmsghdr* %28)
store %struct.cmsghdr* %29, %struct.cmsghdr** %11
%30 = load %struct.cmsghdr*, %struct.cmsghdr** %11
%31 = icmp eq %struct.cmsghdr* %30, null
br i1 %31, label %32, label %35
32: ; preds = %26
%33 = load i32, i32* @stderr
%34 = call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
br label %35
35: ; preds = %32, %26
br label %21
36: ; preds = %21
%37 = load i8*, i8** %7
%38 = load %struct.cmsghdr*, %struct.cmsghdr** %11
%39 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %38, i32 0, i32 2
store i8* %37, i8** %39
%40 = load i8*, i8** %8
%41 = load %struct.cmsghdr*, %struct.cmsghdr** %11
%42 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %41, i32 0, i32 1
store i8* %40, i8** %42
%43 = load i8*, i8** %10
%44 = call i64 @CMSG_LEN(i8* %43)
%45 = load %struct.cmsghdr*, %struct.cmsghdr** %11
%46 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %45, i32 0, i32 0
store i64 %44, i64* %46
%47 = load %struct.cmsghdr*, %struct.cmsghdr** %11
%48 = call i64 @CMSG_DATA(%struct.cmsghdr* %47)
%49 = inttoptr i64 %48 to i8*
store i8* %49, i8** %12
%50 = load i8*, i8** %12
%51 = load i8*, i8** %9
%52 = load i8*, i8** %10
%53 = call i32 @memcpy(i8* %50, i8* %51, i8* %52)
%54 = load %struct.cmsghdr*, %struct.cmsghdr** %11
%55 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %54, i32 0, i32 0
%56 = load i64, i64* %55
%57 = inttoptr i64 %56 to i8*
ret i8* %57
}

LLVM IR (-O2):
%struct.msghdr = type { i32 }
%struct.cmsghdr = type { i64, i8*, i8* }
@stderr = global i32 0
@.str = [34 x i8] c"ERROR: No CMSG buffer allocated! \00"
@.str.1 = [33 x i8] c"ERROR: Out of CMSG buffer space!\00"
define i8* @set_ancillary_attr(%struct.msghdr* %0, i8* %1, i8* %2, i8* %3, i8* %4) {
%6 = tail call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %0)
%7 = icmp eq %struct.cmsghdr* %6, null
br i1 %7, label %8, label %11
8: ; preds = %5
%9 = load i32, i32* @stderr
%10 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %8, %5
%12 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %6, i64 0, i32 0
%13 = load i64, i64* %12
%14 = icmp sgt i64 %13, 0
br i1 %14, label %15, label %26
15: ; preds = %11, %22
%16 = phi %struct.cmsghdr* [ %17, %22 ], [ %6, %11 ]
%17 = tail call %struct.cmsghdr* @CMSG_NXTHDR(%struct.msghdr* %0, %struct.cmsghdr* %16)
%18 = icmp eq %struct.cmsghdr* %17, null
br i1 %18, label %19, label %22
19: ; preds = %15
%20 = load i32, i32* @stderr
%21 = tail call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
br label %22
22: ; preds = %19, %15
%23 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %17, i64 0, i32 0
%24 = load i64, i64* %23
%25 = icmp sgt i64 %24, 0
br i1 %25, label %15, label %26
26: ; preds = %22, %11
%27 = phi %struct.cmsghdr* [ %6, %11 ], [ %17, %22 ]
%28 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %27, i64 0, i32 0
%29 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %27, i64 0, i32 2
store i8* %1, i8** %29
%30 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %27, i64 0, i32 1
store i8* %2, i8** %30
%31 = tail call i64 @CMSG_LEN(i8* %4)
store i64 %31, i64* %28
%32 = tail call i64 @CMSG_DATA(%struct.cmsghdr* %27)
%33 = inttoptr i64 %32 to i8*
%34 = tail call i32 @memcpy(i8* %33, i8* %3, i8* %4)
%35 = load i64, i64* %28
%36 = inttoptr i64 %35 to i8*
ret i8* %36
}