#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 dummy;
};
typedef int /*<<< orphan*/ magic;
typedef int /*<<< orphan*/ cmh;
typedef void *caddr_t;
typedef int /*<<< orphan*/ ancbuf;

/* Variables and functions */
struct cmsghdr *CMSG_FIRSTHDR(struct msghdr *);
int /*<<< orphan*/ bcopy(void *, void *, int);
int /*<<< orphan*/ checkptr(int /*<<< orphan*/ *, void *, char *);
int /*<<< orphan*/ checkstr(char const *, char const *, int, char *);
int /*<<< orphan*/ init_hdrs(struct msghdr *, struct cmsghdr *, char *, int);
int /*<<< orphan*/ memset(void *, int /*<<< orphan*/, int);
int /*<<< orphan*/ set_funcname(char *, int);
int /*<<< orphan*/ strlcpy(char *, char const *, int);
void test_cmsg_firsthdr() {
  struct msghdr mh;
  struct cmsghdr cmh;
  struct cmsghdr *cmhp;
  char ancbuf[1024];
  char magic[] = "MAGIC";
  set_funcname("test_cmsg_firsthdr", sizeof("test_cmsg_firsthdr"));
  init_hdrs(&mh, NULL, NULL, 0);
  mh.msg_control = NULL;
  cmhp = CMSG_FIRSTHDR(&mh);
  checkptr(NULL, (caddr_t)cmhp, "msg_control is NULL\0");
  init_hdrs(&mh, NULL, NULL, 0);
  mh.msg_control = (caddr_t)&cmh;
  mh.msg_controllen = sizeof(cmh) - 1;
  cmhp = CMSG_FIRSTHDR(&mh);
  checkptr(NULL, (caddr_t)cmhp, "msg_controllen < sizeof cmsghdr\0");
  init_hdrs(&mh, NULL, NULL, 0);
  mh.msg_control = (caddr_t)&cmh;
  mh.msg_controllen = 0;
  cmhp = CMSG_FIRSTHDR(&mh);
  checkptr(NULL, (caddr_t)cmhp, "msg_controllen == 0\0");
  init_hdrs(&mh, &cmh, ancbuf, sizeof(ancbuf));
  memset((void *)ancbuf, 0, sizeof(ancbuf));
  mh.msg_control = (caddr_t)ancbuf;
  mh.msg_controllen = sizeof(ancbuf);
  strlcpy((char *)&cmh, (const char *)&magic, sizeof(magic));
  bcopy((void *)&cmh, (void *)ancbuf, sizeof(cmh));
  cmhp = CMSG_FIRSTHDR(&mh);
  checkstr((const char *)&magic, (const char *)cmhp, sizeof(magic),
           "with payload\0");
}


