#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*/ sock_addr;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOMAXCONN;
int accept(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ listen(int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ read(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ write(int, char *, int /*<<< orphan*/);
int main() {
  char buf[256];
  struct sockaddr_in sock_addr;
  sock_addr.sin_family = AF_INET;
  sock_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  sock_addr.sin_port = htons(31337);
  int listen_sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  bind(listen_sock, (struct sockaddr *)&sock_addr, sizeof(sock_addr));
  while (1) {
    listen(listen_sock, SOMAXCONN);
    int data_sock = accept(listen_sock, NULL, NULL);
    while (1) {
      read(data_sock, buf, sizeof(buf));
      buf[sizeof(buf) - 1] = '\0';
      printf("%s\n", buf);
      write(data_sock, buf, strlen(buf));
    }
    close(data_sock);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e2f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x118(%rbp)
mov    0x2e18(%rip),%rax
mov    (%rax),%edi
call   1070 <htonl@plt>
mov    %eax,-0x11c(%rbp)
mov    $0x7a69,%edi
call   1050 <htons@plt>
mov    %eax,-0x120(%rbp)
mov    0x2dfc(%rip),%rax
mov    (%rax),%edi
mov    0x2dcb(%rip),%rax
mov    (%rax),%esi
mov    0x2dda(%rip),%rax
mov    (%rax),%edx
call   10c0 <socket@plt>
mov    %eax,-0x124(%rbp)
mov    -0x124(%rbp),%edi
lea    -0x120(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
mov    -0x124(%rbp),%edi
mov    0x2d99(%rip),%rax
mov    (%rax),%esi
call   1090 <listen@plt>
mov    -0x124(%rbp),%edi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rsi
call   10b0 <accept@plt>
mov    %eax,-0x128(%rbp)
mov    -0x128(%rbp),%edi
lea    -0x110(%rbp),%rsi
mov    $0x100,%edx
call   1080 <read@plt>
movb   $0x0,-0x11(%rbp)
lea    -0x110(%rbp),%rsi
lea    0xd89(%rip),%rdi
call   1060 <printf@plt>
mov    -0x128(%rbp),%eax
mov    %eax,-0x134(%rbp)
lea    -0x110(%rbp),%rax
mov    %rax,-0x130(%rbp)
lea    -0x110(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x134(%rbp),%edi
mov    -0x130(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
jmp    124e <main+0xae>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x110,%rsp
mov    0x2e36(%rip),%rbx
mov    (%rbx),%eax
mov    %eax,0x8(%rsp)
mov    0x2e21(%rip),%rax
mov    (%rax),%edi
call   1070 <htonl@plt>
mov    %eax,0x4(%rsp)
mov    $0x7a69,%edi
call   1050 <htons@plt>
mov    %eax,(%rsp)
mov    (%rbx),%edi
mov    0x2de0(%rip),%rax
mov    (%rax),%esi
mov    0x2def(%rip),%rax
mov    (%rax),%edx
call   10c0 <socket@plt>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0xc,%edx
call   10a0 <bind@plt>
mov    0x2dc0(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1090 <listen@plt>
mov    %ebx,%edi
xor    %esi,%esi
xor    %edx,%edx
call   10b0 <accept@plt>
mov    %eax,%ebp
lea    0x10(%rsp),%rbx
lea    0xdd6(%rip),%r14
nopw   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x100,%edx
call   1080 <read@plt>
movb   $0x0,0x10f(%rsp)
mov    %r14,%rdi
mov    %rbx,%rsi
call   1060 <printf@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
jmp    1230 <main+0x90>

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
@INADDR_ANY = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@SOMAXCONN = global i32 0
@.str = [4 x i8] c"%s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [256 x i8]
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* %1
%6 = load i32, i32* @AF_INET
%7 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 2
store i32 %6, i32* %7
%8 = load i32, i32* @INADDR_ANY
%9 = call i32 @htonl(i32 %8)
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 1
%11 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %10, i32 0, i32 0
store i32 %9, i32* %11
%12 = call i32 @htons(i32 31337)
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 0
store i32 %12, i32* %13
%14 = load i32, i32* @AF_INET
%15 = load i32, i32* @SOCK_STREAM
%16 = load i32, i32* @IPPROTO_TCP
%17 = call i32 @socket(i32 %14, i32 %15, i32 %16)
store i32 %17, i32* %4
%18 = load i32, i32* %4
%19 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%20 = call i32 @bind(i32 %18, %struct.sockaddr* %19, i32 12)
br label %21
21: ; preds = %0
%22 = load i32, i32* %4
%23 = load i32, i32* @SOMAXCONN
%24 = call i32 @listen(i32 %22, i32 %23)
%25 = load i32, i32* %4
%26 = call i32 @accept(i32 %25, i32* null, i32* null)
store i32 %26, i32* %5
br label %27
27: ; preds = %21, %27
%28 = load i32, i32* %5
%29 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%30 = call i32 @read(i32 %28, i8* %29, i32 256)
%31 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 255
store i8 0, i8* %31
%32 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%33 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %32)
%34 = load i32, i32* %5
%35 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%36 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%37 = call i32 @strlen(i8* %36)
%38 = call i32 @write(i32 %34, i8* %35, i32 %37)
br label %27
}

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
@INADDR_ANY = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@SOMAXCONN = global i32 0
@.str = [4 x i8] c"%s\0A\00"
define i32 @main() {
%1 = alloca [256 x i8]
%2 = alloca %struct.sockaddr_in
%3 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
%4 = bitcast %struct.sockaddr_in* %2 to i8*
%5 = load i32, i32* @AF_INET
%6 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %5, i32* %6
%7 = load i32, i32* @INADDR_ANY
%8 = tail call i32 @htonl(i32 %7)
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1, i32 0
store i32 %8, i32* %9
%10 = tail call i32 @htons(i32 31337)
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
store i32 %10, i32* %11
%12 = load i32, i32* @AF_INET
%13 = load i32, i32* @SOCK_STREAM
%14 = load i32, i32* @IPPROTO_TCP
%15 = tail call i32 @socket(i32 %12, i32 %13, i32 %14)
%16 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%17 = call i32 @bind(i32 %15, %struct.sockaddr* %16, i32 12)
%18 = load i32, i32* @SOMAXCONN
%19 = call i32 @listen(i32 %15, i32 %18)
%20 = call i32 @accept(i32 %15, i32* null, i32* null)
%21 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 255
br label %22
22: ; preds = %22, %0
%23 = call i32 @read(i32 %20, i8* %3, i32 256)
store i8 0, i8* %21
%24 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %3)
%25 = call i32 @strlen(i8* %3)
%26 = call i32 @write(i32 %20, i8* %3, i32 %25)
br label %22
}