#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 */
scalar_t__ EINVAL;
scalar_t__ EPROTONOSUPPORT;
int FD_CLOEXEC;
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFD;
int /*<<< orphan*/ F_SETFL;
int O_NONBLOCK;
int SOCK_CLOEXEC;
int SOCK_NONBLOCK;
scalar_t__ errno;
int fcntl(int, int /*<<< orphan*/, ...);
int socketcall(int (*)(int, int, int), int, int, int, int /*<<< orphan*/,
               int /*<<< orphan*/, int /*<<< orphan*/);
int socket(int domain, int type, int protocol) {
  int s = socketcall(socket, domain, type, protocol, 0, 0, 0);
  if (s < 0 && (errno == EINVAL || errno == EPROTONOSUPPORT) &&
      (type & (SOCK_CLOEXEC | SOCK_NONBLOCK))) {
    s = socketcall(socket, domain, type & ~(SOCK_CLOEXEC | SOCK_NONBLOCK),
                   protocol, 0, 0, 0);
    if (s < 0)
      return s;
    if (type & SOCK_CLOEXEC)
      fcntl(s, F_SETFD, FD_CLOEXEC);
    if (type & SOCK_NONBLOCK)
      fcntl(s, F_SETFL, fcntl(s, F_GETFL) | O_NONBLOCK);
  }
  return s;
}


