#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 *);
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);
  }
  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   1050 <CMSG_FIRSTHDR@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11a1 <get_ancillary_attr+0x71>
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
cmp    -0x14(%rbp),%eax
jne    118b <get_ancillary_attr+0x5b>
mov    -0x20(%rbp),%rax
mov    0x4(%rax),%eax
cmp    -0x18(%rbp),%eax
jne    118b <get_ancillary_attr+0x5b>
mov    -0x20(%rbp),%rdi
call   1030 <CMSG_DATA@plt>
mov    %rax,-0x8(%rbp)
jmp    11a9 <get_ancillary_attr+0x79>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1040 <CMSG_NXTHDR@plt>
mov    %rax,-0x20(%rbp)
jmp    114f <get_ancillary_attr+0x1f>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_ancillary_attr>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %esi,%ebx
mov    %rdi,%rbp
call   1050 <CMSG_FIRSTHDR@plt>
xor    %r14d,%r14d
jmp    115b <get_ancillary_attr+0x2b>
nopl   0x0(%rax)
mov    %rbp,%rdi
mov    %rax,%rsi
call   1040 <CMSG_NXTHDR@plt>
test   %rax,%rax
je     1175 <get_ancillary_attr+0x45>
cmp    %ebx,(%rax)
jne    1150 <get_ancillary_attr+0x20>
cmp    %r15d,0x4(%rax)
jne    1150 <get_ancillary_attr+0x20>
mov    %rax,%rdi
call   1030 <CMSG_DATA@plt>
mov    %rax,%r14
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.msghdr = type { i32 }
%struct.cmsghdr = type { i32, i32 }
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 %35
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
store i8* null, i8** %4
br label %35
35: ; preds = %34, %26
%36 = load i8*, i8** %4
ret i8* %36
}

LLVM IR (-O2):
%struct.msghdr = type { i32 }
%struct.cmsghdr = type { i32, i32 }
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 %21
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, %15
%22 = phi i8* [ %17, %15 ], [ null, %3 ], [ null, %18 ]
ret i8* %22
}