#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__ {
  void *s_addr;
};
struct sockaddr_in {
  void *sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ server_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int const, int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ bind(int const, struct sockaddr *, int);
int /*<<< orphan*/ close(int const);
void *htons(int);
int /*<<< orphan*/ listen(int const, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *);
int recv(int const, char *, int const, int /*<<< orphan*/);
int /*<<< orphan*/ send(int const, char *, int, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(void) {
  const int server_sock = socket(AF_INET, SOCK_STREAM, 0);
  struct sockaddr_in server_addr;
  memset(&server_addr, 0, sizeof(server_addr));
  server_addr.sin_family = AF_INET;
  server_addr.sin_addr.s_addr = htons(INADDR_ANY);
  server_addr.sin_port = htons(20000);
  bind(server_sock, (struct sockaddr *)&server_addr, sizeof(server_addr));
  printf("Waiting for connections from clients...\n");
  listen(server_sock, 10);
  const int client_sock = accept(server_sock, NULL, NULL);
  printf("Connection established.\n");
  const int buffer_sz = 100;
  char buffer[buffer_sz];
  int bytes_received = recv(client_sock, buffer, buffer_sz, 0);
  while (bytes_received > 0) {
    send(client_sock, buffer, bytes_received, 0);
    bytes_received = recv(client_sock, buffer, buffer_sz, 0);
  }
  close(client_sock);
  close(server_sock);
  printf("Connection closed.\n");
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e2f(%rip),%rax
mov    (%rax),%edi
mov    0x2e0e(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x8(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1070 <memset@plt>
mov    0x2e03(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2def(%rip),%rax
mov    (%rax),%edi
call   1040 <htons@plt>
mov    %rax,-0x18(%rbp)
mov    $0x4e20,%edi
call   1040 <htons@plt>
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x18,%edx
call   10a0 <bind@plt>
lea    0xdde(%rip),%rdi
call   1060 <printf@plt>
mov    -0x8(%rbp),%edi
mov    $0xa,%esi
call   1090 <listen@plt>
mov    -0x8(%rbp),%edi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rsi
call   10b0 <accept@plt>
mov    %eax,-0x24(%rbp)
lea    0xddc(%rip),%rdi
call   1060 <printf@plt>
movl   $0x64,-0x28(%rbp)
mov    %rsp,%rax
mov    %rax,-0x30(%rbp)
mov    -0x24(%rbp),%edi
lea    -0xa0(%rbp),%rsi
mov    $0x64,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jle    12b5 <main+0x115>
mov    -0x24(%rbp),%edi
mov    -0x34(%rbp),%edx
lea    -0xa0(%rbp),%rsi
xor    %ecx,%ecx
call   1050 <send@plt>
mov    -0x24(%rbp),%edi
lea    -0xa0(%rbp),%rsi
mov    $0x64,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x34(%rbp)
jmp    1279 <main+0xd9>
mov    -0x24(%rbp),%edi
call   1080 <close@plt>
mov    -0x8(%rbp),%edi
call   1080 <close@plt>
lea    0xd76(%rip),%rdi
call   1060 <printf@plt>
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x88,%rsp
mov    0x2e34(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2e12(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,%r14d
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1070 <memset@plt>
mov    0x0(%rbp),%eax
mov    %eax,0x18(%rsp)
mov    0x2df4(%rip),%rax
mov    (%rax),%edi
call   1050 <htons@plt>
mov    %rax,0x10(%rsp)
mov    $0x4e20,%edi
call   1050 <htons@plt>
mov    %rax,0x8(%rsp)
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x18,%edx
call   10a0 <bind@plt>
lea    0xde2(%rip),%rdi
call   1040 <puts@plt>
mov    %r14d,%edi
mov    $0xa,%esi
call   1090 <listen@plt>
mov    %r14d,%edi
xor    %esi,%esi
xor    %edx,%edx
call   10b0 <accept@plt>
mov    %eax,%ebp
lea    0xde3(%rip),%rdi
call   1040 <puts@plt>
mov    %rsp,%r15
lea    0x20(%rsp),%rsi
mov    %ebp,%edi
mov    $0x64,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
jle    1293 <main+0xf3>
lea    0x20(%rsp),%rbx
nopl   0x0(%rax)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x64,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
jg     1270 <main+0xd0>
mov    %ebp,%edi
call   1080 <close@plt>
mov    %r14d,%edi
call   1080 <close@plt>
lea    0xd97(%rip),%rdi
call   1040 <puts@plt>
mov    %r15,%rsp
xor    %eax,%eax
add    $0x88,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i8*, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i8* }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@INADDR_ANY = global i32 0
@.str = [41 x i8] c"Waiting for connections from clients...\0A\00"
@.str.1 = [25 x i8] c"Connection established.\0A\00"
@.str.2 = [20 x i8] c"Connection closed.\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
store i32 0, i32* %1
%8 = load i32, i32* @AF_INET
%9 = load i32, i32* @SOCK_STREAM
%10 = call i32 @socket(i32 %8, i32 %9, i32 0)
store i32 %10, i32* %2
%11 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 24)
%12 = load i32, i32* @AF_INET
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 2
store i32 %12, i32* %13
%14 = load i32, i32* @INADDR_ANY
%15 = call i8* @htons(i32 %14)
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 1
%17 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %16, i32 0, i32 0
store i8* %15, i8** %17
%18 = call i8* @htons(i32 20000)
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 0
store i8* %18, i8** %19
%20 = load i32, i32* %2
%21 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%22 = call i32 @bind(i32 %20, %struct.sockaddr* %21, i32 24)
%23 = call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0))
%24 = load i32, i32* %2
%25 = call i32 @listen(i32 %24, i32 10)
%26 = load i32, i32* %2
%27 = call i32 @accept(i32 %26, i32* null, i32* null)
store i32 %27, i32* %4
%28 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
store i32 100, i32* %5
%29 = call i8* @llvm.stacksave()
store i8* %29, i8** %6
%30 = alloca i8, i64 100
%31 = load i32, i32* %4
%32 = call i32 @recv(i32 %31, i8* %30, i32 100, i32 0)
store i32 %32, i32* %7
br label %33
33: ; preds = %36, %0
%34 = load i32, i32* %7
%35 = icmp sgt i32 %34, 0
br i1 %35, label %36, label %42
36: ; preds = %33
%37 = load i32, i32* %4
%38 = load i32, i32* %7
%39 = call i32 @send(i32 %37, i8* %30, i32 %38, i32 0)
%40 = load i32, i32* %4
%41 = call i32 @recv(i32 %40, i8* %30, i32 100, i32 0)
store i32 %41, i32* %7
br label %33
42: ; preds = %33
%43 = load i32, i32* %4
%44 = call i32 @close(i32 %43)
%45 = load i32, i32* %2
%46 = call i32 @close(i32 %45)
%47 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
%48 = load i8*, i8** %6
%49 = load i32, i32* %1
ret i32 %49
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i8*, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i8* }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@INADDR_ANY = global i32 0
@str = [40 x i8] c"Waiting for connections from clients...\00"
@str.3 = [24 x i8] c"Connection established.\00"
@str.4 = [19 x i8] c"Connection closed.\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_in
%2 = load i32, i32* @AF_INET
%3 = load i32, i32* @SOCK_STREAM
%4 = tail call i32 @socket(i32 %2, i32 %3, i32 0)
%5 = bitcast %struct.sockaddr_in* %1 to i8*
%6 = call i32 @memset(%struct.sockaddr_in* %1, i32 0, i32 24)
%7 = load i32, i32* @AF_INET
%8 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %7, i32* %8
%9 = load i32, i32* @INADDR_ANY
%10 = call i8* @htons(i32 %9)
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1, i32 0
store i8* %10, i8** %11
%12 = call i8* @htons(i32 20000)
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
store i8* %12, i8** %13
%14 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%15 = call i32 @bind(i32 %4, %struct.sockaddr* %14, i32 24)
%16 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @str, i64 0, i64 0))
%17 = call i32 @listen(i32 %4, i32 10)
%18 = call i32 @accept(i32 %4, i32* null, i32* null)
%19 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str.3, i64 0, i64 0))
%20 = call i8* @llvm.stacksave()
%21 = alloca [100 x i8]
%22 = getelementptr inbounds [100 x i8], [100 x i8]* %21, i64 0, i64 0
%23 = call i32 @recv(i32 %18, i8* %22, i32 100, i32 0)
%24 = icmp sgt i32 %23, 0
br i1 %24, label %25, label %30
25: ; preds = %0, %25
%26 = phi i32 [ %28, %25 ], [ %23, %0 ]
%27 = call i32 @send(i32 %18, i8* %22, i32 %26, i32 0)
%28 = call i32 @recv(i32 %18, i8* %22, i32 100, i32 0)
%29 = icmp sgt i32 %28, 0
br i1 %29, label %25, label %30
30: ; preds = %25, %0
%31 = call i32 @close(i32 %18)
%32 = call i32 @close(i32 %4)
%33 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.4, i64 0, i64 0))
ret i32 0
}