x86 assembly (clang, -O0):
<socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%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    0x2e97(%rip),%rdi
xor    %r9d,%r9d
mov    %r9d,%r8d
movl   $0x0,(%rsp)
call   1030 <socketcall@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    128d <socket+0x16d>
mov    0x2e39(%rip),%rax
mov    (%rax),%rax
mov    0x2e1f(%rip),%rcx
cmp    (%rcx),%rax
je     1194 <socket+0x74>
mov    0x2e1f(%rip),%rax
mov    (%rax),%rax
mov    0x2e2d(%rip),%rcx
cmp    (%rcx),%rax
jne    128d <socket+0x16d>
mov    -0xc(%rbp),%eax
mov    0x2e52(%rip),%rcx
mov    (%rcx),%ecx
mov    0x2e21(%rip),%rdx
or     (%rdx),%ecx
and    %ecx,%eax
cmp    $0x0,%eax
je     128d <socket+0x16d>
mov    -0x8(%rbp),%esi
mov    -0xc(%rbp),%edx
mov    0x2e2f(%rip),%rax
mov    (%rax),%eax
mov    0x2dfe(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    %eax,%edx
mov    -0x10(%rbp),%ecx
mov    0x2dfd(%rip),%rdi
xor    %r9d,%r9d
mov    %r9d,%r8d
movl   $0x0,(%rsp)
call   1030 <socketcall@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1205 <socket+0xe5>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1293 <socket+0x173>
mov    -0xc(%rbp),%eax
mov    0x2de1(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1236 <socket+0x116>
mov    -0x14(%rbp),%edi
mov    0x2d9c(%rip),%rax
mov    (%rax),%esi
mov    0x2d7b(%rip),%rax
mov    (%rax),%edx
mov    $0x0,%al
call   1040 <fcntl@plt>
mov    -0xc(%rbp),%eax
mov    0x2d88(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1288 <socket+0x168>
mov    -0x14(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2d58(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2d31(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1040 <fcntl@plt>
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%esi
mov    %eax,%edx
mov    0x2d61(%rip),%rax
or     (%rax),%edx
mov    $0x0,%al
call   1040 <fcntl@plt>
jmp    128d <socket+0x16d>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%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,%r15d
mov    %esi,%r14d
mov    %edi,%ebp
movl   $0x0,(%rsp)
mov    0x2e97(%rip),%rdi
mov    %ebp,%esi
mov    %r14d,%edx
mov    %r15d,%ecx
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1030 <socketcall@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    1224 <socket+0x104>
mov    0x2e3b(%rip),%rax
mov    (%rax),%rax
mov    0x2e21(%rip),%rcx
cmp    (%rcx),%rax
je     1184 <socket+0x64>
mov    0x2e3d(%rip),%rcx
cmp    (%rcx),%rax
jne    1224 <socket+0x104>
mov    0x2e65(%rip),%r13
mov    0x2e36(%rip),%r12
mov    (%r12),%edx
or     0x0(%r13),%edx
test   %r14d,%edx
je     1224 <socket+0x104>
not    %edx
and    %r14d,%edx
movl   $0x0,(%rsp)
mov    0x2e22(%rip),%rdi
mov    %ebp,%esi
mov    %r15d,%ecx
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1030 <socketcall@plt>
mov    %eax,%ebx
test   %eax,%eax
js     1224 <socket+0x104>
test   %r14d,0x0(%r13)
je     11ed <socket+0xcd>
mov    0x2de7(%rip),%rax
mov    (%rax),%esi
mov    0x2dc6(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
xor    %eax,%eax
call   1040 <fcntl@plt>
test   %r14d,(%r12)
je     1224 <socket+0x104>
mov    0x2db6(%rip),%rax
mov    (%rax),%ebp
mov    0x2d95(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %eax,%eax
call   1040 <fcntl@plt>
mov    0x2dcb(%rip),%rcx
or     (%rcx),%eax
mov    %ebx,%edi
mov    %ebp,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <fcntl@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@EINVAL = global i64 0
@EPROTONOSUPPORT = global i64 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
@F_GETFL = 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 = icmp slt i32 %13, 0
br i1 %14, label %15, label %69
15: ; preds = %3
%16 = load i64, i64* @errno
%17 = load i64, i64* @EINVAL
%18 = icmp eq i64 %16, %17
br i1 %18, label %23, label %19
19: ; preds = %15
%20 = load i64, i64* @errno
%21 = load i64, i64* @EPROTONOSUPPORT
%22 = icmp eq i64 %20, %21
br i1 %22, label %23, label %69
23: ; preds = %19, %15
%24 = load i32, i32* %6
%25 = load i32, i32* @SOCK_CLOEXEC
%26 = load i32, i32* @SOCK_NONBLOCK
%27 = or i32 %25, %26
%28 = and i32 %24, %27
%29 = icmp ne i32 %28, 0
br i1 %29, label %30, label %69
30: ; preds = %23
%31 = load i32, i32* %5
%32 = load i32, i32* %6
%33 = load i32, i32* @SOCK_CLOEXEC
%34 = load i32, i32* @SOCK_NONBLOCK
%35 = or i32 %33, %34
%36 = xor i32 %35, -1
%37 = and i32 %32, %36
%38 = load i32, i32* %7
%39 = call i32 @socketcall(i32 (i32, i32, i32)* @socket, i32 %31, i32 %37, i32 %38, i32 0, i32 0, i32 0)
store i32 %39, i32* %8
%40 = load i32, i32* %8
%41 = icmp slt i32 %40, 0
br i1 %41, label %42, label %44
42: ; preds = %30
%43 = load i32, i32* %8
store i32 %43, i32* %4
br label %71
44: ; preds = %30
%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 (i32, i32, ...) @fcntl(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 %68
59: ; preds = %54
%60 = load i32, i32* %8
%61 = load i32, i32* @F_SETFL
%62 = load i32, i32* %8
%63 = load i32, i32* @F_GETFL
%64 = call i32 (i32, i32, ...) @fcntl(i32 %62, i32 %63)
%65 = load i32, i32* @O_NONBLOCK
%66 = or i32 %64, %65
%67 = call i32 (i32, i32, ...) @fcntl(i32 %60, i32 %61, i32 %66)
br label %68
68: ; preds = %59, %54
br label %69
69: ; preds = %68, %23, %19, %3
%70 = load i32, i32* %8
store i32 %70, i32* %4
br label %71
71: ; preds = %69, %42
%72 = load i32, i32* %4
ret i32 %72
}

LLVM IR (-O2):
@errno = global i64 0
@EINVAL = global i64 0
@EPROTONOSUPPORT = global i64 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
@F_GETFL = 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 = icmp slt i32 %4, 0
br i1 %5, label %6, label %43
6: ; preds = %3
%7 = load i64, i64* @errno
%8 = load i64, i64* @EINVAL
%9 = icmp eq i64 %7, %8
%10 = load i64, i64* @EPROTONOSUPPORT
%11 = icmp eq i64 %7, %10
%12 = select i1 %9, i1 true, i1 %11
br i1 %12, label %13, label %43
13: ; preds = %6
%14 = load i32, i32* @SOCK_CLOEXEC
%15 = load i32, i32* @SOCK_NONBLOCK
%16 = or i32 %15, %14
%17 = and i32 %16, %1
%18 = icmp eq i32 %17, 0
br i1 %18, label %43, label %19
19: ; preds = %13
%20 = xor i32 %16, -1
%21 = and i32 %20, %1
%22 = tail call i32 @socketcall(i32 (i32, i32, i32)* @socket, i32 %0, i32 %21, i32 %2, i32 0, i32 0, i32 0)
%23 = icmp slt i32 %22, 0
br i1 %23, label %43, label %24
24: ; preds = %19
%25 = load i32, i32* @SOCK_CLOEXEC
%26 = and i32 %25, %1
%27 = icmp eq i32 %26, 0
br i1 %27, label %32, label %28
28: ; preds = %24
%29 = load i32, i32* @F_SETFD
%30 = load i32, i32* @FD_CLOEXEC
%31 = tail call i32 (i32, i32, ...) @fcntl(i32 %22, i32 %29, i32 %30)
br label %32
32: ; preds = %28, %24
%33 = load i32, i32* @SOCK_NONBLOCK
%34 = and i32 %33, %1
%35 = icmp eq i32 %34, 0
br i1 %35, label %43, label %36
36: ; preds = %32
%37 = load i32, i32* @F_SETFL
%38 = load i32, i32* @F_GETFL
%39 = tail call i32 (i32, i32, ...) @fcntl(i32 %22, i32 %38)
%40 = load i32, i32* @O_NONBLOCK
%41 = or i32 %40, %39
%42 = tail call i32 (i32, i32, ...) @fcntl(i32 %22, i32 %37, i32 %41)
br label %43
43: ; preds = %3, %13, %36, %32, %6, %19
%44 = phi i32 [ %22, %19 ], [ %22, %36 ], [ %22, %32 ], [ %4, %13 ], [ %4, %3 ], [ %4, %6 ]
ret i32 %44
}