#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_nl {
  int /*<<< orphan*/ nl_family;
};
struct nlmsghdr {
  scalar_t__ nlmsg_type;
};
struct msghdr {
  void *msg_name;
  int msg_namelen;
  int msg_iovlen;
  struct iovec *msg_iov;
};
struct iovec {
  char *iov_base;
  int iov_len;
};
typedef scalar_t__ ssize_t;
typedef int /*<<< orphan*/ nladdr;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ AF_NETLINK;
scalar_t__ NLMSG_DONE;
scalar_t__ NLMSG_ERROR;
scalar_t__ NLMSG_OK(struct nlmsghdr *, scalar_t__);
scalar_t__ recvmsg(int const, struct msghdr *, int /*<<< orphan*/);
int check_responses(const int fd) {
  static char buf[8192];
  struct sockaddr_nl nladdr = {.nl_family = AF_NETLINK};
  struct iovec iov = {.iov_base = buf, .iov_len = sizeof(buf)};
  struct msghdr msg = {.msg_name = (void *)&nladdr,
                       .msg_namelen = sizeof(nladdr),
                       .msg_iov = &iov,
                       .msg_iovlen = 1};
  ssize_t ret = recvmsg(fd, &msg, 0);
  if (ret <= 0)
    return 0;
  struct nlmsghdr *h = (struct nlmsghdr *)buf;
  return (NLMSG_OK(h, ret) && h->nlmsg_type != NLMSG_ERROR &&
          h->nlmsg_type != NLMSG_DONE)
             ? 1
             : 0;
}


