#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ AF_NETLINK;
int /*<<< orphan*/ INADDR_LOOPBACK;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ NETLINK_INET_DIAG;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_RAW;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ assert(int);
scalar_t__ bind(int /*<<< orphan*/, struct sockaddr *, int);
scalar_t__ check_responses(int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
scalar_t__ listen(int /*<<< orphan*/, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
scalar_t__ send_query(int, int /*<<< orphan*/, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(void) {
  struct sockaddr_in addr;
  socklen_t len = sizeof(addr);
  memset(&addr, 0, sizeof(addr));
  addr.sin_family = AF_INET;
  addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
  close(0);
  close(1);
  assert(socket(PF_INET, SOCK_STREAM, 0) == 0);
  assert(bind(0, (struct sockaddr *)&addr, len) == 0);
  assert(listen(0, 5) == 0);
  if (socket(AF_NETLINK, SOCK_RAW, NETLINK_INET_DIAG) != 1)
    return 77;
  return (send_query(1, AF_INET, IPPROTO_TCP) && check_responses(1)) ? 0 : 77;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x8,-0x14(%rbp)
lea    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    0x2e2b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    0x2e07(%rip),%rax
mov    (%rax),%edi
call   1040 <htonl@plt>
mov    %eax,-0x10(%rbp)
xor    %edi,%edi
call   1060 <close@plt>
mov    $0x1,%edi
call   1060 <close@plt>
mov    0x2dbd(%rip),%rax
mov    (%rax),%edi
mov    0x2dbc(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
cmp    $0x0,%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
lea    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
xor    %edi,%edi
call   1090 <bind@plt>
cmp    $0x0,%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
xor    %edi,%edi
mov    $0x5,%esi
call   1080 <listen@plt>
cmp    $0x0,%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    0x2d78(%rip),%rax
mov    (%rax),%edi
mov    0x2d87(%rip),%rax
mov    (%rax),%esi
mov    0x2d56(%rip),%rax
mov    (%rax),%edx
call   10b0 <socket@plt>
cmp    $0x1,%eax
je     127e <main+0xee>
movl   $0x4d,-0x4(%rbp)
jmp    12d3 <main+0x143>
mov    0x2d63(%rip),%rax
mov    (%rax),%esi
mov    0x2d4a(%rip),%rax
mov    (%rax),%edx
mov    $0x1,%edi
call   10a0 <send_query@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x15(%rbp)
je     12c0 <main+0x130>
mov    $0x1,%edi
call   1030 <check_responses@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,-0x15(%rbp)
mov    -0x15(%rbp),%dl
mov    $0x4d,%eax
xor    %ecx,%ecx
test   $0x1,%dl
cmovne %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    %rsp,%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    0x2e3b(%rip),%r14
mov    (%r14),%eax
mov    %eax,0x4(%rsp)
mov    0x2e15(%rip),%rax
mov    (%rax),%edi
call   1040 <htonl@plt>
mov    %eax,(%rsp)
xor    %edi,%edi
call   1060 <close@plt>
mov    $0x1,%edi
call   1060 <close@plt>
mov    0x2dcb(%rip),%rax
mov    (%rax),%edi
mov    0x2dca(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
xor    %edi,%edi
test   %eax,%eax
sete   %dil
call   1070 <assert@plt>
xor    %edi,%edi
mov    %rbx,%rsi
mov    $0x8,%edx
call   1090 <bind@plt>
xor    %edi,%edi
test   %rax,%rax
sete   %dil
call   1070 <assert@plt>
xor    %edi,%edi
mov    $0x5,%esi
call   1080 <listen@plt>
xor    %edi,%edi
test   %rax,%rax
sete   %dil
call   1070 <assert@plt>
mov    0x2d8e(%rip),%rax
mov    (%rax),%edi
mov    0x2d9d(%rip),%rax
mov    (%rax),%esi
mov    0x2d6c(%rip),%rax
mov    (%rax),%edx
call   10b0 <socket@plt>
mov    $0x4d,%ebx
cmp    $0x1,%eax
jne    128f <main+0xff>
mov    (%r14),%esi
mov    0x2d71(%rip),%rax
mov    (%rax),%edx
mov    $0x1,%edi
call   10a0 <send_query@plt>
test   %rax,%rax
je     128f <main+0xff>
mov    $0x1,%edi
call   1030 <check_responses@plt>
xor    %ecx,%ecx
test   %rax,%rax
mov    $0x4d,%ebx
cmovne %ecx,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@INADDR_LOOPBACK = global i32 0
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@AF_NETLINK = global i32 0
@SOCK_RAW = global i32 0
@NETLINK_INET_DIAG = global i32 0
@IPPROTO_TCP = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.sockaddr_in
%3 = alloca i32
store i32 0, i32* %1
store i32 8, i32* %3
%4 = call i32 @memset(%struct.sockaddr_in* %2, i32 0, i32 8)
%5 = load i32, i32* @AF_INET
%6 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 1
store i32 %5, i32* %6
%7 = load i32, i32* @INADDR_LOOPBACK
%8 = call i32 @htonl(i32 %7)
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 0
%10 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %9, i32 0, i32 0
store i32 %8, i32* %10
%11 = call i32 @close(i32 0)
%12 = call i32 @close(i32 1)
%13 = load i32, i32* @PF_INET
%14 = load i32, i32* @SOCK_STREAM
%15 = call i32 @socket(i32 %13, i32 %14, i32 0)
%16 = icmp eq i32 %15, 0
%17 = zext i1 %16 to i32
%18 = call i32 @assert(i32 %17)
%19 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%20 = load i32, i32* %3
%21 = call i64 @bind(i32 0, %struct.sockaddr* %19, i32 %20)
%22 = icmp eq i64 %21, 0
%23 = zext i1 %22 to i32
%24 = call i32 @assert(i32 %23)
%25 = call i64 @listen(i32 0, i32 5)
%26 = icmp eq i64 %25, 0
%27 = zext i1 %26 to i32
%28 = call i32 @assert(i32 %27)
%29 = load i32, i32* @AF_NETLINK
%30 = load i32, i32* @SOCK_RAW
%31 = load i32, i32* @NETLINK_INET_DIAG
%32 = call i32 @socket(i32 %29, i32 %30, i32 %31)
%33 = icmp ne i32 %32, 1
br i1 %33, label %34, label %35
34: ; preds = %0
store i32 77, i32* %1
br label %47
35: ; preds = %0
%36 = load i32, i32* @AF_INET
%37 = load i32, i32* @IPPROTO_TCP
%38 = call i64 @send_query(i32 1, i32 %36, i32 %37)
%39 = icmp ne i64 %38, 0
br i1 %39, label %40, label %43
40: ; preds = %35
%41 = call i64 @check_responses(i32 1)
%42 = icmp ne i64 %41, 0
br label %43
43: ; preds = %40, %35
%44 = phi i1 [ false, %35 ], [ %42, %40 ]
%45 = zext i1 %44 to i64
%46 = select i1 %44, i32 0, i32 77
store i32 %46, i32* %1
br label %47
47: ; preds = %43, %34
%48 = load i32, i32* %1
ret i32 %48
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@INADDR_LOOPBACK = global i32 0
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@AF_NETLINK = global i32 0
@SOCK_RAW = global i32 0
@NETLINK_INET_DIAG = global i32 0
@IPPROTO_TCP = global i32 0
define i32 @main() {
%1 = alloca %struct.sockaddr_in
%2 = bitcast %struct.sockaddr_in* %1 to i8*
%3 = call i32 @memset(%struct.sockaddr_in* %1, i32 0, i32 8)
%4 = load i32, i32* @AF_INET
%5 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1
store i32 %4, i32* %5
%6 = load i32, i32* @INADDR_LOOPBACK
%7 = call i32 @htonl(i32 %6)
%8 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0, i32 0
store i32 %7, i32* %8
%9 = call i32 @close(i32 0)
%10 = call i32 @close(i32 1)
%11 = load i32, i32* @PF_INET
%12 = load i32, i32* @SOCK_STREAM
%13 = call i32 @socket(i32 %11, i32 %12, i32 0)
%14 = icmp eq i32 %13, 0
%15 = zext i1 %14 to i32
%16 = call i32 @assert(i32 %15)
%17 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%18 = call i64 @bind(i32 0, %struct.sockaddr* %17, i32 8)
%19 = icmp eq i64 %18, 0
%20 = zext i1 %19 to i32
%21 = call i32 @assert(i32 %20)
%22 = call i64 @listen(i32 0, i32 5)
%23 = icmp eq i64 %22, 0
%24 = zext i1 %23 to i32
%25 = call i32 @assert(i32 %24)
%26 = load i32, i32* @AF_NETLINK
%27 = load i32, i32* @SOCK_RAW
%28 = load i32, i32* @NETLINK_INET_DIAG
%29 = call i32 @socket(i32 %26, i32 %27, i32 %28)
%30 = icmp eq i32 %29, 1
br i1 %30, label %31, label %40
31: ; preds = %0
%32 = load i32, i32* @AF_INET
%33 = load i32, i32* @IPPROTO_TCP
%34 = call i64 @send_query(i32 1, i32 %32, i32 %33)
%35 = icmp eq i64 %34, 0
br i1 %35, label %40, label %36
36: ; preds = %31
%37 = call i64 @check_responses(i32 1)
%38 = icmp eq i64 %37, 0
%39 = select i1 %38, i32 77, i32 0
br label %40
40: ; preds = %31, %36, %0
%41 = phi i32 [ 77, %0 ], [ 77, %31 ], [ %39, %36 ]
ret i32 %41
}