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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ INADDR_ANY;
int SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int accept(int, struct sockaddr *, int *);
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int listen(int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ recv(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ send(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, int);
int socket(int /*<<< orphan*/, int, int /*<<< orphan*/);
int main() {
  int port = 8080;
  int allowconn = 10;
  int socktype = SOCK_STREAM;
  int sockoptval = 1;
  int srvsock;
  int clisock;
  int bindedsock;
  int listensock;
  char climsg[2048];
  char srvmsg[] = "HTTP/1.1 200 OK\r\n"
                  "Content-Type: text/html; charset=UTF-8\r\n\r\n"
                  "<!DOCTYPE html>"
                  "<html>"
                  "<head>"
                  "<title>Simple Server</title>"
                  "</head>"
                  "<body>"
                  "<p>Kaisar Server</p>"
                  "</body></html>\r\n";
  struct sockaddr_in srv;
  struct sockaddr_in cli;
  srv.sin_family = AF_INET;
  srv.sin_port = htons(port);
  srv.sin_addr.s_addr = INADDR_ANY;
  socklen_t socksize = sizeof(cli);
  srvsock = socket(srv.sin_family, socktype, 0);
  if (srvsock < 0) {
    perror("Unable to open socket");
    exit(EXIT_FAILURE);
  }
  setsockopt(srvsock, SOL_SOCKET, SO_REUSEADDR, &sockoptval, sizeof sockoptval);
  bindedsock = bind(srvsock, (struct sockaddr *)&srv, sizeof(struct sockaddr));
  if (bindedsock < 0) {
    perror("Unable to bind socket");
    exit(EXIT_FAILURE);
  }
  listensock = listen(srvsock, allowconn);
  if (listensock < 0) {
    perror("Unable to listen socket");
    exit(EXIT_FAILURE);
  }
  printf("Server started at port %i\n", port);
  while (1) {
    clisock = accept(srvsock, (struct sockaddr *)&cli, &socksize);
    if (clisock > -1) {
      recv(clisock, climsg, sizeof(climsg), 0);
      printf("%s\n", climsg);
      send(clisock, srvmsg, ((sizeof(srvmsg)) - 1), 0);
      close(clisock);
    }
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x910,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x1f90,-0x8(%rbp)
movl   $0xa,-0xc(%rbp)
mov    0x2dd1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
movl   $0x1,-0x14(%rbp)
lea    -0x8e0(%rbp),%rdi
lea    0xdef(%rip),%rsi
mov    $0xa4,%edx
call   1090 <memcpy@plt>
mov    0x2dbe(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8f0(%rbp)
mov    -0x8(%rbp),%edi
call   1050 <htons@plt>
mov    %eax,-0x8e8(%rbp)
mov    0x2d99(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8ec(%rbp)
movl   $0xc,-0x904(%rbp)
mov    -0x8f0(%rbp),%edi
mov    -0x10(%rbp),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1288 <main+0xb8>
lea    0xe2f(%rip),%rdi
call   10c0 <perror@plt>
mov    0x2d3f(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>
mov    -0x18(%rbp),%edi
mov    0x2d56(%rip),%rax
mov    (%rax),%esi
mov    0x2d1d(%rip),%rax
mov    (%rax),%edx
lea    -0x14(%rbp),%rcx
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
mov    -0x18(%rbp),%edi
lea    -0x8f0(%rbp),%rsi
mov    $0x4,%edx
call   10b0 <bind@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    12e7 <main+0x117>
lea    0xde6(%rip),%rdi
call   10c0 <perror@plt>
mov    0x2ce0(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>
mov    -0x18(%rbp),%edi
mov    -0xc(%rbp),%esi
call   10a0 <listen@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1319 <main+0x149>
lea    0xdca(%rip),%rdi
call   10c0 <perror@plt>
mov    0x2cae(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>
mov    -0x8(%rbp),%esi
lea    0xdc5(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x18(%rbp),%edi
lea    -0x900(%rbp),%rsi
lea    -0x904(%rbp),%rdx
call   10d0 <accept@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jle    1396 <main+0x1c6>
mov    -0x1c(%rbp),%edi
lea    -0x830(%rbp),%rsi
mov    $0x800,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
lea    -0x830(%rbp),%rsi
lea    0xd92(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x1c(%rbp),%edi
lea    -0x8e0(%rbp),%rsi
mov    $0xa3,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
mov    -0x1c(%rbp),%edi
call   1080 <close@plt>
jmp    132a <main+0x15a>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x8e8,%rsp
mov    0x2dd0(%rip),%rax
mov    (%rax),%ebx
movl   $0x1,0xc(%rsp)
lea    0xdf7(%rip),%rsi
lea    0x30(%rsp),%rdi
mov    $0xa4,%edx
call   10a0 <memcpy@plt>
mov    0x2dc1(%rip),%rax
mov    (%rax),%ebp
mov    %ebp,0x10(%rsp)
mov    $0x1f90,%edi
call   1060 <htons@plt>
mov    %eax,0x18(%rsp)
mov    0x2d9e(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
movl   $0xc,0x8(%rsp)
mov    %ebp,%edi
mov    %ebx,%esi
xor    %edx,%edx
call   1100 <socket@plt>
test   %eax,%eax
js     1327 <main+0x147>
mov    %eax,%r13d
mov    0x2d83(%rip),%rax
mov    (%rax),%esi
mov    0x2d4a(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %r13d,%edi
mov    $0x4,%r8d
call   1050 <setsockopt@plt>
lea    0x10(%rsp),%rsi
mov    %r13d,%edi
mov    $0x4,%edx
call   10c0 <bind@plt>
test   %eax,%eax
js     1330 <main+0x150>
mov    %r13d,%edi
mov    $0xa,%esi
call   10b0 <listen@plt>
test   %eax,%eax
js     1339 <main+0x159>
lea    0xe2f(%rip),%rdi
mov    $0x1f90,%esi
xor    %eax,%eax
call   1080 <printf@plt>
lea    0x20(%rsp),%r14
lea    0x8(%rsp),%r15
lea    0xe0(%rsp),%rbx
lea    0x30(%rsp),%r12
nopl   0x0(%rax)
mov    %r13d,%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   10e0 <accept@plt>
test   %eax,%eax
js     12e0 <main+0x100>
mov    %eax,%ebp
mov    %eax,%edi
mov    %rbx,%rsi
mov    $0x800,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %rbx,%rdi
call   1040 <puts@plt>
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0xa3,%edx
xor    %ecx,%ecx
call   1070 <send@plt>
mov    %ebp,%edi
call   1090 <close@plt>
jmp    12e0 <main+0x100>
lea    0xd76(%rip),%rdi
jmp    1340 <main+0x160>
lea    0xd83(%rip),%rdi
jmp    1340 <main+0x160>
lea    0xd90(%rip),%rdi
call   10d0 <perror@plt>
mov    0x2c74(%rip),%rax
mov    (%rax),%edi
call   10f0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@SOCK_STREAM = global i32 0
@__const.main.srvmsg = [164 x i8] c"HTTP/1.1 200 OK\0D\0AContent-Type: text/html; charset=UTF-8\0D\0A\0D\0A<!DOCTYPE html><html><head><title>Simple Server</title></head><body><p>Kaisar Server</p></body></html>\0D\0A\00"
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@.str = [22 x i8] c"Unable to open socket\00"
@EXIT_FAILURE = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [22 x i8] c"Unable to bind socket\00"
@.str.2 = [24 x i8] c"Unable to listen socket\00"
@.str.3 = [27 x i8] c"Server started at port %i\0A\00"
@.str.4 = [4 x i8] c"%s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca [2048 x i8]
%11 = alloca [164 x i8]
%12 = alloca %struct.sockaddr_in
%13 = alloca %struct.sockaddr_in
%14 = alloca i32
store i32 0, i32* %1
store i32 8080, i32* %2
store i32 10, i32* %3
%15 = load i32, i32* @SOCK_STREAM
store i32 %15, i32* %4
store i32 1, i32* %5
%16 = bitcast [164 x i8]* %11 to i8*
%17 = load i32, i32* @AF_INET
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %12, i32 0, i32 0
store i32 %17, i32* %18
%19 = load i32, i32* %2
%20 = call i32 @htons(i32 %19)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %12, i32 0, i32 2
store i32 %20, i32* %21
%22 = load i32, i32* @INADDR_ANY
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %12, i32 0, i32 1
%24 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %23, i32 0, i32 0
store i32 %22, i32* %24
store i32 12, i32* %14
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %12, i32 0, i32 0
%26 = load i32, i32* %25
%27 = load i32, i32* %4
%28 = call i32 @socket(i32 %26, i32 %27, i32 0)
store i32 %28, i32* %6
%29 = load i32, i32* %6
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %35
31: ; preds = %0
%32 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%33 = load i32, i32* @EXIT_FAILURE
%34 = call i32 @exit(i32 %33)
unreachable
35: ; preds = %0
%36 = load i32, i32* %6
%37 = load i32, i32* @SOL_SOCKET
%38 = load i32, i32* @SO_REUSEADDR
%39 = call i32 @setsockopt(i32 %36, i32 %37, i32 %38, i32* %5, i32 4)
%40 = load i32, i32* %6
%41 = bitcast %struct.sockaddr_in* %12 to %struct.sockaddr*
%42 = call i32 @bind(i32 %40, %struct.sockaddr* %41, i32 4)
store i32 %42, i32* %8
%43 = load i32, i32* %8
%44 = icmp slt i32 %43, 0
br i1 %44, label %45, label %49
45: ; preds = %35
%46 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
%47 = load i32, i32* @EXIT_FAILURE
%48 = call i32 @exit(i32 %47)
unreachable
49: ; preds = %35
%50 = load i32, i32* %6
%51 = load i32, i32* %3
%52 = call i32 @listen(i32 %50, i32 %51)
store i32 %52, i32* %9
%53 = load i32, i32* %9
%54 = icmp slt i32 %53, 0
br i1 %54, label %55, label %59
55: ; preds = %49
%56 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
%57 = load i32, i32* @EXIT_FAILURE
%58 = call i32 @exit(i32 %57)
unreachable
59: ; preds = %49
%60 = load i32, i32* %2
%61 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0), i32 %60)
br label %62
62: ; preds = %59, %79
%63 = load i32, i32* %6
%64 = bitcast %struct.sockaddr_in* %13 to %struct.sockaddr*
%65 = call i32 @accept(i32 %63, %struct.sockaddr* %64, i32* %14)
store i32 %65, i32* %7
%66 = load i32, i32* %7
%67 = icmp sgt i32 %66, -1
br i1 %67, label %68, label %79
68: ; preds = %62
%69 = load i32, i32* %7
%70 = getelementptr inbounds [2048 x i8], [2048 x i8]* %10, i64 0, i64 0
%71 = call i32 @recv(i32 %69, i8* %70, i32 2048, i32 0)
%72 = getelementptr inbounds [2048 x i8], [2048 x i8]* %10, i64 0, i64 0
%73 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* %72)
%74 = load i32, i32* %7
%75 = getelementptr inbounds [164 x i8], [164 x i8]* %11, i64 0, i64 0
%76 = call i32 @send(i32 %74, i8* %75, i32 163, i32 0)
%77 = load i32, i32* %7
%78 = call i32 @close(i32 %77)
br label %79
79: ; preds = %68, %62
br label %62
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@SOCK_STREAM = global i32 0
@__const.main.srvmsg = [164 x i8] c"HTTP/1.1 200 OK\0D\0AContent-Type: text/html; charset=UTF-8\0D\0A\0D\0A<!DOCTYPE html><html><head><title>Simple Server</title></head><body><p>Kaisar Server</p></body></html>\0D\0A\00"
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@.str = [22 x i8] c"Unable to open socket\00"
@EXIT_FAILURE = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [22 x i8] c"Unable to bind socket\00"
@.str.2 = [24 x i8] c"Unable to listen socket\00"
@.str.3 = [27 x i8] c"Server started at port %i\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [2048 x i8]
%3 = alloca [164 x i8]
%4 = alloca %struct.sockaddr_in
%5 = alloca %struct.sockaddr_in
%6 = alloca i32
%7 = load i32, i32* @SOCK_STREAM
%8 = bitcast i32* %1 to i8*
store i32 1, i32* %1
%9 = getelementptr inbounds [2048 x i8], [2048 x i8]* %2, i64 0, i64 0
%10 = getelementptr inbounds [164 x i8], [164 x i8]* %3, i64 0, i64 0
%11 = bitcast %struct.sockaddr_in* %4 to i8*
%12 = bitcast %struct.sockaddr_in* %5 to i8*
%13 = load i32, i32* @AF_INET
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
store i32 %13, i32* %14
%15 = tail call i32 @htons(i32 8080)
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %15, i32* %16
%17 = load i32, i32* @INADDR_ANY
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1, i32 0
store i32 %17, i32* %18
%19 = bitcast i32* %6 to i8*
store i32 12, i32* %6
%20 = tail call i32 @socket(i32 %13, i32 %7, i32 0)
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %26
22: ; preds = %0
%23 = tail call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%24 = load i32, i32* @EXIT_FAILURE
%25 = tail call i32 @exit(i32 %24)
unreachable
26: ; preds = %0
%27 = load i32, i32* @SOL_SOCKET
%28 = load i32, i32* @SO_REUSEADDR
%29 = call i32 @setsockopt(i32 %20, i32 %27, i32 %28, i32* %1, i32 4)
%30 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%31 = call i32 @bind(i32 %20, %struct.sockaddr* %30, i32 4)
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %37
33: ; preds = %26
%34 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
%35 = load i32, i32* @EXIT_FAILURE
%36 = call i32 @exit(i32 %35)
unreachable
37: ; preds = %26
%38 = call i32 @listen(i32 %20, i32 10)
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %44
40: ; preds = %37
%41 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
%42 = load i32, i32* @EXIT_FAILURE
%43 = call i32 @exit(i32 %42)
unreachable
44: ; preds = %37
%45 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0), i32 8080)
%46 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
br label %47
47: ; preds = %55, %44
%48 = call i32 @accept(i32 %20, %struct.sockaddr* %46, i32* %6)
%49 = icmp sgt i32 %48, -1
br i1 %49, label %50, label %55
50: ; preds = %47
%51 = call i32 @recv(i32 %48, i8* %9, i32 2048, i32 0)
%52 = call i32 @puts(i8* %9)
%53 = call i32 @send(i32 %48, i8* %10, i32 163, i32 0)
%54 = call i32 @close(i32 %48)
br label %55
55: ; preds = %50, %47
br label %47
}