#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 socklen_t;
typedef int /*<<< orphan*/ servaddr;
typedef int /*<<< orphan*/ cliaddr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, int *);
int /*<<< orphan*/ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ listen(int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *);
int recvfrom(int, char *, int, int /*<<< orphan*/, struct sockaddr *, int *);
int /*<<< orphan*/ sendto(int, char *, int /*<<< orphan*/, int /*<<< orphan*/,
                          struct sockaddr *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strlen(char *);
int main(int argc, char **argv) {
  int listenfd, connfd, n;
  struct sockaddr_in servaddr, cliaddr;
  socklen_t clilen;
  char *banner = "Hello TCP client! This is TCP server";
  char buffer[1000];
  listenfd = socket(AF_INET, SOCK_STREAM, 0);
  servaddr.sin_family = AF_INET;
  servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
  servaddr.sin_port = htons(32000);
  bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
  listen(listenfd, 0);
  clilen = sizeof(cliaddr);
  connfd = accept(listenfd, (struct sockaddr *)&cliaddr, &clilen);
  n = recvfrom(connfd, buffer, 1000, 0, (struct sockaddr *)&cliaddr, &clilen);
  buffer[n] = 0;
  sendto(connfd, banner, strlen(banner), 0, (struct sockaddr *)&cliaddr,
         sizeof(cliaddr));
  printf("Received:%s\n", buffer);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe40(%rip),%rax
mov    %rax,-0x48(%rbp)
mov    0x2e1d(%rip),%rax
mov    (%rax),%edi
mov    0x2dfc(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x14(%rbp)
mov    0x2e01(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2ded(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,-0x24(%rbp)
mov    $0x7d00,%edi
call   1040 <htons@plt>
mov    %eax,-0x28(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
mov    -0x14(%rbp),%edi
xor    %esi,%esi
call   1080 <listen@plt>
movl   $0xc,-0x3c(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x38(%rbp),%rsi
lea    -0x3c(%rbp),%rdx
call   10b0 <accept@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%edi
lea    -0x430(%rbp),%rsi
lea    -0x38(%rbp),%r8
mov    $0x3e8,%edx
xor    %ecx,%ecx
lea    -0x3c(%rbp),%r9
call   1090 <recvfrom@plt>
mov    %eax,-0x1c(%rbp)
movslq -0x1c(%rbp),%rax
movb   $0x0,-0x430(%rbp,%rax,1)
mov    -0x18(%rbp),%eax
mov    %eax,-0x43c(%rbp)
mov    -0x48(%rbp),%rax
mov    %rax,-0x438(%rbp)
mov    -0x48(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x43c(%rbp),%edi
mov    -0x438(%rbp),%rsi
mov    %eax,%edx
lea    -0x38(%rbp),%r8
xor    %ecx,%ecx
mov    $0xc,%r9d
call   1070 <sendto@plt>
lea    -0x430(%rbp),%rsi
lea    0xd6e(%rip),%rdi
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x420,%rsp
mov    0x2e32(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2e10(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,%ebx
mov    0x0(%rbp),%eax
mov    %eax,0x18(%rsp)
mov    0x2e07(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,0x14(%rsp)
mov    $0x7d00,%edi
call   1040 <htons@plt>
mov    %eax,0x10(%rsp)
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   10a0 <bind@plt>
mov    %ebx,%edi
xor    %esi,%esi
call   1080 <listen@plt>
movl   $0xc,0xc(%rsp)
lea    0x20(%rsp),%r14
lea    0xc(%rsp),%r15
mov    %ebx,%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   10b0 <accept@plt>
mov    %eax,%ebp
lea    0x30(%rsp),%r12
mov    %eax,%edi
mov    %r12,%rsi
mov    $0x3e8,%edx
xor    %ecx,%ecx
mov    %r14,%r8
mov    %r15,%r9
call   1090 <recvfrom@plt>
cltq
movb   $0x0,0x30(%rsp,%rax,1)
lea    0xda9(%rip),%rbx
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
mov    %r14,%r8
mov    $0xc,%r9d
call   1070 <sendto@plt>
lea    0xda8(%rip),%rdi
mov    %r12,%rsi
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x420,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [37 x i8] c"Hello TCP client! This is TCP server\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@INADDR_ANY = global i32 0
@.str.1 = [13 x i8] c"Received:%s\0A\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 %struct.sockaddr_in
%10 = alloca %struct.sockaddr_in
%11 = alloca i32
%12 = alloca i8*
%13 = alloca [1000 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i8** %12
%14 = load i32, i32* @AF_INET
%15 = load i32, i32* @SOCK_STREAM
%16 = call i32 @socket(i32 %14, i32 %15, i32 0)
store i32 %16, i32* %6
%17 = load i32, i32* @AF_INET
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 2
store i32 %17, i32* %18
%19 = load i32, i32* @INADDR_ANY
%20 = call i32 @htonl(i32 %19)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 1
%22 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %21, i32 0, i32 0
store i32 %20, i32* %22
%23 = call i32 @htons(i32 32000)
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
store i32 %23, i32* %24
%25 = load i32, i32* %6
%26 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%27 = call i32 @bind(i32 %25, %struct.sockaddr* %26, i32 12)
%28 = load i32, i32* %6
%29 = call i32 @listen(i32 %28, i32 0)
store i32 12, i32* %11
%30 = load i32, i32* %6
%31 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%32 = call i32 @accept(i32 %30, %struct.sockaddr* %31, i32* %11)
store i32 %32, i32* %7
%33 = load i32, i32* %7
%34 = getelementptr inbounds [1000 x i8], [1000 x i8]* %13, i64 0, i64 0
%35 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%36 = call i32 @recvfrom(i32 %33, i8* %34, i32 1000, i32 0, %struct.sockaddr* %35, i32* %11)
store i32 %36, i32* %8
%37 = load i32, i32* %8
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds [1000 x i8], [1000 x i8]* %13, i64 0, i64 %38
store i8 0, i8* %39
%40 = load i32, i32* %7
%41 = load i8*, i8** %12
%42 = load i8*, i8** %12
%43 = call i32 @strlen(i8* %42)
%44 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%45 = call i32 @sendto(i32 %40, i8* %41, i32 %43, i32 0, %struct.sockaddr* %44, i32 12)
%46 = getelementptr inbounds [1000 x i8], [1000 x i8]* %13, i64 0, i64 0
%47 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* %46)
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [37 x i8] c"Hello TCP client! This is TCP server\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@INADDR_ANY = global i32 0
@.str.1 = [13 x i8] c"Received:%s\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = alloca [1000 x i8]
%7 = bitcast %struct.sockaddr_in* %3 to i8*
%8 = bitcast %struct.sockaddr_in* %4 to i8*
%9 = bitcast i32* %5 to i8*
%10 = getelementptr inbounds [1000 x i8], [1000 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 = load i32, i32* @AF_INET
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %14, i32* %15
%16 = load i32, i32* @INADDR_ANY
%17 = tail call i32 @htonl(i32 %16)
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
store i32 %17, i32* %18
%19 = tail call i32 @htons(i32 32000)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %19, i32* %20
%21 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%22 = call i32 @bind(i32 %13, %struct.sockaddr* %21, i32 12)
%23 = call i32 @listen(i32 %13, i32 0)
store i32 12, i32* %5
%24 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%25 = call i32 @accept(i32 %13, %struct.sockaddr* %24, i32* %5)
%26 = call i32 @recvfrom(i32 %25, i8* %10, i32 1000, i32 0, %struct.sockaddr* %24, i32* %5)
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds [1000 x i8], [1000 x i8]* %6, i64 0, i64 %27
store i8 0, i8* %28
%29 = call i32 @strlen(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
%30 = call i32 @sendto(i32 %25, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i32 %29, i32 0, %struct.sockaddr* %24, i32 12)
%31 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* %10)
ret i32 0
}