#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 sockaddr_ll {
  int dummy;
};
struct msghdr {
  int msg_namelen;
  int msg_iovlen;
  struct iovec *msg_iov;
  struct sockaddr_ll *msg_name;
};
struct iovec {
  char *iov_base;
  int iov_len;
};
typedef int /*<<< orphan*/ sa;
typedef int /*<<< orphan*/ iov;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ MSG_DONTWAIT;
scalar_t__ recvmsg(int, struct msghdr *, int /*<<< orphan*/);
void drain_socket(int s) {
  char buf[4096];
  struct sockaddr_ll sa;
  struct iovec iov[] = {
      {
          .iov_base = buf,
          .iov_len = sizeof(buf),
      },
  };
  struct msghdr msg = {
      .msg_name = &sa,
      .msg_namelen = sizeof(sa),
      .msg_iov = iov,
      .msg_iovlen = (sizeof(iov) / sizeof((iov)[0])),
  };
  while (recvmsg(s, &msg, MSG_DONTWAIT) > 0) {
  }
}


x86 assembly (clang, -O0):
<drain_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1050,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x1010(%rbp),%rax
mov    %rax,-0x1030(%rbp)
movl   $0x1000,-0x1028(%rbp)
movl   $0x4,-0x1048(%rbp)
movl   $0x1,-0x1044(%rbp)
lea    -0x1030(%rbp),%rax
mov    %rax,-0x1040(%rbp)
lea    -0x1018(%rbp),%rax
mov    %rax,-0x1038(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e70(%rip),%rax
mov    (%rax),%edx
lea    -0x1048(%rbp),%rsi
call   1030 <recvmsg@plt>
cmp    $0x0,%rax
jle    118d <drain_socket+0x7d>
jmp    1166 <drain_socket+0x56>
add    $0x1050,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<drain_socket>:
push   %r15
push   %r14
push   %rbx
sub    $0x1040,%rsp
mov    %edi,%ebx
lea    0x40(%rsp),%rax
mov    %rax,0x10(%rsp)
movl   $0x1000,0x18(%rsp)
movabs $0x100000004,%rax
mov    %rax,0x28(%rsp)
lea    0x10(%rsp),%rax
mov    %rax,0x30(%rsp)
lea    0x8(%rsp),%rax
mov    %rax,0x38(%rsp)
mov    0x2e86(%rip),%r15
lea    0x28(%rsp),%r14
nop
mov    (%r15),%edx
mov    %ebx,%edi
mov    %r14,%rsi
call   1030 <recvmsg@plt>
test   %rax,%rax
jg     1160 <drain_socket+0x50>
add    $0x1040,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.sockaddr_ll = type { i32 }
%struct.iovec = type { i8*, i32 }
%struct.msghdr = type { i32, i32, %struct.iovec*, %struct.sockaddr_ll* }
@MSG_DONTWAIT = global i32 0
define void @drain_socket(i32 %0) {
%2 = alloca i32
%3 = alloca [4096 x i8]
%4 = alloca %struct.sockaddr_ll
%5 = alloca [1 x %struct.iovec]
%6 = alloca %struct.msghdr
store i32 %0, i32* %2
%7 = getelementptr inbounds [1 x %struct.iovec], [1 x %struct.iovec]* %5, i64 0, i64 0
%8 = getelementptr inbounds %struct.iovec, %struct.iovec* %7, i32 0, i32 0
%9 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
store i8* %9, i8** %8
%10 = getelementptr inbounds %struct.iovec, %struct.iovec* %7, i32 0, i32 1
store i32 4096, i32* %10
%11 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %6, i32 0, i32 0
store i32 4, i32* %11
%12 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %6, i32 0, i32 1
store i32 1, i32* %12
%13 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %6, i32 0, i32 2
%14 = getelementptr inbounds [1 x %struct.iovec], [1 x %struct.iovec]* %5, i64 0, i64 0
store %struct.iovec* %14, %struct.iovec** %13
%15 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %6, i32 0, i32 3
store %struct.sockaddr_ll* %4, %struct.sockaddr_ll** %15
br label %16
16: ; preds = %21, %1
%17 = load i32, i32* %2
%18 = load i32, i32* @MSG_DONTWAIT
%19 = call i64 @recvmsg(i32 %17, %struct.msghdr* %6, i32 %18)
%20 = icmp sgt i64 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %16
br label %16
22: ; preds = %16
ret void
}

LLVM IR (-O2):
%struct.sockaddr_ll = type { i32 }
%struct.iovec = type { i8*, i32 }
%struct.msghdr = type { i32, i32, %struct.iovec*, %struct.sockaddr_ll* }
@MSG_DONTWAIT = global i32 0
define void @drain_socket(i32 %0) {
%2 = alloca [4096 x i8]
%3 = alloca %struct.sockaddr_ll
%4 = alloca [1 x %struct.iovec]
%5 = alloca %struct.msghdr
%6 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
%7 = bitcast %struct.sockaddr_ll* %3 to i8*
%8 = bitcast [1 x %struct.iovec]* %4 to i8*
%9 = getelementptr inbounds [1 x %struct.iovec], [1 x %struct.iovec]* %4, i64 0, i64 0
%10 = getelementptr inbounds [1 x %struct.iovec], [1 x %struct.iovec]* %4, i64 0, i64 0, i32 0
store i8* %6, i8** %10
%11 = getelementptr inbounds [1 x %struct.iovec], [1 x %struct.iovec]* %4, i64 0, i64 0, i32 1
store i32 4096, i32* %11
%12 = bitcast %struct.msghdr* %5 to i8*
%13 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i64 0, i32 0
store i32 4, i32* %13
%14 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i64 0, i32 1
store i32 1, i32* %14
%15 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i64 0, i32 2
store %struct.iovec* %9, %struct.iovec** %15
%16 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i64 0, i32 3
store %struct.sockaddr_ll* %3, %struct.sockaddr_ll** %16
br label %17
17: ; preds = %17, %1
%18 = load i32, i32* @MSG_DONTWAIT
%19 = call i64 @recvmsg(i32 %0, %struct.msghdr* %5, i32 %18)
%20 = icmp sgt i64 %19, 0
br i1 %20, label %17, label %21
21: ; preds = %17
ret void
}