#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 {
  int cmsg_level;
  int cmsg_type;
};

/* Variables and functions */
scalar_t__ CMSG_DATA(struct cmsghdr *);
struct cmsghdr *CMSG_FIRSTHDR(struct msghdr *);
struct cmsghdr *CMSG_NXTHDR(struct msghdr *, struct cmsghdr *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ stderr;
void *get_ancillary_attr(struct msghdr *msg, int cmsg_lvl, int cmsg_type) {
  struct cmsghdr *cmsg;
  cmsg = CMSG_FIRSTHDR(msg);
  while (cmsg != NULL) {
    if ((cmsg->cmsg_level == cmsg_lvl) && (cmsg->cmsg_type == cmsg_type)) {
      return (void *)CMSG_DATA(cmsg);
    }
    cmsg = CMSG_NXTHDR(msg, cmsg);
  }
  fprintf(stderr, "ERROR: ATTRIBUTE NOT FOUND!");
  return NULL;
}


x86 assembly (clang, -O0):
<get_ancillary_attr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <CMSG_FIRSTHDR@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11b1 <get_ancillary_attr+0x71>
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
cmp    -0x14(%rbp),%eax
jne    119b <get_ancillary_attr+0x5b>
mov    -0x20(%rbp),%rax
mov    0x4(%rax),%eax
cmp    -0x18(%rbp),%eax
jne    119b <get_ancillary_attr+0x5b>
mov    -0x20(%rbp),%rdi
call   1030 <CMSG_DATA@plt>
mov    %rax,-0x8(%rbp)
jmp    11ce <get_ancillary_attr+0x8e>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1050 <CMSG_NXTHDR@plt>
mov    %rax,-0x20(%rbp)
jmp    115f <get_ancillary_attr+0x1f>
mov    0x2e28(%rip),%rax
mov    (%rax),%edi
lea    0xe3f(%rip),%rsi
call   1040 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_ancillary_attr>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%r14d
mov    %esi,%ebp
mov    %rdi,%rbx
call   1060 <CMSG_FIRSTHDR@plt>
jmp    116b <get_ancillary_attr+0x2b>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rbx,%rdi
mov    %rax,%rsi
call   1050 <CMSG_NXTHDR@plt>
test   %rax,%rax
je     1184 <get_ancillary_attr+0x44>
cmp    %ebp,(%rax)
jne    1160 <get_ancillary_attr+0x20>
cmp    %r14d,0x4(%rax)
jne    1160 <get_ancillary_attr+0x20>
mov    %rax,%rdi
call   1030 <CMSG_DATA@plt>
jmp    119b <get_ancillary_attr+0x5b>
mov    0x2e55(%rip),%rax
mov    (%rax),%edi
lea    0xe6c(%rip),%rsi
call   1040 <fprintf@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.msghdr = type { i32 }
%struct.cmsghdr = type { i32, i32 }
@stderr = global i32 0
@.str = [28 x i8] c"ERROR: ATTRIBUTE NOT FOUND!\00"
define i8* @get_ancillary_attr(%struct.msghdr* %0, i32 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca %struct.msghdr*
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.cmsghdr*
store %struct.msghdr* %0, %struct.msghdr** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%9 = load %struct.msghdr*, %struct.msghdr** %5
%10 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %9)
store %struct.cmsghdr* %10, %struct.cmsghdr** %8
br label %11
11: ; preds = %30, %3
%12 = load %struct.cmsghdr*, %struct.cmsghdr** %8
%13 = icmp ne %struct.cmsghdr* %12, null
br i1 %13, label %14, label %34
14: ; preds = %11
%15 = load %struct.cmsghdr*, %struct.cmsghdr** %8
%16 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %15, i32 0, i32 0
%17 = load i32, i32* %16
%18 = load i32, i32* %6
%19 = icmp eq i32 %17, %18
br i1 %19, label %20, label %30
20: ; preds = %14
%21 = load %struct.cmsghdr*, %struct.cmsghdr** %8
%22 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %21, i32 0, i32 1
%23 = load i32, i32* %22
%24 = load i32, i32* %7
%25 = icmp eq i32 %23, %24
br i1 %25, label %26, label %30
26: ; preds = %20
%27 = load %struct.cmsghdr*, %struct.cmsghdr** %8
%28 = call i64 @CMSG_DATA(%struct.cmsghdr* %27)
%29 = inttoptr i64 %28 to i8*
store i8* %29, i8** %4
br label %37
30: ; preds = %20, %14
%31 = load %struct.msghdr*, %struct.msghdr** %5
%32 = load %struct.cmsghdr*, %struct.cmsghdr** %8
%33 = call %struct.cmsghdr* @CMSG_NXTHDR(%struct.msghdr* %31, %struct.cmsghdr* %32)
store %struct.cmsghdr* %33, %struct.cmsghdr** %8
br label %11
34: ; preds = %11
%35 = load i32, i32* @stderr
%36 = call i32 @fprintf(i32 %35, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
store i8* null, i8** %4
br label %37
37: ; preds = %34, %26
%38 = load i8*, i8** %4
ret i8* %38
}

LLVM IR (-O2):
%struct.msghdr = type { i32 }
%struct.cmsghdr = type { i32, i32 }
@stderr = global i32 0
@.str = [28 x i8] c"ERROR: ATTRIBUTE NOT FOUND!\00"
define i8* @get_ancillary_attr(%struct.msghdr* %0, i32 %1, i32 %2) {
%4 = tail call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %0)
%5 = icmp eq %struct.cmsghdr* %4, null
br i1 %5, label %21, label %6
6: ; preds = %3, %18
%7 = phi %struct.cmsghdr* [ %19, %18 ], [ %4, %3 ]
%8 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %7, i64 0, i32 0
%9 = load i32, i32* %8
%10 = icmp eq i32 %9, %1
br i1 %10, label %11, label %18
11: ; preds = %6
%12 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %7, i64 0, i32 1
%13 = load i32, i32* %12
%14 = icmp eq i32 %13, %2
br i1 %14, label %15, label %18
15: ; preds = %11
%16 = tail call i64 @CMSG_DATA(%struct.cmsghdr* %7)
%17 = inttoptr i64 %16 to i8*
br label %24
18: ; preds = %11, %6
%19 = tail call %struct.cmsghdr* @CMSG_NXTHDR(%struct.msghdr* %0, %struct.cmsghdr* %7)
%20 = icmp eq %struct.cmsghdr* %19, null
br i1 %20, label %21, label %6
21: ; preds = %18, %3
%22 = load i32, i32* @stderr
%23 = tail call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
br label %24
24: ; preds = %21, %15
%25 = phi i8* [ %17, %15 ], [ null, %21 ]
ret i8* %25
}