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

/* Variables and functions */
int /*<<< orphan*/ AF_NETLINK;
int /*<<< orphan*/ NETLINK_KOBJECT_UEVENT;
int /*<<< orphan*/ PF_NETLINK;
int /*<<< orphan*/ SOCK_DGRAM;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ getpid();
int /*<<< orphan*/ memset(struct sockaddr_nl *, int, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int om_uevent_open() {
  int sock, retval;
  struct sockaddr_nl snl;
  memset(&snl, 0x00, sizeof(struct sockaddr_nl));
  snl.nl_family = AF_NETLINK;
  snl.nl_pid = getpid();
  snl.nl_groups = 0xffffffff;
  sock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
  if (sock == -1)
    return sock;
  retval = bind(sock, (struct sockaddr *)&snl, sizeof(struct sockaddr_nl));
  if (retval < 0)
    return retval;
  return sock;
}


x86 assembly (clang, -O0):
<om_uevent_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    0x2e79(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x14(%rbp)
movl   $0xffffffff,-0x18(%rbp)
mov    0x2e4c(%rip),%rax
mov    (%rax),%edi
mov    0x2e5b(%rip),%rax
mov    (%rax),%esi
mov    0x2e5a(%rip),%rax
mov    (%rax),%edx
call   1060 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11ad <om_uevent_open+0x6d>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11dc <om_uevent_open+0x9c>
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1050 <bind@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11d6 <om_uevent_open+0x96>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11dc <om_uevent_open+0x9c>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<om_uevent_open>:
push   %rbx
sub    $0x10,%rsp
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    0x2e7d(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,0x4(%rsp)
movl   $0xffffffff,(%rsp)
mov    0x2e4e(%rip),%rax
mov    (%rax),%edi
mov    0x2e5d(%rip),%rax
mov    (%rax),%esi
mov    0x2e5c(%rip),%rax
mov    (%rax),%edx
call   1060 <socket@plt>
cmp    $0xffffffff,%eax
je     11b0 <om_uevent_open+0x70>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0xc,%edx
call   1050 <bind@plt>
test   %eax,%eax
cmovs  %eax,%ebx
jmp    11b5 <om_uevent_open+0x75>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.sockaddr_nl = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_NETLINK = global i32 0
@PF_NETLINK = global i32 0
@SOCK_DGRAM = global i32 0
@NETLINK_KOBJECT_UEVENT = global i32 0
define i32 @om_uevent_open() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.sockaddr_nl
%5 = call i32 @memset(%struct.sockaddr_nl* %4, i32 0, i32 12)
%6 = load i32, i32* @AF_NETLINK
%7 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %4, i32 0, i32 2
store i32 %6, i32* %7
%8 = call i32 (...) @getpid()
%9 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %4, i32 0, i32 1
store i32 %8, i32* %9
%10 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %4, i32 0, i32 0
store i32 -1, i32* %10
%11 = load i32, i32* @PF_NETLINK
%12 = load i32, i32* @SOCK_DGRAM
%13 = load i32, i32* @NETLINK_KOBJECT_UEVENT
%14 = call i32 @socket(i32 %11, i32 %12, i32 %13)
store i32 %14, i32* %2
%15 = load i32, i32* %2
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %19
17: ; preds = %0
%18 = load i32, i32* %2
store i32 %18, i32* %1
br label %29
19: ; preds = %0
%20 = load i32, i32* %2
%21 = bitcast %struct.sockaddr_nl* %4 to %struct.sockaddr*
%22 = call i32 @bind(i32 %20, %struct.sockaddr* %21, i32 12)
store i32 %22, i32* %3
%23 = load i32, i32* %3
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %19
%26 = load i32, i32* %3
store i32 %26, i32* %1
br label %29
27: ; preds = %19
%28 = load i32, i32* %2
store i32 %28, i32* %1
br label %29
29: ; preds = %27, %25, %17
%30 = load i32, i32* %1
ret i32 %30
}

LLVM IR (-O2):
%struct.sockaddr_nl = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_NETLINK = global i32 0
@PF_NETLINK = global i32 0
@SOCK_DGRAM = global i32 0
@NETLINK_KOBJECT_UEVENT = global i32 0
define i32 @om_uevent_open() {
%1 = alloca %struct.sockaddr_nl
%2 = bitcast %struct.sockaddr_nl* %1 to i8*
%3 = call i32 @memset(%struct.sockaddr_nl* %1, i32 0, i32 12)
%4 = load i32, i32* @AF_NETLINK
%5 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %1, i64 0, i32 2
store i32 %4, i32* %5
%6 = call i32 (...) @getpid()
%7 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %1, i64 0, i32 1
store i32 %6, i32* %7
%8 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %1, i64 0, i32 0
store i32 -1, i32* %8
%9 = load i32, i32* @PF_NETLINK
%10 = load i32, i32* @SOCK_DGRAM
%11 = load i32, i32* @NETLINK_KOBJECT_UEVENT
%12 = call i32 @socket(i32 %9, i32 %10, i32 %11)
%13 = icmp eq i32 %12, -1
br i1 %13, label %19, label %14
14: ; preds = %0
%15 = bitcast %struct.sockaddr_nl* %1 to %struct.sockaddr*
%16 = call i32 @bind(i32 %12, %struct.sockaddr* %15, i32 12)
%17 = icmp slt i32 %16, 0
%18 = select i1 %17, i32 %16, i32 %12
br label %19
19: ; preds = %14, %0
%20 = phi i32 [ -1, %0 ], [ %18, %14 ]
ret i32 %20
}