#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 EINVAL;
int EPROTONOSUPPORT;
int /*<<< orphan*/ FD_CLOEXEC;
int /*<<< orphan*/ F_SETFD;
int /*<<< orphan*/ F_SETFL;
int /*<<< orphan*/ O_NONBLOCK;
int SOCK_CLOEXEC;
int SOCK_NONBLOCK;
int __socketcall(int (*)(int, int, int), int, int, int, int /*<<< orphan*/,
                 int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ __syscall(int, int, int /*<<< orphan*/, int /*<<< orphan*/);
int __syscall_ret(int);
int socket(int domain, int type, int protocol) {
  int s = __socketcall(socket, domain, type, protocol, 0, 0, 0);
  if ((s == -EINVAL || s == -EPROTONOSUPPORT) &&
      (type & (SOCK_CLOEXEC | SOCK_NONBLOCK))) {
    s = __socketcall(socket, domain, type & ~(SOCK_CLOEXEC | SOCK_NONBLOCK),
                     protocol, 0, 0, 0);
    if (s < 0)
      return __syscall_ret(s);
    if (type & SOCK_CLOEXEC)
      __syscall(55, s, F_SETFD, FD_CLOEXEC);
    if (type & SOCK_NONBLOCK)
      __syscall(55, s, F_SETFL, O_NONBLOCK);
  }
  return __syscall_ret(s);
}


x86 assembly (clang, -O0):
<socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    -0x8(%rbp),%esi
mov    -0xc(%rbp),%edx
mov    -0x10(%rbp),%ecx
mov    0x2e87(%rip),%rdi
xor    %r9d,%r9d
mov    %r9d,%r8d
movl   $0x0,(%rsp)
call   1050 <__socketcall@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
xor    %ecx,%ecx
mov    0x2e2e(%rip),%rdx
sub    (%rdx),%ecx
cmp    %ecx,%eax
je     1192 <socket+0x62>
mov    -0x14(%rbp),%eax
xor    %ecx,%ecx
mov    0x2e30(%rip),%rdx
sub    (%rdx),%ecx
cmp    %ecx,%eax
jne    1275 <socket+0x145>
mov    -0xc(%rbp),%eax
mov    0x2e54(%rip),%rcx
mov    (%rcx),%ecx
mov    0x2e23(%rip),%rdx
or     (%rdx),%ecx
and    %ecx,%eax
cmp    $0x0,%eax
je     1275 <socket+0x145>
mov    -0x8(%rbp),%esi
mov    -0xc(%rbp),%edx
mov    0x2e31(%rip),%rax
mov    (%rax),%eax
mov    0x2e00(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    %eax,%edx
mov    -0x10(%rbp),%ecx
mov    0x2dff(%rip),%rdi
xor    %r9d,%r9d
mov    %r9d,%r8d
movl   $0x0,(%rsp)
call   1050 <__socketcall@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1208 <socket+0xd8>
mov    -0x14(%rbp),%edi
call   1040 <__syscall_ret@plt>
mov    %eax,-0x4(%rbp)
jmp    1280 <socket+0x150>
mov    -0xc(%rbp),%eax
mov    0x2dde(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     123c <socket+0x10c>
mov    -0x14(%rbp),%esi
mov    0x2d99(%rip),%rax
mov    (%rax),%edx
mov    0x2d78(%rip),%rax
mov    (%rax),%ecx
mov    $0x37,%edi
call   1030 <__syscall@plt>
mov    -0xc(%rbp),%eax
mov    0x2d82(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1270 <socket+0x140>
mov    -0x14(%rbp),%esi
mov    0x2d55(%rip),%rax
mov    (%rax),%edx
mov    0x2d7c(%rip),%rax
mov    (%rax),%ecx
mov    $0x37,%edi
call   1030 <__syscall@plt>
jmp    1275 <socket+0x145>
mov    -0x14(%rbp),%edi
call   1040 <__syscall_ret@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socket>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %esi,%ebx
mov    %edi,%r15d
movl   $0x0,(%rsp)
mov    0x2e87(%rip),%rdi
mov    %r15d,%esi
mov    %ebx,%edx
mov    %r14d,%ecx
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1050 <__socketcall@plt>
mov    %eax,%ebp
mov    0x2e33(%rip),%rax
mov    %ebp,%ecx
add    (%rax),%ecx
je     1184 <socket+0x54>
mov    0x2e3e(%rip),%rax
mov    (%rax),%eax
add    %ebp,%eax
jne    120f <socket+0xdf>
mov    0x2e65(%rip),%r13
mov    0x2e36(%rip),%r12
mov    (%r12),%edx
or     0x0(%r13),%edx
test   %ebx,%edx
je     120f <socket+0xdf>
not    %edx
and    %ebx,%edx
movl   $0x0,(%rsp)
mov    0x2e28(%rip),%rdi
mov    %r15d,%esi
mov    %r14d,%ecx
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1050 <__socketcall@plt>
mov    %eax,%ebp
test   %eax,%eax
js     120f <socket+0xdf>
test   %ebx,0x0(%r13)
je     11eb <socket+0xbb>
mov    0x2dec(%rip),%rax
mov    (%rax),%edx
mov    0x2dcb(%rip),%rax
mov    (%rax),%ecx
mov    $0x37,%edi
mov    %ebp,%esi
call   1030 <__syscall@plt>
test   %ebx,(%r12)
je     120f <socket+0xdf>
mov    0x2db8(%rip),%rax
mov    (%rax),%edx
mov    0x2ddf(%rip),%rax
mov    (%rax),%ecx
mov    $0x37,%edi
mov    %ebp,%esi
call   1030 <__syscall@plt>
mov    %ebp,%edi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <__syscall_ret@plt>

LLVM IR (-O0):
@EINVAL = global i32 0
@EPROTONOSUPPORT = global i32 0
@SOCK_CLOEXEC = global i32 0
@SOCK_NONBLOCK = global i32 0
@F_SETFD = global i32 0
@FD_CLOEXEC = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
define i32 @socket(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%9 = load i32, i32* %5
%10 = load i32, i32* %6
%11 = load i32, i32* %7
%12 = call i32 @__socketcall(i32 (i32, i32, i32)* @socket, i32 %9, i32 %10, i32 %11, i32 0, i32 0, i32 0)
store i32 %12, i32* %8
%13 = load i32, i32* %8
%14 = load i32, i32* @EINVAL
%15 = sub nsw i32 0, %14
%16 = icmp eq i32 %13, %15
br i1 %16, label %22, label %17
17: ; preds = %3
%18 = load i32, i32* %8
%19 = load i32, i32* @EPROTONOSUPPORT
%20 = sub nsw i32 0, %19
%21 = icmp eq i32 %18, %20
br i1 %21, label %22, label %65
22: ; preds = %17, %3
%23 = load i32, i32* %6
%24 = load i32, i32* @SOCK_CLOEXEC
%25 = load i32, i32* @SOCK_NONBLOCK
%26 = or i32 %24, %25
%27 = and i32 %23, %26
%28 = icmp ne i32 %27, 0
br i1 %28, label %29, label %65
29: ; preds = %22
%30 = load i32, i32* %5
%31 = load i32, i32* %6
%32 = load i32, i32* @SOCK_CLOEXEC
%33 = load i32, i32* @SOCK_NONBLOCK
%34 = or i32 %32, %33
%35 = xor i32 %34, -1
%36 = and i32 %31, %35
%37 = load i32, i32* %7
%38 = call i32 @__socketcall(i32 (i32, i32, i32)* @socket, i32 %30, i32 %36, i32 %37, i32 0, i32 0, i32 0)
store i32 %38, i32* %8
%39 = load i32, i32* %8
%40 = icmp slt i32 %39, 0
br i1 %40, label %41, label %44
41: ; preds = %29
%42 = load i32, i32* %8
%43 = call i32 @__syscall_ret(i32 %42)
store i32 %43, i32* %4
br label %68
44: ; preds = %29
%45 = load i32, i32* %6
%46 = load i32, i32* @SOCK_CLOEXEC
%47 = and i32 %45, %46
%48 = icmp ne i32 %47, 0
br i1 %48, label %49, label %54
49: ; preds = %44
%50 = load i32, i32* %8
%51 = load i32, i32* @F_SETFD
%52 = load i32, i32* @FD_CLOEXEC
%53 = call i32 @__syscall(i32 55, i32 %50, i32 %51, i32 %52)
br label %54
54: ; preds = %49, %44
%55 = load i32, i32* %6
%56 = load i32, i32* @SOCK_NONBLOCK
%57 = and i32 %55, %56
%58 = icmp ne i32 %57, 0
br i1 %58, label %59, label %64
59: ; preds = %54
%60 = load i32, i32* %8
%61 = load i32, i32* @F_SETFL
%62 = load i32, i32* @O_NONBLOCK
%63 = call i32 @__syscall(i32 55, i32 %60, i32 %61, i32 %62)
br label %64
64: ; preds = %59, %54
br label %65
65: ; preds = %64, %22, %17
%66 = load i32, i32* %8
%67 = call i32 @__syscall_ret(i32 %66)
store i32 %67, i32* %4
br label %68
68: ; preds = %65, %41
%69 = load i32, i32* %4
ret i32 %69
}

LLVM IR (-O2):
@EINVAL = global i32 0
@EPROTONOSUPPORT = global i32 0
@SOCK_CLOEXEC = global i32 0
@SOCK_NONBLOCK = global i32 0
@F_SETFD = global i32 0
@FD_CLOEXEC = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
define i32 @socket(i32 %0, i32 %1, i32 %2) {
%4 = tail call i32 @__socketcall(i32 (i32, i32, i32)* @socket, i32 %0, i32 %1, i32 %2, i32 0, i32 0, i32 0)
%5 = load i32, i32* @EINVAL
%6 = sub nsw i32 0, %5
%7 = icmp eq i32 %4, %6
br i1 %7, label %12, label %8
8: ; preds = %3
%9 = load i32, i32* @EPROTONOSUPPORT
%10 = sub nsw i32 0, %9
%11 = icmp eq i32 %4, %10
br i1 %11, label %12, label %39
12: ; preds = %8, %3
%13 = load i32, i32* @SOCK_CLOEXEC
%14 = load i32, i32* @SOCK_NONBLOCK
%15 = or i32 %14, %13
%16 = and i32 %15, %1
%17 = icmp eq i32 %16, 0
br i1 %17, label %39, label %18
18: ; preds = %12
%19 = xor i32 %15, -1
%20 = and i32 %19, %1
%21 = tail call i32 @__socketcall(i32 (i32, i32, i32)* @socket, i32 %0, i32 %20, i32 %2, i32 0, i32 0, i32 0)
%22 = icmp slt i32 %21, 0
br i1 %22, label %39, label %23
23: ; preds = %18
%24 = load i32, i32* @SOCK_CLOEXEC
%25 = and i32 %24, %1
%26 = icmp eq i32 %25, 0
br i1 %26, label %31, label %27
27: ; preds = %23
%28 = load i32, i32* @F_SETFD
%29 = load i32, i32* @FD_CLOEXEC
%30 = tail call i32 @__syscall(i32 55, i32 %21, i32 %28, i32 %29)
br label %31
31: ; preds = %27, %23
%32 = load i32, i32* @SOCK_NONBLOCK
%33 = and i32 %32, %1
%34 = icmp eq i32 %33, 0
br i1 %34, label %39, label %35
35: ; preds = %31
%36 = load i32, i32* @F_SETFL
%37 = load i32, i32* @O_NONBLOCK
%38 = tail call i32 @__syscall(i32 55, i32 %21, i32 %36, i32 %37)
br label %39
39: ; preds = %8, %12, %35, %31, %18
%40 = phi i32 [ %21, %18 ], [ %21, %35 ], [ %21, %31 ], [ %4, %12 ], [ %4, %8 ]
%41 = tail call i32 @__syscall_ret(i32 %40)
ret i32 %41
}