#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 timeval {
  int tv_sec;
  int /*<<< orphan*/ tv_usec;
};
struct sockaddr_nl {
  int /*<<< orphan*/ nl_groups;
  int /*<<< orphan*/ nl_pid;
  int /*<<< orphan*/ nl_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ local;
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
int /*<<< orphan*/ AF_NETLINK;
int /*<<< orphan*/ FD_CLR(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SETSIZE;
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ NETLINK_ROUTE;
int /*<<< orphan*/ RTMGRP_IPV4_IFADDR;
int /*<<< orphan*/ SOCK_RAW;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ getpid();
int /*<<< orphan*/ memset(struct sockaddr_nl *, int /*<<< orphan*/, int);
int /*<<< orphan*/ process(int);
int select(int /*<<< orphan*/, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int listen_ip_addr_change(void) {
  int sock;
  struct sockaddr_nl local;
  sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
  if (sock < 0) {
    fprintf(stderr, "Fail to open netlink socket\n");
    return -1;
  }
  memset(&local, 0, sizeof(local));
  local.nl_family = AF_NETLINK;
  local.nl_pid = getpid();
  local.nl_groups = RTMGRP_IPV4_IFADDR;
  if (bind(sock, (struct sockaddr *)&local, sizeof(local)) < 0) {
    fprintf(stderr, "Fail to bind\n");
    return -1;
  }
  while (1) {
    int retval;
    struct timeval tv = {.tv_sec = 60, .tv_usec = 0};
    fd_set rfds;
    FD_ZERO(&rfds);
    FD_CLR(sock, &rfds);
    FD_SET(sock, &rfds);
    retval = select(FD_SETSIZE, &rfds, NULL, NULL, &tv);
    if (retval == -1) {
      fprintf(stderr, "Fail to select() \n");
      break;
    } else if (retval) {
      process(sock);
    }
  }
  close(sock);
  return 0;
}


x86 assembly (clang, -O0):
<listen_ip_addr_change>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    0x2e19(%rip),%rax
mov    (%rax),%edi
mov    0x2e18(%rip),%rax
mov    (%rax),%esi
mov    0x2ddf(%rip),%rax
mov    (%rax),%edx
call   10d0 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    1206 <listen_ip_addr_change+0x56>
mov    0x2ddc(%rip),%rax
mov    (%rax),%edi
lea    0xe0b(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1314 <listen_ip_addr_change+0x164>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    0x2dbb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x14(%rbp)
mov    0x2dbd(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
cmp    $0x0,%rax
jge    1274 <listen_ip_addr_change+0xc4>
mov    0x2d6e(%rip),%rax
mov    (%rax),%edi
lea    0xdba(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1314 <listen_ip_addr_change+0x164>
jmp    1279 <listen_ip_addr_change+0xc9>
mov    0xdc0(%rip),%rax
mov    %rax,-0x28(%rbp)
lea    -0x2c(%rbp),%rdi
call   1080 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x2c(%rbp),%rsi
call   10b0 <FD_CLR@plt>
mov    -0x8(%rbp),%edi
lea    -0x2c(%rbp),%rsi
call   10c0 <FD_SET@plt>
mov    0x2d14(%rip),%rax
mov    (%rax),%edi
lea    -0x2c(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x28(%rbp),%r8
mov    %rcx,%rdx
call   1090 <select@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    12e9 <listen_ip_addr_change+0x139>
mov    0x2cf2(%rip),%rax
mov    (%rax),%edi
lea    0xd4c(%rip),%rsi
call   1070 <fprintf@plt>
jmp    1305 <listen_ip_addr_change+0x155>
cmpl   $0x0,-0x1c(%rbp)
je     12fb <listen_ip_addr_change+0x14b>
mov    -0x8(%rbp),%edi
call   1050 <process@plt>
jmp    1300 <listen_ip_addr_change+0x150>
jmp    1279 <listen_ip_addr_change+0xc9>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<listen_ip_addr_change>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    0x2e17(%rip),%rbx
mov    (%rbx),%edi
mov    0x2e16(%rip),%rax
mov    (%rax),%esi
mov    0x2ddd(%rip),%rax
mov    (%rax),%edx
call   10d0 <socket@plt>
test   %eax,%eax
js     12a9 <listen_ip_addr_change+0xf9>
mov    %eax,%ebp
lea    0x8(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    (%rbx),%eax
mov    %eax,0x10(%rsp)
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,0xc(%rsp)
mov    0x2de0(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
test   %rax,%rax
js     12bb <listen_ip_addr_change+0x10b>
lea    0x24(%rsp),%rbx
mov    0x2d86(%rip),%r15
lea    0x18(%rsp),%r14
nop
movq   $0x3c,0x18(%rsp)
mov    %rbx,%rdi
call   1080 <FD_ZERO@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
call   10b0 <FD_CLR@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
call   10c0 <FD_SET@plt>
mov    (%r15),%edi
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %r14,%r8
call   1090 <select@plt>
test   %eax,%eax
je     1240 <listen_ip_addr_change+0x90>
cmp    $0xffffffff,%eax
je     1289 <listen_ip_addr_change+0xd9>
mov    %ebp,%edi
call   1050 <process@plt>
jmp    1240 <listen_ip_addr_change+0x90>
mov    0x2d38(%rip),%rax
mov    (%rax),%edi
lea    0xd92(%rip),%rsi
call   1070 <fprintf@plt>
mov    %ebp,%edi
call   1060 <close@plt>
xor    %eax,%eax
jmp    12d5 <listen_ip_addr_change+0x125>
mov    0x2d18(%rip),%rax
mov    (%rax),%edi
lea    0xd47(%rip),%rsi
jmp    12cb <listen_ip_addr_change+0x11b>
mov    0x2d06(%rip),%rax
mov    (%rax),%edi
lea    0xd52(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
%struct.sockaddr_nl = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_NETLINK = global i32 0
@SOCK_RAW = global i32 0
@NETLINK_ROUTE = global i32 0
@stderr = global i32 0
@.str = [29 x i8] c"Fail to open netlink socket\0A\00"
@RTMGRP_IPV4_IFADDR = global i32 0
@.str.1 = [14 x i8] c"Fail to bind\0A\00"
@__const.listen_ip_addr_change.tv = %struct.timeval { i32 60, i32 0 }
@FD_SETSIZE = global i32 0
@.str.2 = [19 x i8] c"Fail to select() \0A\00"
define i32 @listen_ip_addr_change() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.sockaddr_nl
%4 = alloca i32
%5 = alloca %struct.timeval
%6 = alloca i32
%7 = load i32, i32* @AF_NETLINK
%8 = load i32, i32* @SOCK_RAW
%9 = load i32, i32* @NETLINK_ROUTE
%10 = call i32 @socket(i32 %7, i32 %8, i32 %9)
store i32 %10, i32* %2
%11 = load i32, i32* %2
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %0
%14 = load i32, i32* @stderr
%15 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %1
br label %57
16: ; preds = %0
%17 = call i32 @memset(%struct.sockaddr_nl* %3, i32 0, i32 12)
%18 = load i32, i32* @AF_NETLINK
%19 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %3, i32 0, i32 2
store i32 %18, i32* %19
%20 = call i32 (...) @getpid()
%21 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %3, i32 0, i32 1
store i32 %20, i32* %21
%22 = load i32, i32* @RTMGRP_IPV4_IFADDR
%23 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %3, i32 0, i32 0
store i32 %22, i32* %23
%24 = load i32, i32* %2
%25 = bitcast %struct.sockaddr_nl* %3 to %struct.sockaddr*
%26 = call i64 @bind(i32 %24, %struct.sockaddr* %25, i32 12)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %16
%29 = load i32, i32* @stderr
%30 = call i32 @fprintf(i32 %29, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %1
br label %57
31: ; preds = %16
br label %32
32: ; preds = %31, %53
%33 = bitcast %struct.timeval* %5 to i8*
%34 = call i32 @FD_ZERO(i32* %6)
%35 = load i32, i32* %2
%36 = call i32 @FD_CLR(i32 %35, i32* %6)
%37 = load i32, i32* %2
%38 = call i32 @FD_SET(i32 %37, i32* %6)
%39 = load i32, i32* @FD_SETSIZE
%40 = call i32 @select(i32 %39, i32* %6, i32* null, i32* null, %struct.timeval* %5)
store i32 %40, i32* %4
%41 = load i32, i32* %4
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %46
43: ; preds = %32
%44 = load i32, i32* @stderr
%45 = call i32 @fprintf(i32 %44, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
br label %54
46: ; preds = %32
%47 = load i32, i32* %4
%48 = icmp ne i32 %47, 0
br i1 %48, label %49, label %52
49: ; preds = %46
%50 = load i32, i32* %2
%51 = call i32 @process(i32 %50)
br label %52
52: ; preds = %49, %46
br label %53
53: ; preds = %52
br label %32
54: ; preds = %43
%55 = load i32, i32* %2
%56 = call i32 @close(i32 %55)
store i32 0, i32* %1
br label %57
57: ; preds = %54, %28, %13
%58 = load i32, i32* %1
ret i32 %58
}

LLVM IR (-O2):
%struct.sockaddr_nl = type { i32, i32, i32 }
%struct.timeval = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_NETLINK = global i32 0
@SOCK_RAW = global i32 0
@NETLINK_ROUTE = global i32 0
@stderr = global i32 0
@.str = [29 x i8] c"Fail to open netlink socket\0A\00"
@RTMGRP_IPV4_IFADDR = global i32 0
@.str.1 = [14 x i8] c"Fail to bind\0A\00"
@FD_SETSIZE = global i32 0
@.str.2 = [19 x i8] c"Fail to select() \0A\00"
define i32 @listen_ip_addr_change() {
%1 = alloca %struct.sockaddr_nl
%2 = alloca i64
%3 = bitcast i64* %2 to %struct.timeval*
%4 = alloca i32
%5 = bitcast %struct.sockaddr_nl* %1 to i8*
%6 = load i32, i32* @AF_NETLINK
%7 = load i32, i32* @SOCK_RAW
%8 = load i32, i32* @NETLINK_ROUTE
%9 = tail call i32 @socket(i32 %6, i32 %7, i32 %8)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %0
%12 = load i32, i32* @stderr
%13 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
br label %44
14: ; preds = %0
%15 = call i32 @memset(%struct.sockaddr_nl* %1, i32 0, i32 12)
%16 = load i32, i32* @AF_NETLINK
%17 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %1, i64 0, i32 2
store i32 %16, i32* %17
%18 = call i32 (...) @getpid()
%19 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %1, i64 0, i32 1
store i32 %18, i32* %19
%20 = load i32, i32* @RTMGRP_IPV4_IFADDR
%21 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %1, i64 0, i32 0
store i32 %20, i32* %21
%22 = bitcast %struct.sockaddr_nl* %1 to %struct.sockaddr*
%23 = call i64 @bind(i32 %9, %struct.sockaddr* %22, i32 12)
%24 = icmp slt i64 %23, 0
br i1 %24, label %28, label %25
25: ; preds = %14
%26 = bitcast i64* %2 to i8*
%27 = bitcast i32* %4 to i8*
br label %31
28: ; preds = %14
%29 = load i32, i32* @stderr
%30 = call i32 @fprintf(i32 %29, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %44
31: ; preds = %25, %39
store i64 60, i64* %2
%32 = call i32 @FD_ZERO(i32* %4)
%33 = call i32 @FD_CLR(i32 %9, i32* %4)
%34 = call i32 @FD_SET(i32 %9, i32* %4)
%35 = load i32, i32* @FD_SETSIZE
%36 = call i32 @select(i32 %35, i32* %4, i32* null, i32* null, %struct.timeval* %3)
switch i32 %36, label %37 [
i32 -1, label %40
i32 0, label %39
]
37: ; preds = %31
%38 = call i32 @process(i32 %9)
br label %39
39: ; preds = %37, %31
br label %31
40: ; preds = %31
%41 = load i32, i32* @stderr
%42 = call i32 @fprintf(i32 %41, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
%43 = call i32 @close(i32 %9)
br label %44
44: ; preds = %40, %28, %11
%45 = phi i32 [ -1, %11 ], [ -1, %28 ], [ 0, %40 ]
ret i32 %45
}