#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 udev_monitor {
  int dummy;
};
struct udev_device {
  int dummy;
};
struct udev {
  int dummy;
};
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int STDIN_FILENO;
int /*<<< orphan*/ print_device(struct udev_device *);
int /*<<< orphan*/ printf(char *, ...);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ udev_device_unref(struct udev_device *);
scalar_t__ udev_monitor_enable_receiving(struct udev_monitor *);
scalar_t__
udev_monitor_filter_add_match_subsystem_devtype(struct udev_monitor *, char *,
                                                char *);
int udev_monitor_get_fd(struct udev_monitor *);
struct udev_monitor *udev_monitor_new_from_netlink(struct udev *, char *);
struct udev_device *udev_monitor_receive_device(struct udev_monitor *);
int /*<<< orphan*/ udev_monitor_unref(struct udev_monitor *);
int test_monitor(struct udev *udev) {
  struct udev_monitor *udev_monitor;
  fd_set readfds;
  int fd;
  udev_monitor = udev_monitor_new_from_netlink(udev, "udev");
  if (udev_monitor == NULL) {
    printf("no socket\n");
    return -1;
  }
  if (udev_monitor_filter_add_match_subsystem_devtype(udev_monitor, "block",
                                                      NULL) < 0 ||
      udev_monitor_filter_add_match_subsystem_devtype(udev_monitor, "tty",
                                                      NULL) < 0 ||
      udev_monitor_filter_add_match_subsystem_devtype(udev_monitor, "usb",
                                                      "usb_device") < 0) {
    printf("filter failed\n");
    return -1;
  }
  if (udev_monitor_enable_receiving(udev_monitor) < 0) {
    printf("bind failed\n");
    return -1;
  }
  fd = udev_monitor_get_fd(udev_monitor);
  FD_ZERO(&readfds);
  while (1) {
    struct udev_device *device;
    int fdcount;
    FD_SET(STDIN_FILENO, &readfds);
    FD_SET(fd, &readfds);
    printf("waiting for events from udev, press ENTER to exit\n");
    fdcount = select(fd + 1, &readfds, NULL, NULL, NULL);
    printf("select fd count: %i\n", fdcount);
    if (FD_ISSET(fd, &readfds)) {
      device = udev_monitor_receive_device(udev_monitor);
      if (device == NULL) {
        printf("no device from socket\n");
        continue;
      }
      print_device(device);
      udev_device_unref(device);
    }
    if (FD_ISSET(STDIN_FILENO, &readfds)) {
      printf("exiting loop\n");
      break;
    }
  }
  udev_monitor_unref(udev_monitor);
  return 0;
}


