#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*/ socklen_t;
typedef int /*<<< orphan*/ opt;
typedef int /*<<< orphan*/ address;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOL_SOCKET;
int SO_REUSEADDR;
int SO_REUSEPORT;
int accept(int, struct sockaddr *, int /*<<< orphan*/ *);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
scalar_t__ listen(int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int /*<<< orphan*/ send(int, char *, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int, int *, int);
int /*<<< orphan*/ strlen(char *);
int server_mode(int server_fd, int port) {
  int new_socket, valread;
  struct sockaddr_in address;
  int opt = 1;
  int addrlen = sizeof(address);
  char buffer[1024] = {0};
  char *hello = "Hello from server";
  if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt,
                 sizeof(opt))) {
    perror("setsockopt");
    exit(EXIT_FAILURE);
  }
  address.sin_family = AF_INET;
  address.sin_addr.s_addr = INADDR_ANY;
  address.sin_port = htons(port);
  if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0) {
    perror("ERROR: couldn't bind");
    exit(EXIT_FAILURE);
  }
  if (listen(server_fd, 3) < 0) {
    perror("ERROR: listen failure");
    exit(EXIT_FAILURE);
  }
  if ((new_socket = accept(server_fd, (struct sockaddr *)&address,
                           (socklen_t *)&addrlen)) < 0) {
    perror("ERROR: couldn't accept");
    exit(EXIT_FAILURE);
  }
  valread = read(new_socket, buffer, 1024);
  printf("%s\n", buffer);
  send(new_socket, hello, strlen(hello), 0);
  printf("Hello message sent\n");
  return 0;
}


