#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 {
  size_t msg_controllen;
  scalar_t__ msg_control;
};
struct cmsghdr {
  int /*<<< orphan*/ cmsg_len;
};

/* Variables and functions */
int CMSG_ALIGN(int /*<<< orphan*/);
struct cmsghdr *__attribute__((weak))
__cmsg_nxthdr(struct msghdr *msg, struct cmsghdr *cmsg) {
  struct cmsghdr *ptr;
  ptr =
      (struct cmsghdr *)(((unsigned char *)cmsg) + CMSG_ALIGN(cmsg->cmsg_len));
  size_t len = (unsigned long)((char *)(ptr + 1) - (char *)msg->msg_control);
  if (len > msg->msg_controllen) {
    return NULL;
  }
  return ptr;
}


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
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
call   1030 <CMSG_ALIGN@plt>
mov    %eax,%ecx
mov    -0x30(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
add    $0x4,%rax
mov    -0x10(%rbp),%rcx
mov    0x8(%rcx),%rcx
sub    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    -0x10(%rbp),%rcx
cmp    (%rcx),%rax
jbe    1178 <__cmsg_nxthdr+0x68>
movq   $0x0,-0x8(%rbp)
jmp    1180 <__cmsg_nxthdr+0x70>
mov    -0x20(%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    %rsi,%rbx
mov    %rdi,%r14
mov    (%rsi),%edi
call   1030 <CMSG_ALIGN@plt>
movslq %eax,%rcx
add    %rbx,%rcx
mov    %rcx,%rdx
sub    0x8(%r14),%rdx
add    $0x4,%rdx
xor    %eax,%eax
cmp    (%r14),%rdx
cmovbe %rcx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
%struct.msghdr = type { i64, i64 }
%struct.cmsghdr = type { i32 }
define weak %struct.cmsghdr* @__cmsg_nxthdr(%struct.msghdr* %0, %struct.cmsghdr* %1) {
%3 = bitcast %struct.cmsghdr* %1 to i8*
%4 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %1, i64 0, i32 0
%5 = load i32, i32* %4
%6 = tail call i32 @CMSG_ALIGN(i32 %5)
%7 = sext i32 %6 to i64
%8 = getelementptr inbounds i8, i8* %3, i64 %7
%9 = getelementptr inbounds i8, i8* %8, i64 4
%10 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %0, i64 0, i32 1
%11 = load i64, i64* %10
%12 = ptrtoint i8* %9 to i64
%13 = sub i64 %12, %11
%14 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %0, i64 0, i32 0
%15 = load i64, i64* %14
%16 = icmp ugt i64 %13, %15
%17 = bitcast i8* %8 to %struct.cmsghdr*
%18 = select i1 %16, %struct.cmsghdr* null, %struct.cmsghdr* %17
ret %struct.cmsghdr* %18
}