x86 assembly (clang, -O0):
<test_monitor>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe19(%rip),%rsi
call   10b0 <udev_monitor_new_from_netlink@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1215 <test_monitor+0x45>
lea    0xe03(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13bf <test_monitor+0x1ef>
mov    -0x18(%rbp),%rdi
lea    0xdf0(%rip),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   10c0 <udev_monitor_filter_add_match_subsystem_devtype@plt>
cmp    $0x0,%rax
jl     1272 <test_monitor+0xa2>
mov    -0x18(%rbp),%rdi
lea    0xdd8(%rip),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   10c0 <udev_monitor_filter_add_match_subsystem_devtype@plt>
cmp    $0x0,%rax
jl     1272 <test_monitor+0xa2>
mov    -0x18(%rbp),%rdi
lea    0xdbe(%rip),%rsi
lea    0xdbb(%rip),%rdx
call   10c0 <udev_monitor_filter_add_match_subsystem_devtype@plt>
cmp    $0x0,%rax
jge    128c <test_monitor+0xbc>
lea    0xdb0(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13bf <test_monitor+0x1ef>
mov    -0x18(%rbp),%rdi
call   10d0 <udev_monitor_enable_receiving@plt>
cmp    $0x0,%rax
jge    12b9 <test_monitor+0xe9>
lea    0xd92(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13bf <test_monitor+0x1ef>
mov    -0x18(%rbp),%rdi
call   1090 <udev_monitor_get_fd@plt>
mov    %eax,-0x20(%rbp)
lea    -0x1c(%rbp),%rdi
call   1070 <FD_ZERO@plt>
mov    0x2d0b(%rip),%rax
mov    (%rax),%edi
lea    -0x1c(%rbp),%rsi
call   10e0 <FD_SET@plt>
mov    -0x20(%rbp),%edi
lea    -0x1c(%rbp),%rsi
call   10e0 <FD_SET@plt>
lea    0xd52(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x20(%rbp),%edi
add    $0x1,%edi
lea    -0x1c(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
mov    %r8,%rcx
call   1080 <select@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%esi
lea    0xd57(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x20(%rbp),%edi
lea    -0x1c(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     137b <test_monitor+0x1ab>
mov    -0x18(%rbp),%rdi
call   1040 <udev_monitor_receive_device@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1369 <test_monitor+0x199>
lea    0xd30(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    12ce <test_monitor+0xfe>
mov    -0x28(%rbp),%rdi
call   10f0 <print_device@plt>
mov    -0x28(%rbp),%rdi
call   10a0 <udev_device_unref@plt>
mov    0x2c5e(%rip),%rax
mov    (%rax),%edi
lea    -0x1c(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     13aa <test_monitor+0x1da>
lea    0xd06(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    13af <test_monitor+0x1df>
jmp    12ce <test_monitor+0xfe>
mov    -0x18(%rbp),%rdi
call   1050 <udev_monitor_unref@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_monitor>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
lea    0xe0e(%rip),%rsi
call   10c0 <udev_monitor_new_from_netlink@plt>
test   %rax,%rax
je     1333 <test_monitor+0x153>
mov    %rax,%r14
lea    0xdfb(%rip),%rsi
mov    %rax,%rdi
xor    %edx,%edx
call   10d0 <udev_monitor_filter_add_match_subsystem_devtype@plt>
test   %rax,%rax
js     132a <test_monitor+0x14a>
lea    0xde7(%rip),%rsi
mov    %r14,%rdi
xor    %edx,%edx
call   10d0 <udev_monitor_filter_add_match_subsystem_devtype@plt>
test   %rax,%rax
js     132a <test_monitor+0x14a>
lea    0xdd1(%rip),%rsi
lea    0xdce(%rip),%rdx
mov    %r14,%rdi
call   10d0 <udev_monitor_filter_add_match_subsystem_devtype@plt>
test   %rax,%rax
js     132a <test_monitor+0x14a>
mov    %r14,%rdi
call   10e0 <udev_monitor_enable_receiving@plt>
test   %rax,%rax
js     136b <test_monitor+0x18b>
mov    %r14,%rdi
call   10a0 <udev_monitor_get_fd@plt>
mov    %eax,%ebp
lea    0x4(%rsp),%rbx
mov    %rbx,%rdi
call   1080 <FD_ZERO@plt>
mov    %ebp,%r15d
add    $0x1,%r15d
mov    0x2d54(%rip),%r13
jmp    12b8 <test_monitor+0xd8>
xchg   %ax,%ax
mov    %rax,%r12
mov    %rax,%rdi
call   1100 <print_device@plt>
mov    %r12,%rdi
call   10b0 <udev_device_unref@plt>
mov    0x0(%r13),%edi
mov    %rbx,%rsi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
jne    1353 <test_monitor+0x173>
mov    0x0(%r13),%edi
mov    %rbx,%rsi
call   10f0 <FD_SET@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
call   10f0 <FD_SET@plt>
lea    0xd5e(%rip),%rdi
call   1030 <puts@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1090 <select@plt>
lea    0xd2b(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     12a3 <test_monitor+0xc3>
mov    %r14,%rdi
call   1050 <udev_monitor_receive_device@plt>
test   %rax,%rax
jne    1290 <test_monitor+0xb0>
lea    0xd4f(%rip),%rdi
call   1030 <puts@plt>
jmp    12b8 <test_monitor+0xd8>
lea    0xd63(%rip),%rdi
jmp    133a <test_monitor+0x15a>
lea    0xd68(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd0b(%rip),%rdi
call   1030 <puts@plt>
mov    %r14,%rdi
call   1060 <udev_monitor_unref@plt>
xor    %eax,%eax
jmp    1344 <test_monitor+0x164>
lea    0xd16(%rip),%rdi
jmp    133a <test_monitor+0x15a>

LLVM IR (-O0):
%struct.udev = type { i32 }
%struct.udev_monitor = type { i32 }
%struct.udev_device = type { i32 }
@.str = [5 x i8] c"udev\00"
@.str.1 = [11 x i8] c"no socket\0A\00"
@.str.2 = [6 x i8] c"block\00"
@.str.3 = [4 x i8] c"tty\00"
@.str.4 = [4 x i8] c"usb\00"
@.str.5 = [11 x i8] c"usb_device\00"
@.str.6 = [15 x i8] c"filter failed\0A\00"
@.str.7 = [13 x i8] c"bind failed\0A\00"
@STDIN_FILENO = global i32 0
@.str.8 = [51 x i8] c"waiting for events from udev, press ENTER to exit\0A\00"
@.str.9 = [21 x i8] c"select fd count: %i\0A\00"
@.str.10 = [23 x i8] c"no device from socket\0A\00"
@.str.11 = [14 x i8] c"exiting loop\0A\00"
define i32 @test_monitor(%struct.udev* %0) {
%2 = alloca i32
%3 = alloca %struct.udev*
%4 = alloca %struct.udev_monitor*
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.udev_device*
%8 = alloca i32
store %struct.udev* %0, %struct.udev** %3
%9 = load %struct.udev*, %struct.udev** %3
%10 = call %struct.udev_monitor* @udev_monitor_new_from_netlink(%struct.udev* %9, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store %struct.udev_monitor* %10, %struct.udev_monitor** %4
%11 = load %struct.udev_monitor*, %struct.udev_monitor** %4
%12 = icmp eq %struct.udev_monitor* %11, null
br i1 %12, label %13, label %15
13: ; preds = %1
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %75
15: ; preds = %1
%16 = load %struct.udev_monitor*, %struct.udev_monitor** %4
%17 = call i64 @udev_monitor_filter_add_match_subsystem_devtype(%struct.udev_monitor* %16, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* null)
%18 = icmp slt i64 %17, 0
br i1 %18, label %27, label %19
19: ; preds = %15
%20 = load %struct.udev_monitor*, %struct.udev_monitor** %4
%21 = call i64 @udev_monitor_filter_add_match_subsystem_devtype(%struct.udev_monitor* %20, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* null)
%22 = icmp slt i64 %21, 0
br i1 %22, label %27, label %23
23: ; preds = %19
%24 = load %struct.udev_monitor*, %struct.udev_monitor** %4
%25 = call i64 @udev_monitor_filter_add_match_subsystem_devtype(%struct.udev_monitor* %24, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0))
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %23, %19, %15
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0))
store i32 -1, i32* %2
br label %75
29: ; preds = %23
%30 = load %struct.udev_monitor*, %struct.udev_monitor** %4
%31 = call i64 @udev_monitor_enable_receiving(%struct.udev_monitor* %30)
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %29
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.7, i64 0, i64 0))
store i32 -1, i32* %2
br label %75
35: ; preds = %29
%36 = load %struct.udev_monitor*, %struct.udev_monitor** %4
%37 = call i32 @udev_monitor_get_fd(%struct.udev_monitor* %36)
store i32 %37, i32* %6
%38 = call i32 @FD_ZERO(i32* %5)
br label %39
39: ; preds = %35, %58, %71
%40 = load i32, i32* @STDIN_FILENO
%41 = call i32 @FD_SET(i32 %40, i32* %5)
%42 = load i32, i32* %6
%43 = call i32 @FD_SET(i32 %42, i32* %5)
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.8, i64 0, i64 0))
%45 = load i32, i32* %6
%46 = add nsw i32 %45, 1
%47 = call i32 @select(i32 %46, i32* %5, i32* null, i32* null, i32* null)
store i32 %47, i32* %8
%48 = load i32, i32* %8
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.9, i64 0, i64 0), i32 %48)
%50 = load i32, i32* %6
%51 = call i64 @FD_ISSET(i32 %50, i32* %5)
%52 = icmp ne i64 %51, 0
br i1 %52, label %53, label %65
53: ; preds = %39
%54 = load %struct.udev_monitor*, %struct.udev_monitor** %4
%55 = call %struct.udev_device* @udev_monitor_receive_device(%struct.udev_monitor* %54)
store %struct.udev_device* %55, %struct.udev_device** %7
%56 = load %struct.udev_device*, %struct.udev_device** %7
%57 = icmp eq %struct.udev_device* %56, null
br i1 %57, label %58, label %60
58: ; preds = %53
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.10, i64 0, i64 0))
br label %39
60: ; preds = %53
%61 = load %struct.udev_device*, %struct.udev_device** %7
%62 = call i32 @print_device(%struct.udev_device* %61)
%63 = load %struct.udev_device*, %struct.udev_device** %7
%64 = call i32 @udev_device_unref(%struct.udev_device* %63)
br label %65
65: ; preds = %60, %39
%66 = load i32, i32* @STDIN_FILENO
%67 = call i64 @FD_ISSET(i32 %66, i32* %5)
%68 = icmp ne i64 %67, 0
br i1 %68, label %69, label %71
69: ; preds = %65
%70 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.11, i64 0, i64 0))
br label %72
71: ; preds = %65
br label %39
72: ; preds = %69
%73 = load %struct.udev_monitor*, %struct.udev_monitor** %4
%74 = call i32 @udev_monitor_unref(%struct.udev_monitor* %73)
store i32 0, i32* %2
br label %75
75: ; preds = %72, %33, %27, %13
%76 = load i32, i32* %2
ret i32 %76
}