x86 assembly (clang, -O0):
<check_responses>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2eae(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2cba(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0x2cb7(%rip),%rax
mov    %rax,-0x18(%rbp)
lea    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
movl   $0x4,-0x30(%rbp)
movl   $0x1,-0x2c(%rbp)
lea    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x38(%rbp),%rsi
xor    %edx,%edx
call   1040 <recvmsg@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jg     1194 <check_responses+0x74>
movl   $0x0,-0x4(%rbp)
jmp    1204 <check_responses+0xe4>
lea    0x2ea5(%rip),%rax
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rdi
mov    -0x40(%rbp),%rsi
call   1030 <NLMSG_OK@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x49(%rbp)
je     11f1 <check_responses+0xd1>
mov    -0x48(%rbp),%rax
mov    (%rax),%rcx
xor    %eax,%eax
mov    0x2e1a(%rip),%rdx
cmp    (%rdx),%rcx
mov    %al,-0x49(%rbp)
je     11f1 <check_responses+0xd1>
mov    -0x48(%rbp),%rax
mov    (%rax),%rax
mov    0x2de8(%rip),%rcx
cmp    (%rcx),%rax
setne  %al
mov    %al,-0x49(%rbp)
mov    -0x49(%rbp),%dl
xor    %eax,%eax
mov    $0x1,%ecx
test   $0x1,%dl
cmovne %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_responses>:
push   %rbx
sub    $0x30,%rsp
mov    0x2eb4(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%rsp)
movups 0x2cc0(%rip),%xmm0
movaps %xmm0,0x20(%rsp)
mov    %rsp,%rax
mov    %rax,0x8(%rsp)
movabs $0x100000004,%rax
mov    %rax,0x10(%rsp)
lea    0x20(%rsp),%rax
mov    %rax,0x18(%rsp)
lea    0x8(%rsp),%rsi
xor    %ebx,%ebx
xor    %edx,%edx
call   1040 <recvmsg@plt>
test   %rax,%rax
jle    11a7 <check_responses+0x87>
lea    0x2ec8(%rip),%rdi
mov    %rax,%rsi
call   1030 <NLMSG_OK@plt>
test   %rax,%rax
je     11a7 <check_responses+0x87>
mov    0x2eb4(%rip),%rax
mov    0x2e55(%rip),%rcx
cmp    (%rcx),%rax
je     11a7 <check_responses+0x87>
mov    0x2e31(%rip),%rcx
xor    %ebx,%ebx
cmp    (%rcx),%rax
setne  %bl
mov    %ebx,%eax
add    $0x30,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.iovec = type { i8*, i32 }
%struct.sockaddr_nl = type { i32 }
%struct.msghdr = type { i8*, i32, i32, %struct.iovec* }
%struct.nlmsghdr = type { i64 }
@check_responses.buf = internal global [8192 x i8] zeroinitializer
@AF_NETLINK = global i32 0
@__const.check_responses.iov = %struct.iovec { i8* getelementptr inbounds ([8192 x i8], [8192 x i8]* @check_responses.buf, i32 0, i32 0), i32 8192 }
@NLMSG_ERROR = global i64 0
@NLMSG_DONE = global i64 0
define i32 @check_responses(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.sockaddr_nl
%5 = alloca %struct.iovec
%6 = alloca %struct.msghdr
%7 = alloca i64
%8 = alloca %struct.nlmsghdr*
store i32 %0, i32* %3
%9 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %4, i32 0, i32 0
%10 = load i32, i32* @AF_NETLINK
store i32 %10, i32* %9
%11 = bitcast %struct.iovec* %5 to i8*
%12 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %6, i32 0, i32 0
%13 = bitcast %struct.sockaddr_nl* %4 to i8*
store i8* %13, i8** %12
%14 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %6, i32 0, i32 1
store i32 4, i32* %14
%15 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %6, i32 0, i32 2
store i32 1, i32* %15
%16 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %6, i32 0, i32 3
store %struct.iovec* %5, %struct.iovec** %16
%17 = load i32, i32* %3
%18 = call i64 @recvmsg(i32 %17, %struct.msghdr* %6, i32 0)
store i64 %18, i64* %7
%19 = load i64, i64* %7
%20 = icmp sle i64 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %1
store i32 0, i32* %2
br label %43
22: ; preds = %1
store %struct.nlmsghdr* bitcast ([8192 x i8]* @check_responses.buf to %struct.nlmsghdr*), %struct.nlmsghdr** %8
%23 = load %struct.nlmsghdr*, %struct.nlmsghdr** %8
%24 = load i64, i64* %7
%25 = call i64 @NLMSG_OK(%struct.nlmsghdr* %23, i64 %24)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %39
27: ; preds = %22
%28 = load %struct.nlmsghdr*, %struct.nlmsghdr** %8
%29 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %28, i32 0, i32 0
%30 = load i64, i64* %29
%31 = load i64, i64* @NLMSG_ERROR
%32 = icmp ne i64 %30, %31
br i1 %32, label %33, label %39
33: ; preds = %27
%34 = load %struct.nlmsghdr*, %struct.nlmsghdr** %8
%35 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %34, i32 0, i32 0
%36 = load i64, i64* %35
%37 = load i64, i64* @NLMSG_DONE
%38 = icmp ne i64 %36, %37
br label %39
39: ; preds = %33, %27, %22
%40 = phi i1 [ false, %27 ], [ false, %22 ], [ %38, %33 ]
%41 = zext i1 %40 to i64
%42 = select i1 %40, i32 1, i32 0
store i32 %42, i32* %2
br label %43
43: ; preds = %39, %21
%44 = load i32, i32* %2
ret i32 %44
}

LLVM IR (-O2):
%struct.iovec = type { i8*, i32 }
%struct.sockaddr_nl = type { i32 }
%struct.msghdr = type { i8*, i32, i32, %struct.iovec* }
%struct.nlmsghdr = type { i64 }
@check_responses.buf = internal global [8192 x i8] zeroinitializer
@AF_NETLINK = global i32 0
@__const.check_responses.iov = %struct.iovec { i8* getelementptr inbounds ([8192 x i8], [8192 x i8]* @check_responses.buf, i32 0, i32 0), i32 8192 }
@NLMSG_ERROR = global i64 0
@NLMSG_DONE = global i64 0
define i32 @check_responses(i32 %0) {
%2 = alloca %struct.sockaddr_nl
%3 = alloca %struct.iovec
%4 = alloca %struct.msghdr
%5 = bitcast %struct.sockaddr_nl* %2 to i8*
%6 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %2, i64 0, i32 0
%7 = load i32, i32* @AF_NETLINK
store i32 %7, i32* %6
%8 = bitcast %struct.iovec* %3 to i8*
%9 = bitcast %struct.msghdr* %4 to i8*
%10 = bitcast %struct.msghdr* %4 to %struct.sockaddr_nl**
store %struct.sockaddr_nl* %2, %struct.sockaddr_nl** %10
%11 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %4, i64 0, i32 1
store i32 4, i32* %11
%12 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %4, i64 0, i32 2
store i32 1, i32* %12
%13 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %4, i64 0, i32 3
store %struct.iovec* %3, %struct.iovec** %13
%14 = call i64 @recvmsg(i32 %0, %struct.msghdr* %4, i32 0)
%15 = icmp slt i64 %14, 1
br i1 %15, label %27, label %16
16: ; preds = %1
%17 = call i64 @NLMSG_OK(%struct.nlmsghdr* bitcast ([8192 x i8]* @check_responses.buf to %struct.nlmsghdr*), i64 %14)
%18 = icmp eq i64 %17, 0
br i1 %18, label %27, label %19
19: ; preds = %16
%20 = load i64, i64* bitcast ([8192 x i8]* @check_responses.buf to i64*)
%21 = load i64, i64* @NLMSG_ERROR
%22 = icmp eq i64 %20, %21
br i1 %22, label %27, label %23
23: ; preds = %19
%24 = load i64, i64* @NLMSG_DONE
%25 = icmp ne i64 %20, %24
%26 = zext i1 %25 to i32
br label %27
27: ; preds = %16, %19, %23, %1
%28 = phi i32 [ 0, %1 ], [ 0, %19 ], [ 0, %16 ], [ %26, %23 ]
ret i32 %28
}