#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ sin;
typedef int /*<<< orphan*/ on;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ PF_INET;
int SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int listen(int, int);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, int, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int do_open_server_socket(int port, int connection_type) {
  int sock;
  struct sockaddr_in sin;
  sock = socket(PF_INET, connection_type, 0);
  if (sock == -1) {
    fprintf(stderr, "socket failed:  %s\n", strerror(errno));
    return -1;
  }
  {
    int on = 1;
    if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1) {
      return -1;
    }
  }
  sin.sin_family = AF_INET;
  sin.sin_addr.s_addr = htonl(INADDR_ANY);
  sin.sin_port = htons(port);
  if (bind(sock, (struct sockaddr *)&sin, sizeof(sin)) == -1) {
    fprintf(stderr, "bind failed:  %s\n", strerror(errno));
    return -1;
  }
  if (connection_type == SOCK_STREAM) {
    if (listen(sock, 5) == -1) {
      fprintf(stderr, "listen failed:  %s\n", strerror(errno));
      return -1;
    }
  }
  return sock;
}


x86 assembly (clang, -O0):
<do_open_server_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    0x2e1b(%rip),%rax
mov    (%rax),%edi
mov    -0xc(%rbp),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    11e6 <do_open_server_socket+0x66>
mov    0x2e13(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2df7(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x28(%rbp),%edi
mov    %rax,%rdx
lea    0xe2b(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    130b <do_open_server_socket+0x18b>
movl   $0x1,-0x24(%rbp)
mov    -0x10(%rbp),%edi
mov    0x2df1(%rip),%rax
mov    (%rax),%esi
mov    0x2da8(%rip),%rax
mov    (%rax),%edx
lea    -0x24(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    1226 <do_open_server_socket+0xa6>
movl   $0xffffffff,-0x4(%rbp)
jmp    130b <do_open_server_socket+0x18b>
mov    0x2db3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2d9f(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1080 <bind@plt>
cmp    $0xffffffff,%eax
jne    12a0 <do_open_server_socket+0x120>
mov    0x2d59(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2d3d(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x2c(%rbp),%edi
mov    %rax,%rdx
lea    0xd85(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    130b <do_open_server_socket+0x18b>
mov    -0xc(%rbp),%eax
mov    0x2d16(%rip),%rcx
cmp    (%rcx),%eax
jne    1305 <do_open_server_socket+0x185>
mov    -0x10(%rbp),%edi
mov    $0x5,%esi
call   1070 <listen@plt>
cmp    $0xffffffff,%eax
jne    1300 <do_open_server_socket+0x180>
mov    0x2cf9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2cdd(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x30(%rbp),%edi
mov    %rax,%rdx
lea    0xd37(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    130b <do_open_server_socket+0x18b>
jmp    1305 <do_open_server_socket+0x185>
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_open_server_socket>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebp
mov    %edi,%r14d
mov    0x2e1c(%rip),%rax
mov    (%rax),%edi
xor    %edx,%edx
call   10a0 <socket@plt>
cmp    $0xffffffff,%eax
je     1254 <do_open_server_socket+0xd4>
mov    %eax,%ebx
movl   $0x1,(%rsp)
mov    0x2e32(%rip),%rax
mov    (%rax),%esi
mov    0x2de9(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
je     129c <do_open_server_socket+0x11c>
mov    0x2dff(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    0x2dea(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,0x4(%rsp)
mov    %r14d,%edi
call   1040 <htons@plt>
mov    %eax,(%rsp)
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1080 <bind@plt>
cmp    $0xffffffff,%eax
je     1274 <do_open_server_socket+0xf4>
mov    0x2da1(%rip),%rax
cmp    %ebp,(%rax)
jne    12a1 <do_open_server_socket+0x121>
mov    %ebx,%edi
mov    $0x5,%esi
call   1070 <listen@plt>
cmp    $0xffffffff,%eax
jne    12a1 <do_open_server_socket+0x121>
mov    0x2d8d(%rip),%rax
mov    (%rax),%ebx
mov    0x2d74(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xdd4(%rip),%rsi
jmp    1292 <do_open_server_socket+0x112>
mov    0x2d6d(%rip),%rax
mov    (%rax),%ebx
mov    0x2d54(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xd8e(%rip),%rsi
jmp    1292 <do_open_server_socket+0x112>
mov    0x2d4d(%rip),%rax
mov    (%rax),%ebx
mov    0x2d34(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xd82(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1060 <fprintf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@stderr = global i32 0
@.str = [20 x i8] c"socket failed: %s\0A\00"
@errno = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@.str.1 = [18 x i8] c"bind failed: %s\0A\00"
@SOCK_STREAM = global i32 0
@.str.2 = [20 x i8] c"listen failed: %s\0A\00"
define i32 @do_open_server_socket(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.sockaddr_in
%8 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%9 = load i32, i32* @PF_INET
%10 = load i32, i32* %5
%11 = call i32 @socket(i32 %9, i32 %10, i32 0)
store i32 %11, i32* %6
%12 = load i32, i32* %6
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %19
14: ; preds = %2
%15 = load i32, i32* @stderr
%16 = load i32, i32* @errno
%17 = call i8* @strerror(i32 %16)
%18 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %17)
store i32 -1, i32* %3
br label %61
19: ; preds = %2
store i32 1, i32* %8
%20 = load i32, i32* %6
%21 = load i32, i32* @SOL_SOCKET
%22 = load i32, i32* @SO_REUSEADDR
%23 = call i32 @setsockopt(i32 %20, i32 %21, i32 %22, i32* %8, i32 4)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %26
25: ; preds = %19
store i32 -1, i32* %3
br label %61
26: ; preds = %19
%27 = load i32, i32* @AF_INET
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i32 %27, i32* %28
%29 = load i32, i32* @INADDR_ANY
%30 = call i32 @htonl(i32 %29)
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
%32 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %31, i32 0, i32 0
store i32 %30, i32* %32
%33 = load i32, i32* %4
%34 = call i32 @htons(i32 %33)
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
store i32 %34, i32* %35
%36 = load i32, i32* %6
%37 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%38 = call i32 @bind(i32 %36, %struct.sockaddr* %37, i32 12)
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %45
40: ; preds = %26
%41 = load i32, i32* @stderr
%42 = load i32, i32* @errno
%43 = call i8* @strerror(i32 %42)
%44 = call i32 @fprintf(i32 %41, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %43)
store i32 -1, i32* %3
br label %61
45: ; preds = %26
%46 = load i32, i32* %5
%47 = load i32, i32* @SOCK_STREAM
%48 = icmp eq i32 %46, %47
br i1 %48, label %49, label %59
49: ; preds = %45
%50 = load i32, i32* %6
%51 = call i32 @listen(i32 %50, i32 5)
%52 = icmp eq i32 %51, -1
br i1 %52, label %53, label %58
53: ; preds = %49
%54 = load i32, i32* @stderr
%55 = load i32, i32* @errno
%56 = call i8* @strerror(i32 %55)
%57 = call i32 @fprintf(i32 %54, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %56)
store i32 -1, i32* %3
br label %61
58: ; preds = %49
br label %59
59: ; preds = %58, %45
%60 = load i32, i32* %6
store i32 %60, i32* %3
br label %61
61: ; preds = %59, %53, %40, %25, %14
%62 = load i32, i32* %3
ret i32 %62
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@stderr = global i32 0
@.str = [20 x i8] c"socket failed: %s\0A\00"
@errno = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@.str.1 = [18 x i8] c"bind failed: %s\0A\00"
@SOCK_STREAM = global i32 0
@.str.2 = [20 x i8] c"listen failed: %s\0A\00"
define i32 @do_open_server_socket(i32 %0, i32 %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = bitcast %struct.sockaddr_in* %3 to i8*
%6 = load i32, i32* @PF_INET
%7 = tail call i32 @socket(i32 %6, i32 %1, i32 0)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %14
9: ; preds = %2
%10 = load i32, i32* @stderr
%11 = load i32, i32* @errno
%12 = tail call i8* @strerror(i32 %11)
%13 = tail call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %12)
br label %47
14: ; preds = %2
%15 = bitcast i32* %4 to i8*
store i32 1, i32* %4
%16 = load i32, i32* @SOL_SOCKET
%17 = load i32, i32* @SO_REUSEADDR
%18 = call i32 @setsockopt(i32 %7, i32 %16, i32 %17, i32* %4, i32 4)
%19 = icmp eq i32 %18, -1
br i1 %19, label %47, label %20
20: ; preds = %14
%21 = load i32, i32* @AF_INET
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %21, i32* %22
%23 = load i32, i32* @INADDR_ANY
%24 = call i32 @htonl(i32 %23)
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
store i32 %24, i32* %25
%26 = call i32 @htons(i32 %0)
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %26, i32* %27
%28 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%29 = call i32 @bind(i32 %7, %struct.sockaddr* %28, i32 12)
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %36
31: ; preds = %20
%32 = load i32, i32* @stderr
%33 = load i32, i32* @errno
%34 = call i8* @strerror(i32 %33)
%35 = call i32 @fprintf(i32 %32, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %34)
br label %47
36: ; preds = %20
%37 = load i32, i32* @SOCK_STREAM
%38 = icmp eq i32 %37, %1
br i1 %38, label %39, label %47
39: ; preds = %36
%40 = call i32 @listen(i32 %7, i32 5)
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %47
42: ; preds = %39
%43 = load i32, i32* @stderr
%44 = load i32, i32* @errno
%45 = call i8* @strerror(i32 %44)
%46 = call i32 @fprintf(i32 %43, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %45)
br label %47
47: ; preds = %36, %39, %14, %42, %31, %9
%48 = phi i32 [ -1, %9 ], [ -1, %31 ], [ -1, %42 ], [ -1, %14 ], [ %7, %39 ], [ %7, %36 ]
ret i32 %48
}