LLVM IR (-O2):
%struct.udev = type { i32 }
%struct.udev_monitor = type { i32 }
%struct.udev_device = type { i32 }
@.str = [5 x i8] c"udev\00"
@.str.2 = [6 x i8] c"block\00"
@.str.3 = [4 x i8] c"tty\00"
@.str.4 = [4 x i8] c"usb\00"
@.str.5 = [11 x i8] c"usb_device\00"
@STDIN_FILENO = global i32 0
@.str.9 = [21 x i8] c"select fd count: %i\0A\00"
@str = [50 x i8] c"waiting for events from udev, press ENTER to exit\00"
@str.12 = [13 x i8] c"exiting loop\00"
@str.13 = [22 x i8] c"no device from socket\00"
@str.14 = [12 x i8] c"bind failed\00"
@str.15 = [14 x i8] c"filter failed\00"
@str.16 = [10 x i8] c"no socket\00"
define i32 @test_monitor(%struct.udev* %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = tail call %struct.udev_monitor* @udev_monitor_new_from_netlink(%struct.udev* %0, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq %struct.udev_monitor* %4, null
br i1 %5, label %6, label %8
6: ; preds = %1
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str.16, i64 0, i64 0))
br label %53
8: ; preds = %1
%9 = tail call i64 @udev_monitor_filter_add_match_subsystem_devtype(%struct.udev_monitor* %4, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* null)
%10 = icmp slt i64 %9, 0
br i1 %10, label %17, label %11
11: ; preds = %8
%12 = tail call i64 @udev_monitor_filter_add_match_subsystem_devtype(%struct.udev_monitor* %4, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* null)
%13 = icmp slt i64 %12, 0
br i1 %13, label %17, label %14
14: ; preds = %11
%15 = tail call i64 @udev_monitor_filter_add_match_subsystem_devtype(%struct.udev_monitor* %4, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0))
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %14, %11, %8
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.15, i64 0, i64 0))
br label %53
19: ; preds = %14
%20 = tail call i64 @udev_monitor_enable_receiving(%struct.udev_monitor* %4)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %19
%23 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.14, i64 0, i64 0))
br label %53
24: ; preds = %19
%25 = tail call i32 @udev_monitor_get_fd(%struct.udev_monitor* %4)
%26 = call i32 @FD_ZERO(i32* %2)
%27 = add nsw i32 %25, 1
br label %28
28: ; preds = %49, %24
%29 = load i32, i32* @STDIN_FILENO
%30 = call i32 @FD_SET(i32 %29, i32* %2)
%31 = call i32 @FD_SET(i32 %25, i32* %2)
%32 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([50 x i8], [50 x i8]* @str, i64 0, i64 0))
%33 = call i32 @select(i32 %27, i32* %2, i32* null, i32* null, i32* null)
%34 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.9, i64 0, i64 0), i32 %33)
%35 = call i64 @FD_ISSET(i32 %25, i32* %2)
%36 = icmp eq i64 %35, 0
br i1 %36, label %45, label %37
37: ; preds = %28
%38 = call %struct.udev_device* @udev_monitor_receive_device(%struct.udev_monitor* %4)
%39 = icmp eq %struct.udev_device* %38, null
br i1 %39, label %40, label %42
40: ; preds = %37
%41 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str.13, i64 0, i64 0))
br label %49
42: ; preds = %37
%43 = call i32 @print_device(%struct.udev_device* %38)
%44 = call i32 @udev_device_unref(%struct.udev_device* %38)
br label %45
45: ; preds = %42, %28
%46 = load i32, i32* @STDIN_FILENO
%47 = call i64 @FD_ISSET(i32 %46, i32* %2)
%48 = icmp eq i64 %47, 0
br i1 %48, label %49, label %50
49: ; preds = %45, %40
br label %28
50: ; preds = %45
%51 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.12, i64 0, i64 0))
%52 = call i32 @udev_monitor_unref(%struct.udev_monitor* %4)
br label %53
53: ; preds = %50, %22, %17, %6
%54 = phi i32 [ -1, %6 ], [ -1, %17 ], [ -1, %22 ], [ 0, %50 ]
ret i32 %54
}