#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*/ servAddr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, int /*<<< orphan*/ *);
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ htons(int);
int 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*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strlen(char *);
int main() {
  int ret = 0;
  int sockfd = socket(AF_INET, SOCK_STREAM, 0);
  if (sockfd == -1) {
    perror("[SERVER SOCKET] Error Creating Socket!\n");
    return 1;
  }
  struct sockaddr_in servAddr;
  servAddr.sin_family = AF_INET;
  servAddr.sin_addr.s_addr = INADDR_ANY;
  servAddr.sin_port = htons(50123);
  int addrlen = sizeof(servAddr);
  ret = bind(sockfd, (struct sockaddr *)&servAddr, addrlen);
  if (ret == -1) {
    perror("[SERVER SOCKET] Error bind!\n");
    return 1;
  }
  printf("[SERVER SOCKET] listening for connection...\n");
  static int count = 0;
  ret = listen(sockfd, 5);
  if (ret == -1) {
    perror("[SERVER SOCKET] Error listen!\n");
    return 1;
  }
  printf("[SERVER SOCKET] accepting connection... %d\n", ++count);
  int new_socket =
      accept(sockfd, (struct sockaddr *)&servAddr, (socklen_t *)&addrlen);
  if (new_socket == -1) {
    perror("[SERVER SOCKET] Error accept!\n");
    return 1;
  }
  char data[(4 * 1024)] = {0};
  int valread = 0;
  do {
    valread = read(new_socket, data, (4 * 1024));
    send(new_socket, data, strlen(data), 0);
  } while (valread);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1040,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
mov    0x2e18(%rip),%rax
mov    (%rax),%edi
mov    0x2df7(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    1207 <main+0x57>
lea    0xe0a(%rip),%rdi
call   10b0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    137d <main+0x1cd>
mov    0x2dda(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2dc6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    $0xc3cb,%edi
call   1040 <htons@plt>
mov    %eax,-0x18(%rbp)
movl   $0xc,-0x1c(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   10a0 <bind@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    1267 <main+0xb7>
lea    0xdd2(%rip),%rdi
call   10b0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    137d <main+0x1cd>
lea    0xdd7(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0xc(%rbp),%edi
mov    $0x5,%esi
call   1090 <listen@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    12a7 <main+0xf7>
lea    0xddc(%rip),%rdi
call   10b0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    137d <main+0x1cd>
mov    0x2ddb(%rip),%esi
add    $0x1,%esi
mov    %esi,0x2dd2(%rip)
lea    0xdd4(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0xc(%rbp),%edi
lea    -0x18(%rbp),%rsi
lea    -0x1c(%rbp),%rdx
call   10c0 <accept@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    12f9 <main+0x149>
lea    0xdd5(%rip),%rdi
call   10b0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    137d <main+0x1cd>
lea    -0x1020(%rbp),%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1070 <memset@plt>
movl   $0x0,-0x1024(%rbp)
mov    -0x20(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    $0x1000,%edx
call   1080 <read@plt>
mov    %eax,-0x1024(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x1034(%rbp)
lea    -0x1020(%rbp),%rax
mov    %rax,-0x1030(%rbp)
lea    -0x1020(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x1034(%rbp),%edi
mov    -0x1030(%rbp),%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1050 <send@plt>
cmpl   $0x0,-0x1024(%rbp)
jne    1316 <main+0x166>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x1020,%rsp
mov    0x2e16(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2df4(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
cmp    $0xffffffff,%eax
je     12cf <main+0x10f>
mov    %eax,%ebx
mov    0x0(%rbp),%eax
mov    %eax,0x18(%rsp)
mov    0x2de2(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    $0xc3cb,%edi
call   1050 <htons@plt>
mov    %eax,0x10(%rsp)
movl   $0xc,0xc(%rsp)
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   10b0 <bind@plt>
cmp    $0xffffffff,%eax
je     12d8 <main+0x118>
lea    0xe74(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
mov    $0x5,%esi
call   10a0 <listen@plt>
cmp    $0xffffffff,%eax
je     12e1 <main+0x121>
mov    0x2e35(%rip),%esi
add    $0x1,%esi
mov    %esi,0x2e2c(%rip)
lea    0xdf9(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
lea    0x10(%rsp),%rsi
lea    0xc(%rsp),%rdx
mov    %ebx,%edi
call   10d0 <accept@plt>
cmp    $0xffffffff,%eax
je     12ea <main+0x12a>
mov    %eax,%ebx
lea    0x20(%rsp),%r14
mov    $0x1000,%edx
mov    %r14,%rdi
xor    %esi,%esi
call   1080 <memset@plt>
xchg   %ax,%ax
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1000,%edx
call   1090 <read@plt>
mov    %eax,%ebp
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
test   %ebp,%ebp
jne    12a0 <main+0xe0>
xor    %eax,%eax
jmp    12fb <main+0x13b>
lea    0xd2a(%rip),%rdi
jmp    12f1 <main+0x131>
lea    0xd49(%rip),%rdi
jmp    12f1 <main+0x131>
lea    0xd5d(%rip),%rdi
jmp    12f1 <main+0x131>
lea    0xd9f(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%eax
add    $0x1020,%rsp
pop    %rbx
pop    %r14
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 = [40 x i8] c"[SERVER SOCKET] Error Creating Socket!\0A\00"
@INADDR_ANY = global i32 0
@.str.1 = [29 x i8] c"[SERVER SOCKET] Error bind!\0A\00"
@.str.2 = [45 x i8] c"[SERVER SOCKET] listening for connection...\0A\00"
@main.count = internal global i32 0
@.str.3 = [31 x i8] c"[SERVER SOCKET] Error listen!\0A\00"
@.str.4 = [44 x i8] c"[SERVER SOCKET] accepting connection... %d\0A\00"
@.str.5 = [31 x i8] c"[SERVER SOCKET] Error accept!\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = alloca i32
%7 = alloca [4096 x i8]
%8 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %2
%9 = load i32, i32* @AF_INET
%10 = load i32, i32* @SOCK_STREAM
%11 = call i32 @socket(i32 %9, i32 %10, i32 0)
store i32 %11, i32* %3
%12 = load i32, i32* %3
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %0
%15 = call i32 @perror(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %66
16: ; preds = %0
%17 = load i32, i32* @AF_INET
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 2
store i32 %17, i32* %18
%19 = load i32, i32* @INADDR_ANY
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 1
%21 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %20, i32 0, i32 0
store i32 %19, i32* %21
%22 = call i32 @htons(i32 50123)
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 0
store i32 %22, i32* %23
store i32 12, i32* %5
%24 = load i32, i32* %3
%25 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%26 = load i32, i32* %5
%27 = call i32 @bind(i32 %24, %struct.sockaddr* %25, i32 %26)
store i32 %27, i32* %2
%28 = load i32, i32* %2
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %32
30: ; preds = %16
%31 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
br label %66
32: ; preds = %16
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0))
%34 = load i32, i32* %3
%35 = call i32 @listen(i32 %34, i32 5)
store i32 %35, i32* %2
%36 = load i32, i32* %2
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %40
38: ; preds = %32
%39 = call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %1
br label %66
40: ; preds = %32
%41 = load i32, i32* @main.count
%42 = add nsw i32 %41, 1
store i32 %42, i32* @main.count
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.4, i64 0, i64 0), i32 %42)
%44 = load i32, i32* %3
%45 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%46 = call i32 @accept(i32 %44, %struct.sockaddr* %45, i32* %5)
store i32 %46, i32* %6
%47 = load i32, i32* %6
%48 = icmp eq i32 %47, -1
br i1 %48, label %49, label %51
49: ; preds = %40
%50 = call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %1
br label %66
51: ; preds = %40
%52 = bitcast [4096 x i8]* %7 to i8*
store i32 0, i32* %8
br label %53
53: ; preds = %62, %51
%54 = load i32, i32* %6
%55 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%56 = call i32 @read(i32 %54, i8* %55, i32 4096)
store i32 %56, i32* %8
%57 = load i32, i32* %6
%58 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%59 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%60 = call i32 @strlen(i8* %59)
%61 = call i32 @send(i32 %57, i8* %58, i32 %60, i32 0)
br label %62
62: ; preds = %53
%63 = load i32, i32* %8
%64 = icmp ne i32 %63, 0
br i1 %64, label %53, label %65
65: ; preds = %62
store i32 0, i32* %1
br label %66
66: ; preds = %65, %49, %38, %30, %14
%67 = load i32, i32* %1
ret i32 %67
}

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 = [40 x i8] c"[SERVER SOCKET] Error Creating Socket!\0A\00"
@INADDR_ANY = global i32 0
@.str.1 = [29 x i8] c"[SERVER SOCKET] Error bind!\0A\00"
@main.count = internal unnamed_addr global i32 0
@.str.3 = [31 x i8] c"[SERVER SOCKET] Error listen!\0A\00"
@.str.4 = [44 x i8] c"[SERVER SOCKET] accepting connection... %d\0A\00"
@.str.5 = [31 x i8] c"[SERVER SOCKET] Error accept!\0A\00"
@str = [44 x i8] c"[SERVER SOCKET] listening for connection...\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_in
%2 = alloca i32
%3 = alloca [4096 x i8]
%4 = load i32, i32* @AF_INET
%5 = load i32, i32* @SOCK_STREAM
%6 = tail call i32 @socket(i32 %4, i32 %5, i32 0)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = tail call i32 @perror(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0))
br label %48
10: ; preds = %0
%11 = bitcast %struct.sockaddr_in* %1 to i8*
%12 = load i32, i32* @AF_INET
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %12, i32* %13
%14 = load i32, i32* @INADDR_ANY
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1, i32 0
store i32 %14, i32* %15
%16 = tail call i32 @htons(i32 50123)
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
store i32 %16, i32* %17
%18 = bitcast i32* %2 to i8*
store i32 12, i32* %2
%19 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%20 = call i32 @bind(i32 %6, %struct.sockaddr* %19, i32 12)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %24
22: ; preds = %10
%23 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
br label %46
24: ; preds = %10
%25 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @str, i64 0, i64 0))
%26 = call i32 @listen(i32 %6, i32 5)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %30
28: ; preds = %24
%29 = call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0))
br label %46
30: ; preds = %24
%31 = load i32, i32* @main.count
%32 = add nsw i32 %31, 1
store i32 %32, i32* @main.count
%33 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @.str.4, i64 0, i64 0), i32 %32)
%34 = call i32 @accept(i32 %6, %struct.sockaddr* %19, i32* %2)
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %38
36: ; preds = %30
%37 = call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0))
br label %46
38: ; preds = %30
%39 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
br label %40
40: ; preds = %40, %38
%41 = call i32 @read(i32 %34, i8* %39, i32 4096)
%42 = call i32 @strlen(i8* %39)
%43 = call i32 @send(i32 %34, i8* %39, i32 %42, i32 0)
%44 = icmp eq i32 %41, 0
br i1 %44, label %45, label %40
45: ; preds = %40
br label %46
46: ; preds = %36, %45, %28, %22
%47 = phi i32 [ 1, %22 ], [ 1, %28 ], [ 1, %36 ], [ 0, %45 ]
br label %48
48: ; preds = %46, %8
%49 = phi i32 [ 1, %8 ], [ %47, %46 ]
ret i32 %49
}