#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_iovlen;
  char **msg_control;
  int msg_controllen;
  struct iovec *msg_iov;
  int /*<<< orphan*/ *msg_name;
};
struct iovec {
  char **iov_base;
  int iov_len;
};
typedef int /*<<< orphan*/ packet;
typedef int /*<<< orphan*/ msg;
typedef int /*<<< orphan*/ cmsg_buf;

/* Variables and functions */
int ETH_FRAME_LEN;
int MSG_DONTWAIT;
int /*<<< orphan*/ memset(struct msghdr *, int /*<<< orphan*/, int);
long recvmsg(int, struct msghdr *, int);
void drain(int sock, int errorqueue) {
  struct msghdr msg;
  struct iovec iovec;
  char packet[ETH_FRAME_LEN];
  char cmsg_buf[(256)];
  long len;
  do {
    memset(&msg, 0, sizeof(msg));
    iovec.iov_base = &packet;
    iovec.iov_len = sizeof(packet);
    msg.msg_name = NULL;
    msg.msg_iov = &iovec;
    msg.msg_iovlen = 1;
    msg.msg_control = &cmsg_buf;
    msg.msg_controllen = sizeof(cmsg_buf);
    len = recvmsg(sock, &msg, MSG_DONTWAIT | errorqueue);
  } while (len > 0);
}


x86 assembly (clang, -O0):
<drain>:
push   %rbp
mov    %rsp,%rbp
sub    $0x170,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    0x2ea0(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x168(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x160(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x50(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1030 <memset@plt>
mov    -0x160(%rbp),%rcx
mov    -0x168(%rbp),%rax
mov    %rcx,-0x40(%rbp)
mov    %eax,-0x38(%rbp)
movq   $0x0,-0x10(%rbp)
lea    -0x40(%rbp),%rax
mov    %rax,-0x18(%rbp)
movl   $0x1,-0x30(%rbp)
lea    -0x150(%rbp),%rax
mov    %rax,-0x28(%rbp)
movl   $0x100,-0x20(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e21(%rip),%rax
mov    (%rax),%edx
or     -0x8(%rbp),%edx
lea    -0x30(%rbp),%rsi
call   1040 <recvmsg@plt>
mov    %rax,-0x158(%rbp)
cmpq   $0x0,-0x158(%rbp)
jg     1167 <drain+0x47>
mov    -0x48(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<drain>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x148,%rsp
mov    %esi,%r14d
mov    %edi,%r15d
mov    %rsp,-0x30(%rbp)
mov    0x2e93(%rip),%rax
mov    (%rax),%r13d
mov    %rsp,%r12
mov    %r13,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r12
mov    %r12,%rsp
lea    -0x68(%rbp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1030 <memset@plt>
mov    %r12,-0x40(%rbp)
mov    %r13d,-0x38(%rbp)
movq   $0x0,-0x48(%rbp)
lea    -0x40(%rbp),%rax
mov    %rax,-0x50(%rbp)
movl   $0x1,-0x68(%rbp)
lea    -0x170(%rbp),%rax
mov    %rax,-0x60(%rbp)
movl   $0x100,-0x58(%rbp)
mov    0x2e39(%rip),%rax
mov    (%rax),%edx
or     %r14d,%edx
mov    %r15d,%edi
mov    %rbx,%rsi
call   1040 <recvmsg@plt>
test   %rax,%rax
jg     1160 <drain+0x40>
mov    -0x30(%rbp),%rsp
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.msghdr = type { i32, i8**, i32, %struct.iovec*, i32* }
%struct.iovec = type { i8**, i32 }
@ETH_FRAME_LEN = global i32 0
@MSG_DONTWAIT = global i32 0
define void @drain(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.msghdr
%6 = alloca %struct.iovec
%7 = alloca i8*
%8 = alloca i64
%9 = alloca [256 x i8]
%10 = alloca i64
store i32 %0, i32* %3
store i32 %1, i32* %4
%11 = load i32, i32* @ETH_FRAME_LEN
%12 = zext i32 %11 to i64
%13 = call i8* @llvm.stacksave()
store i8* %13, i8** %7
%14 = alloca i8, i64 %12
store i64 %12, i64* %8
br label %15
15: ; preds = %32, %2
%16 = call i32 @memset(%struct.msghdr* %5, i32 0, i32 40)
%17 = bitcast i8* %14 to i8**
%18 = getelementptr inbounds %struct.iovec, %struct.iovec* %6, i32 0, i32 0
store i8** %17, i8*** %18
%19 = trunc i64 %12 to i32
%20 = getelementptr inbounds %struct.iovec, %struct.iovec* %6, i32 0, i32 1
store i32 %19, i32* %20
%21 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i32 0, i32 4
store i32* null, i32** %21
%22 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i32 0, i32 3
store %struct.iovec* %6, %struct.iovec** %22
%23 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i32 0, i32 0
store i32 1, i32* %23
%24 = bitcast [256 x i8]* %9 to i8**
%25 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i32 0, i32 1
store i8** %24, i8*** %25
%26 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i32 0, i32 2
store i32 256, i32* %26
%27 = load i32, i32* %3
%28 = load i32, i32* @MSG_DONTWAIT
%29 = load i32, i32* %4
%30 = or i32 %28, %29
%31 = call i64 @recvmsg(i32 %27, %struct.msghdr* %5, i32 %30)
store i64 %31, i64* %10
br label %32
32: ; preds = %15
%33 = load i64, i64* %10
%34 = icmp sgt i64 %33, 0
br i1 %34, label %15, label %35
35: ; preds = %32
%36 = load i8*, i8** %7
ret void
}

LLVM IR (-O2):
%struct.msghdr = type { i32, i8**, i32, %struct.iovec*, i32* }
%struct.iovec = type { i8**, i32 }
@ETH_FRAME_LEN = global i32 0
@MSG_DONTWAIT = global i32 0
define void @drain(i32 %0, i32 %1) {
%3 = alloca %struct.msghdr
%4 = alloca %struct.iovec
%5 = alloca [256 x i8]
%6 = bitcast %struct.msghdr* %3 to i8*
%7 = bitcast %struct.iovec* %4 to i8*
%8 = load i32, i32* @ETH_FRAME_LEN
%9 = zext i32 %8 to i64
%10 = tail call i8* @llvm.stacksave()
%11 = alloca i8, i64 %9
%12 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%13 = bitcast %struct.iovec* %4 to i8**
%14 = getelementptr inbounds %struct.iovec, %struct.iovec* %4, i64 0, i32 1
%15 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %3, i64 0, i32 4
%16 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %3, i64 0, i32 3
%17 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %3, i64 0, i32 0
%18 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %3, i64 0, i32 1
%19 = bitcast i8*** %18 to [256 x i8]**
%20 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %3, i64 0, i32 2
br label %21
21: ; preds = %21, %2
%22 = call i32 @memset(%struct.msghdr* %3, i32 0, i32 40)
store i8* %11, i8** %13
store i32 %8, i32* %14
store i32* null, i32** %15
store %struct.iovec* %4, %struct.iovec** %16
store i32 1, i32* %17
store [256 x i8]* %5, [256 x i8]** %19
store i32 256, i32* %20
%23 = load i32, i32* @MSG_DONTWAIT
%24 = or i32 %23, %1
%25 = call i64 @recvmsg(i32 %0, %struct.msghdr* %3, i32 %24)
%26 = icmp sgt i64 %25, 0
br i1 %26, label %21, label %27
27: ; preds = %21
ret void
}