x86 assembly (clang, -O0):
<test_cmsg_firsthdr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    0xe6f(%rip),%eax
mov    %eax,-0x426(%rbp)
mov    0xe66(%rip),%ax
mov    %ax,-0x422(%rbp)
lea    0xe5a(%rip),%rdi
mov    $0x13,%esi
call   1060 <set_funcname@plt>
lea    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%edx
xor    %ecx,%ecx
mov    %rdx,%rsi
call   1090 <init_hdrs@plt>
movq   $0x0,-0x8(%rbp)
lea    -0x10(%rbp),%rdi
call   10a0 <CMSG_FIRSTHDR@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
lea    0xe2d(%rip),%rdx
call   1040 <checkptr@plt>
lea    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%edx
xor    %ecx,%ecx
mov    %rdx,%rsi
call   1090 <init_hdrs@plt>
lea    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
movl   $0x3,-0x10(%rbp)
lea    -0x10(%rbp),%rdi
call   10a0 <CMSG_FIRSTHDR@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
lea    0xe00(%rip),%rdx
call   1040 <checkptr@plt>
lea    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%edx
xor    %ecx,%ecx
mov    %rdx,%rsi
call   1090 <init_hdrs@plt>
lea    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
movl   $0x0,-0x10(%rbp)
lea    -0x10(%rbp),%rdi
call   10a0 <CMSG_FIRSTHDR@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
lea    0xddf(%rip),%rdx
call   1040 <checkptr@plt>
lea    -0x420(%rbp),%rdx
lea    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
mov    $0x400,%ecx
call   1090 <init_hdrs@plt>
lea    -0x420(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1030 <memset@plt>
lea    -0x420(%rbp),%rax
mov    %rax,-0x8(%rbp)
movl   $0x400,-0x10(%rbp)
lea    -0x18(%rbp),%rdi
lea    -0x426(%rbp),%rsi
mov    $0x6,%edx
call   1070 <strlcpy@plt>
lea    -0x18(%rbp),%rdi
lea    -0x420(%rbp),%rsi
mov    $0x4,%edx
call   1080 <bcopy@plt>
lea    -0x10(%rbp),%rdi
call   10a0 <CMSG_FIRSTHDR@plt>
mov    %rax,-0x20(%rbp)
lea    -0x426(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    $0x6,%edx
lea    0xd63(%rip),%rcx
call   1050 <checkstr@plt>
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_cmsg_firsthdr>:
push   %r15
push   %r14
push   %rbx
sub    $0x420,%rsp
movw   $0x43,0x1c(%rsp)
movl   $0x4947414d,0x18(%rsp)
49
lea    0xe74(%rip),%rdi
mov    $0x13,%esi
call   1060 <set_funcname@plt>
lea    0x8(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1080 <init_hdrs@plt>
movq   $0x0,0x10(%rsp)
mov    %r15,%rdi
call   1090 <CMSG_FIRSTHDR@plt>
lea    0xe52(%rip),%rdx
xor    %edi,%edi
mov    %rax,%rsi
call   1040 <checkptr@plt>
mov    %r15,%rdi
xor    %esi,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1080 <init_hdrs@plt>
mov    %rsp,%r14
mov    %r14,0x10(%rsp)
movl   $0x3,0x8(%rsp)
mov    %r15,%rdi
call   1090 <CMSG_FIRSTHDR@plt>
lea    0xe30(%rip),%rdx
xor    %edi,%edi
mov    %rax,%rsi
call   1040 <checkptr@plt>
mov    %r15,%rdi
xor    %esi,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1080 <init_hdrs@plt>
mov    %r14,0x10(%rsp)
movl   $0x0,0x8(%rsp)
mov    %r15,%rdi
call   1090 <CMSG_FIRSTHDR@plt>
lea    0xe1d(%rip),%rdx
xor    %edi,%edi
mov    %rax,%rsi
call   1040 <checkptr@plt>
lea    0x20(%rsp),%rbx
mov    %r15,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
mov    $0x400,%ecx
call   1080 <init_hdrs@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1030 <memset@plt>
mov    %rbx,0x10(%rsp)
movl   $0x400,0x8(%rsp)
lea    0x18(%rsp),%rbx
mov    %r14,%rdi
mov    %rbx,%rsi
mov    $0x6,%edx
call   1070 <strlcpy@plt>
mov    (%rsp),%eax
mov    %eax,0x20(%rsp)
mov    %r15,%rdi
call   1090 <CMSG_FIRSTHDR@plt>
lea    0xdc9(%rip),%rcx
mov    %rbx,%rdi
mov    %rax,%rsi
mov    $0x6,%edx
call   1050 <checkstr@plt>
add    $0x420,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.msghdr = type { i32, i32* }
%struct.cmsghdr = type { i32 }
@__const.test_cmsg_firsthdr.magic = [6 x i8] c"MAGIC\00"
@.str = [19 x i8] c"test_cmsg_firsthdr\00"
@.str.1 = [21 x i8] c"msg_control is NULL\00\00"
@.str.2 = [33 x i8] c"msg_controllen < sizeof cmsghdr\00\00"
@.str.3 = [21 x i8] c"msg_controllen == 0\00\00"
@.str.4 = [14 x i8] c"with payload\00\00"
define void @test_cmsg_firsthdr() {
%1 = alloca %struct.msghdr
%2 = alloca %struct.cmsghdr
%3 = alloca %struct.cmsghdr*
%4 = alloca [1024 x i8]
%5 = alloca [6 x i8]
%6 = bitcast [6 x i8]* %5 to i8*
%7 = call i32 @set_funcname(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 19)
%8 = call i32 @init_hdrs(%struct.msghdr* %1, %struct.cmsghdr* null, i8* null, i32 0)
%9 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %1, i32 0, i32 1
store i32* null, i32** %9
%10 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %1)
store %struct.cmsghdr* %10, %struct.cmsghdr** %3
%11 = load %struct.cmsghdr*, %struct.cmsghdr** %3
%12 = bitcast %struct.cmsghdr* %11 to i8*
%13 = call i32 @checkptr(i32* null, i8* %12, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%14 = call i32 @init_hdrs(%struct.msghdr* %1, %struct.cmsghdr* null, i8* null, i32 0)
%15 = bitcast %struct.cmsghdr* %2 to i8*
%16 = bitcast i8* %15 to i32*
%17 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %1, i32 0, i32 1
store i32* %16, i32** %17
%18 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %1, i32 0, i32 0
store i32 3, i32* %18
%19 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %1)
store %struct.cmsghdr* %19, %struct.cmsghdr** %3
%20 = load %struct.cmsghdr*, %struct.cmsghdr** %3
%21 = bitcast %struct.cmsghdr* %20 to i8*
%22 = call i32 @checkptr(i32* null, i8* %21, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0))
%23 = call i32 @init_hdrs(%struct.msghdr* %1, %struct.cmsghdr* null, i8* null, i32 0)
%24 = bitcast %struct.cmsghdr* %2 to i8*
%25 = bitcast i8* %24 to i32*
%26 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %1, i32 0, i32 1
store i32* %25, i32** %26
%27 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %1, i32 0, i32 0
store i32 0, i32* %27
%28 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %1)
store %struct.cmsghdr* %28, %struct.cmsghdr** %3
%29 = load %struct.cmsghdr*, %struct.cmsghdr** %3
%30 = bitcast %struct.cmsghdr* %29 to i8*
%31 = call i32 @checkptr(i32* null, i8* %30, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
%32 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%33 = call i32 @init_hdrs(%struct.msghdr* %1, %struct.cmsghdr* %2, i8* %32, i32 1024)
%34 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%35 = call i32 @memset(i8* %34, i32 0, i32 1024)
%36 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%37 = bitcast i8* %36 to i32*
%38 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %1, i32 0, i32 1
store i32* %37, i32** %38
%39 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %1, i32 0, i32 0
store i32 1024, i32* %39
%40 = bitcast %struct.cmsghdr* %2 to i8*
%41 = bitcast [6 x i8]* %5 to i8*
%42 = call i32 @strlcpy(i8* %40, i8* %41, i32 6)
%43 = bitcast %struct.cmsghdr* %2 to i8*
%44 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%45 = call i32 @bcopy(i8* %43, i8* %44, i32 4)
%46 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %1)
store %struct.cmsghdr* %46, %struct.cmsghdr** %3
%47 = bitcast [6 x i8]* %5 to i8*
%48 = load %struct.cmsghdr*, %struct.cmsghdr** %3
%49 = bitcast %struct.cmsghdr* %48 to i8*
%50 = call i32 @checkstr(i8* %47, i8* %49, i32 6, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
ret void
}

LLVM IR (-O2):
%struct.msghdr = type { i32, i32* }
%struct.cmsghdr = type { i32 }
@__const.test_cmsg_firsthdr.magic = [6 x i8] c"MAGIC\00"
@.str = [19 x i8] c"test_cmsg_firsthdr\00"
@.str.1 = [21 x i8] c"msg_control is NULL\00\00"
@.str.2 = [33 x i8] c"msg_controllen < sizeof cmsghdr\00\00"
@.str.3 = [21 x i8] c"msg_controllen == 0\00\00"
@.str.4 = [14 x i8] c"with payload\00\00"
define void @test_cmsg_firsthdr() {
%1 = alloca %struct.msghdr
%2 = alloca %struct.cmsghdr
%3 = alloca [1024 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 [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%9 = tail call i32 @set_funcname(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 19)
%10 = call i32 @init_hdrs(%struct.msghdr* %1, %struct.cmsghdr* null, i8* null, i32 0)
%11 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %1, i64 0, i32 1
store i32* null, i32** %11
%12 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %1)
%13 = bitcast %struct.cmsghdr* %12 to i8*
%14 = call i32 @checkptr(i32* null, i8* %13, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%15 = call i32 @init_hdrs(%struct.msghdr* %1, %struct.cmsghdr* null, i8* null, i32 0)
%16 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %2, i64 0, i32 0
store i32* %16, i32** %11
%17 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %1, i64 0, i32 0
store i32 3, i32* %17
%18 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %1)
%19 = bitcast %struct.cmsghdr* %18 to i8*
%20 = call i32 @checkptr(i32* null, i8* %19, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0))
%21 = call i32 @init_hdrs(%struct.msghdr* %1, %struct.cmsghdr* null, i8* null, i32 0)
store i32* %16, i32** %11
store i32 0, i32* %17
%22 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %1)
%23 = bitcast %struct.cmsghdr* %22 to i8*
%24 = call i32 @checkptr(i32* null, i8* %23, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
%25 = call i32 @init_hdrs(%struct.msghdr* %1, %struct.cmsghdr* %2, i8* %8, i32 1024)
%26 = call i32 @memset(i8* %8, i32 0, i32 1024)
%27 = bitcast i32** %11 to [1024 x i8]**
store [1024 x i8]* %3, [1024 x i8]** %27
store i32 1024, i32* %17
%28 = call i32 @strlcpy(i8* %7, i8* %5, i32 6)
%29 = bitcast [1024 x i8]* %3 to i32*
%30 = load i32, i32* %16
store i32 %30, i32* %29
%31 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %1)
%32 = bitcast %struct.cmsghdr* %31 to i8*
%33 = call i32 @checkstr(i8* %5, i8* %32, i32 6, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
ret void
}