#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 */
typedef int /*<<< orphan*/ svr_addr;
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;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ PF_INET;
int SOCK_STREAM;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ htons(int);
scalar_t__ listen(int, int);
int socket(int /*<<< orphan*/, int, int /*<<< orphan*/);
int serversock(int UDPorTCP, int portN, int qlen) {
  struct sockaddr_in svr_addr;
  int sock;
  if (portN < 0 || portN > 65535 || qlen < 0)
    return -2;
  bzero((char *)&svr_addr, sizeof(svr_addr));
  svr_addr.sin_family = AF_INET;
  svr_addr.sin_addr.s_addr = INADDR_ANY;
  svr_addr.sin_port = htons(portN);
  sock = socket(PF_INET, UDPorTCP, 0);
  if (sock < 0)
    return -3;
  if (bind(sock, (struct sockaddr *)&svr_addr, sizeof(svr_addr)) < 0)
    return -4;
  if (UDPorTCP == SOCK_STREAM && listen(sock, qlen) < 0)
    return -5;
  return sock;
}


x86 assembly (clang, -O0):
<serversock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
cmpl   $0x0,-0xc(%rbp)
jl     1182 <serversock+0x32>
cmpl   $0xffff,-0xc(%rbp)
jg     1182 <serversock+0x32>
cmpl   $0x0,-0x10(%rbp)
jge    118e <serversock+0x3e>
movl   $0xfffffffe,-0x4(%rbp)
jmp    124b <serversock+0xfb>
lea    -0x20(%rbp),%rdi
mov    $0xc,%esi
call   1060 <bzero@plt>
mov    0x2e45(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2e31(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0xc(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    0x2e02(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11eb <serversock+0x9b>
movl   $0xfffffffd,-0x4(%rbp)
jmp    124b <serversock+0xfb>
mov    -0x24(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1050 <bind@plt>
cmp    $0x0,%rax
jge    1212 <serversock+0xc2>
movl   $0xfffffffc,-0x4(%rbp)
jmp    124b <serversock+0xfb>
mov    -0x8(%rbp),%eax
mov    0x2db4(%rip),%rcx
cmp    (%rcx),%eax
jne    1245 <serversock+0xf5>
mov    -0x24(%rbp),%edi
mov    -0x10(%rbp),%esi
call   1040 <listen@plt>
cmp    $0x0,%rax
jge    1245 <serversock+0xf5>
movl   $0xfffffffb,-0x4(%rbp)
jmp    124b <serversock+0xfb>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<serversock>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    $0xfffffffe,%ebx
cmp    $0xffff,%esi
ja     120c <serversock+0xbc>
mov    %edx,%r14d
test   %edx,%edx
js     120c <serversock+0xbc>
mov    %esi,%ebp
mov    %edi,%r15d
lea    0x8(%rsp),%rdi
mov    $0xc,%esi
call   1060 <bzero@plt>
mov    0x2e57(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    0x2e42(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    %ebp,%edi
call   1030 <htons@plt>
mov    %eax,0x8(%rsp)
mov    0x2e12(%rip),%rax
mov    (%rax),%edi
mov    %r15d,%esi
xor    %edx,%edx
call   1070 <socket@plt>
test   %eax,%eax
js     1200 <serversock+0xb0>
mov    %eax,%ebx
lea    0x8(%rsp),%rsi
mov    %eax,%edi
mov    $0xc,%edx
call   1050 <bind@plt>
test   %rax,%rax
js     1207 <serversock+0xb7>
mov    0x2deb(%rip),%rax
cmp    %r15d,(%rax)
jne    120c <serversock+0xbc>
mov    %ebx,%edi
mov    %r14d,%esi
call   1040 <listen@plt>
test   %rax,%rax
jns    120c <serversock+0xbc>
mov    $0xfffffffb,%ebx
jmp    120c <serversock+0xbc>
mov    $0xfffffffd,%ebx
jmp    120c <serversock+0xbc>
mov    $0xfffffffc,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
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
@INADDR_ANY = global i32 0
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
define i32 @serversock(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.sockaddr_in
%9 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%10 = load i32, i32* %6
%11 = icmp slt i32 %10, 0
br i1 %11, label %18, label %12
12: ; preds = %3
%13 = load i32, i32* %6
%14 = icmp sgt i32 %13, 65535
br i1 %14, label %18, label %15
15: ; preds = %12
%16 = load i32, i32* %7
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %15, %12, %3
store i32 -2, i32* %4
br label %54
19: ; preds = %15
%20 = bitcast %struct.sockaddr_in* %8 to i8*
%21 = call i32 @bzero(i8* %20, i32 12)
%22 = load i32, i32* @AF_INET
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
store i32 %22, i32* %23
%24 = load i32, i32* @INADDR_ANY
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
%26 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %25, i32 0, i32 0
store i32 %24, i32* %26
%27 = load i32, i32* %6
%28 = call i32 @htons(i32 %27)
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
store i32 %28, i32* %29
%30 = load i32, i32* @PF_INET
%31 = load i32, i32* %5
%32 = call i32 @socket(i32 %30, i32 %31, i32 0)
store i32 %32, i32* %9
%33 = load i32, i32* %9
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %36
35: ; preds = %19
store i32 -3, i32* %4
br label %54
36: ; preds = %19
%37 = load i32, i32* %9
%38 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%39 = call i64 @bind(i32 %37, %struct.sockaddr* %38, i32 12)
%40 = icmp slt i64 %39, 0
br i1 %40, label %41, label %42
41: ; preds = %36
store i32 -4, i32* %4
br label %54
42: ; preds = %36
%43 = load i32, i32* %5
%44 = load i32, i32* @SOCK_STREAM
%45 = icmp eq i32 %43, %44
br i1 %45, label %46, label %52
46: ; preds = %42
%47 = load i32, i32* %9
%48 = load i32, i32* %7
%49 = call i64 @listen(i32 %47, i32 %48)
%50 = icmp slt i64 %49, 0
br i1 %50, label %51, label %52
51: ; preds = %46
store i32 -5, i32* %4
br label %54
52: ; preds = %46, %42
%53 = load i32, i32* %9
store i32 %53, i32* %4
br label %54
54: ; preds = %52, %51, %41, %35, %18
%55 = load i32, i32* %4
ret i32 %55
}

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
@INADDR_ANY = global i32 0
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
define i32 @serversock(i32 %0, i32 %1, i32 %2) {
%4 = alloca %struct.sockaddr_in
%5 = bitcast %struct.sockaddr_in* %4 to i8*
%6 = icmp ugt i32 %1, 65535
%7 = icmp slt i32 %2, 0
%8 = or i1 %6, %7
br i1 %8, label %31, label %9
9: ; preds = %3
%10 = call i32 @bzero(i8* %5, i32 12)
%11 = load i32, i32* @AF_INET
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %11, i32* %12
%13 = load i32, i32* @INADDR_ANY
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1, i32 0
store i32 %13, i32* %14
%15 = tail call i32 @htons(i32 %1)
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
store i32 %15, i32* %16
%17 = load i32, i32* @PF_INET
%18 = tail call i32 @socket(i32 %17, i32 %0, i32 0)
%19 = icmp slt i32 %18, 0
br i1 %19, label %31, label %20
20: ; preds = %9
%21 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%22 = call i64 @bind(i32 %18, %struct.sockaddr* %21, i32 12)
%23 = icmp slt i64 %22, 0
br i1 %23, label %31, label %24
24: ; preds = %20
%25 = load i32, i32* @SOCK_STREAM
%26 = icmp eq i32 %25, %0
br i1 %26, label %27, label %30
27: ; preds = %24
%28 = call i64 @listen(i32 %18, i32 %2)
%29 = icmp slt i64 %28, 0
br i1 %29, label %31, label %30
30: ; preds = %27, %24
br label %31
31: ; preds = %27, %20, %9, %3, %30
%32 = phi i32 [ %18, %30 ], [ -2, %3 ], [ -3, %9 ], [ -4, %20 ], [ -5, %27 ]
ret i32 %32
}