#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*/ serv_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ htons(int);
int listen(int, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int get_listening_socket(int port, int single_connection) {
  struct sockaddr_in serv_addr;
  if (port < 1 || port > 65535) {
    return -1;
  }
  int listen_socket = socket(AF_INET, SOCK_STREAM, 0);
  if (listen_socket < 0) {
    fprintf(stderr, "ERROR opening socket.\n");
    return -1;
  }
  bzero((char *)&serv_addr, sizeof(serv_addr));
  serv_addr.sin_family = AF_INET;
  serv_addr.sin_addr.s_addr = INADDR_ANY;
  serv_addr.sin_port = htons(port);
  if (bind(listen_socket, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) <
      0) {
    fprintf(stderr, "ERROR on binding (port %d).\n", port);
    if (listen_socket != -1)
      close(listen_socket);
    (listen_socket) = -1;
    return -1;
  }
  if (listen(listen_socket, (single_connection) ? 1 : 5) == -1) {
    fprintf(stderr, "ERROR on listening (port %d).\n", port);
    if (listen_socket != -1)
      close(listen_socket);
    (listen_socket) = -1;
    return -1;
  }
  return listen_socket;
}


x86 assembly (clang, -O0):
<get_listening_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
cmpl   $0x1,-0x8(%rbp)
jl     1195 <get_listening_socket+0x25>
cmpl   $0xffff,-0x8(%rbp)
jle    11a1 <get_listening_socket+0x31>
movl   $0xffffffff,-0x4(%rbp)
jmp    12de <get_listening_socket+0x16e>
mov    0x2e40(%rip),%rax
mov    (%rax),%edi
mov    0x2e17(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11ea <get_listening_socket+0x7a>
mov    0x2e02(%rip),%rax
mov    (%rax),%edi
lea    0xe29(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12de <get_listening_socket+0x16e>
lea    -0x18(%rbp),%rdi
mov    $0xc,%esi
call   1080 <bzero@plt>
mov    0x2de9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2dd5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x18(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1070 <bind@plt>
cmp    $0x0,%rax
jge    1275 <get_listening_socket+0x105>
mov    0x2d93(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%edx
lea    0xdce(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
cmpl   $0xffffffff,-0x1c(%rbp)
je     1262 <get_listening_socket+0xf2>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x1c(%rbp)
movl   $0xffffffff,-0x4(%rbp)
jmp    12de <get_listening_socket+0x16e>
mov    -0x1c(%rbp),%edi
mov    -0xc(%rbp),%ecx
mov    $0x5,%esi
mov    $0x1,%eax
cmp    $0x0,%ecx
cmovne %eax,%esi
call   1060 <listen@plt>
cmp    $0xffffffff,%eax
jne    12d8 <get_listening_socket+0x168>
mov    0x2d30(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%edx
lea    0xd88(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
cmpl   $0xffffffff,-0x1c(%rbp)
je     12c5 <get_listening_socket+0x155>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x1c(%rbp)
movl   $0xffffffff,-0x4(%rbp)
jmp    12de <get_listening_socket+0x16e>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_listening_socket>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebx
lea    -0x10000(%rbx),%eax
mov    $0xffffffff,%r14d
cmp    $0xffff0001,%eax
jb     126c <get_listening_socket+0xfc>
mov    %esi,%r15d
mov    0x2e47(%rip),%r13
mov    0x0(%r13),%edi
mov    0x2e1c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
test   %eax,%eax
js     1221 <get_listening_socket+0xb1>
mov    %eax,%ebp
lea    0x8(%rsp),%r12
mov    %r12,%rdi
mov    $0xc,%esi
call   1080 <bzero@plt>
mov    0x0(%r13),%eax
mov    %eax,0x10(%rsp)
mov    0x2e04(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    %ebx,%edi
call   1030 <htons@plt>
mov    %eax,0x8(%rsp)
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0xc,%edx
call   1070 <bind@plt>
test   %rax,%rax
js     123a <get_listening_socket+0xca>
xor    %eax,%eax
test   %r15d,%r15d
sete   %al
lea    0x1(,%rax,4),%esi
mov    %ebp,%edi
call   1060 <listen@plt>
cmp    $0xffffffff,%eax
je     124c <get_listening_socket+0xdc>
mov    %ebp,%r14d
jmp    126c <get_listening_socket+0xfc>
mov    0x2da8(%rip),%rax
mov    (%rax),%edi
lea    0xdcf(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    126c <get_listening_socket+0xfc>
mov    0x2d8f(%rip),%rax
mov    (%rax),%edi
lea    0xdcd(%rip),%rsi
jmp    125c <get_listening_socket+0xec>
mov    0x2d7d(%rip),%rax
mov    (%rax),%edi
lea    0xdd8(%rip),%rsi
mov    %ebx,%edx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %ebp,%edi
call   1040 <close@plt>
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.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@stderr = global i32 0
@.str = [23 x i8] c"ERROR opening socket.\0A\00"
@INADDR_ANY = global i32 0
@.str.1 = [29 x i8] c"ERROR on binding (port %d).\0A\00"
@.str.2 = [31 x i8] c"ERROR on listening (port %d).\0A\00"
define i32 @get_listening_socket(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.sockaddr_in
%7 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%8 = load i32, i32* %4
%9 = icmp slt i32 %8, 1
br i1 %9, label %13, label %10
10: ; preds = %2
%11 = load i32, i32* %4
%12 = icmp sgt i32 %11, 65535
br i1 %12, label %13, label %14
13: ; preds = %10, %2
store i32 -1, i32* %3
br label %68
14: ; preds = %10
%15 = load i32, i32* @AF_INET
%16 = load i32, i32* @SOCK_STREAM
%17 = call i32 @socket(i32 %15, i32 %16, i32 0)
store i32 %17, i32* %7
%18 = load i32, i32* %7
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %14
%21 = load i32, i32* @stderr
%22 = call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %68
23: ; preds = %14
%24 = bitcast %struct.sockaddr_in* %6 to i8*
%25 = call i32 @bzero(i8* %24, i32 12)
%26 = load i32, i32* @AF_INET
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 %26, i32* %27
%28 = load i32, i32* @INADDR_ANY
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
%30 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %29, i32 0, i32 0
store i32 %28, i32* %30
%31 = load i32, i32* %4
%32 = call i32 @htons(i32 %31)
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
store i32 %32, i32* %33
%34 = load i32, i32* %7
%35 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%36 = call i64 @bind(i32 %34, %struct.sockaddr* %35, i32 12)
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %48
38: ; preds = %23
%39 = load i32, i32* @stderr
%40 = load i32, i32* %4
%41 = call i32 (i32, i8*, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i32 %40)
%42 = load i32, i32* %7
%43 = icmp ne i32 %42, -1
br i1 %43, label %44, label %47
44: ; preds = %38
%45 = load i32, i32* %7
%46 = call i32 @close(i32 %45)
br label %47
47: ; preds = %44, %38
store i32 -1, i32* %7
store i32 -1, i32* %3
br label %68
48: ; preds = %23
%49 = load i32, i32* %7
%50 = load i32, i32* %5
%51 = icmp ne i32 %50, 0
%52 = zext i1 %51 to i64
%53 = select i1 %51, i32 1, i32 5
%54 = call i32 @listen(i32 %49, i32 %53)
%55 = icmp eq i32 %54, -1
br i1 %55, label %56, label %66
56: ; preds = %48
%57 = load i32, i32* @stderr
%58 = load i32, i32* %4
%59 = call i32 (i32, i8*, ...) @fprintf(i32 %57, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i32 %58)
%60 = load i32, i32* %7
%61 = icmp ne i32 %60, -1
br i1 %61, label %62, label %65
62: ; preds = %56
%63 = load i32, i32* %7
%64 = call i32 @close(i32 %63)
br label %65
65: ; preds = %62, %56
store i32 -1, i32* %7
store i32 -1, i32* %3
br label %68
66: ; preds = %48
%67 = load i32, i32* %7
store i32 %67, i32* %3
br label %68
68: ; preds = %66, %65, %47, %20, %13
%69 = load i32, i32* %3
ret i32 %69
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@stderr = global i32 0
@.str = [23 x i8] c"ERROR opening socket.\0A\00"
@INADDR_ANY = global i32 0
@.str.1 = [29 x i8] c"ERROR on binding (port %d).\0A\00"
@.str.2 = [31 x i8] c"ERROR on listening (port %d).\0A\00"
define i32 @get_listening_socket(i32 %0, i32 %1) {
%3 = alloca %struct.sockaddr_in
%4 = bitcast %struct.sockaddr_in* %3 to i8*
%5 = add i32 %0, -65536
%6 = icmp ult i32 %5, -65535
br i1 %6, label %39, label %7
7: ; preds = %2
%8 = load i32, i32* @AF_INET
%9 = load i32, i32* @SOCK_STREAM
%10 = tail call i32 @socket(i32 %8, i32 %9, i32 0)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %7
%13 = load i32, i32* @stderr
%14 = tail call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
br label %39
15: ; preds = %7
%16 = call i32 @bzero(i8* %4, i32 12)
%17 = load i32, i32* @AF_INET
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %17, i32* %18
%19 = load i32, i32* @INADDR_ANY
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
store i32 %19, i32* %20
%21 = tail call i32 @htons(i32 %0)
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %21, i32* %22
%23 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%24 = call i64 @bind(i32 %10, %struct.sockaddr* %23, i32 12)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %30
26: ; preds = %15
%27 = load i32, i32* @stderr
%28 = call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i32 %0)
%29 = call i32 @close(i32 %10)
br label %39
30: ; preds = %15
%31 = icmp eq i32 %1, 0
%32 = select i1 %31, i32 5, i32 1
%33 = call i32 @listen(i32 %10, i32 %32)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %39
35: ; preds = %30
%36 = load i32, i32* @stderr
%37 = call i32 (i32, i8*, ...) @fprintf(i32 %36, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i32 %0)
%38 = call i32 @close(i32 %10)
br label %39
39: ; preds = %12, %26, %35, %30, %2
%40 = phi i32 [ -1, %2 ], [ -1, %12 ], [ -1, %26 ], [ -1, %35 ], [ %10, %30 ]
ret i32 %40
}