#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 FD_CLOEXEC;
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int O_NONBLOCK;
int /*<<< orphan*/ close(int);
scalar_t__ fcntl(int, int /*<<< orphan*/, ...);
int socket(int, int, int /*<<< orphan*/);
int eh_socket(int family, int type, bool cloexec, bool nonblock) {
  int fd;
  if ((fd = socket(family, type, 0)) < 0)
    goto socket_done;
  if (cloexec || nonblock) {
    int fl2, fl = fcntl(fd, F_GETFL);
    if (fl < 0)
      goto socket_failed;
    fl2 = fl;
    if (cloexec)
      fl2 |= FD_CLOEXEC;
    if (nonblock)
      fl2 |= O_NONBLOCK;
    if (fl != fl2 && fcntl(fd, F_SETFL, fl2) < 0)
      goto socket_failed;
    goto socket_done;
  }
socket_failed:
  close(fd);
  fd = -1;
socket_done:
  return fd;
}


x86 assembly (clang, -O0):
<eh_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    %ecx,-0x10(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
xor    %edx,%edx
call   1050 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    1162 <eh_socket+0x32>
jmp    121d <eh_socket+0xed>
cmpl   $0x0,-0xc(%rbp)
jne    1176 <eh_socket+0x46>
cmpl   $0x0,-0x10(%rbp)
je     1209 <eh_socket+0xd9>
mov    -0x14(%rbp),%edi
mov    0x2e48(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    119b <eh_socket+0x6b>
jmp    120e <eh_socket+0xde>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     11ba <eh_socket+0x8a>
mov    0x2e1e(%rip),%rax
mov    (%rax),%eax
or     -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x10(%rbp)
je     11d3 <eh_socket+0xa3>
mov    0x2e1d(%rip),%rax
mov    (%rax),%eax
or     -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x18(%rbp),%eax
je     1204 <eh_socket+0xd4>
mov    -0x14(%rbp),%edi
mov    0x2def(%rip),%rax
mov    (%rax),%esi
mov    -0x18(%rbp),%edx
mov    $0x0,%al
call   1030 <fcntl@plt>
cmp    $0x0,%rax
jge    1204 <eh_socket+0xd4>
jmp    120e <eh_socket+0xde>
jmp    121d <eh_socket+0xed>
jmp    120e <eh_socket+0xde>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x14(%rbp)
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<eh_socket>:
push   %rbp
push   %r14
push   %rbx
mov    %ecx,%r14d
mov    %edx,%ebp
xor    %edx,%edx
call   1050 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
js     11ad <eh_socket+0x7d>
mov    %r14d,%eax
or     %ebp,%eax
je     11a1 <eh_socket+0x71>
mov    0x2e74(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %eax,%eax
call   1030 <fcntl@plt>
test   %eax,%eax
js     11a1 <eh_socket+0x71>
test   %ebp,%ebp
je     1170 <eh_socket+0x40>
mov    0x2e62(%rip),%rcx
mov    (%rcx),%ebp
or     %eax,%ebp
test   %r14d,%r14d
je     1181 <eh_socket+0x51>
mov    0x2e6a(%rip),%rcx
mov    (%rcx),%r14d
or     %r14d,%ebp
cmp    %eax,%ebp
je     11ad <eh_socket+0x7d>
mov    0x2e49(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %ebp,%edx
xor    %eax,%eax
call   1030 <fcntl@plt>
test   %rax,%rax
jns    11ad <eh_socket+0x7d>
mov    %ebx,%edi
call   1040 <close@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@F_GETFL = global i32 0
@FD_CLOEXEC = global i32 0
@O_NONBLOCK = global i32 0
@F_SETFL = global i32 0
define i32 @eh_socket(i32 %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32 %3, i32* %8
%12 = load i32, i32* %5
%13 = load i32, i32* %6
%14 = call i32 @socket(i32 %12, i32 %13, i32 0)
store i32 %14, i32* %9
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %4
br label %62
17: ; preds = %4
%18 = load i32, i32* %7
%19 = icmp ne i32 %18, 0
br i1 %19, label %23, label %20
20: ; preds = %17
%21 = load i32, i32* %8
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %58
23: ; preds = %20, %17
%24 = load i32, i32* %9
%25 = load i32, i32* @F_GETFL
%26 = call i64 (i32, i32, ...) @fcntl(i32 %24, i32 %25)
%27 = trunc i64 %26 to i32
store i32 %27, i32* %11
%28 = load i32, i32* %11
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %23
br label %59
31: ; preds = %23
%32 = load i32, i32* %11
store i32 %32, i32* %10
%33 = load i32, i32* %7
%34 = icmp ne i32 %33, 0
br i1 %34, label %35, label %39
35: ; preds = %31
%36 = load i32, i32* @FD_CLOEXEC
%37 = load i32, i32* %10
%38 = or i32 %37, %36
store i32 %38, i32* %10
br label %39
39: ; preds = %35, %31
%40 = load i32, i32* %8
%41 = icmp ne i32 %40, 0
br i1 %41, label %42, label %46
42: ; preds = %39
%43 = load i32, i32* @O_NONBLOCK
%44 = load i32, i32* %10
%45 = or i32 %44, %43
store i32 %45, i32* %10
br label %46
46: ; preds = %42, %39
%47 = load i32, i32* %11
%48 = load i32, i32* %10
%49 = icmp ne i32 %47, %48
br i1 %49, label %50, label %57
50: ; preds = %46
%51 = load i32, i32* %9
%52 = load i32, i32* @F_SETFL
%53 = load i32, i32* %10
%54 = call i64 (i32, i32, ...) @fcntl(i32 %51, i32 %52, i32 %53)
%55 = icmp slt i64 %54, 0
br i1 %55, label %56, label %57
56: ; preds = %50
br label %59
57: ; preds = %50, %46
br label %62
58: ; preds = %20
br label %59
59: ; preds = %58, %56, %30
%60 = load i32, i32* %9
%61 = call i32 @close(i32 %60)
store i32 -1, i32* %9
br label %62
62: ; preds = %59, %57, %16
%63 = load i32, i32* %9
ret i32 %63
}

LLVM IR (-O2):
@F_GETFL = global i32 0
@FD_CLOEXEC = global i32 0
@O_NONBLOCK = global i32 0
@F_SETFL = global i32 0
define i32 @eh_socket(i32 %0, i32 %1, i32 %2, i32 %3) {
%5 = tail call i32 @socket(i32 %0, i32 %1, i32 0)
%6 = icmp slt i32 %5, 0
br i1 %6, label %31, label %7
7: ; preds = %4
%8 = icmp eq i32 %2, 0
%9 = icmp eq i32 %3, 0
%10 = or i32 %3, %2
%11 = icmp eq i32 %10, 0
br i1 %11, label %29, label %12
12: ; preds = %7
%13 = load i32, i32* @F_GETFL
%14 = tail call i64 (i32, i32, ...) @fcntl(i32 %5, i32 %13)
%15 = trunc i64 %14 to i32
%16 = icmp slt i32 %15, 0
br i1 %16, label %29, label %17
17: ; preds = %12
%18 = load i32, i32* @FD_CLOEXEC
%19 = select i1 %8, i32 0, i32 %18
%20 = or i32 %19, %15
%21 = load i32, i32* @O_NONBLOCK
%22 = select i1 %9, i32 0, i32 %21
%23 = or i32 %20, %22
%24 = icmp eq i32 %23, %15
br i1 %24, label %31, label %25
25: ; preds = %17
%26 = load i32, i32* @F_SETFL
%27 = tail call i64 (i32, i32, ...) @fcntl(i32 %5, i32 %26, i32 %23)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %25, %12, %7
%30 = tail call i32 @close(i32 %5)
br label %31
31: ; preds = %25, %17, %29, %4
%32 = phi i32 [ %5, %4 ], [ -1, %29 ], [ %5, %17 ], [ %5, %25 ]
ret i32 %32
}