#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;
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);
  if (socket(PF_INET, SOCK_STREAM, 0) ||
      bind(0, (struct sockaddr *)&addr, len) || listen(0, 5) ||
      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    0x2e3b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    0x2e17(%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    0x2dcd(%rip),%rax
mov    (%rax),%edi
mov    0x2dcc(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
cmp    $0x0,%eax
jne    124d <main+0xcd>
lea    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
xor    %edi,%edi
call   1080 <bind@plt>
cmp    $0x0,%rax
jne    124d <main+0xcd>
xor    %edi,%edi
mov    $0x5,%esi
call   1070 <listen@plt>
cmp    $0x0,%rax
jne    124d <main+0xcd>
mov    0x2d9d(%rip),%rax
mov    (%rax),%edi
mov    0x2dac(%rip),%rax
mov    (%rax),%esi
mov    0x2d7b(%rip),%rax
mov    (%rax),%edx
call   10a0 <socket@plt>
cmp    $0x1,%eax
je     1259 <main+0xd9>
movl   $0x4d,-0x4(%rbp)
jmp    12ae <main+0x12e>
mov    0x2d88(%rip),%rax
mov    (%rax),%esi
mov    0x2d6f(%rip),%rax
mov    (%rax),%edx
mov    $0x1,%edi
call   1090 <send_query@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x15(%rbp)
je     129b <main+0x11b>
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,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    0x2e4e(%rip),%r14
mov    (%r14),%eax
mov    %eax,0x4(%rsp)
mov    0x2e28(%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    0x2dde(%rip),%rax
mov    (%rax),%edi
mov    0x2ddd(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    $0x4d,%ebx
test   %eax,%eax
jne    1261 <main+0xe1>
mov    %rsp,%rsi
xor    %edi,%edi
mov    $0x8,%edx
call   1080 <bind@plt>
test   %rax,%rax
jne    1261 <main+0xe1>
xor    %edi,%edi
mov    $0x5,%esi
call   1070 <listen@plt>
test   %rax,%rax
jne    1261 <main+0xe1>
mov    0x2db7(%rip),%rax
mov    (%rax),%edi
mov    0x2dc6(%rip),%rax
mov    (%rax),%esi
mov    0x2d95(%rip),%rax
mov    (%rax),%edx
call   10a0 <socket@plt>
cmp    $0x1,%eax
jne    1261 <main+0xe1>
mov    (%r14),%esi
mov    0x2d9f(%rip),%rax
mov    (%rax),%edx
mov    $0x1,%edi
call   1090 <send_query@plt>
test   %rax,%rax
je     1261 <main+0xe1>
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 ne i32 %15, 0
br i1 %16, label %31, label %17
17: ; preds = %0
%18 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%19 = load i32, i32* %3
%20 = call i64 @bind(i32 0, %struct.sockaddr* %18, i32 %19)
%21 = icmp ne i64 %20, 0
br i1 %21, label %31, label %22
22: ; preds = %17
%23 = call i64 @listen(i32 0, i32 5)
%24 = icmp ne i64 %23, 0
br i1 %24, label %31, label %25
25: ; preds = %22
%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 ne i32 %29, 1
br i1 %30, label %31, label %32
31: ; preds = %25, %22, %17, %0
store i32 77, i32* %1
br label %44
32: ; preds = %25
%33 = load i32, i32* @AF_INET
%34 = load i32, i32* @IPPROTO_TCP
%35 = call i64 @send_query(i32 1, i32 %33, i32 %34)
%36 = icmp ne i64 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %32
%38 = call i64 @check_responses(i32 1)
%39 = icmp ne i64 %38, 0
br label %40
40: ; preds = %37, %32
%41 = phi i1 [ false, %32 ], [ %39, %37 ]
%42 = zext i1 %41 to i64
%43 = select i1 %41, i32 0, i32 77
store i32 %43, i32* %1
br label %44
44: ; preds = %40, %31
%45 = load i32, i32* %1
ret i32 %45
}

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
br i1 %14, label %15, label %37
15: ; preds = %0
%16 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%17 = call i64 @bind(i32 0, %struct.sockaddr* %16, i32 8)
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %37
19: ; preds = %15
%20 = call i64 @listen(i32 0, i32 5)
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %37
22: ; preds = %19
%23 = load i32, i32* @AF_NETLINK
%24 = load i32, i32* @SOCK_RAW
%25 = load i32, i32* @NETLINK_INET_DIAG
%26 = call i32 @socket(i32 %23, i32 %24, i32 %25)
%27 = icmp eq i32 %26, 1
br i1 %27, label %28, label %37
28: ; preds = %22
%29 = load i32, i32* @AF_INET
%30 = load i32, i32* @IPPROTO_TCP
%31 = call i64 @send_query(i32 1, i32 %29, i32 %30)
%32 = icmp eq i64 %31, 0
br i1 %32, label %37, label %33
33: ; preds = %28
%34 = call i64 @check_responses(i32 1)
%35 = icmp eq i64 %34, 0
%36 = select i1 %35, i32 77, i32 0
br label %37
37: ; preds = %28, %33, %0, %15, %19, %22
%38 = phi i32 [ 77, %22 ], [ 77, %19 ], [ 77, %15 ], [ 77, %0 ], [ 77, %28 ], [ %36, %33 ]
ret i32 %38
}