#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 */
struct so_options {
  int dummy;
};

/* Variables and functions */
int SOCK_STREAM;
int errno;
int /*<<< orphan*/ so_closesocket(int *);
int so_opts2flags(struct so_options const *);
int so_setfl(int, int, int, int);
int socket(int, int, int /*<<< orphan*/);
int so_socket(int domain, int type, const struct so_options *opts,
              int *_error) {
  int error, fd, flags, mask, need;
  if (-1 == (fd = socket(domain, type, 0)))
    goto syerr;
  flags = so_opts2flags(opts);
  mask = (domain == SOCK_STREAM) ? ~0 : ~(0x08 | 0x10);
  need = ~(0x08 | 0x10 | 0x20);
  if ((error = so_setfl(fd, flags, mask, need)))
    goto error;
  return fd;
syerr:
  error = errno;
  goto error;
error:
  *_error = error;
  so_closesocket(&fd);
  return -1;
}


x86 assembly (clang, -O0):
<so_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
xor    %edx,%edx
call   1060 <socket@plt>
mov    %eax,%ecx
mov    %ecx,-0x28(%rbp)
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    117a <so_socket+0x3a>
jmp    11d8 <so_socket+0x98>
mov    -0x18(%rbp),%rdi
call   1050 <so_opts2flags@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x8(%rbp),%edx
mov    0x2e50(%rip),%rax
mov    (%rax),%esi
mov    $0xffffffe7,%eax
mov    $0xffffffff,%ecx
cmp    %esi,%edx
cmove  %ecx,%eax
mov    %eax,-0x30(%rbp)
movl   $0xffffffc7,-0x34(%rbp)
mov    -0x28(%rbp),%edi
mov    -0x2c(%rbp),%esi
mov    -0x30(%rbp),%edx
mov    -0x34(%rbp),%ecx
call   1040 <so_setfl@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
je     11cd <so_socket+0x8d>
jmp    11e4 <so_socket+0xa4>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11fd <so_socket+0xbd>
mov    0x2df9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
lea    -0x28(%rbp),%rdi
call   1030 <so_closesocket@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<so_socket>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %rdx,%r15
mov    %edi,%ebx
xor    %edx,%edx
call   1060 <socket@plt>
mov    %eax,0x4(%rsp)
cmp    $0xffffffff,%eax
je     1193 <so_socket+0x53>
mov    %eax,%ebp
mov    %r15,%rdi
call   1050 <so_opts2flags@plt>
mov    0x2e70(%rip),%rcx
cmp    %ebx,(%rcx)
mov    $0xffffffff,%ecx
mov    $0xffffffe7,%edx
cmove  %ecx,%edx
mov    %ebp,%edi
mov    %eax,%esi
mov    $0xffffffc7,%ecx
call   1040 <so_setfl@plt>
test   %eax,%eax
jne    119c <so_socket+0x5c>
jmp    11ae <so_socket+0x6e>
mov    0x2e3e(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%r14)
lea    0x4(%rsp),%rdi
call   1030 <so_closesocket@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.so_options = type { i32 }
@SOCK_STREAM = global i32 0
@errno = global i32 0
define i32 @so_socket(i32 %0, i32 %1, %struct.so_options* %2, i32* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.so_options*
%9 = alloca i32*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
store i32 %0, i32* %6
store i32 %1, i32* %7
store %struct.so_options* %2, %struct.so_options** %8
store i32* %3, i32** %9
%15 = load i32, i32* %6
%16 = load i32, i32* %7
%17 = call i32 @socket(i32 %15, i32 %16, i32 0)
store i32 %17, i32* %11
%18 = icmp eq i32 -1, %17
br i1 %18, label %19, label %20
19: ; preds = %4
br label %37
20: ; preds = %4
%21 = load %struct.so_options*, %struct.so_options** %8
%22 = call i32 @so_opts2flags(%struct.so_options* %21)
store i32 %22, i32* %12
%23 = load i32, i32* %6
%24 = load i32, i32* @SOCK_STREAM
%25 = icmp eq i32 %23, %24
%26 = zext i1 %25 to i64
%27 = select i1 %25, i32 -1, i32 -25
store i32 %27, i32* %13
store i32 -57, i32* %14
%28 = load i32, i32* %11
%29 = load i32, i32* %12
%30 = load i32, i32* %13
%31 = load i32, i32* %14
%32 = call i32 @so_setfl(i32 %28, i32 %29, i32 %30, i32 %31)
store i32 %32, i32* %10
%33 = icmp ne i32 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %20
br label %39
35: ; preds = %20
%36 = load i32, i32* %11
store i32 %36, i32* %5
br label %43
37: ; preds = %19
%38 = load i32, i32* @errno
store i32 %38, i32* %10
br label %39
39: ; preds = %37, %34
%40 = load i32, i32* %10
%41 = load i32*, i32** %9
store i32 %40, i32* %41
%42 = call i32 @so_closesocket(i32* %11)
store i32 -1, i32* %5
br label %43
43: ; preds = %39, %35
%44 = load i32, i32* %5
ret i32 %44
}

LLVM IR (-O2):
%struct.so_options = type { i32 }
@SOCK_STREAM = global i32 0
@errno = global i32 0
define i32 @so_socket(i32 %0, i32 %1, %struct.so_options* %2, i32* nocapture writeonly %3) {
%5 = alloca i32
%6 = bitcast i32* %5 to i8*
%7 = tail call i32 @socket(i32 %0, i32 %1, i32 0)
store i32 %7, i32* %5
%8 = icmp eq i32 %7, -1
br i1 %8, label %16, label %9
9: ; preds = %4
%10 = tail call i32 @so_opts2flags(%struct.so_options* %2)
%11 = load i32, i32* @SOCK_STREAM
%12 = icmp eq i32 %11, %0
%13 = select i1 %12, i32 -1, i32 -25
%14 = tail call i32 @so_setfl(i32 %7, i32 %10, i32 %13, i32 -57)
%15 = icmp eq i32 %14, 0
br i1 %15, label %21, label %18
16: ; preds = %4
%17 = load i32, i32* @errno
br label %18
18: ; preds = %9, %16
%19 = phi i32 [ %17, %16 ], [ %14, %9 ]
store i32 %19, i32* %3
%20 = call i32 @so_closesocket(i32* %5)
br label %21
21: ; preds = %9, %18
%22 = phi i32 [ -1, %18 ], [ %7, %9 ]
ret i32 %22
}