#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 nl_groups;
  int /*<<< orphan*/ nl_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_NETLINK;
int /*<<< orphan*/ NETLINK_ROUTE;
int /*<<< orphan*/ PF_NETLINK;
int RTMGRP_IPV4_IFADDR;
int RTMGRP_LINK;
int /*<<< orphan*/ SOCK_RAW;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ memset(struct sockaddr_nl *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int open_router_watch_socket(void) {
  int s;
  struct sockaddr_nl addr;
  s = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
  if (s == -1) {
    do {
      printf("socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE): %m");
      printf("\n");
    } while (0);
    return -1;
  }
  memset(&addr, 0, sizeof(addr));
  addr.nl_family = AF_NETLINK;
  addr.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR;
  if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
    do {
      printf("bind(netlink): %m");
      printf("\n");
    } while (0);
    close(s);
    return -1;
  }
  return s;
}


x86 assembly (clang, -O0):
<open_router_watch_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e61(%rip),%rax
mov    (%rax),%edi
mov    0x2e70(%rip),%rax
mov    (%rax),%esi
mov    0x2e3f(%rip),%rax
mov    (%rax),%edx
call   1070 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11ae <open_router_watch_socket+0x5e>
jmp    118a <open_router_watch_socket+0x3a>
lea    0xe6f(%rip),%rdi
call   1030 <printf@plt>
lea    0xe93(%rip),%rdi
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1231 <open_router_watch_socket+0xe1>
lea    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    0x2e0b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    0x2e27(%rip),%rax
mov    (%rax),%eax
mov    0x2e0e(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0x8,%edx
call   1060 <bind@plt>
cmp    $0x0,%rax
jge    122b <open_router_watch_socket+0xdb>
jmp    11ff <open_router_watch_socket+0xaf>
lea    0xe2c(%rip),%rdi
call   1030 <printf@plt>
lea    0xe1e(%rip),%rdi
call   1030 <printf@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1231 <open_router_watch_socket+0xe1>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_router_watch_socket>:
push   %r14
push   %rbx
push   %rax
mov    0x2e55(%rip),%rax
mov    (%rax),%edi
mov    0x2e64(%rip),%rax
mov    (%rax),%esi
mov    0x2e33(%rip),%rax
mov    (%rax),%edx
call   1080 <socket@plt>
cmp    $0xffffffff,%eax
je     11f2 <open_router_watch_socket+0x92>
mov    %eax,%ebx
mov    %rsp,%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    0x2e2c(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rsp)
mov    0x2e47(%rip),%rax
mov    0x2e30(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    %ecx,(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x8,%edx
call   1070 <bind@plt>
test   %rax,%rax
jns    120d <open_router_watch_socket+0xad>
lea    0xe56(%rip),%rdi
call   1040 <printf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    %ebx,%edi
call   1060 <close@plt>
jmp    1208 <open_router_watch_socket+0xa8>
lea    0xe07(%rip),%rdi
call   1040 <printf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.sockaddr_nl = type { i32, i32 }
%struct.sockaddr = type { i32 }
@PF_NETLINK = global i32 0
@SOCK_RAW = global i32 0
@NETLINK_ROUTE = global i32 0
@.str = [48 x i8] c"socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE): %m\00"
@.str.1 = [2 x i8] c"\0A\00"
@AF_NETLINK = global i32 0
@RTMGRP_LINK = global i32 0
@RTMGRP_IPV4_IFADDR = global i32 0
@.str.2 = [18 x i8] c"bind(netlink): %m\00"
define i32 @open_router_watch_socket() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.sockaddr_nl
%4 = load i32, i32* @PF_NETLINK
%5 = load i32, i32* @SOCK_RAW
%6 = load i32, i32* @NETLINK_ROUTE
%7 = call i32 @socket(i32 %4, i32 %5, i32 %6)
store i32 %7, i32* %2
%8 = load i32, i32* %2
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %15
10: ; preds = %0
br label %11
11: ; preds = %10
%12 = call i32 @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str, i64 0, i64 0))
%13 = call i32 @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %14
14: ; preds = %11
store i32 -1, i32* %1
br label %36
15: ; preds = %0
%16 = call i32 @memset(%struct.sockaddr_nl* %3, i32 0, i32 8)
%17 = load i32, i32* @AF_NETLINK
%18 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %3, i32 0, i32 1
store i32 %17, i32* %18
%19 = load i32, i32* @RTMGRP_LINK
%20 = load i32, i32* @RTMGRP_IPV4_IFADDR
%21 = or i32 %19, %20
%22 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %3, i32 0, i32 0
store i32 %21, i32* %22
%23 = load i32, i32* %2
%24 = bitcast %struct.sockaddr_nl* %3 to %struct.sockaddr*
%25 = call i64 @bind(i32 %23, %struct.sockaddr* %24, i32 8)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %34
27: ; preds = %15
br label %28
28: ; preds = %27
%29 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
%30 = call i32 @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %31
31: ; preds = %28
%32 = load i32, i32* %2
%33 = call i32 @close(i32 %32)
store i32 -1, i32* %1
br label %36
34: ; preds = %15
%35 = load i32, i32* %2
store i32 %35, i32* %1
br label %36
36: ; preds = %34, %31, %14
%37 = load i32, i32* %1
ret i32 %37
}

LLVM IR (-O2):
%struct.sockaddr_nl = type { i32, i32 }
%struct.sockaddr = type { i32 }
@PF_NETLINK = global i32 0
@SOCK_RAW = global i32 0
@NETLINK_ROUTE = global i32 0
@.str = [48 x i8] c"socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE): %m\00"
@AF_NETLINK = global i32 0
@RTMGRP_LINK = global i32 0
@RTMGRP_IPV4_IFADDR = global i32 0
@.str.2 = [18 x i8] c"bind(netlink): %m\00"
define i32 @open_router_watch_socket() {
%1 = alloca %struct.sockaddr_nl
%2 = bitcast %struct.sockaddr_nl* %1 to i8*
%3 = load i32, i32* @PF_NETLINK
%4 = load i32, i32* @SOCK_RAW
%5 = load i32, i32* @NETLINK_ROUTE
%6 = tail call i32 @socket(i32 %3, i32 %4, i32 %5)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %11
8: ; preds = %0
%9 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([48 x i8], [48 x i8]* @.str, i64 0, i64 0))
%10 = tail call i32 @putchar(i32 10)
br label %26
11: ; preds = %0
%12 = call i32 @memset(%struct.sockaddr_nl* %1, i32 0, i32 8)
%13 = load i32, i32* @AF_NETLINK
%14 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %1, i64 0, i32 1
store i32 %13, i32* %14
%15 = load i32, i32* @RTMGRP_LINK
%16 = load i32, i32* @RTMGRP_IPV4_IFADDR
%17 = or i32 %16, %15
%18 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %1, i64 0, i32 0
store i32 %17, i32* %18
%19 = bitcast %struct.sockaddr_nl* %1 to %struct.sockaddr*
%20 = call i64 @bind(i32 %6, %struct.sockaddr* %19, i32 8)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %26
22: ; preds = %11
%23 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
%24 = call i32 @putchar(i32 10)
%25 = call i32 @close(i32 %6)
br label %26
26: ; preds = %11, %22, %8
%27 = phi i32 [ -1, %8 ], [ -1, %22 ], [ %6, %11 ]
ret i32 %27
}