#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 */

/* Variables and functions */
int /*<<< orphan*/ EINVAL;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
scalar_t__ pev_sock_add(int, void (*)(int, void *), void *);
int socket(int, int, int);
int pev_sock_open(int domain, int type, int proto, void (*cb)(int, void *),
                  void *arg) {
  int sd;
  if (!cb) {
    errno = EINVAL;
    return -1;
  }
  sd = socket(domain, type, proto);
  if (sd < 0)
    return -1;
  if (pev_sock_add(sd, cb, arg) < 0) {
    close(sd);
    return -1;
  }
  return sd;
}


x86 assembly (clang, -O0):
<pev_sock_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
mov    %r8,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1172 <pev_sock_open+0x42>
mov    0x2e7d(%rip),%rax
mov    (%rax),%ecx
mov    0x2e7c(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11cd <pev_sock_open+0x9d>
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
call   1050 <socket@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1199 <pev_sock_open+0x69>
movl   $0xffffffff,-0x4(%rbp)
jmp    11cd <pev_sock_open+0x9d>
mov    -0x24(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1040 <pev_sock_add@plt>
cmp    $0x0,%rax
jge    11c7 <pev_sock_open+0x97>
mov    -0x24(%rbp),%edi
call   1030 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11cd <pev_sock_open+0x9d>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pev_sock_open>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %rcx,%rcx
je     116a <pev_sock_open+0x3a>
mov    %r8,%r14
mov    %rcx,%rbx
call   1050 <socket@plt>
mov    $0xffffffff,%r15d
test   %eax,%eax
js     118b <pev_sock_open+0x5b>
mov    %eax,%ebp
mov    %eax,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1040 <pev_sock_add@plt>
test   %rax,%rax
js     1184 <pev_sock_open+0x54>
mov    %ebp,%r15d
jmp    118b <pev_sock_open+0x5b>
mov    0x2e67(%rip),%rax
mov    (%rax),%eax
mov    0x2e66(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%r15d
jmp    118b <pev_sock_open+0x5b>
mov    %ebp,%edi
call   1030 <close@plt>
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@EINVAL = global i32 0
@errno = global i32 0
define i32 @pev_sock_open(i32 %0, i32 %1, i32 %2, void (i32, i8*)* %3, i8* %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca void (i32, i8*)*
%11 = alloca i8*
%12 = alloca i32
store i32 %0, i32* %7
store i32 %1, i32* %8
store i32 %2, i32* %9
store void (i32, i8*)* %3, void (i32, i8*)** %10
store i8* %4, i8** %11
%13 = load void (i32, i8*)*, void (i32, i8*)** %10
%14 = icmp ne void (i32, i8*)* %13, null
br i1 %14, label %17, label %15
15: ; preds = %5
%16 = load i32, i32* @EINVAL
store i32 %16, i32* @errno
store i32 -1, i32* %6
br label %36
17: ; preds = %5
%18 = load i32, i32* %7
%19 = load i32, i32* %8
%20 = load i32, i32* %9
%21 = call i32 @socket(i32 %18, i32 %19, i32 %20)
store i32 %21, i32* %12
%22 = load i32, i32* %12
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %17
store i32 -1, i32* %6
br label %36
25: ; preds = %17
%26 = load i32, i32* %12
%27 = load void (i32, i8*)*, void (i32, i8*)** %10
%28 = load i8*, i8** %11
%29 = call i64 @pev_sock_add(i32 %26, void (i32, i8*)* %27, i8* %28)
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %25
%32 = load i32, i32* %12
%33 = call i32 @close(i32 %32)
store i32 -1, i32* %6
br label %36
34: ; preds = %25
%35 = load i32, i32* %12
store i32 %35, i32* %6
br label %36
36: ; preds = %34, %31, %24, %15
%37 = load i32, i32* %6
ret i32 %37
}

LLVM IR (-O2):
@EINVAL = global i32 0
@errno = global i32 0
define i32 @pev_sock_open(i32 %0, i32 %1, i32 %2, void (i32, i8*)* %3, i8* %4) {
%6 = icmp eq void (i32, i8*)* %3, null
br i1 %6, label %7, label %9
7: ; preds = %5
%8 = load i32, i32* @EINVAL
store i32 %8, i32* @errno
br label %17
9: ; preds = %5
%10 = tail call i32 @socket(i32 %0, i32 %1, i32 %2)
%11 = icmp slt i32 %10, 0
br i1 %11, label %17, label %12
12: ; preds = %9
%13 = tail call i64 @pev_sock_add(i32 %10, void (i32, i8*)* %3, i8* %4)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = tail call i32 @close(i32 %10)
br label %17
17: ; preds = %12, %9, %15, %7
%18 = phi i32 [ -1, %15 ], [ -1, %7 ], [ -1, %9 ], [ %10, %12 ]
ret i32 %18
}