#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_GETFD;
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFD;
int /*<<< orphan*/ F_SETFL;
int O_NONBLOCK;
int fcntl(int, int /*<<< orphan*/, ...);
int socket(int, int, int);
int _socket(int domain, int type, int protocol) {
  int s = socket(domain, type & ~(0x8000 | 0x4000), protocol);
  int flags;
  if (s == -1)
    return s;
  if (type & 0x8000) {
    flags = fcntl(s, F_GETFD);
    fcntl(s, F_SETFD, flags | FD_CLOEXEC);
  }
  if (type & 0x4000) {
    flags = fcntl(s, F_GETFL);
    fcntl(s, F_SETFL, flags | O_NONBLOCK);
  }
  return 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),%edi
mov    -0xc(%rbp),%esi
and    $0xffff3fff,%esi
mov    -0x10(%rbp),%edx
call   1040 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    115d <_socket+0x3d>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11ef <_socket+0xcf>
mov    -0xc(%rbp),%eax
and    $0x8000,%eax
cmp    $0x0,%eax
je     11a3 <_socket+0x83>
mov    -0x14(%rbp),%edi
mov    0x2e48(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2e4a(%rip),%rax
mov    (%rax),%esi
mov    -0x18(%rbp),%edx
mov    0x2e2e(%rip),%rax
or     (%rax),%edx
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    -0xc(%rbp),%eax
and    $0x4000,%eax
cmp    $0x0,%eax
je     11e9 <_socket+0xc9>
mov    -0x14(%rbp),%edi
mov    0x2dfa(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2dfc(%rip),%rax
mov    (%rax),%esi
mov    -0x18(%rbp),%edx
mov    0x2e08(%rip),%rax
or     (%rax),%edx
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_socket>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
and    $0xffff3fff,%esi
call   1040 <socket@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     11a5 <_socket+0x85>
test   $0x8000,%ebp
je     116e <_socket+0x4e>
mov    0x2e7a(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %eax,%eax
call   1030 <fcntl@plt>
mov    0x2e80(%rip),%rcx
mov    (%rcx),%esi
mov    0x2e67(%rip),%rcx
or     (%rcx),%eax
mov    %ebx,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <fcntl@plt>
test   $0x4000,%ebp
je     11a5 <_socket+0x85>
mov    0x2e3b(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %eax,%eax
call   1030 <fcntl@plt>
mov    0x2e41(%rip),%rcx
mov    (%rcx),%esi
mov    0x2e50(%rip),%rcx
or     (%rcx),%eax
mov    %ebx,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <fcntl@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@F_GETFD = global i32 0
@F_SETFD = global i32 0
@FD_CLOEXEC = global i32 0
@F_GETFL = 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
%9 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%10 = load i32, i32* %5
%11 = load i32, i32* %6
%12 = and i32 %11, -49153
%13 = load i32, i32* %7
%14 = call i32 @socket(i32 %10, i32 %12, i32 %13)
store i32 %14, i32* %8
%15 = load i32, i32* %8
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %19
17: ; preds = %3
%18 = load i32, i32* %8
store i32 %18, i32* %4
br label %49
19: ; preds = %3
%20 = load i32, i32* %6
%21 = and i32 %20, 32768
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %33
23: ; preds = %19
%24 = load i32, i32* %8
%25 = load i32, i32* @F_GETFD
%26 = call i32 (i32, i32, ...) @fcntl(i32 %24, i32 %25)
store i32 %26, i32* %9
%27 = load i32, i32* %8
%28 = load i32, i32* @F_SETFD
%29 = load i32, i32* %9
%30 = load i32, i32* @FD_CLOEXEC
%31 = or i32 %29, %30
%32 = call i32 (i32, i32, ...) @fcntl(i32 %27, i32 %28, i32 %31)
br label %33
33: ; preds = %23, %19
%34 = load i32, i32* %6
%35 = and i32 %34, 16384
%36 = icmp ne i32 %35, 0
br i1 %36, label %37, label %47
37: ; preds = %33
%38 = load i32, i32* %8
%39 = load i32, i32* @F_GETFL
%40 = call i32 (i32, i32, ...) @fcntl(i32 %38, i32 %39)
store i32 %40, i32* %9
%41 = load i32, i32* %8
%42 = load i32, i32* @F_SETFL
%43 = load i32, i32* %9
%44 = load i32, i32* @O_NONBLOCK
%45 = or i32 %43, %44
%46 = call i32 (i32, i32, ...) @fcntl(i32 %41, i32 %42, i32 %45)
br label %47
47: ; preds = %37, %33
%48 = load i32, i32* %8
store i32 %48, i32* %4
br label %49
49: ; preds = %47, %17
%50 = load i32, i32* %4
ret i32 %50
}

LLVM IR (-O2):
@F_GETFD = global i32 0
@F_SETFD = global i32 0
@FD_CLOEXEC = global i32 0
@F_GETFL = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
define i32 @_socket(i32 %0, i32 %1, i32 %2) {
%4 = and i32 %1, -49153
%5 = tail call i32 @socket(i32 %0, i32 %4, i32 %2)
%6 = icmp eq i32 %5, -1
br i1 %6, label %27, label %7
7: ; preds = %3
%8 = and i32 %1, 32768
%9 = icmp eq i32 %8, 0
br i1 %9, label %17, label %10
10: ; preds = %7
%11 = load i32, i32* @F_GETFD
%12 = tail call i32 (i32, i32, ...) @fcntl(i32 %5, i32 %11)
%13 = load i32, i32* @F_SETFD
%14 = load i32, i32* @FD_CLOEXEC
%15 = or i32 %14, %12
%16 = tail call i32 (i32, i32, ...) @fcntl(i32 %5, i32 %13, i32 %15)
br label %17
17: ; preds = %10, %7
%18 = and i32 %1, 16384
%19 = icmp eq i32 %18, 0
br i1 %19, label %27, label %20
20: ; preds = %17
%21 = load i32, i32* @F_GETFL
%22 = tail call i32 (i32, i32, ...) @fcntl(i32 %5, i32 %21)
%23 = load i32, i32* @F_SETFL
%24 = load i32, i32* @O_NONBLOCK
%25 = or i32 %24, %22
%26 = tail call i32 (i32, i32, ...) @fcntl(i32 %5, i32 %23, i32 %25)
br label %27
27: ; preds = %17, %20, %3
ret i32 %5
}