#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 */

/* Type definitions */
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_addr;
};
struct sockaddr {
  int dummy;
};
typedef scalar_t__ ssize_t;
typedef int socklen_t;
typedef int /*<<< orphan*/ client_addr;
typedef int /*<<< orphan*/ buffer;

/* Variables and functions */
int /*<<< orphan*/ INADDR_ANY;
int accept(int, struct sockaddr *, int *);
int atoi(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
int /*<<< orphan*/ free(char *);
char *inet_ntoa(int /*<<< orphan*/);
int init_server(int, int /*<<< orphan*/);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ recv(int, char *, int, int /*<<< orphan*/);
char *rstrip(char *, int);
int /*<<< orphan*/ send(int, char *, size_t, int /*<<< orphan*/);
int /*<<< orphan*/ stdout;
scalar_t__ strcmp(char *, char *);
int main(int argc, char **argv) {
  int port;
  if (argc <= 1) {
    printf("usage: ./echo_server port\n");
    return -1;
  }
  port = atoi(argv[1]);
  if (port == 0) {
    printf("bad port: %s\n", argv[1]);
    return -1;
  }
  printf("port is %d\n", port);
  int server_sock_fd = init_server(port, INADDR_ANY);
  if (server_sock_fd == -1) {
    return -1;
  }
  char buffer[1024];
  struct sockaddr_in client_addr;
  socklen_t socklen = sizeof(client_addr);
  while (1) {
    int con = accept(server_sock_fd, (struct sockaddr *)&client_addr, &socklen);
    if (con < 0) {
      printf("accept failed");
      close(con);
      return -1;
    }
    printf("recv client connection %s %d\n", inet_ntoa(client_addr.sin_addr),
           ntohs(client_addr.sin_port));
    while (1) {
      memset(buffer, 0, sizeof(buffer));
      ssize_t len = recv(con, buffer, sizeof(buffer), 0);
      char *word = rstrip(buffer, (int)len);
      if (strcmp(word, "exit") == 0) {
        free(word);
        break;
      }
      free(word);
      fputs(buffer, stdout);
      send(con, buffer, (size_t)len, 0);
    }
    printf("closing client");
    fflush(stdout);
    close(con);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x450,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x1,-0x8(%rbp)
jg     122d <main+0x3d>
lea    0xde6(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    141a <main+0x22a>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1110 <atoi@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    1269 <main+0x79>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xdc5(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    141a <main+0x22a>
mov    -0x14(%rbp),%esi
lea    0xdb6(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
mov    -0x14(%rbp),%edi
mov    0x2d64(%rip),%rax
mov    (%rax),%esi
call   10e0 <init_server@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    12a4 <main+0xb4>
movl   $0xffffffff,-0x4(%rbp)
jmp    141a <main+0x22a>
movl   $0x8,-0x42c(%rbp)
mov    -0x18(%rbp),%edi
lea    -0x428(%rbp),%rsi
lea    -0x42c(%rbp),%rdx
call   1100 <accept@plt>
mov    %eax,-0x430(%rbp)
cmpl   $0x0,-0x430(%rbp)
jge    12fc <main+0x10c>
lea    0xd57(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
mov    -0x430(%rbp),%edi
call   10b0 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    141a <main+0x22a>
mov    -0x424(%rbp),%edi
call   1060 <inet_ntoa@plt>
mov    %rax,-0x448(%rbp)
mov    -0x428(%rbp),%edi
call   10f0 <ntohs@plt>
mov    -0x448(%rbp),%rsi
mov    %eax,%edx
lea    0xd1a(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
lea    -0x420(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   10a0 <memset@plt>
mov    -0x430(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1050 <recv@plt>
mov    %rax,-0x438(%rbp)
lea    -0x420(%rbp),%rdi
mov    -0x438(%rbp),%rax
mov    %eax,%esi
call   1030 <rstrip@plt>
mov    %rax,-0x440(%rbp)
mov    -0x440(%rbp),%rdi
lea    0xcd4(%rip),%rsi
call   10c0 <strcmp@plt>
cmp    $0x0,%rax
jne    13ad <main+0x1bd>
mov    -0x440(%rbp),%rdi
call   1040 <free@plt>
jmp    13ee <main+0x1fe>
mov    -0x440(%rbp),%rdi
call   1040 <free@plt>
lea    -0x420(%rbp),%rdi
mov    0x2c11(%rip),%rax
mov    (%rax),%esi
call   1090 <fputs@plt>
mov    -0x430(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    -0x438(%rbp),%rdx
xor    %ecx,%ecx
call   1070 <send@plt>
jmp    1330 <main+0x140>
lea    0xc71(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
mov    0x2bd5(%rip),%rax
mov    (%rax),%edi
call   10d0 <fflush@plt>
mov    -0x430(%rbp),%edi
call   10b0 <close@plt>
jmp    12ae <main+0xbe>
mov    -0x4(%rbp),%eax
add    $0x450,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x428,%rsp
cmp    $0x1,%edi
jg     1227 <main+0x27>
lea    0xe3d(%rip),%rdi
call   1060 <puts@plt>
jmp    13a9 <main+0x1a9>
mov    0x8(%rsi),%rbp
mov    %rbp,%rdi
call   1120 <atoi@plt>
test   %eax,%eax
je     1381 <main+0x181>
mov    %eax,%ebx
lea    0xdca(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1090 <printf@plt>
mov    0x2d94(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   10f0 <init_server@plt>
cmp    $0xffffffff,%eax
je     13a9 <main+0x1a9>
mov    %eax,%r14d
movl   $0x8,0xc(%rsp)
lea    0x10(%rsp),%rsi
lea    0xc(%rsp),%rdx
mov    %eax,%edi
call   1110 <accept@plt>
mov    %eax,%ebp
test   %eax,%eax
js     1394 <main+0x194>
lea    0x20(%rsp),%rbx
mov    0x2d40(%rip),%r12
mov    %r14d,0x1c(%rsp)
jmp    12db <main+0xdb>
nop
lea    0xda4(%rip),%rdi
xor    %eax,%eax
call   1090 <printf@plt>
mov    (%r12),%edi
call   10e0 <fflush@plt>
mov    %ebp,%edi
call   10c0 <close@plt>
mov    0x1c(%rsp),%edi
lea    0x10(%rsp),%rsi
lea    0xc(%rsp),%rdx
call   1110 <accept@plt>
mov    %eax,%ebp
test   %eax,%eax
js     1394 <main+0x194>
mov    0x14(%rsp),%edi
call   1070 <inet_ntoa@plt>
mov    %rax,%r14
mov    0x10(%rsp),%edi
call   1100 <ntohs@plt>
lea    0xd31(%rip),%rdi
mov    %r14,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1090 <printf@plt>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   10b0 <memset@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1050 <recv@plt>
mov    %rax,%r14
mov    %rbx,%rdi
mov    %r14d,%esi
call   1030 <rstrip@plt>
mov    %rax,%r13
mov    %rax,%rdi
lea    0xcfb(%rip),%rsi
call   10d0 <strcmp@plt>
mov    %rax,%r15
mov    %r13,%rdi
call   1040 <free@plt>
test   %r15,%r15
je     12a0 <main+0xa0>
mov    (%r12),%esi
mov    %rbx,%rdi
call   10a0 <fputs@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
xor    %ecx,%ecx
call   1080 <send@plt>
jmp    1310 <main+0x110>
lea    0xc78(%rip),%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1090 <printf@plt>
jmp    13a9 <main+0x1a9>
lea    0xc7f(%rip),%rdi
xor    %eax,%eax
call   1090 <printf@plt>
mov    %ebp,%edi
call   10c0 <close@plt>
mov    $0xffffffff,%eax
add    $0x428,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [27 x i8] c"usage: ./echo_server port\0A\00"
@.str.1 = [14 x i8] c"bad port: %s\0A\00"
@.str.2 = [12 x i8] c"port is %d\0A\00"
@INADDR_ANY = global i32 0
@.str.3 = [14 x i8] c"accept failed\00"
@.str.4 = [30 x i8] c"recv client connection %s %d\0A\00"
@.str.5 = [5 x i8] c"exit\00"
@stdout = global i32 0
@.str.6 = [15 x i8] c"closing client\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 [1024 x i8]
%9 = alloca %struct.sockaddr_in
%10 = alloca i32
%11 = alloca i32
%12 = alloca i64
%13 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%14 = load i32, i32* %4
%15 = icmp sle i32 %14, 1
br i1 %15, label %16, label %18
16: ; preds = %2
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %90
18: ; preds = %2
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 1
%21 = load i8*, i8** %20
%22 = call i32 @atoi(i8* %21)
store i32 %22, i32* %6
%23 = load i32, i32* %6
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %30
25: ; preds = %18
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 1
%28 = load i8*, i8** %27
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* %28)
store i32 -1, i32* %3
br label %90
30: ; preds = %18
%31 = load i32, i32* %6
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 %31)
%33 = load i32, i32* %6
%34 = load i32, i32* @INADDR_ANY
%35 = call i32 @init_server(i32 %33, i32 %34)
store i32 %35, i32* %7
%36 = load i32, i32* %7
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %39
38: ; preds = %30
store i32 -1, i32* %3
br label %90
39: ; preds = %30
store i32 8, i32* %10
br label %40
40: ; preds = %39, %84
%41 = load i32, i32* %7
%42 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%43 = call i32 @accept(i32 %41, %struct.sockaddr* %42, i32* %10)
store i32 %43, i32* %11
%44 = load i32, i32* %11
%45 = icmp slt i32 %44, 0
br i1 %45, label %46, label %50
46: ; preds = %40
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
%48 = load i32, i32* %11
%49 = call i32 @close(i32 %48)
store i32 -1, i32* %3
br label %90
50: ; preds = %40
%51 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 1
%52 = load i32, i32* %51
%53 = call i8* @inet_ntoa(i32 %52)
%54 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
%55 = load i32, i32* %54
%56 = call i32 @ntohs(i32 %55)
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0), i8* %53, i32 %56)
br label %58
58: ; preds = %50, %74
%59 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%60 = call i32 @memset(i8* %59, i32 0, i32 1024)
%61 = load i32, i32* %11
%62 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%63 = call i64 @recv(i32 %61, i8* %62, i32 1024, i32 0)
store i64 %63, i64* %12
%64 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%65 = load i64, i64* %12
%66 = trunc i64 %65 to i32
%67 = call i8* @rstrip(i8* %64, i32 %66)
store i8* %67, i8** %13
%68 = load i8*, i8** %13
%69 = call i64 @strcmp(i8* %68, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
%70 = icmp eq i64 %69, 0
br i1 %70, label %71, label %74
71: ; preds = %58
%72 = load i8*, i8** %13
%73 = call i32 @free(i8* %72)
br label %84
74: ; preds = %58
%75 = load i8*, i8** %13
%76 = call i32 @free(i8* %75)
%77 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%78 = load i32, i32* @stdout
%79 = call i32 @fputs(i8* %77, i32 %78)
%80 = load i32, i32* %11
%81 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%82 = load i64, i64* %12
%83 = call i32 @send(i32 %80, i8* %81, i64 %82, i32 0)
br label %58
84: ; preds = %71
%85 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0))
%86 = load i32, i32* @stdout
%87 = call i32 @fflush(i32 %86)
%88 = load i32, i32* %11
%89 = call i32 @close(i32 %88)
br label %40
90: ; preds = %46, %38, %25, %16
%91 = load i32, i32* %3
ret i32 %91
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr = type { i32 }
@.str.1 = [14 x i8] c"bad port: %s\0A\00"
@.str.2 = [12 x i8] c"port is %d\0A\00"
@INADDR_ANY = global i32 0
@.str.3 = [14 x i8] c"accept failed\00"
@.str.4 = [30 x i8] c"recv client connection %s %d\0A\00"
@.str.5 = [5 x i8] c"exit\00"
@stdout = global i32 0
@.str.6 = [15 x i8] c"closing client\00"
@str = [26 x i8] c"usage: ./echo_server port\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [1024 x i8]
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = icmp slt i32 %0, 2
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str, i64 0, i64 0))
br label %60
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = tail call i32 @atoi(i8* %11)
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %9
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* %11)
br label %60
16: ; preds = %9
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 %12)
%18 = load i32, i32* @INADDR_ANY
%19 = tail call i32 @init_server(i32 %12, i32 %18)
%20 = icmp eq i32 %19, -1
br i1 %20, label %60, label %21
21: ; preds = %16
%22 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%23 = bitcast %struct.sockaddr_in* %4 to i8*
%24 = bitcast i32* %5 to i8*
store i32 8, i32* %5
%25 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
%28 = call i32 @accept(i32 %19, %struct.sockaddr* %25, i32* %5)
%29 = icmp slt i32 %28, 0
br i1 %29, label %56, label %30
30: ; preds = %21, %49
%31 = phi i32 [ %54, %49 ], [ %28, %21 ]
%32 = load i32, i32* %26
%33 = call i8* @inet_ntoa(i32 %32)
%34 = load i32, i32* %27
%35 = call i32 @ntohs(i32 %34)
%36 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0), i8* %33, i32 %35)
br label %37
37: ; preds = %45, %30
%38 = call i32 @memset(i8* %22, i32 0, i32 1024)
%39 = call i64 @recv(i32 %31, i8* %22, i32 1024, i32 0)
%40 = trunc i64 %39 to i32
%41 = call i8* @rstrip(i8* %22, i32 %40)
%42 = call i64 @strcmp(i8* %41, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
%43 = icmp eq i64 %42, 0
%44 = call i32 @free(i8* %41)
br i1 %43, label %49, label %45
45: ; preds = %37
%46 = load i32, i32* @stdout
%47 = call i32 @fputs(i8* %22, i32 %46)
%48 = call i32 @send(i32 %31, i8* %22, i64 %39, i32 0)
br label %37
49: ; preds = %37
%50 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0))
%51 = load i32, i32* @stdout
%52 = call i32 @fflush(i32 %51)
%53 = call i32 @close(i32 %31)
%54 = call i32 @accept(i32 %19, %struct.sockaddr* %25, i32* %5)
%55 = icmp slt i32 %54, 0
br i1 %55, label %56, label %30
56: ; preds = %49, %21
%57 = phi i32 [ %28, %21 ], [ %54, %49 ]
%58 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
%59 = call i32 @close(i32 %57)
br label %60
60: ; preds = %56, %16, %14, %7
ret i32 -1
}