#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;
  int /*<<< orphan*/ *msg_control;
};
struct cmsghdr {
  int cmsg_len;
};
typedef int /*<<< orphan*/ magic;
typedef int /*<<< orphan*/ cmh;
typedef int /*<<< orphan*/ *caddr_t;
typedef int /*<<< orphan*/ ancbuf;

/* Variables and functions */
struct cmsghdr *CMSG_FIRSTHDR(struct msghdr *);
void *CMSG_LEN(int /*<<< orphan*/);
struct cmsghdr *CMSG_NXTHDR(struct msghdr *, struct cmsghdr *);
int CMSG_SPACE(int /*<<< orphan*/);
int /*<<< orphan*/ bcopy(void *, void *, int);
int /*<<< orphan*/ checkptr(int /*<<< orphan*/ *, int /*<<< orphan*/ *, char *);
int /*<<< orphan*/ checkstr(char const *, char const *, int, char *);
int /*<<< orphan*/ init_hdrs(struct msghdr *, struct cmsghdr *, char *, int);
int /*<<< orphan*/ set_funcname(char *, int);
int /*<<< orphan*/ strlcpy(char *, char const *, int);
void test_cmsg_nexthdr() {
  struct msghdr mh;
  struct cmsghdr cmh;
  struct cmsghdr *cmhp, *cmhnextp;
  char ancbuf[10240];
  char magic[] = "MAGIC";
  set_funcname("test_cmsg_nexthdr", sizeof("test_cmsg_nexthdr"));
  init_hdrs(&mh, &cmh, ancbuf, sizeof(ancbuf));
  mh.msg_control = (caddr_t)ancbuf;
  mh.msg_controllen = CMSG_SPACE(0) * 2;
  cmh.cmsg_len = CMSG_LEN(0);
  bcopy((void *)&cmh, (void *)ancbuf, sizeof(cmh));
  strlcpy((char *)&cmh, (const char *)&magic, sizeof(magic));
  bcopy((void *)&cmh, (void *)((caddr_t)ancbuf + CMSG_SPACE(0)), sizeof(cmh));
  cmhp = CMSG_FIRSTHDR(&mh);
  cmhnextp = CMSG_NXTHDR(&mh, cmhp);
  checkstr((const char *)&magic, (const char *)cmhnextp, sizeof(magic),
           "more than one cmsghdr\0");
  init_hdrs(&mh, &cmh, ancbuf, sizeof(ancbuf));
  mh.msg_control = (caddr_t)ancbuf;
  mh.msg_controllen = CMSG_SPACE(0);
  cmh.cmsg_len = CMSG_LEN(0);
  bcopy((void *)&cmh, (void *)ancbuf, sizeof(cmh));
  cmhp = CMSG_FIRSTHDR(&mh);
  cmhnextp = CMSG_NXTHDR(&mh, cmhp);
  checkptr(NULL, (caddr_t)cmhnextp, "only one cmsghdr\0");
  init_hdrs(&mh, &cmh, ancbuf, sizeof(ancbuf));
  mh.msg_control = (caddr_t)ancbuf;
  mh.msg_controllen = sizeof(ancbuf);
  cmh.cmsg_len = sizeof(ancbuf);
  bcopy((void *)&cmh, (void *)ancbuf, sizeof(cmh));
  cmhp = CMSG_FIRSTHDR(&mh);
  cmhnextp = CMSG_NXTHDR(&mh, NULL);
  checkptr((caddr_t)cmhp, (caddr_t)cmhnextp, "null second argument\0");
}


