#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 msg_controllen;
  scalar_t__ msg_control;
};
struct cmsghdr {
  scalar_t__ cmsg_len;
};

/* Variables and functions */
int CMSG_ALIGN(scalar_t__);
struct cmsghdr *__cmsg_nxthdr(struct msghdr *mhdr, struct cmsghdr *cmsg) {
  if ((size_t)cmsg->cmsg_len < sizeof(struct cmsghdr))
    return NULL;
  cmsg = (struct cmsghdr *)((unsigned char *)cmsg + CMSG_ALIGN(cmsg->cmsg_len));
  if ((unsigned char *)(cmsg + 1) >
          ((unsigned char *)mhdr->msg_control + mhdr->msg_controllen) ||
      ((unsigned char *)cmsg + CMSG_ALIGN(cmsg->cmsg_len) >
       ((unsigned char *)mhdr->msg_control + mhdr->msg_controllen)))
    return NULL;
  return cmsg;
}


x86 assembly (clang, -O0):
<__cmsg_nxthdr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rax
cmpq   $0x8,(%rax)
jae    113b <__cmsg_nxthdr+0x2b>
movq   $0x0,-0x8(%rbp)
jmp    11d2 <__cmsg_nxthdr+0xc2>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
call   1030 <CMSG_ALIGN@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
add    $0x8,%rax
mov    -0x10(%rbp),%rcx
mov    0x8(%rcx),%rcx
mov    -0x10(%rbp),%rdx
movslq (%rdx),%rdx
add    %rdx,%rcx
cmp    %rcx,%rax
ja     11bd <__cmsg_nxthdr+0xad>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
call   1030 <CMSG_ALIGN@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    -0x10(%rbp),%rcx
mov    0x8(%rcx),%rcx
mov    -0x10(%rbp),%rdx
movslq (%rdx),%rdx
add    %rdx,%rcx
cmp    %rcx,%rax
jbe    11ca <__cmsg_nxthdr+0xba>
movq   $0x0,-0x8(%rbp)
jmp    11d2 <__cmsg_nxthdr+0xc2>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__cmsg_nxthdr>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    (%rsi),%rdi
cmp    $0x8,%rdi
jb     113e <__cmsg_nxthdr+0x2e>
mov    %rsi,%rbx
call   1030 <CMSG_ALIGN@plt>
cltq
lea    (%rbx,%rax,1),%rcx
add    $0x8,%rcx
movslq (%r14),%rdx
add    0x8(%r14),%rdx
cmp    %rdx,%rcx
jbe    1142 <__cmsg_nxthdr+0x32>
xor    %eax,%eax
jmp    1163 <__cmsg_nxthdr+0x53>
add    %rax,%rbx
mov    (%rbx),%rdi
call   1030 <CMSG_ALIGN@plt>
movslq %eax,%rcx
add    %rbx,%rcx
movslq (%r14),%rdx
add    0x8(%r14),%rdx
xor    %eax,%eax
cmp    %rdx,%rcx
cmovbe %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.msghdr = type { i32, i64 }
%struct.cmsghdr = type { i64 }
define %struct.cmsghdr* @__cmsg_nxthdr(%struct.msghdr* %0, %struct.cmsghdr* %1) {
%3 = alloca %struct.cmsghdr*
%4 = alloca %struct.msghdr*
%5 = alloca %struct.cmsghdr*
store %struct.msghdr* %0, %struct.msghdr** %4
store %struct.cmsghdr* %1, %struct.cmsghdr** %5
%6 = load %struct.cmsghdr*, %struct.cmsghdr** %5
%7 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %6, i32 0, i32 0
%8 = load i64, i64* %7
%9 = icmp ult i64 %8, 8
br i1 %9, label %10, label %11
10: ; preds = %2
store %struct.cmsghdr* null, %struct.cmsghdr** %3
br label %56
11: ; preds = %2
%12 = load %struct.cmsghdr*, %struct.cmsghdr** %5
%13 = bitcast %struct.cmsghdr* %12 to i8*
%14 = load %struct.cmsghdr*, %struct.cmsghdr** %5
%15 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %14, i32 0, i32 0
%16 = load i64, i64* %15
%17 = call i32 @CMSG_ALIGN(i64 %16)
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i8, i8* %13, i64 %18
%20 = bitcast i8* %19 to %struct.cmsghdr*
store %struct.cmsghdr* %20, %struct.cmsghdr** %5
%21 = load %struct.cmsghdr*, %struct.cmsghdr** %5
%22 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %21, i64 1
%23 = bitcast %struct.cmsghdr* %22 to i8*
%24 = load %struct.msghdr*, %struct.msghdr** %4
%25 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %24, i32 0, i32 1
%26 = load i64, i64* %25
%27 = inttoptr i64 %26 to i8*
%28 = load %struct.msghdr*, %struct.msghdr** %4
%29 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %28, i32 0, i32 0
%30 = load i32, i32* %29
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %27, i64 %31
%33 = icmp ugt i8* %23, %32
br i1 %33, label %53, label %34
34: ; preds = %11
%35 = load %struct.cmsghdr*, %struct.cmsghdr** %5
%36 = bitcast %struct.cmsghdr* %35 to i8*
%37 = load %struct.cmsghdr*, %struct.cmsghdr** %5
%38 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %37, i32 0, i32 0
%39 = load i64, i64* %38
%40 = call i32 @CMSG_ALIGN(i64 %39)
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i8, i8* %36, i64 %41
%43 = load %struct.msghdr*, %struct.msghdr** %4
%44 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %43, i32 0, i32 1
%45 = load i64, i64* %44
%46 = inttoptr i64 %45 to i8*
%47 = load %struct.msghdr*, %struct.msghdr** %4
%48 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %47, i32 0, i32 0
%49 = load i32, i32* %48
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds i8, i8* %46, i64 %50
%52 = icmp ugt i8* %42, %51
br i1 %52, label %53, label %54
53: ; preds = %34, %11
store %struct.cmsghdr* null, %struct.cmsghdr** %3
br label %56
54: ; preds = %34
%55 = load %struct.cmsghdr*, %struct.cmsghdr** %5
store %struct.cmsghdr* %55, %struct.cmsghdr** %3
br label %56
56: ; preds = %54, %53, %10
%57 = load %struct.cmsghdr*, %struct.cmsghdr** %3
ret %struct.cmsghdr* %57
}

