#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 {
  char *msg_control;
  int msg_controllen;
  int msg_flags;
  struct iovec *msg_iov;
  int /*<<< orphan*/ member_0;
};
struct iovec {
  char *iov_base;
  int iov_len;
  int /*<<< orphan*/ member_0;
};
struct cfg {
  int dummy;
};
typedef int /*<<< orphan*/ payload;
typedef int /*<<< orphan*/ control;

/* Variables and functions */
int MSG_CTRUNC;
int /*<<< orphan*/ MSG_ERRQUEUE;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ error(int, int /*<<< orphan*/, char *);
int /*<<< orphan*/ parse_errqueue(struct msghdr *, struct cfg *, int);
int recvmsg(int, struct msghdr *, int /*<<< orphan*/);
void recv_errqueue(int fd, struct cfg *cfg) {
  static char payload[1000];
  static char control[1000];
  struct msghdr msg = {0};
  struct iovec iov = {0};
  int ret;
  iov.iov_base = payload;
  iov.iov_len = sizeof(payload);
  msg.msg_iov = &iov;
  msg.msg_control = control;
  msg.msg_controllen = sizeof(control);
  ret = recvmsg(fd, &msg, MSG_ERRQUEUE);
  if (ret == -1)
    error(1, errno, "recv err");
  if (msg.msg_flags & MSG_CTRUNC)
    error(1, errno, "recv err: control truncated");
  parse_errqueue(&msg, cfg, ret);
}


