#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 size_t ssize_t;
typedef int /*<<< orphan*/ serv_addr;
typedef int /*<<< orphan*/ recv_buff;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
scalar_t__ listen(int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
size_t read(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(void) {
  static char recv_buff[5000];
  int mainsock;
  if ((mainsock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
    perror("socket(2) failed");
    exit(EXIT_FAILURE);
  }
  struct sockaddr_in serv_addr;
  serv_addr.sin_family = AF_INET;
  serv_addr.sin_port = htons(12000);
  serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  if (bind(mainsock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
    perror("bind(2) failed");
    exit(EXIT_FAILURE);
  }
  if (listen(mainsock, 10) < 0) {
    perror("listen(2) failed");
    exit(EXIT_FAILURE);
  }
  int sock;
  if ((sock = accept(mainsock, NULL, NULL)) < 0) {
    perror("accept(2) failed");
    exit(EXIT_FAILURE);
  }
  while (1) {
    ssize_t n;
    if ((n = read(sock, recv_buff, sizeof(recv_buff) - 1)) < 0) {
      perror("read(2) failed");
      break;
    }
    recv_buff[n] = '\0';
    printf("Server received: %s\n", recv_buff);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e32(%rip),%rax
mov    (%rax),%edi
mov    0x2e09(%rip),%rax
mov    (%rax),%esi
mov    0x2e10(%rip),%rax
mov    (%rax),%edx
call   10c0 <socket@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    11f5 <main+0x55>
lea    0xe1e(%rip),%rdi
call   1090 <perror@plt>
mov    0x2dd2(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    0x2dec(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    $0x2ee0,%edi
call   1030 <htons@plt>
mov    %eax,-0x14(%rbp)
mov    0x2dcb(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1080 <bind@plt>
cmp    $0x0,%rax
jge    1254 <main+0xb4>
lea    0xdd0(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d73(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x8(%rbp),%edi
mov    $0xa,%esi
call   1070 <listen@plt>
cmp    $0x0,%rax
jge    1285 <main+0xe5>
lea    0xdae(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d42(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x8(%rbp),%edi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rsi
call   10a0 <accept@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    12ba <main+0x11a>
lea    0xd8a(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d0d(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
jmp    12bf <main+0x11f>
mov    -0x1c(%rbp),%edi
lea    0x2db7(%rip),%rsi
mov    $0x1387,%edx
call   1060 <read@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jae    12f2 <main+0x152>
lea    0xd5a(%rip),%rdi
call   1090 <perror@plt>
jmp    1319 <main+0x179>
mov    -0x28(%rbp),%rcx
lea    0x2d83(%rip),%rax
movb   $0x0,(%rax,%rcx,1)
lea    0xd49(%rip),%rdi
lea    0x2d71(%rip),%rsi
call   1040 <printf@plt>
jmp    12bf <main+0x11f>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2e39(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2e0f(%rip),%rax
mov    (%rax),%esi
mov    0x2e16(%rip),%rax
mov    (%rax),%edx
call   10c0 <socket@plt>
test   %eax,%eax
js     1260 <main+0xc0>
mov    %eax,%ebx
mov    0x0(%rbp),%eax
mov    %eax,0x8(%rsp)
mov    $0x2ee0,%edi
call   1030 <htons@plt>
mov    %eax,0x4(%rsp)
mov    0x2df1(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,(%rsp)
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1080 <bind@plt>
test   %rax,%rax
js     1269 <main+0xc9>
mov    %ebx,%edi
mov    $0xa,%esi
call   1070 <listen@plt>
test   %rax,%rax
js     1272 <main+0xd2>
mov    %ebx,%edi
xor    %esi,%esi
xor    %edx,%edx
call   10a0 <accept@plt>
test   %eax,%eax
js     127b <main+0xdb>
mov    %eax,%ebp
lea    0x2e4a(%rip),%rbx
lea    0xe05(%rip),%r14
nopl   (%rax)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1387,%edx
call   1060 <read@plt>
movb   $0x0,(%rax,%rbx,1)
mov    %r14,%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
jmp    1240 <main+0xa0>
lea    0xd99(%rip),%rdi
jmp    1282 <main+0xe2>
lea    0xda1(%rip),%rdi
jmp    1282 <main+0xe2>
lea    0xda7(%rip),%rdi
jmp    1282 <main+0xe2>
lea    0xdaf(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d32(%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 }
@main.recv_buff = internal global [5000 x i8] zeroinitializer
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str = [17 x i8] c"socket(2) failed\00"
@EXIT_FAILURE = global i32 0
@INADDR_ANY = global i32 0
@.str.1 = [15 x i8] c"bind(2) failed\00"
@.str.2 = [17 x i8] c"listen(2) failed\00"
@.str.3 = [17 x i8] c"accept(2) failed\00"
@.str.4 = [15 x i8] c"read(2) failed\00"
@.str.5 = [21 x i8] c"Server received: %s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = alloca i64
store i32 0, i32* %1
%6 = load i32, i32* @AF_INET
%7 = load i32, i32* @SOCK_STREAM
%8 = load i32, i32* @IPPROTO_TCP
%9 = call i32 @socket(i32 %6, i32 %7, i32 %8)
store i32 %9, i32* %2
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %15
11: ; preds = %0
%12 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%13 = load i32, i32* @EXIT_FAILURE
%14 = call i32 @exit(i32 %13)
unreachable
15: ; preds = %0
%16 = load i32, i32* @AF_INET
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 2
store i32 %16, i32* %17
%18 = call i32 @htons(i32 12000)
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 1
store i32 %18, i32* %19
%20 = load i32, i32* @INADDR_ANY
%21 = call i32 @htonl(i32 %20)
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 0
%23 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %22, i32 0, i32 0
store i32 %21, i32* %23
%24 = load i32, i32* %2
%25 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%26 = call i64 @bind(i32 %24, %struct.sockaddr* %25, i32 12)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %32
28: ; preds = %15
%29 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%30 = load i32, i32* @EXIT_FAILURE
%31 = call i32 @exit(i32 %30)
unreachable
32: ; preds = %15
%33 = load i32, i32* %2
%34 = call i64 @listen(i32 %33, i32 10)
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %40
36: ; preds = %32
%37 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
%38 = load i32, i32* @EXIT_FAILURE
%39 = call i32 @exit(i32 %38)
unreachable
40: ; preds = %32
%41 = load i32, i32* %2
%42 = call i32 @accept(i32 %41, i32* null, i32* null)
store i32 %42, i32* %4
%43 = icmp slt i32 %42, 0
br i1 %43, label %44, label %48
44: ; preds = %40
%45 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
%46 = load i32, i32* @EXIT_FAILURE
%47 = call i32 @exit(i32 %46)
unreachable
48: ; preds = %40
br label %49
49: ; preds = %48, %55
%50 = load i32, i32* %4
%51 = call i64 @read(i32 %50, i8* getelementptr inbounds ([5000 x i8], [5000 x i8]* @main.recv_buff, i64 0, i64 0), i32 4999)
store i64 %51, i64* %5
%52 = icmp ult i64 %51, 0
br i1 %52, label %53, label %55
53: ; preds = %49
%54 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0))
br label %59
55: ; preds = %49
%56 = load i64, i64* %5
%57 = getelementptr inbounds [5000 x i8], [5000 x i8]* @main.recv_buff, i64 0, i64 %56
store i8 0, i8* %57
%58 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([5000 x i8], [5000 x i8]* @main.recv_buff, i64 0, i64 0))
br label %49
59: ; preds = %53
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@main.recv_buff = internal global [5000 x i8] zeroinitializer
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str = [17 x i8] c"socket(2) failed\00"
@EXIT_FAILURE = global i32 0
@INADDR_ANY = global i32 0
@.str.1 = [15 x i8] c"bind(2) failed\00"
@.str.2 = [17 x i8] c"listen(2) failed\00"
@.str.3 = [17 x i8] c"accept(2) failed\00"
@.str.5 = [21 x i8] c"Server received: %s\0A\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_in
%2 = load i32, i32* @AF_INET
%3 = load i32, i32* @SOCK_STREAM
%4 = load i32, i32* @IPPROTO_TCP
%5 = tail call i32 @socket(i32 %2, i32 %3, i32 %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %11
7: ; preds = %0
%8 = tail call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%9 = load i32, i32* @EXIT_FAILURE
%10 = tail call i32 @exit(i32 %9)
unreachable
11: ; preds = %0
%12 = bitcast %struct.sockaddr_in* %1 to i8*
%13 = load i32, i32* @AF_INET
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %13, i32* %14
%15 = tail call i32 @htons(i32 12000)
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1
store i32 %15, i32* %16
%17 = load i32, i32* @INADDR_ANY
%18 = tail call i32 @htonl(i32 %17)
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0, i32 0
store i32 %18, i32* %19
%20 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%21 = call i64 @bind(i32 %5, %struct.sockaddr* %20, i32 12)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %27
23: ; preds = %11
%24 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%25 = load i32, i32* @EXIT_FAILURE
%26 = call i32 @exit(i32 %25)
unreachable
27: ; preds = %11
%28 = call i64 @listen(i32 %5, i32 10)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %34
30: ; preds = %27
%31 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
%32 = load i32, i32* @EXIT_FAILURE
%33 = call i32 @exit(i32 %32)
unreachable
34: ; preds = %27
%35 = call i32 @accept(i32 %5, i32* null, i32* null)
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %41
37: ; preds = %34
%38 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
%39 = load i32, i32* @EXIT_FAILURE
%40 = call i32 @exit(i32 %39)
unreachable
41: ; preds = %34, %41
%42 = call i64 @read(i32 %35, i8* getelementptr inbounds ([5000 x i8], [5000 x i8]* @main.recv_buff, i64 0, i64 0), i32 4999)
%43 = getelementptr inbounds [5000 x i8], [5000 x i8]* @main.recv_buff, i64 0, i64 %42
store i8 0, i8* %43
%44 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([5000 x i8], [5000 x i8]* @main.recv_buff, i64 0, i64 0))
br label %41
}