#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*/ u_long;
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ s;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
scalar_t__ IPPROTO_TCP;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(void *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ htons(int);
scalar_t__ listen(int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ pmap_set(int /*<<< orphan*/, int /*<<< orphan*/,
                            int /*<<< orphan*/, unsigned short);
int /*<<< orphan*/ pmap_unset(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ sock_optimize(int, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, scalar_t__);
scalar_t__ sockport(int);
int tcp_server(int prog, int rdwr) {
  int sock;
  struct sockaddr_in s;
  if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
    perror("socket");
    exit(1);
  }
  sock_optimize(sock, rdwr);
  bzero((void *)&s, sizeof(s));
  s.sin_family = AF_INET;
  if (prog < 0) {
    s.sin_port = htons(-prog);
  }
  if (bind(sock, (struct sockaddr *)&s, sizeof(s)) < 0) {
    perror("bind");
    exit(2);
  }
  if (listen(sock, 100) < 0) {
    perror("listen");
    exit(4);
  }
  if (prog > 0) {
    (void)pmap_unset((u_long)prog, (u_long)1);
    if (!pmap_set((u_long)prog, (u_long)1, (u_long)IPPROTO_TCP,
                  (unsigned short)sockport(sock))) {
      perror("pmap_set");
      exit(5);
    }
  }
  return (sock);
}


x86 assembly (clang, -O0):
<tcp_server>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    0x2e23(%rip),%rax
mov    (%rax),%edi
mov    0x2e02(%rip),%rax
mov    (%rax),%esi
mov    0x2e09(%rip),%rax
mov    (%rax),%rdx
call   10d0 <socket@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    1201 <tcp_server+0x51>
lea    0xe0e(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0xc(%rbp),%edi
mov    -0x8(%rbp),%esi
call   10a0 <sock_optimize@plt>
lea    -0x18(%rbp),%rdi
mov    $0x8,%esi
call   1090 <bzero@plt>
mov    0x2dc7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x4(%rbp)
jge    123d <tcp_server+0x8d>
xor    %edi,%edi
sub    -0x4(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x18(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x8,%edx
call   1070 <bind@plt>
cmp    $0x0,%rax
jge    126e <tcp_server+0xbe>
lea    0xda8(%rip),%rdi
call   1080 <perror@plt>
mov    $0x2,%edi
call   10b0 <exit@plt>
mov    -0xc(%rbp),%edi
mov    $0x64,%esi
call   1050 <listen@plt>
cmp    $0x0,%rax
jge    129b <tcp_server+0xeb>
lea    0xd80(%rip),%rdi
call   1080 <perror@plt>
mov    $0x4,%edi
call   10b0 <exit@plt>
cmpl   $0x0,-0x4(%rbp)
jle    1304 <tcp_server+0x154>
mov    -0x4(%rbp),%edi
mov    $0x1,%esi
call   1030 <pmap_unset@plt>
mov    -0x4(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    0x2d21(%rip),%rax
mov    (%rax),%rax
mov    %eax,-0x1c(%rbp)
mov    -0xc(%rbp),%edi
call   1060 <sockport@plt>
mov    -0x20(%rbp),%edi
mov    -0x1c(%rbp),%edx
mov    $0x1,%esi
movzwl %ax,%ecx
call   10c0 <pmap_set@plt>
cmp    $0x0,%eax
jne    12ff <tcp_server+0x14f>
lea    0xd23(%rip),%rdi
call   1080 <perror@plt>
mov    $0x5,%edi
call   10b0 <exit@plt>
jmp    1304 <tcp_server+0x154>
mov    -0xc(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tcp_server>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebx
mov    %edi,%r14d
mov    0x2e20(%rip),%r12
mov    (%r12),%edi
mov    0x2dfd(%rip),%rax
mov    (%rax),%esi
mov    0x2e04(%rip),%r15
mov    (%r15),%rdx
call   10d0 <socket@plt>
test   %eax,%eax
js     1289 <tcp_server+0xd9>
mov    %eax,%ebp
mov    %eax,%edi
mov    %ebx,%esi
call   10a0 <sock_optimize@plt>
lea    0x8(%rsp),%rdi
mov    $0x8,%esi
call   1090 <bzero@plt>
mov    (%r12),%eax
mov    %eax,0xc(%rsp)
test   %r14d,%r14d
jns    1221 <tcp_server+0x71>
mov    %r14d,%edi
neg    %edi
call   1040 <htons@plt>
mov    %eax,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %ebp,%edi
mov    $0x8,%edx
call   1070 <bind@plt>
test   %rax,%rax
js     129f <tcp_server+0xef>
mov    %ebp,%edi
mov    $0x64,%esi
call   1050 <listen@plt>
test   %rax,%rax
js     12b5 <tcp_server+0x105>
test   %r14d,%r14d
jle    127a <tcp_server+0xca>
mov    %r14d,%edi
mov    $0x1,%esi
call   1030 <pmap_unset@plt>
mov    (%r15),%ebx
mov    %ebp,%edi
call   1060 <sockport@plt>
movzwl %ax,%ecx
mov    %r14d,%edi
mov    $0x1,%esi
mov    %ebx,%edx
call   10c0 <pmap_set@plt>
test   %eax,%eax
je     12cb <tcp_server+0x11b>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd70(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    0xd61(%rip),%rdi
call   1080 <perror@plt>
mov    $0x2,%edi
call   10b0 <exit@plt>
lea    0xd50(%rip),%rdi
call   1080 <perror@plt>
mov    $0x4,%edi
call   10b0 <exit@plt>
lea    0xd41(%rip),%rdi
call   1080 <perror@plt>
mov    $0x5,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i64 0
@.str = [7 x i8] c"socket\00"
@.str.1 = [5 x i8] c"bind\00"
@.str.2 = [7 x i8] c"listen\00"
@.str.3 = [9 x i8] c"pmap_set\00"
define i32 @tcp_server(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.sockaddr_in
store i32 %0, i32* %3
store i32 %1, i32* %4
%7 = load i32, i32* @AF_INET
%8 = load i32, i32* @SOCK_STREAM
%9 = load i64, i64* @IPPROTO_TCP
%10 = call i32 @socket(i32 %7, i32 %8, i64 %9)
store i32 %10, i32* %5
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %2
%16 = load i32, i32* %5
%17 = load i32, i32* %4
%18 = call i32 @sock_optimize(i32 %16, i32 %17)
%19 = bitcast %struct.sockaddr_in* %6 to i8*
%20 = call i32 @bzero(i8* %19, i32 8)
%21 = load i32, i32* @AF_INET
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
store i32 %21, i32* %22
%23 = load i32, i32* %3
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %30
25: ; preds = %15
%26 = load i32, i32* %3
%27 = sub nsw i32 0, %26
%28 = call i32 @htons(i32 %27)
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
store i32 %28, i32* %29
br label %30
30: ; preds = %25, %15
%31 = load i32, i32* %5
%32 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%33 = call i64 @bind(i32 %31, %struct.sockaddr* %32, i32 8)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %30
%36 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%37 = call i32 @exit(i32 2)
unreachable
38: ; preds = %30
%39 = load i32, i32* %5
%40 = call i64 @listen(i32 %39, i32 100)
%41 = icmp slt i64 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %38
%43 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%44 = call i32 @exit(i32 4)
unreachable
45: ; preds = %38
%46 = load i32, i32* %3
%47 = icmp sgt i32 %46, 0
br i1 %47, label %48, label %63
48: ; preds = %45
%49 = load i32, i32* %3
%50 = call i32 @pmap_unset(i32 %49, i32 1)
%51 = load i32, i32* %3
%52 = load i64, i64* @IPPROTO_TCP
%53 = trunc i64 %52 to i32
%54 = load i32, i32* %5
%55 = call i64 @sockport(i32 %54)
%56 = trunc i64 %55 to i16
%57 = call i32 @pmap_set(i32 %51, i32 1, i32 %53, i16 zeroext %56)
%58 = icmp ne i32 %57, 0
br i1 %58, label %62, label %59
59: ; preds = %48
%60 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%61 = call i32 @exit(i32 5)
unreachable
62: ; preds = %48
br label %63
63: ; preds = %62, %45
%64 = load i32, i32* %5
ret i32 %64
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i64 0
@.str = [7 x i8] c"socket\00"
@.str.1 = [5 x i8] c"bind\00"
@.str.2 = [7 x i8] c"listen\00"
@.str.3 = [9 x i8] c"pmap_set\00"
define i32 @tcp_server(i32 %0, i32 %1) {
%3 = alloca %struct.sockaddr_in
%4 = bitcast %struct.sockaddr_in* %3 to i8*
%5 = load i32, i32* @AF_INET
%6 = load i32, i32* @SOCK_STREAM
%7 = load i64, i64* @IPPROTO_TCP
%8 = tail call i32 @socket(i32 %5, i32 %6, i64 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %2
%14 = tail call i32 @sock_optimize(i32 %8, i32 %1)
%15 = call i32 @bzero(i8* %4, i32 8)
%16 = load i32, i32* @AF_INET
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %16, i32* %17
%18 = icmp slt i32 %0, 0
br i1 %18, label %19, label %23
19: ; preds = %13
%20 = sub nsw i32 0, %0
%21 = tail call i32 @htons(i32 %20)
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %21, i32* %22
br label %23
23: ; preds = %19, %13
%24 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%25 = call i64 @bind(i32 %8, %struct.sockaddr* %24, i32 8)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %23
%28 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%29 = call i32 @exit(i32 2)
unreachable
30: ; preds = %23
%31 = call i64 @listen(i32 %8, i32 100)
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %30
%34 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%35 = call i32 @exit(i32 4)
unreachable
36: ; preds = %30
%37 = icmp sgt i32 %0, 0
br i1 %37, label %38, label %49
38: ; preds = %36
%39 = call i32 @pmap_unset(i32 %0, i32 1)
%40 = load i64, i64* @IPPROTO_TCP
%41 = trunc i64 %40 to i32
%42 = call i64 @sockport(i32 %8)
%43 = trunc i64 %42 to i16
%44 = call i32 @pmap_set(i32 %0, i32 1, i32 %41, i16 zeroext %43)
%45 = icmp eq i32 %44, 0
br i1 %45, label %46, label %49
46: ; preds = %38
%47 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%48 = call i32 @exit(i32 5)
unreachable
49: ; preds = %38, %36
ret i32 %8
}