#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 {
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ option_value;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int accept(int, struct sockaddr *, int *);
int bind(int, struct sockaddr *, int);
int close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int listen(int, int);
int /*<<< orphan*/ perror(char *);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ treat_request(int, int);
int main(int argc, char **argv) {
  int listen_socket, client_socket;
  int option_value, connection_counter;
  struct sockaddr_in server_address, client_address;
  socklen_t address_length;
  address_length = sizeof(struct sockaddr_in);
  listen_socket = socket(AF_INET, SOCK_STREAM, 0);
  if (listen_socket == -1) {
    perror("Error creating socket");
    exit(EXIT_FAILURE);
  }
  option_value = 1;
  if (setsockopt(listen_socket, SOL_SOCKET, SO_REUSEADDR, &option_value,
                 sizeof(option_value)) == -1) {
    perror("Error setting socket option");
    exit(EXIT_FAILURE);
  }
  server_address.sin_family = AF_INET;
  server_address.sin_port = htons(2342);
  server_address.sin_addr.s_addr = htonl(INADDR_ANY);
  if (bind(listen_socket, (struct sockaddr *)&server_address, address_length) ==
      -1) {
    perror("Error binding to socket");
    exit(EXIT_FAILURE);
  }
  if (listen(listen_socket, 5) == -1) {
    perror("Error setting connection to server mode");
    exit(EXIT_FAILURE);
  }
  connection_counter = 0;
  while (1) {
    client_socket = accept(listen_socket, (struct sockaddr *)&client_address,
                           &address_length);
    if (client_socket == -1) {
      perror("Connection error");
      continue;
    }
    connection_counter += 1;
    treat_request(client_socket, connection_counter);
    if (close(client_socket) == -1) {
      perror("Error closing connection");
    }
  }
  if (close(listen_socket) == -1) {
    perror("Error closing socket");
    exit(EXIT_FAILURE);
  }
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xc,-0x44(%rbp)
mov    0x2e0c(%rip),%rax
mov    (%rax),%edi
mov    0x2deb(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    120d <main+0x5d>
lea    0xe06(%rip),%rdi
call   1090 <perror@plt>
mov    0x2dba(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
movl   $0x1,-0x1c(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2dca(%rip),%rax
mov    (%rax),%esi
mov    0x2d91(%rip),%rax
mov    (%rax),%edx
lea    -0x1c(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    125b <main+0xab>
lea    0xdce(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d6c(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    0x2d7e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    $0x926,%edi
call   1040 <htons@plt>
mov    %eax,-0x2c(%rbp)
mov    0x2d5d(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,-0x30(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    -0x44(%rbp),%edx
call   1080 <bind@plt>
cmp    $0xffffffff,%eax
jne    12b7 <main+0x107>
lea    0xd8e(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d10(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x14(%rbp),%edi
mov    $0x5,%esi
call   1070 <listen@plt>
cmp    $0xffffffff,%eax
jne    12e7 <main+0x137>
lea    0xd76(%rip),%rdi
call   1090 <perror@plt>
mov    0x2ce0(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
movl   $0x0,-0x20(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x40(%rbp),%rsi
lea    -0x44(%rbp),%rdx
call   10a0 <accept@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    131c <main+0x16c>
lea    0xd60(%rip),%rdi
call   1090 <perror@plt>
jmp    12ee <main+0x13e>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%edi
mov    -0x20(%rbp),%esi
call   10c0 <treat_request@plt>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
cmp    $0xffffffff,%eax
jne    134d <main+0x19d>
lea    0xd3b(%rip),%rdi
call   1090 <perror@plt>
jmp    12ee <main+0x13e>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
movl   $0xc,(%rsp)
mov    0x2e14(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2df2(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
cmp    $0xffffffff,%eax
je     12f7 <main+0x147>
mov    %eax,%ebx
movl   $0x1,0x4(%rsp)
mov    0x2def(%rip),%rax
mov    (%rax),%esi
mov    0x2db6(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
je     1300 <main+0x150>
mov    0x0(%rbp),%eax
mov    %eax,0x10(%rsp)
mov    $0x926,%edi
call   1040 <htons@plt>
mov    %eax,0xc(%rsp)
mov    0x2d9d(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1080 <bind@plt>
cmp    $0xffffffff,%eax
je     1309 <main+0x159>
mov    %ebx,%edi
mov    $0x5,%esi
call   1070 <listen@plt>
cmp    $0xffffffff,%eax
je     1312 <main+0x162>
xor    %r14d,%r14d
lea    0x18(%rsp),%r15
mov    %rsp,%rbp
lea    0xdeb(%rip),%r12
nopw   0x0(%rax,%rax,1)
mov    %ebx,%edi
mov    %r15,%rsi
mov    %rbp,%rdx
call   10a0 <accept@plt>
mov    %eax,%r13d
cmp    $0xffffffff,%eax
jne    12cd <main+0x11d>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r12,%rdi
call   1090 <perror@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    %rbp,%rdx
call   10a0 <accept@plt>
cmp    $0xffffffff,%eax
je     12b0 <main+0x100>
mov    %eax,%r13d
add    $0x1,%r14d
mov    %r13d,%edi
mov    %r14d,%esi
call   10c0 <treat_request@plt>
mov    %r13d,%edi
call   1060 <close@plt>
cmp    $0xffffffff,%eax
jne    1290 <main+0xe0>
lea    0xd93(%rip),%rdi
call   1090 <perror@plt>
jmp    1290 <main+0xe0>
lea    0xd02(%rip),%rdi
jmp    1319 <main+0x169>
lea    0xd0f(%rip),%rdi
jmp    1319 <main+0x169>
lea    0xd22(%rip),%rdi
jmp    1319 <main+0x169>
lea    0xd31(%rip),%rdi
call   1090 <perror@plt>
mov    0x2c9b(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [22 x i8] c"Error creating socket\00"
@EXIT_FAILURE = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [28 x i8] c"Error setting socket option\00"
@INADDR_ANY = global i32 0
@.str.2 = [24 x i8] c"Error binding to socket\00"
@.str.3 = [40 x i8] c"Error setting connection to server mode\00"
@.str.4 = [17 x i8] c"Connection error\00"
@.str.5 = [25 x i8] c"Error closing connection\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.sockaddr_in
%11 = alloca %struct.sockaddr_in
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 12, i32* %12
%13 = load i32, i32* @AF_INET
%14 = load i32, i32* @SOCK_STREAM
%15 = call i32 @socket(i32 %13, i32 %14, i32 0)
store i32 %15, i32* %6
%16 = load i32, i32* %6
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %22
18: ; preds = %2
%19 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%20 = load i32, i32* @EXIT_FAILURE
%21 = call i32 @exit(i32 %20)
unreachable
22: ; preds = %2
store i32 1, i32* %8
%23 = load i32, i32* %6
%24 = load i32, i32* @SOL_SOCKET
%25 = load i32, i32* @SO_REUSEADDR
%26 = call i32 @setsockopt(i32 %23, i32 %24, i32 %25, i32* %8, i32 4)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %32
28: ; preds = %22
%29 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
%30 = load i32, i32* @EXIT_FAILURE
%31 = call i32 @exit(i32 %30)
unreachable
32: ; preds = %22
%33 = load i32, i32* @AF_INET
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 2
store i32 %33, i32* %34
%35 = call i32 @htons(i32 2342)
%36 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 1
store i32 %35, i32* %36
%37 = load i32, i32* @INADDR_ANY
%38 = call i32 @htonl(i32 %37)
%39 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 0
%40 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %39, i32 0, i32 0
store i32 %38, i32* %40
%41 = load i32, i32* %6
%42 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%43 = load i32, i32* %12
%44 = call i32 @bind(i32 %41, %struct.sockaddr* %42, i32 %43)
%45 = icmp eq i32 %44, -1
br i1 %45, label %46, label %50
46: ; preds = %32
%47 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
%48 = load i32, i32* @EXIT_FAILURE
%49 = call i32 @exit(i32 %48)
unreachable
50: ; preds = %32
%51 = load i32, i32* %6
%52 = call i32 @listen(i32 %51, i32 5)
%53 = icmp eq i32 %52, -1
br i1 %53, label %54, label %58
54: ; preds = %50
%55 = call i32 @perror(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0))
%56 = load i32, i32* @EXIT_FAILURE
%57 = call i32 @exit(i32 %56)
unreachable
58: ; preds = %50
store i32 0, i32* %9
br label %59
59: ; preds = %58, %65, %78
%60 = load i32, i32* %6
%61 = bitcast %struct.sockaddr_in* %11 to %struct.sockaddr*
%62 = call i32 @accept(i32 %60, %struct.sockaddr* %61, i32* %12)
store i32 %62, i32* %7
%63 = load i32, i32* %7
%64 = icmp eq i32 %63, -1
br i1 %64, label %65, label %67
65: ; preds = %59
%66 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0))
br label %59
67: ; preds = %59
%68 = load i32, i32* %9
%69 = add nsw i32 %68, 1
store i32 %69, i32* %9
%70 = load i32, i32* %7
%71 = load i32, i32* %9
%72 = call i32 @treat_request(i32 %70, i32 %71)
%73 = load i32, i32* %7
%74 = call i32 @close(i32 %73)
%75 = icmp eq i32 %74, -1
br i1 %75, label %76, label %78
76: ; preds = %67
%77 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0))
br label %78
78: ; preds = %76, %67
br label %59
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [22 x i8] c"Error creating socket\00"
@EXIT_FAILURE = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [28 x i8] c"Error setting socket option\00"
@INADDR_ANY = global i32 0
@.str.2 = [24 x i8] c"Error binding to socket\00"
@.str.3 = [40 x i8] c"Error setting connection to server mode\00"
@.str.4 = [17 x i8] c"Connection error\00"
@.str.5 = [25 x i8] c"Error closing connection\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = alloca %struct.sockaddr_in
%6 = alloca i32
%7 = bitcast i32* %3 to i8*
%8 = bitcast %struct.sockaddr_in* %4 to i8*
%9 = bitcast %struct.sockaddr_in* %5 to i8*
%10 = bitcast i32* %6 to i8*
store i32 12, i32* %6
%11 = load i32, i32* @AF_INET
%12 = load i32, i32* @SOCK_STREAM
%13 = tail call i32 @socket(i32 %11, i32 %12, i32 0)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %19
15: ; preds = %2
%16 = tail call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%17 = load i32, i32* @EXIT_FAILURE
%18 = tail call i32 @exit(i32 %17)
unreachable
19: ; preds = %2
store i32 1, i32* %3
%20 = load i32, i32* @SOL_SOCKET
%21 = load i32, i32* @SO_REUSEADDR
%22 = call i32 @setsockopt(i32 %13, i32 %20, i32 %21, i32* %3, i32 4)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %28
24: ; preds = %19
%25 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
%26 = load i32, i32* @EXIT_FAILURE
%27 = call i32 @exit(i32 %26)
unreachable
28: ; preds = %19
%29 = load i32, i32* @AF_INET
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %29, i32* %30
%31 = call i32 @htons(i32 2342)
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
store i32 %31, i32* %32
%33 = load i32, i32* @INADDR_ANY
%34 = call i32 @htonl(i32 %33)
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0, i32 0
store i32 %34, i32* %35
%36 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%37 = call i32 @bind(i32 %13, %struct.sockaddr* %36, i32 12)
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %43
39: ; preds = %28
%40 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
%41 = load i32, i32* @EXIT_FAILURE
%42 = call i32 @exit(i32 %41)
unreachable
43: ; preds = %28
%44 = call i32 @listen(i32 %13, i32 5)
%45 = icmp eq i32 %44, -1
br i1 %45, label %48, label %46
46: ; preds = %43
%47 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
br label %65
48: ; preds = %43
%49 = call i32 @perror(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0))
%50 = load i32, i32* @EXIT_FAILURE
%51 = call i32 @exit(i32 %50)
unreachable
52: ; preds = %65, %52
%53 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0))
%54 = call i32 @accept(i32 %13, %struct.sockaddr* %47, i32* %6)
%55 = icmp eq i32 %54, -1
br i1 %55, label %52, label %56
56: ; preds = %52, %65
%57 = phi i32 [ %67, %65 ], [ %54, %52 ]
%58 = add nuw nsw i32 %66, 1
%59 = call i32 @treat_request(i32 %57, i32 %58)
%60 = call i32 @close(i32 %57)
%61 = icmp eq i32 %60, -1
br i1 %61, label %62, label %64
62: ; preds = %56
%63 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0))
br label %64
64: ; preds = %62, %56
br label %65
65: ; preds = %64, %46
%66 = phi i32 [ 0, %46 ], [ %58, %64 ]
%67 = call i32 @accept(i32 %13, %struct.sockaddr* %47, i32* %6)
%68 = icmp eq i32 %67, -1
br i1 %68, label %52, label %56
}