LLVM IR (-O2):
%struct.msghdr = type { i32, i64 }
%struct.cmsghdr = type { i64 }
define %struct.cmsghdr* @__cmsg_nxthdr(%struct.msghdr* nocapture %0, %struct.cmsghdr* %1) {
%3 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %1, i64 0, i32 0
%4 = load i64, i64* %3
%5 = icmp ult i64 %4, 8
br i1 %5, label %34, label %6
6: ; preds = %2
%7 = bitcast %struct.cmsghdr* %1 to i8*
%8 = tail call i32 @CMSG_ALIGN(i64 %4)
%9 = sext i32 %8 to i64
%10 = getelementptr inbounds i8, i8* %7, i64 %9
%11 = getelementptr inbounds i8, i8* %10, i64 8
%12 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %0, i64 0, i32 1
%13 = load i64, i64* %12
%14 = inttoptr i64 %13 to i8*
%15 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %0, i64 0, i32 0
%16 = load i32, i32* %15
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds i8, i8* %14, i64 %17
%19 = icmp ugt i8* %11, %18
br i1 %19, label %34, label %20
20: ; preds = %6
%21 = bitcast i8* %10 to %struct.cmsghdr*
%22 = bitcast i8* %10 to i64*
%23 = load i64, i64* %22
%24 = tail call i32 @CMSG_ALIGN(i64 %23)
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %10, i64 %25
%27 = load i64, i64* %12
%28 = inttoptr i64 %27 to i8*
%29 = load i32, i32* %15
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %28, i64 %30
%32 = icmp ugt i8* %26, %31
%33 = select i1 %32, %struct.cmsghdr* null, %struct.cmsghdr* %21
br label %34
34: ; preds = %20, %6, %2
%35 = phi %struct.cmsghdr* [ null, %2 ], [ null, %6 ], [ %33, %20 ]
ret %struct.cmsghdr* %35
}