#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 {
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ server_sockaddr;
typedef int /*<<< orphan*/ client_addr;
typedef int /*<<< orphan*/ buffer;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, int *);
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int listen(int, int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int recv(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ send(int, char *, int, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stdout;
scalar_t__ strcmp(char *, char *);
int main() {
  int server_sockfd = socket(AF_INET, SOCK_STREAM, 0);
  char buffer[1024];
  struct sockaddr_in client_addr;
  socklen_t length = sizeof(client_addr);
  int conn = accept(server_sockfd, (struct sockaddr *)&client_addr, &length);
  struct sockaddr_in server_sockaddr;
  server_sockaddr.sin_family = AF_INET;
  server_sockaddr.sin_port = htons(8887);
  server_sockaddr.sin_addr.s_addr = htonl(INADDR_ANY);
  if (bind(server_sockfd, (struct sockaddr *)&server_sockaddr,
           sizeof(server_sockaddr)) == -1) {
    perror("bind");
    exit(1);
  }
  if (listen(server_sockfd, 20) == -1) {
    perror("listen");
    exit(1);
  }
  if (conn < 0) {
    perror("connect");
    exit(1);
  }
  while (1) {
    memset(buffer, 0, sizeof(buffer));
    int len = recv(conn, buffer, sizeof(buffer), 0);
    if (strcmp(buffer, "exit\n") == 0)
      break;
    fputs(buffer, stdout);
    send(conn, buffer, len, 0);
  }
  close(conn);
  close(server_sockfd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2def(%rip),%rax
mov    (%rax),%edi
mov    0x2dce(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
mov    %eax,-0x8(%rbp)
movl   $0xc,-0x424(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x420(%rbp),%rsi
lea    -0x424(%rbp),%rdx
call   10e0 <accept@plt>
mov    %eax,-0x428(%rbp)
mov    0x2dad(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x430(%rbp)
mov    $0x22b7,%edi
call   1040 <htons@plt>
mov    %eax,-0x434(%rbp)
mov    0x2d86(%rip),%rax
mov    (%rax),%edi
call   1070 <htonl@plt>
mov    %eax,-0x438(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x438(%rbp),%rsi
mov    $0xc,%edx
call   10c0 <bind@plt>
cmp    $0xffffffff,%eax
jne    129a <main+0xba>
lea    0xd75(%rip),%rdi
call   10d0 <perror@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
mov    -0x8(%rbp),%edi
mov    $0x14,%esi
call   10b0 <listen@plt>
cmp    $0xffffffff,%eax
jne    12c6 <main+0xe6>
lea    0xd4e(%rip),%rdi
call   10d0 <perror@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
cmpl   $0x0,-0x428(%rbp)
jge    12e9 <main+0x109>
lea    0xd32(%rip),%rdi
call   10d0 <perror@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
jmp    12ee <main+0x10e>
lea    -0x410(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1080 <memset@plt>
mov    -0x428(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x43c(%rbp)
lea    -0x410(%rbp),%rdi
lea    0xce6(%rip),%rsi
call   10a0 <strcmp@plt>
cmp    $0x0,%rax
jne    1342 <main+0x162>
jmp    1376 <main+0x196>
lea    -0x410(%rbp),%rdi
mov    0x2c78(%rip),%rax
mov    (%rax),%esi
call   1060 <fputs@plt>
mov    -0x428(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    -0x43c(%rbp),%edx
xor    %ecx,%ecx
call   1050 <send@plt>
jmp    12ee <main+0x10e>
mov    -0x428(%rbp),%edi
call   1090 <close@plt>
mov    -0x8(%rbp),%edi
call   1090 <close@plt>
xor    %eax,%eax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x438,%rsp
mov    0x2df0(%rip),%rbx
mov    (%rbx),%edi
mov    0x2dcf(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
mov    %eax,%r14d
movl   $0xc,0xc(%rsp)
lea    0x20(%rsp),%rsi
lea    0xc(%rsp),%rdx
mov    %eax,%edi
call   10e0 <accept@plt>
mov    %eax,%r12d
mov    (%rbx),%eax
mov    %eax,0x18(%rsp)
mov    $0x22b7,%edi
call   1040 <htons@plt>
mov    %eax,0x14(%rsp)
mov    0x2d9c(%rip),%rax
mov    (%rax),%edi
call   1070 <htonl@plt>
mov    %eax,0x10(%rsp)
lea    0x10(%rsp),%rsi
mov    %r14d,%edi
mov    $0xc,%edx
call   10c0 <bind@plt>
cmp    $0xffffffff,%eax
je     1352 <main+0x172>
mov    %r14d,%edi
mov    $0x14,%esi
call   10b0 <listen@plt>
cmp    $0xffffffff,%eax
je     135b <main+0x17b>
test   %r12d,%r12d
js     1364 <main+0x184>
lea    0x30(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1080 <memset@plt>
mov    %r12d,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,%ebp
lea    0xd5c(%rip),%rsi
mov    %rbx,%rdi
call   10a0 <strcmp@plt>
test   %rax,%rax
je     132e <main+0x14e>
mov    0x2cfc(%rip),%r13
lea    0x30(%rsp),%rbx
lea    0xd3c(%rip),%r15
nopl   0x0(%rax,%rax,1)
mov    0x0(%r13),%esi
mov    %rbx,%rdi
call   1060 <fputs@plt>
mov    %r12d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
xor    %ecx,%ecx
call   1050 <send@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1080 <memset@plt>
mov    %r12d,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
mov    %r15,%rsi
call   10a0 <strcmp@plt>
test   %rax,%rax
jne    12e0 <main+0x100>
mov    %r12d,%edi
call   1090 <close@plt>
mov    %r14d,%edi
call   1090 <close@plt>
xor    %eax,%eax
add    $0x438,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xca7(%rip),%rdi
jmp    136b <main+0x18b>
lea    0xca3(%rip),%rdi
jmp    136b <main+0x18b>
lea    0xca1(%rip),%rdi
call   10d0 <perror@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@INADDR_ANY = global i32 0
@.str = [5 x i8] c"bind\00"
@.str.1 = [7 x i8] c"listen\00"
@.str.2 = [8 x i8] c"connect\00"
@.str.3 = [6 x i8] c"exit\0A\00"
@stdout = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [1024 x i8]
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.sockaddr_in
%8 = alloca i32
store i32 0, i32* %1
%9 = load i32, i32* @AF_INET
%10 = load i32, i32* @SOCK_STREAM
%11 = call i32 @socket(i32 %9, i32 %10, i32 0)
store i32 %11, i32* %2
store i32 12, i32* %5
%12 = load i32, i32* %2
%13 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%14 = call i32 @accept(i32 %12, %struct.sockaddr* %13, i32* %5)
store i32 %14, i32* %6
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i32 %15, i32* %16
%17 = call i32 @htons(i32 8887)
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
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* %7, i32 0, i32 0
%22 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %21, i32 0, i32 0
store i32 %20, i32* %22
%23 = load i32, i32* %2
%24 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%25 = call i32 @bind(i32 %23, %struct.sockaddr* %24, i32 12)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %30
27: ; preds = %0
%28 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %0
%31 = load i32, i32* %2
%32 = call i32 @listen(i32 %31, i32 20)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %37
34: ; preds = %30
%35 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%36 = call i32 @exit(i32 1)
unreachable
37: ; preds = %30
%38 = load i32, i32* %6
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %43
40: ; preds = %37
%41 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%42 = call i32 @exit(i32 1)
unreachable
43: ; preds = %37
br label %44
44: ; preds = %43, %54
%45 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%46 = call i32 @memset(i8* %45, i32 0, i32 1024)
%47 = load i32, i32* %6
%48 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%49 = call i32 @recv(i32 %47, i8* %48, i32 1024, i32 0)
store i32 %49, i32* %8
%50 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%51 = call i64 @strcmp(i8* %50, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%52 = icmp eq i64 %51, 0
br i1 %52, label %53, label %54
53: ; preds = %44
br label %62
54: ; preds = %44
%55 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%56 = load i32, i32* @stdout
%57 = call i32 @fputs(i8* %55, i32 %56)
%58 = load i32, i32* %6
%59 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%60 = load i32, i32* %8
%61 = call i32 @send(i32 %58, i8* %59, i32 %60, i32 0)
br label %44
62: ; preds = %53
%63 = load i32, i32* %6
%64 = call i32 @close(i32 %63)
%65 = load i32, i32* %2
%66 = call i32 @close(i32 %65)
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@INADDR_ANY = global i32 0
@.str = [5 x i8] c"bind\00"
@.str.1 = [7 x i8] c"listen\00"
@.str.2 = [8 x i8] c"connect\00"
@.str.3 = [6 x i8] c"exit\0A\00"
@stdout = global i32 0
define i32 @main() {
%1 = alloca [1024 x i8]
%2 = alloca %struct.sockaddr_in
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = load i32, i32* @AF_INET
%6 = load i32, i32* @SOCK_STREAM
%7 = tail call i32 @socket(i32 %5, i32 %6, i32 0)
%8 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%9 = bitcast %struct.sockaddr_in* %2 to i8*
%10 = bitcast i32* %3 to i8*
store i32 12, i32* %3
%11 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%12 = call i32 @accept(i32 %7, %struct.sockaddr* %11, i32* %3)
%13 = bitcast %struct.sockaddr_in* %4 to i8*
%14 = load i32, i32* @AF_INET
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %14, i32* %15
%16 = call i32 @htons(i32 8887)
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
store i32 %16, i32* %17
%18 = load i32, i32* @INADDR_ANY
%19 = call i32 @htonl(i32 %18)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0, i32 0
store i32 %19, i32* %20
%21 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%22 = call i32 @bind(i32 %7, %struct.sockaddr* %21, i32 12)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %27
24: ; preds = %0
%25 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %0
%28 = call i32 @listen(i32 %7, i32 20)
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %33
30: ; preds = %27
%31 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%32 = call i32 @exit(i32 1)
unreachable
33: ; preds = %27
%34 = icmp slt i32 %12, 0
br i1 %34, label %40, label %35
35: ; preds = %33
%36 = call i32 @memset(i8* %8, i32 0, i32 1024)
%37 = call i32 @recv(i32 %12, i8* %8, i32 1024, i32 0)
%38 = call i64 @strcmp(i8* %8, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%39 = icmp eq i64 %38, 0
br i1 %39, label %52, label %43
40: ; preds = %33
%41 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%42 = call i32 @exit(i32 1)
unreachable
43: ; preds = %35, %43
%44 = phi i32 [ %49, %43 ], [ %37, %35 ]
%45 = load i32, i32* @stdout
%46 = call i32 @fputs(i8* %8, i32 %45)
%47 = call i32 @send(i32 %12, i8* %8, i32 %44, i32 0)
%48 = call i32 @memset(i8* %8, i32 0, i32 1024)
%49 = call i32 @recv(i32 %12, i8* %8, i32 1024, i32 0)
%50 = call i64 @strcmp(i8* %8, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%51 = icmp eq i64 %50, 0
br i1 %51, label %52, label %43
52: ; preds = %43, %35
%53 = call i32 @close(i32 %12)
%54 = call i32 @close(i32 %7)
ret i32 0
}