x86 assembly (clang, -O0):
<test_cmsg_nexthdr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2850,%rsp
mov    0xe4f(%rip),%eax
mov    %eax,-0x2836(%rbp)
mov    0xe46(%rip),%ax
mov    %ax,-0x2832(%rbp)
lea    0xe3a(%rip),%rdi
mov    $0x12,%esi
call   1070 <set_funcname@plt>
lea    -0x2830(%rbp),%rdx
lea    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
mov    $0x2800,%ecx
call   10b0 <init_hdrs@plt>
lea    -0x2830(%rbp),%rax
mov    %rax,-0x8(%rbp)
xor    %edi,%edi
call   1060 <CMSG_SPACE@plt>
shl    $0x1,%eax
mov    %eax,-0x10(%rbp)
xor    %edi,%edi
call   1040 <CMSG_LEN@plt>
mov    %eax,-0x18(%rbp)
lea    -0x18(%rbp),%rdi
lea    -0x2830(%rbp),%rsi
mov    $0x4,%edx
call   10a0 <bcopy@plt>
lea    -0x18(%rbp),%rdi
lea    -0x2836(%rbp),%rsi
mov    $0x6,%edx
call   1090 <strlcpy@plt>
lea    -0x18(%rbp),%rax
mov    %rax,-0x2840(%rbp)
lea    -0x2830(%rbp),%rax
mov    %rax,-0x2848(%rbp)
xor    %edi,%edi
call   1060 <CMSG_SPACE@plt>
mov    -0x2848(%rbp),%rsi
mov    -0x2840(%rbp),%rdi
cltq
shl    $0x2,%rax
add    %rax,%rsi
mov    $0x4,%edx
call   10a0 <bcopy@plt>
lea    -0x10(%rbp),%rdi
call   10c0 <CMSG_FIRSTHDR@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rsi
lea    -0x10(%rbp),%rdi
call   1080 <CMSG_NXTHDR@plt>
mov    %rax,-0x28(%rbp)
lea    -0x2836(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    $0x6,%edx
lea    0xd67(%rip),%rcx
call   1050 <checkstr@plt>
lea    -0x2830(%rbp),%rdx
lea    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
mov    $0x2800,%ecx
call   10b0 <init_hdrs@plt>
lea    -0x2830(%rbp),%rax
mov    %rax,-0x8(%rbp)
xor    %edi,%edi
call   1060 <CMSG_SPACE@plt>
mov    %eax,-0x10(%rbp)
xor    %edi,%edi
call   1040 <CMSG_LEN@plt>
mov    %eax,-0x18(%rbp)
lea    -0x18(%rbp),%rdi
lea    -0x2830(%rbp),%rsi
mov    $0x4,%edx
call   10a0 <bcopy@plt>
lea    -0x10(%rbp),%rdi
call   10c0 <CMSG_FIRSTHDR@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rsi
lea    -0x10(%rbp),%rdi
call   1080 <CMSG_NXTHDR@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
lea    0xcff(%rip),%rdx
call   1030 <checkptr@plt>
lea    -0x2830(%rbp),%rdx
lea    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
mov    $0x2800,%ecx
call   10b0 <init_hdrs@plt>
lea    -0x2830(%rbp),%rax
mov    %rax,-0x8(%rbp)
movl   $0x2800,-0x10(%rbp)
movl   $0x2800,-0x18(%rbp)
lea    -0x18(%rbp),%rdi
lea    -0x2830(%rbp),%rsi
mov    $0x4,%edx
call   10a0 <bcopy@plt>
lea    -0x10(%rbp),%rdi
call   10c0 <CMSG_FIRSTHDR@plt>
mov    %rax,-0x20(%rbp)
lea    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1080 <CMSG_NXTHDR@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
lea    0xc98(%rip),%rdx
call   1030 <checkptr@plt>
add    $0x2850,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_cmsg_nexthdr>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x2820,%rsp
movw   $0x43,0xc(%rsp)
movl   $0x4947414d,0x8(%rsp)
49
lea    0xe50(%rip),%rdi
mov    $0x12,%esi
call   1070 <set_funcname@plt>
lea    0x10(%rsp),%r13
mov    %rsp,%r15
lea    0x20(%rsp),%r14
mov    %r13,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
mov    $0x2800,%ecx
call   10a0 <init_hdrs@plt>
mov    %r14,0x18(%rsp)
xor    %edi,%edi
call   1060 <CMSG_SPACE@plt>
add    %eax,%eax
mov    %eax,0x10(%rsp)
xor    %edi,%edi
call   1040 <CMSG_LEN@plt>
mov    %eax,(%rsp)
mov    %eax,0x20(%rsp)
lea    0x8(%rsp),%r12
mov    %r15,%rdi
mov    %r12,%rsi
mov    $0x6,%edx
call   1090 <strlcpy@plt>
xor    %edi,%edi
call   1060 <CMSG_SPACE@plt>
cltq
mov    (%rsp),%ecx
mov    %ecx,0x20(%rsp,%rax,4)
mov    %r13,%rdi
call   10b0 <CMSG_FIRSTHDR@plt>
mov    %r13,%rdi
mov    %rax,%rsi
call   1080 <CMSG_NXTHDR@plt>
lea    0xdd9(%rip),%rcx
mov    %r12,%rdi
mov    %rax,%rsi
mov    $0x6,%edx
call   1050 <checkstr@plt>
mov    %r13,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
mov    $0x2800,%ecx
call   10a0 <init_hdrs@plt>
mov    %r14,0x18(%rsp)
xor    %edi,%edi
call   1060 <CMSG_SPACE@plt>
mov    %eax,0x10(%rsp)
xor    %edi,%edi
call   1040 <CMSG_LEN@plt>
mov    %eax,(%rsp)
mov    %eax,0x20(%rsp)
mov    %r13,%rdi
call   10b0 <CMSG_FIRSTHDR@plt>
mov    %r13,%rdi
mov    %rax,%rsi
call   1080 <CMSG_NXTHDR@plt>
lea    0xd95(%rip),%rdx
xor    %edi,%edi
mov    %rax,%rsi
call   1030 <checkptr@plt>
mov    %r13,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
mov    $0x2800,%ecx
call   10a0 <init_hdrs@plt>
mov    %r14,0x18(%rsp)
movl   $0x2800,0x10(%rsp)
movl   $0x2800,(%rsp)
movl   $0x2800,0x20(%rsp)
mov    %r13,%rdi
call   10b0 <CMSG_FIRSTHDR@plt>
mov    %rax,%rbx
mov    %r13,%rdi
xor    %esi,%esi
call   1080 <CMSG_NXTHDR@plt>
lea    0xd52(%rip),%rdx
mov    %rbx,%rdi
mov    %rax,%rsi
call   1030 <checkptr@plt>
add    $0x2820,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.msghdr = type { i32, i32* }
%struct.cmsghdr = type { i32 }
@__const.test_cmsg_nexthdr.magic = [6 x i8] c"MAGIC\00"
@.str = [18 x i8] c"test_cmsg_nexthdr\00"
@.str.1 = [23 x i8] c"more than one cmsghdr\00\00"
@.str.2 = [18 x i8] c"only one cmsghdr\00\00"
@.str.3 = [22 x i8] c"null second argument\00\00"
define void @test_cmsg_nexthdr() {
%1 = alloca %struct.msghdr
%2 = alloca %struct.cmsghdr
%3 = alloca %struct.cmsghdr*
%4 = alloca %struct.cmsghdr*
%5 = alloca [10240 x i8]
%6 = alloca [6 x i8]
%7 = bitcast [6 x i8]* %6 to i8*
%8 = call i32 @set_funcname(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 18)
%9 = getelementptr inbounds [10240 x i8], [10240 x i8]* %5, i64 0, i64 0
%10 = call i32 @init_hdrs(%struct.msghdr* %1, %struct.cmsghdr* %2, i8* %9, i32 10240)
%11 = getelementptr inbounds [10240 x i8], [10240 x i8]* %5, i64 0, i64 0
%12 = bitcast i8* %11 to i32*
%13 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %1, i32 0, i32 1
store i32* %12, i32** %13
%14 = call i32 @CMSG_SPACE(i32 0)
%15 = mul nsw i32 %14, 2
%16 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %1, i32 0, i32 0
store i32 %15, i32* %16
%17 = call i8* @CMSG_LEN(i32 0)
%18 = ptrtoint i8* %17 to i32
%19 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %2, i32 0, i32 0
store i32 %18, i32* %19
%20 = bitcast %struct.cmsghdr* %2 to i8*
%21 = getelementptr inbounds [10240 x i8], [10240 x i8]* %5, i64 0, i64 0
%22 = call i32 @bcopy(i8* %20, i8* %21, i32 4)
%23 = bitcast %struct.cmsghdr* %2 to i8*
%24 = bitcast [6 x i8]* %6 to i8*
%25 = call i32 @strlcpy(i8* %23, i8* %24, i32 6)
%26 = bitcast %struct.cmsghdr* %2 to i8*
%27 = getelementptr inbounds [10240 x i8], [10240 x i8]* %5, i64 0, i64 0
%28 = bitcast i8* %27 to i32*
%29 = call i32 @CMSG_SPACE(i32 0)
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i32, i32* %28, i64 %30
%32 = bitcast i32* %31 to i8*
%33 = call i32 @bcopy(i8* %26, i8* %32, i32 4)
%34 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %1)
store %struct.cmsghdr* %34, %struct.cmsghdr** %3
%35 = load %struct.cmsghdr*, %struct.cmsghdr** %3
%36 = call %struct.cmsghdr* @CMSG_NXTHDR(%struct.msghdr* %1, %struct.cmsghdr* %35)
store %struct.cmsghdr* %36, %struct.cmsghdr** %4
%37 = bitcast [6 x i8]* %6 to i8*
%38 = load %struct.cmsghdr*, %struct.cmsghdr** %4
%39 = bitcast %struct.cmsghdr* %38 to i8*
%40 = call i32 @checkstr(i8* %37, i8* %39, i32 6, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
%41 = getelementptr inbounds [10240 x i8], [10240 x i8]* %5, i64 0, i64 0
%42 = call i32 @init_hdrs(%struct.msghdr* %1, %struct.cmsghdr* %2, i8* %41, i32 10240)
%43 = getelementptr inbounds [10240 x i8], [10240 x i8]* %5, i64 0, i64 0
%44 = bitcast i8* %43 to i32*
%45 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %1, i32 0, i32 1
store i32* %44, i32** %45
%46 = call i32 @CMSG_SPACE(i32 0)
%47 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %1, i32 0, i32 0
store i32 %46, i32* %47
%48 = call i8* @CMSG_LEN(i32 0)
%49 = ptrtoint i8* %48 to i32
%50 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %2, i32 0, i32 0
store i32 %49, i32* %50
%51 = bitcast %struct.cmsghdr* %2 to i8*
%52 = getelementptr inbounds [10240 x i8], [10240 x i8]* %5, i64 0, i64 0
%53 = call i32 @bcopy(i8* %51, i8* %52, i32 4)
%54 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %1)
store %struct.cmsghdr* %54, %struct.cmsghdr** %3
%55 = load %struct.cmsghdr*, %struct.cmsghdr** %3
%56 = call %struct.cmsghdr* @CMSG_NXTHDR(%struct.msghdr* %1, %struct.cmsghdr* %55)
store %struct.cmsghdr* %56, %struct.cmsghdr** %4
%57 = load %struct.cmsghdr*, %struct.cmsghdr** %4
%58 = bitcast %struct.cmsghdr* %57 to i32*
%59 = call i32 @checkptr(i32* null, i32* %58, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
%60 = getelementptr inbounds [10240 x i8], [10240 x i8]* %5, i64 0, i64 0
%61 = call i32 @init_hdrs(%struct.msghdr* %1, %struct.cmsghdr* %2, i8* %60, i32 10240)
%62 = getelementptr inbounds [10240 x i8], [10240 x i8]* %5, i64 0, i64 0
%63 = bitcast i8* %62 to i32*
%64 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %1, i32 0, i32 1
store i32* %63, i32** %64
%65 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %1, i32 0, i32 0
store i32 10240, i32* %65
%66 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %2, i32 0, i32 0
store i32 10240, i32* %66
%67 = bitcast %struct.cmsghdr* %2 to i8*
%68 = getelementptr inbounds [10240 x i8], [10240 x i8]* %5, i64 0, i64 0
%69 = call i32 @bcopy(i8* %67, i8* %68, i32 4)
%70 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %1)
store %struct.cmsghdr* %70, %struct.cmsghdr** %3
%71 = call %struct.cmsghdr* @CMSG_NXTHDR(%struct.msghdr* %1, %struct.cmsghdr* null)
store %struct.cmsghdr* %71, %struct.cmsghdr** %4
%72 = load %struct.cmsghdr*, %struct.cmsghdr** %3
%73 = bitcast %struct.cmsghdr* %72 to i32*
%74 = load %struct.cmsghdr*, %struct.cmsghdr** %4
%75 = bitcast %struct.cmsghdr* %74 to i32*
%76 = call i32 @checkptr(i32* %73, i32* %75, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
ret void
}

LLVM IR (-O2):
%struct.msghdr = type { i32, i32* }
%struct.cmsghdr = type { i32 }
@__const.test_cmsg_nexthdr.magic = [6 x i8] c"MAGIC\00"
@.str = [18 x i8] c"test_cmsg_nexthdr\00"
@.str.1 = [23 x i8] c"more than one cmsghdr\00\00"
@.str.2 = [18 x i8] c"only one cmsghdr\00\00"
@.str.3 = [22 x i8] c"null second argument\00\00"
define void @test_cmsg_nexthdr() {
%1 = alloca %struct.msghdr
%2 = alloca %struct.cmsghdr
%3 = alloca [10240 x i8]
%4 = alloca [6 x i8]
%5 = getelementptr inbounds [6 x i8], [6 x i8]* %4, i64 0, i64 0
%6 = bitcast %struct.msghdr* %1 to i8*
%7 = bitcast %struct.cmsghdr* %2 to i8*
%8 = getelementptr inbounds [10240 x i8], [10240 x i8]* %3, i64 0, i64 0
%9 = tail call i32 @set_funcname(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 18)
%10 = call i32 @init_hdrs(%struct.msghdr* %1, %struct.cmsghdr* %2, i8* %8, i32 10240)
%11 = bitcast [10240 x i8]* %3 to i32*
%12 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %1, i64 0, i32 1
%13 = bitcast i32** %12 to [10240 x i8]**
store [10240 x i8]* %3, [10240 x i8]** %13
%14 = call i32 @CMSG_SPACE(i32 0)
%15 = shl nsw i32 %14, 1
%16 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %1, i64 0, i32 0
store i32 %15, i32* %16
%17 = call i8* @CMSG_LEN(i32 0)
%18 = ptrtoint i8* %17 to i64
%19 = trunc i64 %18 to i32
%20 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %2, i64 0, i32 0
store i32 %19, i32* %20
store i32 %19, i32* %11
%21 = call i32 @strlcpy(i8* %7, i8* %5, i32 6)
%22 = call i32 @CMSG_SPACE(i32 0)
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i32, i32* %11, i64 %23
%25 = load i32, i32* %20
store i32 %25, i32* %24
%26 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %1)
%27 = call %struct.cmsghdr* @CMSG_NXTHDR(%struct.msghdr* %1, %struct.cmsghdr* %26)
%28 = bitcast %struct.cmsghdr* %27 to i8*
%29 = call i32 @checkstr(i8* %5, i8* %28, i32 6, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
%30 = call i32 @init_hdrs(%struct.msghdr* %1, %struct.cmsghdr* %2, i8* %8, i32 10240)
store [10240 x i8]* %3, [10240 x i8]** %13
%31 = call i32 @CMSG_SPACE(i32 0)
store i32 %31, i32* %16
%32 = call i8* @CMSG_LEN(i32 0)
%33 = ptrtoint i8* %32 to i64
%34 = trunc i64 %33 to i32
store i32 %34, i32* %20
store i32 %34, i32* %11
%35 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %1)
%36 = call %struct.cmsghdr* @CMSG_NXTHDR(%struct.msghdr* %1, %struct.cmsghdr* %35)
%37 = getelementptr %struct.cmsghdr, %struct.cmsghdr* %36, i64 0, i32 0
%38 = call i32 @checkptr(i32* null, i32* %37, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
%39 = call i32 @init_hdrs(%struct.msghdr* %1, %struct.cmsghdr* %2, i8* %8, i32 10240)
store [10240 x i8]* %3, [10240 x i8]** %13
store i32 10240, i32* %16
store i32 10240, i32* %20
store i32 10240, i32* %11
%40 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %1)
%41 = call %struct.cmsghdr* @CMSG_NXTHDR(%struct.msghdr* %1, %struct.cmsghdr* null)
%42 = getelementptr %struct.cmsghdr, %struct.cmsghdr* %40, i64 0, i32 0
%43 = getelementptr %struct.cmsghdr, %struct.cmsghdr* %41, i64 0, i32 0
%44 = call i32 @checkptr(i32* %42, i32* %43, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
ret void
}