#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);
int /*<<< orphan*/ getchar();
void *htons(int);
int /*<<< orphan*/ listen(int const, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *);
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");
  getchar();
  close(client_sock);
  close(server_sock);
  printf("Connection closed.\n");
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
mov    0x2e28(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x4(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1050 <memset@plt>
mov    0x2e1d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e09(%rip),%rax
mov    (%rax),%edi
call   1030 <htons@plt>
mov    %rax,-0x18(%rbp)
mov    $0x4e20,%edi
call   1030 <htons@plt>
mov    %rax,-0x20(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x18,%edx
call   1090 <bind@plt>
lea    0xdf8(%rip),%rdi
call   1040 <printf@plt>
mov    -0x4(%rbp),%edi
mov    $0xa,%esi
call   1080 <listen@plt>
mov    -0x4(%rbp),%edi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rsi
call   10a0 <accept@plt>
mov    %eax,-0x24(%rbp)
lea    0xdf6(%rip),%rdi
call   1040 <printf@plt>
mov    $0x0,%al
call   1070 <getchar@plt>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
mov    -0x4(%rbp),%edi
call   1060 <close@plt>
lea    0xdec(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    0x2e49(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2e27(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,%ebx
lea    0x8(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1050 <memset@plt>
mov    0x0(%rbp),%eax
mov    %eax,0x18(%rsp)
mov    0x2e0a(%rip),%rax
mov    (%rax),%edi
call   1040 <htons@plt>
mov    %rax,0x10(%rsp)
mov    $0x4e20,%edi
call   1040 <htons@plt>
mov    %rax,0x8(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x18,%edx
call   1090 <bind@plt>
lea    0xdf9(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
mov    $0xa,%esi
call   1080 <listen@plt>
mov    %ebx,%edi
xor    %esi,%esi
xor    %edx,%edx
call   10a0 <accept@plt>
mov    %eax,%ebp
lea    0xdfc(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1070 <getchar@plt>
mov    %ebp,%edi
call   1060 <close@plt>
mov    %ebx,%edi
call   1060 <close@plt>
lea    0xdf3(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
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 %struct.sockaddr_in
%3 = alloca i32
%4 = load i32, i32* @AF_INET
%5 = load i32, i32* @SOCK_STREAM
%6 = call i32 @socket(i32 %4, i32 %5, i32 0)
store i32 %6, i32* %1
%7 = call i32 @memset(%struct.sockaddr_in* %2, i32 0, i32 24)
%8 = load i32, i32* @AF_INET
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 2
store i32 %8, i32* %9
%10 = load i32, i32* @INADDR_ANY
%11 = call i8* @htons(i32 %10)
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 1
%13 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %12, i32 0, i32 0
store i8* %11, i8** %13
%14 = call i8* @htons(i32 20000)
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 0
store i8* %14, i8** %15
%16 = load i32, i32* %1
%17 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%18 = call i32 @bind(i32 %16, %struct.sockaddr* %17, i32 24)
%19 = call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0))
%20 = load i32, i32* %1
%21 = call i32 @listen(i32 %20, i32 10)
%22 = load i32, i32* %1
%23 = call i32 @accept(i32 %22, i32* null, i32* null)
store i32 %23, i32* %3
%24 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
%25 = call i32 (...) @getchar()
%26 = load i32, i32* %3
%27 = call i32 @close(i32 %26)
%28 = load i32, i32* %1
%29 = call i32 @close(i32 %28)
%30 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
ret i32 0
}

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 i32 (...) @getchar()
%21 = call i32 @close(i32 %18)
%22 = call i32 @close(i32 %4)
%23 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.4, i64 0, i64 0))
ret i32 0
}