#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 */
typedef int /*<<< orphan*/ val;
struct sockaddr_in {
  int dummy;
};
struct sockaddr {
  int dummy;
};
struct linger {
  int /*<<< orphan*/ member_1;
  int /*<<< orphan*/ member_0;
};
typedef int /*<<< orphan*/ sockaddr;
typedef int /*<<< orphan*/ ling;

/* Variables and functions */
int /*<<< orphan*/ EINVAL;
int /*<<< orphan*/ FD_CLOEXEC;
int /*<<< orphan*/ F_SETFD;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SHUT_RDWR;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_LINGER;
int /*<<< orphan*/ SO_REUSEADDR;
int /*<<< orphan*/ TCP_NODELAY;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fcntl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ listen(int, int);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              void *, int);
int /*<<< orphan*/ shutdown(int, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int string2sockaddr(char const *, int, struct sockaddr_in *);
int open_socket(const char *host, int port) {
  int val = 1;
  struct sockaddr_in sockaddr;
  int sock;
  struct linger ling = {0, 0};
  errno = EINVAL;
  if ((host == NULL || !*host) && port == 0)
    return -1;
  sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
  if (sock == -1)
    return -1;
  setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
  setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &val, sizeof(val));
  setsockopt(sock, SOL_SOCKET, SO_LINGER, (void *)&ling, sizeof(ling));
  if (string2sockaddr(host, port, &sockaddr) == -1 ||
      bind(sock, (struct sockaddr *)&sockaddr, sizeof(sockaddr)) == -1) {
    shutdown(sock, SHUT_RDWR);
    close(sock);
    return -1;
  }
  listen(sock, -1);
  fcntl(sock, F_SETFD, FD_CLOEXEC);
  return sock;
}