x86 assembly (clang, -O0):
<server_mode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x450,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
movl   $0x1,-0x24(%rbp)
movl   $0xc,-0x28(%rbp)
lea    -0x430(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1080 <memset@plt>
lea    0xe07(%rip),%rax
mov    %rax,-0x438(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2dde(%rip),%rax
mov    (%rax),%esi
mov    0x2da5(%rip),%rax
mov    (%rax),%edx
mov    0x2dbc(%rip),%rax
or     (%rax),%edx
lea    -0x24(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
je     1251 <server_mode+0x91>
lea    0xdd4(%rip),%rdi
call   10c0 <perror@plt>
mov    0x2d76(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>
mov    0x2d88(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2d6c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
call   1050 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   10b0 <bind@plt>
cmp    $0x0,%rax
jge    12a9 <server_mode+0xe9>
lea    0xd87(%rip),%rdi
call   10c0 <perror@plt>
mov    0x2d1e(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>
mov    -0x4(%rbp),%edi
mov    $0x3,%esi
call   10a0 <listen@plt>
cmp    $0x0,%rax
jge    12da <server_mode+0x11a>
lea    0xd6b(%rip),%rdi
call   10c0 <perror@plt>
mov    0x2ced(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>
mov    -0x4(%rbp),%edi
lea    -0x20(%rbp),%rsi
lea    -0x28(%rbp),%rdx
call   10d0 <accept@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    1310 <server_mode+0x150>
lea    0xd4b(%rip),%rdi
call   10c0 <perror@plt>
mov    0x2cb7(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>
mov    -0xc(%rbp),%edi
lea    -0x430(%rbp),%rsi
mov    $0x400,%edx
call   1090 <read@plt>
mov    %eax,-0x10(%rbp)
lea    -0x430(%rbp),%rsi
lea    0xd2a(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x444(%rbp)
mov    -0x438(%rbp),%rax
mov    %rax,-0x440(%rbp)
mov    -0x438(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x444(%rbp),%edi
mov    -0x440(%rbp),%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
lea    0xce7(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
xor    %eax,%eax
add    $0x450,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<server_mode>:
push   %rbp
push   %rbx
sub    $0x428,%rsp
mov    %esi,%ebp
mov    %edi,%ebx
movl   $0x1,0xc(%rsp)
movl   $0xc,0x8(%rsp)
lea    0x20(%rsp),%rdi
mov    $0x400,%edx
xor    %esi,%esi
call   1080 <memset@plt>
mov    0x2df3(%rip),%rax
mov    (%rax),%esi
mov    0x2dba(%rip),%rax
mov    0x2dd3(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
test   %rax,%rax
jne    12dc <server_mode+0x11c>
mov    0x2db5(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    0x2d98(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    %ebp,%edi
call   1060 <htons@plt>
mov    %eax,0x10(%rsp)
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   10b0 <bind@plt>
test   %rax,%rax
js     12e5 <server_mode+0x125>
mov    %ebx,%edi
mov    $0x3,%esi
call   10a0 <listen@plt>
test   %rax,%rax
js     12ee <server_mode+0x12e>
lea    0x10(%rsp),%rsi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   10d0 <accept@plt>
test   %eax,%eax
js     12f7 <server_mode+0x137>
mov    %eax,%ebx
lea    0x20(%rsp),%rbp
mov    %eax,%edi
mov    %rbp,%rsi
mov    $0x400,%edx
call   1090 <read@plt>
mov    %rbp,%rdi
call   1030 <puts@plt>
lea    0xd52(%rip),%rbp
mov    %rbp,%rdi
call   1050 <strlen@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1070 <send@plt>
lea    0xd94(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x428,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xd2f(%rip),%rdi
jmp    12fe <server_mode+0x13e>
lea    0xd31(%rip),%rdi
jmp    12fe <server_mode+0x13e>
lea    0xd3d(%rip),%rdi
jmp    12fe <server_mode+0x13e>
lea    0xd4a(%rip),%rdi
call   10c0 <perror@plt>
mov    0x2cb6(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [18 x i8] c"Hello from server\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@SO_REUSEPORT = global i32 0
@.str.1 = [11 x i8] c"setsockopt\00"
@EXIT_FAILURE = global i32 0
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@.str.2 = [21 x i8] c"ERROR: couldn't bind\00"
@.str.3 = [22 x i8] c"ERROR: listen failure\00"
@.str.4 = [23 x i8] c"ERROR: couldn't accept\00"
@.str.5 = [4 x i8] c"%s\0A\00"
@.str.6 = [20 x i8] c"Hello message sent\0A\00"
define i32 @server_mode(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.sockaddr_in
%8 = alloca i32
%9 = alloca i32
%10 = alloca [1024 x i8]
%11 = alloca i8*
store i32 %0, i32* %3
store i32 %1, i32* %4
store i32 1, i32* %8
store i32 12, i32* %9
%12 = bitcast [1024 x i8]* %10 to i8*
store i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8** %11
%13 = load i32, i32* %3
%14 = load i32, i32* @SOL_SOCKET
%15 = load i32, i32* @SO_REUSEADDR
%16 = load i32, i32* @SO_REUSEPORT
%17 = or i32 %15, %16
%18 = call i64 @setsockopt(i32 %13, i32 %14, i32 %17, i32* %8, i32 4)
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %24
20: ; preds = %2
%21 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%22 = load i32, i32* @EXIT_FAILURE
%23 = call i32 @exit(i32 %22)
unreachable
24: ; preds = %2
%25 = load i32, i32* @AF_INET
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i32 %25, i32* %26
%27 = load i32, i32* @INADDR_ANY
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
%29 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %28, i32 0, i32 0
store i32 %27, i32* %29
%30 = load i32, i32* %4
%31 = call i32 @htons(i32 %30)
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
store i32 %31, i32* %32
%33 = load i32, i32* %3
%34 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%35 = call i64 @bind(i32 %33, %struct.sockaddr* %34, i32 12)
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %41
37: ; preds = %24
%38 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%39 = load i32, i32* @EXIT_FAILURE
%40 = call i32 @exit(i32 %39)
unreachable
41: ; preds = %24
%42 = load i32, i32* %3
%43 = call i64 @listen(i32 %42, i32 3)
%44 = icmp slt i64 %43, 0
br i1 %44, label %45, label %49
45: ; preds = %41
%46 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
%47 = load i32, i32* @EXIT_FAILURE
%48 = call i32 @exit(i32 %47)
unreachable
49: ; preds = %41
%50 = load i32, i32* %3
%51 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%52 = call i32 @accept(i32 %50, %struct.sockaddr* %51, i32* %9)
store i32 %52, i32* %5
%53 = icmp slt i32 %52, 0
br i1 %53, label %54, label %58
54: ; preds = %49
%55 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0))
%56 = load i32, i32* @EXIT_FAILURE
%57 = call i32 @exit(i32 %56)
unreachable
58: ; preds = %49
%59 = load i32, i32* %5
%60 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%61 = call i32 @read(i32 %59, i8* %60, i32 1024)
store i32 %61, i32* %6
%62 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%63 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i8* %62)
%64 = load i32, i32* %5
%65 = load i8*, i8** %11
%66 = load i8*, i8** %11
%67 = call i32 @strlen(i8* %66)
%68 = call i32 @send(i32 %64, i8* %65, i32 %67, i32 0)
%69 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.6, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [18 x i8] c"Hello from server\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@SO_REUSEPORT = global i32 0
@.str.1 = [11 x i8] c"setsockopt\00"
@EXIT_FAILURE = global i32 0
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@.str.2 = [21 x i8] c"ERROR: couldn't bind\00"
@.str.3 = [22 x i8] c"ERROR: listen failure\00"
@.str.4 = [23 x i8] c"ERROR: couldn't accept\00"
@str = [19 x i8] c"Hello message sent\00"
define i32 @server_mode(i32 %0, i32 %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = alloca i32
%6 = alloca [1024 x i8]
%7 = bitcast %struct.sockaddr_in* %3 to i8*
%8 = bitcast i32* %4 to i8*
store i32 1, i32* %4
%9 = bitcast i32* %5 to i8*
store i32 12, i32* %5
%10 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%11 = load i32, i32* @SOL_SOCKET
%12 = load i32, i32* @SO_REUSEADDR
%13 = load i32, i32* @SO_REUSEPORT
%14 = or i32 %13, %12
%15 = call i64 @setsockopt(i32 %0, i32 %11, i32 %14, i32* %4, i32 4)
%16 = icmp eq i64 %15, 0
br i1 %16, label %21, label %17
17: ; preds = %2
%18 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%19 = load i32, i32* @EXIT_FAILURE
%20 = call i32 @exit(i32 %19)
unreachable
21: ; preds = %2
%22 = load i32, i32* @AF_INET
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %22, i32* %23
%24 = load i32, i32* @INADDR_ANY
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
store i32 %24, i32* %25
%26 = call i32 @htons(i32 %1)
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %26, i32* %27
%28 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%29 = call i64 @bind(i32 %0, %struct.sockaddr* %28, i32 12)
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %35
31: ; preds = %21
%32 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%33 = load i32, i32* @EXIT_FAILURE
%34 = call i32 @exit(i32 %33)
unreachable
35: ; preds = %21
%36 = call i64 @listen(i32 %0, i32 3)
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %42
38: ; preds = %35
%39 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
%40 = load i32, i32* @EXIT_FAILURE
%41 = call i32 @exit(i32 %40)
unreachable
42: ; preds = %35
%43 = call i32 @accept(i32 %0, %struct.sockaddr* %28, i32* %5)
%44 = icmp slt i32 %43, 0
br i1 %44, label %45, label %49
45: ; preds = %42
%46 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0))
%47 = load i32, i32* @EXIT_FAILURE
%48 = call i32 @exit(i32 %47)
unreachable
49: ; preds = %42
%50 = call i32 @read(i32 %43, i8* %10, i32 1024)
%51 = call i32 @puts(i8* %10)
%52 = call i32 @strlen(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%53 = call i32 @send(i32 %43, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %52, i32 0)
%54 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
ret i32 0
}