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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, int /*<<< orphan*/ *);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ listen(int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ puts(char *);
int recv(int, char *, int, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ write(int, char *, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int socket_desc, client_sock, c, read_size;
  struct sockaddr_in server, client;
  char client_message[2000];
  socket_desc = socket(AF_INET, SOCK_STREAM, 0);
  if (socket_desc == -1) {
    printf("Could not create socket");
  }
  puts("Socket created");
  server.sin_family = AF_INET;
  server.sin_addr.s_addr = INADDR_ANY;
  server.sin_port = htons(8888);
  if (bind(socket_desc, (struct sockaddr *)&server, sizeof(server)) < 0) {
    perror("bind failed. Error");
    return 1;
  }
  puts("bind done");
  listen(socket_desc, 3);
  puts("Waiting for incoming connections...");
  c = sizeof(struct sockaddr_in);
  client_sock =
      accept(socket_desc, (struct sockaddr *)&client, (socklen_t *)&c);
  if (client_sock < 0) {
    perror("accept failed");
    return 1;
  }
  puts("Connection accepted");
  while ((read_size = recv(client_sock, client_message, 2000, 0)) > 0) {
    write(client_sock, client_message, strlen(client_message));
  }
  if (read_size == 0) {
    puts("Client disconnected");
    fflush(stdout);
  } else if (read_size == -1) {
    perror("recv failed");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x820,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e08(%rip),%rax
mov    (%rax),%edi
mov    0x2de7(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    120b <main+0x4b>
lea    0xdfa(%rip),%rdi
call   1080 <printf@plt>
lea    0xe06(%rip),%rdi
call   1040 <puts@plt>
mov    0x2dca(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2db6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    $0x22b8,%edi
call   1070 <htons@plt>
mov    %eax,-0x30(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0xc,%edx
call   10b0 <bind@plt>
cmp    $0x0,%rax
jge    126f <main+0xaf>
lea    0xdc9(%rip),%rdi
call   10c0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1385 <main+0x1c5>
lea    0xdc4(%rip),%rdi
call   1040 <puts@plt>
mov    -0x14(%rbp),%edi
mov    $0x3,%esi
call   10a0 <listen@plt>
lea    0xdb5(%rip),%rdi
call   1040 <puts@plt>
movl   $0xc,-0x1c(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x40(%rbp),%rsi
lea    -0x1c(%rbp),%rdx
call   10d0 <accept@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    12d0 <main+0x110>
lea    0xda9(%rip),%rdi
call   10c0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1385 <main+0x1c5>
lea    0xd9f(%rip),%rdi
call   1040 <puts@plt>
mov    -0x18(%rbp),%edi
lea    -0x810(%rbp),%rsi
mov    $0x7d0,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jle    133a <main+0x17a>
mov    -0x18(%rbp),%eax
mov    %eax,-0x81c(%rbp)
lea    -0x810(%rbp),%rax
mov    %rax,-0x818(%rbp)
lea    -0x810(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x81c(%rbp),%edi
mov    -0x818(%rbp),%rsi
mov    %eax,%edx
call   1050 <write@plt>
jmp    12dc <main+0x11c>
cmpl   $0x0,-0x20(%rbp)
jne    1363 <main+0x1a3>
lea    0xd3f(%rip),%rdi
call   1040 <puts@plt>
mov    0x2c71(%rip),%rax
mov    (%rax),%edi
call   1090 <fflush@plt>
jmp    137e <main+0x1be>
cmpl   $0xffffffff,-0x20(%rbp)
jne    1379 <main+0x1b9>
lea    0xd2a(%rip),%rdi
call   10c0 <perror@plt>
jmp    137e <main+0x1be>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x820,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x800,%rsp
mov    0x2e16(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2df4(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    11f8 <main+0x38>
lea    0xe0d(%rip),%rdi
call   1080 <printf@plt>
lea    0xe19(%rip),%rdi
call   1040 <puts@plt>
mov    0x0(%rbp),%eax
mov    %eax,0x18(%rsp)
mov    0x2dce(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    $0x22b8,%edi
call   1070 <htons@plt>
mov    %eax,0x10(%rsp)
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   10b0 <bind@plt>
test   %rax,%rax
js     131c <main+0x15c>
lea    0xdf3(%rip),%rdi
call   1040 <puts@plt>
mov    %ebx,%edi
mov    $0x3,%esi
call   10a0 <listen@plt>
lea    0xde5(%rip),%rdi
call   1040 <puts@plt>
movl   $0xc,0xc(%rsp)
lea    0x20(%rsp),%rsi
lea    0xc(%rsp),%rdx
mov    %ebx,%edi
call   10d0 <accept@plt>
test   %eax,%eax
js     1325 <main+0x165>
mov    %eax,%ebp
lea    0xde8(%rip),%rdi
call   1040 <puts@plt>
lea    0x30(%rsp),%rsi
xor    %r14d,%r14d
mov    %ebp,%edi
mov    $0x7d0,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
jle    12e9 <main+0x129>
lea    0x30(%rsp),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rbx,%rdi
call   1060 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1050 <write@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x7d0,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
jg     12c0 <main+0x100>
test   %eax,%eax
je     1300 <main+0x140>
cmp    $0xffffffff,%eax
jne    1337 <main+0x177>
lea    0xda5(%rip),%rdi
call   10c0 <perror@plt>
jmp    1337 <main+0x177>
lea    0xd83(%rip),%rdi
call   1040 <puts@plt>
mov    0x2cb5(%rip),%rax
mov    (%rax),%edi
call   1090 <fflush@plt>
jmp    1337 <main+0x177>
lea    0xd04(%rip),%rdi
jmp    132c <main+0x16c>
lea    0xd3c(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%r14d
mov    %r14d,%eax
add    $0x800,%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 = [24 x i8] c"Could not create socket\00"
@.str.1 = [15 x i8] c"Socket created\00"
@INADDR_ANY = global i32 0
@.str.2 = [19 x i8] c"bind failed. Error\00"
@.str.3 = [10 x i8] c"bind done\00"
@.str.4 = [36 x i8] c"Waiting for incoming connections...\00"
@.str.5 = [14 x i8] c"accept failed\00"
@.str.6 = [20 x i8] c"Connection accepted\00"
@.str.7 = [20 x i8] c"Client disconnected\00"
@stdout = global i32 0
@.str.8 = [12 x i8] c"recv failed\00"
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 [2000 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%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 %20
18: ; preds = %2
%19 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
br label %20
20: ; preds = %18, %2
%21 = call i32 @puts(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%22 = load i32, i32* @AF_INET
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 2
store i32 %22, i32* %23
%24 = load i32, i32* @INADDR_ANY
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 1
%26 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %25, i32 0, i32 0
store i32 %24, i32* %26
%27 = call i32 @htons(i32 8888)
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 0
store i32 %27, i32* %28
%29 = load i32, i32* %6
%30 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%31 = call i64 @bind(i32 %29, %struct.sockaddr* %30, i32 12)
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %20
%34 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %74
35: ; preds = %20
%36 = call i32 @puts(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
%37 = load i32, i32* %6
%38 = call i32 @listen(i32 %37, i32 3)
%39 = call i32 @puts(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0))
store i32 12, i32* %8
%40 = load i32, i32* %6
%41 = bitcast %struct.sockaddr_in* %11 to %struct.sockaddr*
%42 = call i32 @accept(i32 %40, %struct.sockaddr* %41, i32* %8)
store i32 %42, i32* %7
%43 = load i32, i32* %7
%44 = icmp slt i32 %43, 0
br i1 %44, label %45, label %47
45: ; preds = %35
%46 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %3
br label %74
47: ; preds = %35
%48 = call i32 @puts(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.6, i64 0, i64 0))
br label %49
49: ; preds = %54, %47
%50 = load i32, i32* %7
%51 = getelementptr inbounds [2000 x i8], [2000 x i8]* %12, i64 0, i64 0
%52 = call i32 @recv(i32 %50, i8* %51, i32 2000, i32 0)
store i32 %52, i32* %9
%53 = icmp sgt i32 %52, 0
br i1 %53, label %54, label %60
54: ; preds = %49
%55 = load i32, i32* %7
%56 = getelementptr inbounds [2000 x i8], [2000 x i8]* %12, i64 0, i64 0
%57 = getelementptr inbounds [2000 x i8], [2000 x i8]* %12, i64 0, i64 0
%58 = call i32 @strlen(i8* %57)
%59 = call i32 @write(i32 %55, i8* %56, i32 %58)
br label %49
60: ; preds = %49
%61 = load i32, i32* %9
%62 = icmp eq i32 %61, 0
br i1 %62, label %63, label %67
63: ; preds = %60
%64 = call i32 @puts(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.7, i64 0, i64 0))
%65 = load i32, i32* @stdout
%66 = call i32 @fflush(i32 %65)
br label %73
67: ; preds = %60
%68 = load i32, i32* %9
%69 = icmp eq i32 %68, -1
br i1 %69, label %70, label %72
70: ; preds = %67
%71 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.8, i64 0, i64 0))
br label %72
72: ; preds = %70, %67
br label %73
73: ; preds = %72, %63
store i32 0, i32* %3
br label %74
74: ; preds = %73, %45, %33
%75 = load i32, i32* %3
ret i32 %75
}

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 = [24 x i8] c"Could not create socket\00"
@.str.1 = [15 x i8] c"Socket created\00"
@INADDR_ANY = global i32 0
@.str.2 = [19 x i8] c"bind failed. Error\00"
@.str.3 = [10 x i8] c"bind done\00"
@.str.4 = [36 x i8] c"Waiting for incoming connections...\00"
@.str.5 = [14 x i8] c"accept failed\00"
@.str.6 = [20 x i8] c"Connection accepted\00"
@.str.7 = [20 x i8] c"Client disconnected\00"
@stdout = global i32 0
@.str.8 = [12 x i8] c"recv failed\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = alloca %struct.sockaddr_in
%6 = alloca [2000 x i8]
%7 = bitcast i32* %3 to i8*
%8 = bitcast %struct.sockaddr_in* %4 to i8*
%9 = bitcast %struct.sockaddr_in* %5 to i8*
%10 = getelementptr inbounds [2000 x i8], [2000 x i8]* %6, i64 0, i64 0
%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 %17
15: ; preds = %2
%16 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
br label %17
17: ; preds = %15, %2
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%19 = load i32, i32* @AF_INET
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %19, i32* %20
%21 = load i32, i32* @INADDR_ANY
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1, i32 0
store i32 %21, i32* %22
%23 = tail call i32 @htons(i32 8888)
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
store i32 %23, i32* %24
%25 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%26 = call i64 @bind(i32 %13, %struct.sockaddr* %25, i32 12)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %17
%29 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
br label %56
30: ; preds = %17
%31 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
%32 = call i32 @listen(i32 %13, i32 3)
%33 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0))
store i32 12, i32* %3
%34 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%35 = call i32 @accept(i32 %13, %struct.sockaddr* %34, i32* %3)
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %30
%38 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0))
br label %56
39: ; preds = %30
%40 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.6, i64 0, i64 0))
%41 = call i32 @recv(i32 %35, i8* %10, i32 2000, i32 0)
%42 = icmp sgt i32 %41, 0
br i1 %42, label %43, label %48
43: ; preds = %39, %43
%44 = call i32 @strlen(i8* %10)
%45 = call i32 @write(i32 %35, i8* %10, i32 %44)
%46 = call i32 @recv(i32 %35, i8* %10, i32 2000, i32 0)
%47 = icmp sgt i32 %46, 0
br i1 %47, label %43, label %48
48: ; preds = %43, %39
%49 = phi i32 [ %41, %39 ], [ %46, %43 ]
switch i32 %49, label %56 [
i32 0, label %50
i32 -1, label %54
]
50: ; preds = %48
%51 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.7, i64 0, i64 0))
%52 = load i32, i32* @stdout
%53 = call i32 @fflush(i32 %52)
br label %56
54: ; preds = %48
%55 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.8, i64 0, i64 0))
br label %56
56: ; preds = %50, %54, %48, %37, %28
%57 = phi i32 [ 1, %28 ], [ 1, %37 ], [ 0, %48 ], [ 0, %54 ], [ 0, %50 ]
ret i32 %57
}