#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_flags;
  int /*<<< orphan*/ member_0;
};

/* Variables and functions */
scalar_t__ EAGAIN;
int MSG_CTRUNC;
int MSG_ERRQUEUE;
scalar_t__ errno;
int /*<<< orphan*/ error(int, scalar_t__, char *, ...);
int recvmsg(int, struct msghdr *, int);
void flush_zerocopy(int fd) {
  struct msghdr msg = {0};
  int ret;
  while (1) {
    ret = recvmsg(fd, &msg, MSG_ERRQUEUE);
    if (ret == -1 && errno == EAGAIN)
      break;
    if (ret == -1)
      error(1, errno, "errqueue");
    if (msg.msg_flags != (MSG_ERRQUEUE | MSG_CTRUNC))
      error(1, 0, "errqueue: flags 0x%x\n", msg.msg_flags);
    msg.msg_flags = 0;
  }
}


x86 assembly (clang, -O0):
<flush_zerocopy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1030 <memset@plt>
mov    -0x4(%rbp),%edi
mov    0x2e8b(%rip),%rax
mov    (%rax),%edx
lea    -0x10(%rbp),%rsi
call   1040 <recvmsg@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    118c <flush_zerocopy+0x5c>
mov    0x2e5c(%rip),%rax
mov    (%rax),%rax
mov    0x2e42(%rip),%rcx
cmp    (%rcx),%rax
jne    118c <flush_zerocopy+0x5c>
jmp    11f6 <flush_zerocopy+0xc6>
cmpl   $0xffffffff,-0x14(%rbp)
jne    11b3 <flush_zerocopy+0x83>
mov    0x2e33(%rip),%rax
mov    (%rax),%rsi
mov    $0x1,%edi
lea    0xe54(%rip),%rdx
mov    $0x0,%al
call   1050 <error@plt>
mov    -0x10(%rbp),%eax
mov    0x2e23(%rip),%rcx
mov    (%rcx),%ecx
mov    0x2e12(%rip),%rdx
or     (%rdx),%ecx
cmp    %ecx,%eax
je     11ea <flush_zerocopy+0xba>
mov    -0x10(%rbp),%ecx
mov    $0x1,%edi
xor    %eax,%eax
mov    %eax,%esi
lea    0xe26(%rip),%rdx
mov    $0x0,%al
call   1050 <error@plt>
movl   $0x0,-0x10(%rbp)
jmp    114b <flush_zerocopy+0x1b>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<flush_zerocopy>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edi,%ebx
movq   $0x0,(%rsp)
mov    0x2ea4(%rip),%r13
mov    0x0(%r13),%edx
mov    %rsp,%r14
mov    0x2e76(%rip),%rbp
mov    0x2e87(%rip),%r15
lea    0xeb1(%rip),%r12
jmp    1167 <flush_zerocopy+0x47>
nopw   0x0(%rax,%rax,1)
movl   $0x0,(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
call   1030 <recvmsg@plt>
cmp    $0xffffffff,%eax
jne    1199 <flush_zerocopy+0x79>
mov    0x2e53(%rip),%rax
mov    (%rax),%rsi
cmp    0x0(%rbp),%rsi
je     11c0 <flush_zerocopy+0xa0>
mov    $0x1,%edi
lea    0xe6e(%rip),%rdx
xor    %eax,%eax
call   1040 <error@plt>
mov    (%rsp),%ecx
mov    0x0(%r13),%edx
mov    (%r15),%eax
or     %edx,%eax
cmp    %eax,%ecx
je     1160 <flush_zerocopy+0x40>
mov    $0x1,%edi
xor    %esi,%esi
mov    %r12,%rdx
xor    %eax,%eax
call   1040 <error@plt>
mov    0x0(%r13),%edx
jmp    1160 <flush_zerocopy+0x40>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.msghdr = type { i32, i32 }
@MSG_ERRQUEUE = global i32 0
@errno = global i64 0
@EAGAIN = global i64 0
@.str = [9 x i8] c"errqueue\00"
@MSG_CTRUNC = global i32 0
@.str.1 = [22 x i8] c"errqueue: flags 0x%x\0A\00"
define void @flush_zerocopy(i32 %0) {
%2 = alloca i64
%3 = bitcast i64* %2 to %struct.msghdr*
%4 = bitcast i64* %2 to i8*
store i64 0, i64* %2
%5 = bitcast i64* %2 to i32*
%6 = load i32, i32* @MSG_ERRQUEUE
br label %7
7: ; preds = %26, %1
%8 = phi i32 [ %27, %26 ], [ %6, %1 ]
%9 = call i32 @recvmsg(i32 %0, %struct.msghdr* %3, i32 %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %17
11: ; preds = %7
%12 = load i64, i64* @errno
%13 = load i64, i64* @EAGAIN
%14 = icmp eq i64 %12, %13
br i1 %14, label %28, label %15
15: ; preds = %11
%16 = call i32 (i32, i64, i8*, ...) @error(i32 1, i64 %12, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
br label %17
17: ; preds = %7, %15
%18 = load i32, i32* %5
%19 = load i32, i32* @MSG_ERRQUEUE
%20 = load i32, i32* @MSG_CTRUNC
%21 = or i32 %20, %19
%22 = icmp eq i32 %18, %21
br i1 %22, label %26, label %23
23: ; preds = %17
%24 = call i32 (i32, i64, i8*, ...) @error(i32 1, i64 0, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 %18)
%25 = load i32, i32* @MSG_ERRQUEUE
br label %26
26: ; preds = %23, %17
%27 = phi i32 [ %25, %23 ], [ %19, %17 ]
store i32 0, i32* %5
br label %7
28: ; preds = %11
ret void
}