x86 assembly (clang, -O0):
<recv_errqueue>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1030 <memset@plt>
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1030 <memset@plt>
lea    0x2eda(%rip),%rax
mov    %rax,-0x40(%rbp)
movl   $0x3e8,-0x38(%rbp)
lea    -0x40(%rbp),%rax
mov    %rax,-0x20(%rbp)
lea    0x32b0(%rip),%rax
mov    %rax,-0x30(%rbp)
movl   $0x3e8,-0x28(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e3b(%rip),%rax
mov    (%rax),%edx
lea    -0x30(%rbp),%rsi
call   1040 <recvmsg@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0xffffffff,-0x44(%rbp)
jne    11d7 <recv_errqueue+0x97>
mov    0x2e0c(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edi
lea    0xe2e(%rip),%rdx
call   1050 <error@plt>
mov    -0x24(%rbp),%eax
mov    0x2df7(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1206 <recv_errqueue+0xc6>
mov    0x2ddd(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edi
lea    0xe08(%rip),%rdx
call   1050 <error@plt>
mov    -0x10(%rbp),%rsi
mov    -0x44(%rbp),%edx
lea    -0x30(%rbp),%rdi
call   1060 <parse_errqueue@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<recv_errqueue>:
push   %rbp
push   %rbx
sub    $0x38,%rsp
mov    %rsi,%rbx
xorps  %xmm0,%xmm0
movups %xmm0,0x20(%rsp)
movq   $0x0,0x30(%rsp)
movq   $0x0,0x10(%rsp)
lea    0x2ef6(%rip),%rax
mov    %rax,0x8(%rsp)
movl   $0x3e8,0x10(%rsp)
lea    0x8(%rsp),%rax
mov    %rax,0x28(%rsp)
lea    0x32c8(%rip),%rax
mov    %rax,0x18(%rsp)
movl   $0x3e8,0x20(%rsp)
mov    0x2e54(%rip),%rax
mov    (%rax),%edx
lea    0x18(%rsp),%rsi
call   1030 <recvmsg@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    11b9 <recv_errqueue+0x89>
mov    0x2e2a(%rip),%rax
mov    (%rax),%esi
lea    0xe51(%rip),%rdx
mov    $0x1,%edi
call   1040 <error@plt>
mov    0x2e18(%rip),%rax
mov    (%rax),%eax
test   %eax,0x24(%rsp)
je     11e2 <recv_errqueue+0xb2>
mov    0x2e01(%rip),%rax
mov    (%rax),%esi
lea    0xe31(%rip),%rdx
mov    $0x1,%edi
call   1040 <error@plt>
lea    0x18(%rsp),%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1050 <parse_errqueue@plt>
add    $0x38,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.cfg = type { i32 }
%struct.msghdr = type { i8*, i32, i32, %struct.iovec*, i32 }
%struct.iovec = type { i8*, i32, i32 }
@recv_errqueue.payload = internal global [1000 x i8] zeroinitializer
@recv_errqueue.control = internal global [1000 x i8] zeroinitializer
@MSG_ERRQUEUE = global i32 0
@errno = global i32 0
@.str = [9 x i8] c"recv err\00"
@MSG_CTRUNC = global i32 0
@.str.1 = [28 x i8] c"recv err: control truncated\00"
define void @recv_errqueue(i32 %0, %struct.cfg* %1) {
%3 = alloca i32
%4 = alloca %struct.cfg*
%5 = alloca %struct.msghdr
%6 = alloca %struct.iovec
%7 = alloca i32
store i32 %0, i32* %3
store %struct.cfg* %1, %struct.cfg** %4
%8 = bitcast %struct.msghdr* %5 to i8*
%9 = bitcast %struct.iovec* %6 to i8*
%10 = getelementptr inbounds %struct.iovec, %struct.iovec* %6, i32 0, i32 0
store i8* getelementptr inbounds ([1000 x i8], [1000 x i8]* @recv_errqueue.payload, i64 0, i64 0), i8** %10
%11 = getelementptr inbounds %struct.iovec, %struct.iovec* %6, i32 0, i32 1
store i32 1000, i32* %11
%12 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i32 0, i32 3
store %struct.iovec* %6, %struct.iovec** %12
%13 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i32 0, i32 0
store i8* getelementptr inbounds ([1000 x i8], [1000 x i8]* @recv_errqueue.control, i64 0, i64 0), i8** %13
%14 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i32 0, i32 1
store i32 1000, i32* %14
%15 = load i32, i32* %3
%16 = load i32, i32* @MSG_ERRQUEUE
%17 = call i32 @recvmsg(i32 %15, %struct.msghdr* %5, i32 %16)
store i32 %17, i32* %7
%18 = load i32, i32* %7
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %23
20: ; preds = %2
%21 = load i32, i32* @errno
%22 = call i32 @error(i32 1, i32 %21, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
br label %23
23: ; preds = %20, %2
%24 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i32 0, i32 2
%25 = load i32, i32* %24
%26 = load i32, i32* @MSG_CTRUNC
%27 = and i32 %25, %26
%28 = icmp ne i32 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %23
%30 = load i32, i32* @errno
%31 = call i32 @error(i32 1, i32 %30, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
br label %32
32: ; preds = %29, %23
%33 = load %struct.cfg*, %struct.cfg** %4
%34 = load i32, i32* %7
%35 = call i32 @parse_errqueue(%struct.msghdr* %5, %struct.cfg* %33, i32 %34)
ret void
}

LLVM IR (-O2):
%struct.cfg = type { i32 }
%struct.msghdr = type { i8*, i32, i32, %struct.iovec*, i32 }
%struct.iovec = type { i8*, i32, i32 }
@recv_errqueue.payload = internal global [1000 x i8] zeroinitializer
@recv_errqueue.control = internal global [1000 x i8] zeroinitializer
@MSG_ERRQUEUE = global i32 0
@errno = global i32 0
@.str = [9 x i8] c"recv err\00"
@MSG_CTRUNC = global i32 0
@.str.1 = [28 x i8] c"recv err: control truncated\00"
define void @recv_errqueue(i32 %0, %struct.cfg* %1) {
%3 = alloca %struct.msghdr
%4 = alloca %struct.iovec
%5 = bitcast %struct.msghdr* %3 to i8*
%6 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %3, i64 0, i32 1
%7 = bitcast i32* %6 to i8*
%8 = bitcast %struct.iovec* %4 to i8*
%9 = getelementptr inbounds %struct.iovec, %struct.iovec* %4, i64 0, i32 1
%10 = bitcast i32* %9 to i64*
store i64 0, i64* %10
%11 = getelementptr inbounds %struct.iovec, %struct.iovec* %4, i64 0, i32 0
store i8* getelementptr inbounds ([1000 x i8], [1000 x i8]* @recv_errqueue.payload, i64 0, i64 0), i8** %11
%12 = getelementptr inbounds %struct.iovec, %struct.iovec* %4, i64 0, i32 1
store i32 1000, i32* %12
%13 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %3, i64 0, i32 3
store %struct.iovec* %4, %struct.iovec** %13
%14 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %3, i64 0, i32 0
store i8* getelementptr inbounds ([1000 x i8], [1000 x i8]* @recv_errqueue.control, i64 0, i64 0), i8** %14
%15 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %3, i64 0, i32 1
store i32 1000, i32* %15
%16 = load i32, i32* @MSG_ERRQUEUE
%17 = call i32 @recvmsg(i32 %0, %struct.msghdr* %3, i32 %16)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %22
19: ; preds = %2
%20 = load i32, i32* @errno
%21 = call i32 @error(i32 1, i32 %20, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
br label %22
22: ; preds = %19, %2
%23 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %3, i64 0, i32 2
%24 = load i32, i32* %23
%25 = load i32, i32* @MSG_CTRUNC
%26 = and i32 %25, %24
%27 = icmp eq i32 %26, 0
br i1 %27, label %31, label %28
28: ; preds = %22
%29 = load i32, i32* @errno
%30 = call i32 @error(i32 1, i32 %29, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
br label %31
31: ; preds = %28, %22
%32 = call i32 @parse_errqueue(%struct.msghdr* %3, %struct.cfg* %1, i32 %17)
ret void
}