x86 assembly (clang, -O0):
<open_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0x1,-0x18(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1070 <memset@plt>
mov    0x2dd3(%rip),%rax
mov    (%rax),%ecx
mov    0x2dda(%rip),%rax
mov    %ecx,(%rax)
cmpq   $0x0,-0x10(%rbp)
je     11e0 <open_socket+0x50>
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
jne    11f6 <open_socket+0x66>
cmpl   $0x0,-0x14(%rbp)
jne    11f6 <open_socket+0x66>
movl   $0xffffffff,-0x4(%rbp)
jmp    1320 <open_socket+0x190>
mov    0x2d9b(%rip),%rax
mov    (%rax),%edi
mov    0x2daa(%rip),%rax
mov    (%rax),%esi
mov    0x2db9(%rip),%rax
mov    (%rax),%edx
call   10b0 <socket@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    122f <open_socket+0x9f>
movl   $0xffffffff,-0x4(%rbp)
jmp    1320 <open_socket+0x190>
mov    -0x24(%rbp),%edi
mov    0x2da7(%rip),%rax
mov    (%rax),%esi
mov    0x2d46(%rip),%rax
mov    (%rax),%edx
lea    -0x18(%rbp),%rcx
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
mov    -0x24(%rbp),%edi
mov    0x2d6b(%rip),%rax
mov    (%rax),%esi
mov    0x2d72(%rip),%rax
mov    (%rax),%edx
lea    -0x18(%rbp),%rcx
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
mov    -0x24(%rbp),%edi
mov    0x2d5f(%rip),%rax
mov    (%rax),%esi
mov    0x2d46(%rip),%rax
mov    (%rax),%edx
lea    -0x30(%rbp),%rcx
mov    $0x8,%r8d
call   1040 <setsockopt@plt>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
lea    -0x20(%rbp),%rdx
call   1030 <string2sockaddr@plt>
cmp    $0xffffffff,%eax
je     12ce <open_socket+0x13e>
mov    -0x24(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x4,%edx
call   10a0 <bind@plt>
cmp    $0xffffffff,%eax
jne    12f3 <open_socket+0x163>
mov    -0x24(%rbp),%edi
mov    0x2d18(%rip),%rax
mov    (%rax),%esi
call   1060 <shutdown@plt>
mov    -0x24(%rbp),%edi
call   1080 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1320 <open_socket+0x190>
mov    -0x24(%rbp),%edi
mov    $0xffffffff,%esi
call   1090 <listen@plt>
mov    -0x24(%rbp),%edi
mov    0x2cae(%rip),%rax
mov    (%rax),%esi
mov    0x2c95(%rip),%rax
mov    (%rax),%edx
call   1050 <fcntl@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_socket>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %esi,%r15d
mov    %rdi,%rbx
movl   $0x1,0xc(%rsp)
movq   $0x0,0x10(%rsp)
mov    0x2de4(%rip),%rax
mov    (%rax),%eax
mov    0x2deb(%rip),%rcx
mov    %eax,(%rcx)
test   %rdi,%rdi
je     11d2 <open_socket+0x52>
test   %r15d,%r15d
jne    11db <open_socket+0x5b>
mov    $0xffffffff,%r14d
cmpb   $0x0,(%rbx)
je     12ea <open_socket+0x16a>
jmp    11db <open_socket+0x5b>
test   %r15d,%r15d
je     12e4 <open_socket+0x164>
mov    0x2db6(%rip),%rax
mov    (%rax),%edi
mov    0x2dc5(%rip),%rax
mov    (%rax),%esi
mov    0x2dd4(%rip),%r13
mov    0x0(%r13),%edx
call   10a0 <socket@plt>
mov    $0xffffffff,%r14d
cmp    $0xffffffff,%eax
je     12ea <open_socket+0x16a>
mov    %eax,%ebp
mov    0x2dcb(%rip),%rax
mov    (%rax),%esi
mov    0x2d6a(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%r12
mov    %ebp,%edi
mov    %r12,%rcx
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
mov    0x0(%r13),%esi
mov    0x2d98(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
mov    %r12,%rcx
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
mov    0x2d87(%rip),%rax
mov    (%rax),%esi
mov    0x2d6e(%rip),%rax
mov    (%rax),%edx
lea    0x10(%rsp),%rcx
mov    %ebp,%edi
mov    $0x8,%r8d
call   1040 <setsockopt@plt>
lea    0x8(%rsp),%rdx
mov    %rbx,%rdi
mov    %r15d,%esi
call   1030 <string2sockaddr@plt>
cmp    $0xffffffff,%eax
je     12cb <open_socket+0x14b>
lea    0x8(%rsp),%rsi
mov    %ebp,%edi
mov    $0x4,%edx
call   1090 <bind@plt>
cmp    $0xffffffff,%eax
je     12cb <open_socket+0x14b>
mov    %ebp,%edi
mov    $0xffffffff,%esi
call   1080 <listen@plt>
mov    0x2d04(%rip),%rax
mov    (%rax),%esi
mov    0x2ceb(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
call   1050 <fcntl@plt>
mov    %ebp,%r14d
jmp    12ea <open_socket+0x16a>
mov    0x2d1e(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1060 <shutdown@plt>
mov    %ebp,%edi
call   1070 <close@plt>
jmp    12ea <open_socket+0x16a>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.linger = type { i32, i32 }
%struct.sockaddr = type { i32 }
@EINVAL = global i32 0
@errno = global i32 0
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@TCP_NODELAY = global i32 0
@SO_LINGER = global i32 0
@SHUT_RDWR = global i32 0
@F_SETFD = global i32 0
@FD_CLOEXEC = global i32 0
define i32 @open_socket(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.sockaddr_in
%8 = alloca i32
%9 = alloca %struct.linger
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 1, i32* %6
%10 = bitcast %struct.linger* %9 to i8*
%11 = load i32, i32* @EINVAL
store i32 %11, i32* @errno
%12 = load i8*, i8** %4
%13 = icmp eq i8* %12, null
br i1 %13, label %18, label %14
14: ; preds = %2
%15 = load i8*, i8** %4
%16 = load i8, i8* %15
%17 = icmp ne i8 %16, 0
br i1 %17, label %22, label %18
18: ; preds = %14, %2
%19 = load i32, i32* %5
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %18
store i32 -1, i32* %3
br label %69
22: ; preds = %18, %14
%23 = load i32, i32* @PF_INET
%24 = load i32, i32* @SOCK_STREAM
%25 = load i32, i32* @IPPROTO_TCP
%26 = call i32 @socket(i32 %23, i32 %24, i32 %25)
store i32 %26, i32* %8
%27 = load i32, i32* %8
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %30
29: ; preds = %22
store i32 -1, i32* %3
br label %69
30: ; preds = %22
%31 = load i32, i32* %8
%32 = load i32, i32* @SOL_SOCKET
%33 = load i32, i32* @SO_REUSEADDR
%34 = bitcast i32* %6 to i8*
%35 = call i32 @setsockopt(i32 %31, i32 %32, i32 %33, i8* %34, i32 4)
%36 = load i32, i32* %8
%37 = load i32, i32* @IPPROTO_TCP
%38 = load i32, i32* @TCP_NODELAY
%39 = bitcast i32* %6 to i8*
%40 = call i32 @setsockopt(i32 %36, i32 %37, i32 %38, i8* %39, i32 4)
%41 = load i32, i32* %8
%42 = load i32, i32* @SOL_SOCKET
%43 = load i32, i32* @SO_LINGER
%44 = bitcast %struct.linger* %9 to i8*
%45 = call i32 @setsockopt(i32 %41, i32 %42, i32 %43, i8* %44, i32 8)
%46 = load i8*, i8** %4
%47 = load i32, i32* %5
%48 = call i32 @string2sockaddr(i8* %46, i32 %47, %struct.sockaddr_in* %7)
%49 = icmp eq i32 %48, -1
br i1 %49, label %55, label %50
50: ; preds = %30
%51 = load i32, i32* %8
%52 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%53 = call i32 @bind(i32 %51, %struct.sockaddr* %52, i32 4)
%54 = icmp eq i32 %53, -1
br i1 %54, label %55, label %61
55: ; preds = %50, %30
%56 = load i32, i32* %8
%57 = load i32, i32* @SHUT_RDWR
%58 = call i32 @shutdown(i32 %56, i32 %57)
%59 = load i32, i32* %8
%60 = call i32 @close(i32 %59)
store i32 -1, i32* %3
br label %69
61: ; preds = %50
%62 = load i32, i32* %8
%63 = call i32 @listen(i32 %62, i32 -1)
%64 = load i32, i32* %8
%65 = load i32, i32* @F_SETFD
%66 = load i32, i32* @FD_CLOEXEC
%67 = call i32 @fcntl(i32 %64, i32 %65, i32 %66)
%68 = load i32, i32* %8
store i32 %68, i32* %3
br label %69
69: ; preds = %61, %55, %29, %21
%70 = load i32, i32* %3
ret i32 %70
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@EINVAL = global i32 0
@errno = global i32 0
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@TCP_NODELAY = global i32 0
@SO_LINGER = global i32 0
@SHUT_RDWR = global i32 0
@F_SETFD = global i32 0
@FD_CLOEXEC = global i32 0
define i32 @open_socket(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = alloca i64
%6 = bitcast i32* %3 to i8*
store i32 1, i32* %3
%7 = bitcast %struct.sockaddr_in* %4 to i8*
%8 = bitcast i64* %5 to i8*
store i64 0, i64* %5
%9 = load i32, i32* @EINVAL
store i32 %9, i32* @errno
%10 = icmp eq i8* %0, null
br i1 %10, label %16, label %11
11: ; preds = %2
%12 = load i8, i8* %0
%13 = icmp eq i8 %12, 0
%14 = icmp eq i32 %1, 0
%15 = and i1 %14, %13
br i1 %15, label %49, label %18
16: ; preds = %2
%17 = icmp eq i32 %1, 0
br i1 %17, label %49, label %18
18: ; preds = %16, %11
%19 = load i32, i32* @PF_INET
%20 = load i32, i32* @SOCK_STREAM
%21 = load i32, i32* @IPPROTO_TCP
%22 = tail call i32 @socket(i32 %19, i32 %20, i32 %21)
%23 = icmp eq i32 %22, -1
br i1 %23, label %49, label %24
24: ; preds = %18
%25 = load i32, i32* @SOL_SOCKET
%26 = load i32, i32* @SO_REUSEADDR
%27 = call i32 @setsockopt(i32 %22, i32 %25, i32 %26, i8* %6, i32 4)
%28 = load i32, i32* @IPPROTO_TCP
%29 = load i32, i32* @TCP_NODELAY
%30 = call i32 @setsockopt(i32 %22, i32 %28, i32 %29, i8* %6, i32 4)
%31 = load i32, i32* @SOL_SOCKET
%32 = load i32, i32* @SO_LINGER
%33 = call i32 @setsockopt(i32 %22, i32 %31, i32 %32, i8* %8, i32 8)
%34 = call i32 @string2sockaddr(i8* %0, i32 %1, %struct.sockaddr_in* %4)
%35 = icmp eq i32 %34, -1
br i1 %35, label %40, label %36
36: ; preds = %24
%37 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%38 = call i32 @bind(i32 %22, %struct.sockaddr* %37, i32 4)
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %44
40: ; preds = %36, %24
%41 = load i32, i32* @SHUT_RDWR
%42 = call i32 @shutdown(i32 %22, i32 %41)
%43 = call i32 @close(i32 %22)
br label %49
44: ; preds = %36
%45 = call i32 @listen(i32 %22, i32 -1)
%46 = load i32, i32* @F_SETFD
%47 = load i32, i32* @FD_CLOEXEC
%48 = call i32 @fcntl(i32 %22, i32 %46, i32 %47)
br label %49
49: ; preds = %18, %16, %11, %44, %40
%50 = phi i32 [ -1, %40 ], [ %22, %44 ], [ -1, %11 ], [ -1, %16 ], [ -1, %18 ]
ret i32 %50
}