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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ htons(int const);
int /*<<< orphan*/ listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int const);
int process_connection(int, char const *);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int server(const int port, const char *path) {
  int sockfd;
  struct sockaddr_in serv_addr;
  int optval;
  sockfd = socket(AF_INET, SOCK_STREAM, 0);
  if (sockfd < 0) {
    perror("ERROR opening socket");
    return 1;
  }
  optval = 1;
  setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval));
  memset(&serv_addr, 0, 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(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
    perror("ERROR on binding");
    return 1;
  }
  listen(sockfd, 1024);
  printf("Listening on port %i.\n", port);
  for (;;) {
    int n;
    n = process_connection(sockfd, path);
    if (n < 0)
      goto error;
  }
  close(sockfd);
  return 0;
error:
  close(sockfd);
  perror("ERROR unexpected error occurred");
  return 1;
}


x86 assembly (clang, -O0):
<server>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e2a(%rip),%rax
mov    (%rax),%edi
mov    0x2e09(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11ed <server+0x4d>
lea    0xe24(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12d8 <server+0x138>
movl   $0x1,-0x24(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2dea(%rip),%rax
mov    (%rax),%esi
mov    0x2db9(%rip),%rax
mov    (%rax),%edx
lea    -0x24(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2db1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2d9d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1090 <bind@plt>
cmp    $0x0,%rax
jge    127e <server+0xde>
lea    0xda8(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12d8 <server+0x138>
mov    -0x14(%rbp),%edi
mov    $0x400,%esi
call   1080 <listen@plt>
mov    -0x8(%rbp),%esi
lea    0xd91(%rip),%rdi
call   1050 <printf@plt>
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   10c0 <process_connection@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    12b8 <server+0x118>
jmp    12bd <server+0x11d>
jmp    129a <server+0xfa>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
lea    0xd71(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<server>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %rsi,%rbx
mov    %edi,%r14d
mov    0x2e27(%rip),%r12
mov    (%r12),%edi
mov    0x2e04(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
test   %eax,%eax
jns    11dd <server+0x3d>
lea    0xe28(%rip),%rdi
jmp    129c <server+0xfc>
mov    %eax,%ebp
movl   $0x1,0xc(%rsp)
mov    0x2dfa(%rip),%rax
mov    (%rax),%esi
mov    0x2dc9(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
lea    0x10(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    (%r12),%eax
mov    %eax,0x18(%rsp)
mov    0x2daa(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    %r14d,%edi
call   1040 <htons@plt>
mov    %eax,0x10(%rsp)
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0xc,%edx
call   1090 <bind@plt>
test   %rax,%rax
jns    125d <server+0xbd>
lea    0xdba(%rip),%rdi
jmp    129c <server+0xfc>
mov    %ebp,%edi
mov    $0x400,%esi
call   1080 <listen@plt>
lea    0xdb6(%rip),%rdi
mov    %r14d,%esi
call   1050 <printf@plt>
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %rbx,%rsi
call   10c0 <process_connection@plt>
test   %eax,%eax
jns    1280 <server+0xe0>
mov    %ebp,%edi
call   1070 <close@plt>
lea    0xda1(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
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
@.str = [21 x i8] c"ERROR opening socket\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@INADDR_ANY = global i32 0
@.str.1 = [17 x i8] c"ERROR on binding\00"
@.str.2 = [23 x i8] c"Listening on port %i.\0A\00"
@.str.3 = [32 x i8] c"ERROR unexpected error occurred\00"
define i32 @server(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca %struct.sockaddr_in
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
%10 = load i32, i32* @AF_INET
%11 = load i32, i32* @SOCK_STREAM
%12 = call i32 @socket(i32 %10, i32 %11, i32 0)
store i32 %12, i32* %6
%13 = load i32, i32* %6
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %2
%16 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %54
17: ; preds = %2
store i32 1, i32* %8
%18 = load i32, i32* %6
%19 = load i32, i32* @SOL_SOCKET
%20 = load i32, i32* @SO_REUSEADDR
%21 = call i32 @setsockopt(i32 %18, i32 %19, i32 %20, i32* %8, i32 4)
%22 = call i32 @memset(%struct.sockaddr_in* %7, i32 0, i32 12)
%23 = load i32, i32* @AF_INET
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i32 %23, i32* %24
%25 = load i32, i32* @INADDR_ANY
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
%27 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %26, i32 0, i32 0
store i32 %25, i32* %27
%28 = load i32, i32* %4
%29 = call i32 @htons(i32 %28)
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
store i32 %29, i32* %30
%31 = load i32, i32* %6
%32 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%33 = call i64 @bind(i32 %31, %struct.sockaddr* %32, i32 12)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %17
%36 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %54
37: ; preds = %17
%38 = load i32, i32* %6
%39 = call i32 @listen(i32 %38, i32 1024)
%40 = load i32, i32* %4
%41 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i32 %40)
br label %42
42: ; preds = %49, %37
%43 = load i32, i32* %6
%44 = load i8*, i8** %5
%45 = call i32 @process_connection(i32 %43, i8* %44)
store i32 %45, i32* %9
%46 = load i32, i32* %9
%47 = icmp slt i32 %46, 0
br i1 %47, label %48, label %49
48: ; preds = %42
br label %50
49: ; preds = %42
br label %42
50: ; preds = %48
%51 = load i32, i32* %6
%52 = call i32 @close(i32 %51)
%53 = call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %3
br label %54
54: ; preds = %50, %35, %15
%55 = load i32, i32* %3
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
@SOCK_STREAM = global i32 0
@.str = [21 x i8] c"ERROR opening socket\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@INADDR_ANY = global i32 0
@.str.1 = [17 x i8] c"ERROR on binding\00"
@.str.2 = [23 x i8] c"Listening on port %i.\0A\00"
@.str.3 = [32 x i8] c"ERROR unexpected error occurred\00"
define i32 @server(i32 %0, i8* %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = bitcast %struct.sockaddr_in* %3 to i8*
%6 = bitcast i32* %4 to i8*
%7 = load i32, i32* @AF_INET
%8 = load i32, i32* @SOCK_STREAM
%9 = tail call i32 @socket(i32 %7, i32 %8, i32 0)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = tail call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %38
13: ; preds = %2
store i32 1, i32* %4
%14 = load i32, i32* @SOL_SOCKET
%15 = load i32, i32* @SO_REUSEADDR
%16 = call i32 @setsockopt(i32 %9, i32 %14, i32 %15, i32* %4, i32 4)
%17 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 12)
%18 = load i32, i32* @AF_INET
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %18, i32* %19
%20 = load i32, i32* @INADDR_ANY
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
store i32 %20, i32* %21
%22 = call i32 @htons(i32 %0)
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %22, i32* %23
%24 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%25 = call i64 @bind(i32 %9, %struct.sockaddr* %24, i32 12)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %13
%28 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
br label %38
29: ; preds = %13
%30 = call i32 @listen(i32 %9, i32 1024)
%31 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i32 %0)
br label %32
32: ; preds = %32, %29
%33 = call i32 @process_connection(i32 %9, i8* %1)
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %32
35: ; preds = %32
%36 = call i32 @close(i32 %9)
%37 = call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0))
br label %38
38: ; preds = %35, %27, %11
ret i32 1
}