#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 */
typedef int /*<<< orphan*/ out;
typedef int /*<<< orphan*/ in;

/* Variables and functions */
int /*<<< orphan*/ client_count(int);
int close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char *, ...);
int /*<<< orphan*/ memset(char **, int /*<<< orphan*/, int);
int /*<<< orphan*/ recv_msg(int, char *);
int /*<<< orphan*/ send_msg(int, char *);
int /*<<< orphan*/ sprintf(char *, char *, char *, char *, ...);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int strlen(char *);
scalar_t__ strncmp(char *, char *, int);
void *echo(void *arg) {
  char in[512], out[512] = {'\0'};
  int user_fd = *(int *)arg;
  sprintf(out, "%s: %s type a message, or /quit to quit\n", "echo", ">>");
  send_msg(user_fd, out);
  while (strncmp(in, "/quit", 5) != 0) {
    recv_msg(user_fd, (char *)&in);
    if (in[strlen(in) - 1] == '\n' || in[strlen(in) - 1] == '\r') {
      in[strlen(in) - 1] = '\0';
    }
    sprintf(out, "%s: %s received: %s\n", "echo", ">>", in);
    send_msg(user_fd, out);
  }
  sprintf(out, "%s: %s goodbye!\n", "echo", ">>");
  send_msg(user_fd, out);
  memset(&in, 0, sizeof(in));
  memset(&out, 0, sizeof(out));
  client_count(-1);
  fprintf(stdout, "%s: %s client disconnected [fd: %d]\n", "echo",
          "\033[1;32m   OK >>\033[0m", user_fd);
  if (close(user_fd) == -1) {
    fprintf(stderr, "%s: %s failed to close user socket\n", "echo",
            "\033[1;31mERROR >>\033[0m");
    return (void *)-1;
  }
  return (void *)0;
}


x86 assembly (clang, -O0):
<echo>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x410(%rbp),%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   1060 <memset@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x414(%rbp)
lea    -0x410(%rbp),%rdi
lea    0xe34(%rip),%rsi
lea    0xe56(%rip),%rdx
lea    0xe54(%rip),%rcx
mov    $0x0,%al
call   10b0 <sprintf@plt>
mov    -0x414(%rbp),%edi
lea    -0x410(%rbp),%rsi
call   10a0 <send_msg@plt>
lea    -0x210(%rbp),%rdi
lea    0xe30(%rip),%rsi
mov    $0x5,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
je     12c5 <echo+0x135>
mov    -0x414(%rbp),%edi
lea    -0x210(%rbp),%rsi
call   1040 <recv_msg@plt>
lea    -0x210(%rbp),%rdi
call   1050 <strlen@plt>
sub    $0x1,%eax
cltq
movsbl -0x210(%rbp,%rax,1),%eax
ff
cmp    $0xa,%eax
je     126b <echo+0xdb>
lea    -0x210(%rbp),%rdi
call   1050 <strlen@plt>
sub    $0x1,%eax
cltq
movsbl -0x210(%rbp,%rax,1),%eax
ff
cmp    $0xd,%eax
jne    1284 <echo+0xf4>
lea    -0x210(%rbp),%rdi
call   1050 <strlen@plt>
sub    $0x1,%eax
cltq
movb   $0x0,-0x210(%rbp,%rax,1)
lea    -0x410(%rbp),%rdi
lea    -0x210(%rbp),%r8
lea    0xd9e(%rip),%rsi
lea    0xd89(%rip),%rdx
lea    0xd87(%rip),%rcx
mov    $0x0,%al
call   10b0 <sprintf@plt>
mov    -0x414(%rbp),%edi
lea    -0x410(%rbp),%rsi
call   10a0 <send_msg@plt>
jmp    11f3 <echo+0x63>
lea    -0x410(%rbp),%rdi
lea    0xd79(%rip),%rsi
lea    0xd4f(%rip),%rdx
lea    0xd4d(%rip),%rcx
mov    $0x0,%al
call   10b0 <sprintf@plt>
mov    -0x414(%rbp),%edi
lea    -0x410(%rbp),%rsi
call   10a0 <send_msg@plt>
lea    -0x210(%rbp),%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   1060 <memset@plt>
lea    -0x410(%rbp),%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   1060 <memset@plt>
mov    $0xffffffff,%edi
call   1080 <client_count@plt>
mov    0x2ca7(%rip),%rax
mov    (%rax),%edi
mov    -0x414(%rbp),%r8d
lea    0xd1c(%rip),%rsi
lea    0xce1(%rip),%rdx
lea    0xd33(%rip),%rcx
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    -0x414(%rbp),%edi
call   1070 <close@plt>
cmp    $0xffffffff,%eax
jne    139f <echo+0x20f>
mov    0x2c6f(%rip),%rax
mov    (%rax),%edi
lea    0xd1c(%rip),%rsi
lea    0xca8(%rip),%rdx
lea    0xd32(%rip),%rcx
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    $0xffffffffffffffff,%rax
mov    %rax,-0x8(%rbp)
jmp    13a7 <echo+0x217>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<echo>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x408,%rsp
mov    %rdi,%rbx
mov    %rsp,%r14
mov    $0x200,%edx
mov    %r14,%rdi
xor    %esi,%esi
call   1060 <memset@plt>
mov    (%rbx),%ebp
lea    0xe41(%rip),%rsi
lea    0xe63(%rip),%rdx
lea    0xe61(%rip),%rcx
mov    %r14,%rdi
xor    %eax,%eax
call   10b0 <sprintf@plt>
mov    %ebp,%edi
mov    %r14,%rsi
call   10a0 <send_msg@plt>
lea    0xe49(%rip),%rsi
lea    0x200(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x5,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
je     1296 <echo+0x106>
lea    0xe1c(%rip),%r15
lea    0xe1a(%rip),%r12
mov    %rsp,%r13
lea    0xe13(%rip),%r14
jmp    1268 <echo+0xd8>
mov    %rbx,%rdi
call   1050 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%rbx,1)
mov    %r13,%rdi
lea    0xdfe(%rip),%rsi
mov    %r15,%rdx
mov    %r12,%rcx
mov    %rbx,%r8
xor    %eax,%eax
call   10b0 <sprintf@plt>
mov    %ebp,%edi
mov    %r13,%rsi
call   10a0 <send_msg@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    $0x5,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
je     1296 <echo+0x106>
mov    %ebp,%edi
mov    %rbx,%rsi
call   1040 <recv_msg@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
cltq
cmpb   $0xa,-0x1(%rax,%rbx,1)
je     1220 <echo+0x90>
mov    %rbx,%rdi
call   1050 <strlen@plt>
cltq
cmpb   $0xd,-0x1(%rax,%rbx,1)
je     1220 <echo+0x90>
jmp    122f <echo+0x9f>
lea    0xdaf(%rip),%rsi
lea    0xd85(%rip),%r14
lea    0xd83(%rip),%rcx
xor    %r15d,%r15d
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    %r14,%rdx
xor    %eax,%eax
call   10b0 <sprintf@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
call   10a0 <send_msg@plt>
lea    0x200(%rsp),%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   1060 <memset@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   1060 <memset@plt>
mov    $0xffffffff,%edi
call   1080 <client_count@plt>
mov    0x2cdc(%rip),%rax
mov    (%rax),%edi
lea    0xd58(%rip),%rsi
lea    0xd76(%rip),%rcx
mov    %r14,%rdx
mov    %ebp,%r8d
xor    %eax,%eax
call   1090 <fprintf@plt>
mov    %ebp,%edi
call   1070 <close@plt>
cmp    $0xffffffff,%eax
jne    1351 <echo+0x1c1>
mov    0x2cb4(%rip),%rax
mov    (%rax),%edi
lea    0xd61(%rip),%rsi
lea    0xced(%rip),%rdx
lea    0xd77(%rip),%rcx
xor    %eax,%eax
call   1090 <fprintf@plt>
mov    $0xffffffffffffffff,%r15
mov    %r15,%rax
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [41 x i8] c"%s: %s type a message, or /quit to quit\0A\00"
@.str.1 = [5 x i8] c"echo\00"
@.str.2 = [3 x i8] c">>\00"
@.str.3 = [6 x i8] c"/quit\00"
@.str.4 = [21 x i8] c"%s: %s received: %s\0A\00"
@.str.5 = [17 x i8] c"%s: %s goodbye!\0A\00"
@stdout = global i32 0
@.str.6 = [37 x i8] c"%s: %s client disconnected [fd: %d]\0A\00"
@.str.7 = [20 x i8] c"\1B[1;32m OK >>\1B[0m\00"
@stderr = global i32 0
@.str.8 = [36 x i8] c"%s: %s failed to close user socket\0A\00"
@.str.9 = [20 x i8] c"\1B[1;31mERROR >>\1B[0m\00"
define i8* @echo(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca [512 x i8]
%5 = alloca [512 x i8]
%6 = alloca i32
store i8* %0, i8** %3
%7 = bitcast [512 x i8]* %5 to i8*
%8 = load i8*, i8** %3
%9 = bitcast i8* %8 to i32*
%10 = load i32, i32* %9
store i32 %10, i32* %6
%11 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%12 = call i32 (i8*, i8*, i8*, i8*, ...) @sprintf(i8* %11, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%13 = load i32, i32* %6
%14 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%15 = call i32 @send_msg(i32 %13, i8* %14)
br label %16
16: ; preds = %47, %1
%17 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%18 = call i64 @strncmp(i8* %17, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32 5)
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %54
20: ; preds = %16
%21 = load i32, i32* %6
%22 = bitcast [512 x i8]* %4 to i8*
%23 = call i32 @recv_msg(i32 %21, i8* %22)
%24 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%25 = call i32 @strlen(i8* %24)
%26 = sub nsw i32 %25, 1
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 %27
%29 = load i8, i8* %28
%30 = sext i8 %29 to i32
%31 = icmp eq i32 %30, 10
br i1 %31, label %41, label %32
32: ; preds = %20
%33 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%34 = call i32 @strlen(i8* %33)
%35 = sub nsw i32 %34, 1
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 %36
%38 = load i8, i8* %37
%39 = sext i8 %38 to i32
%40 = icmp eq i32 %39, 13
br i1 %40, label %41, label %47
41: ; preds = %32, %20
%42 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%43 = call i32 @strlen(i8* %42)
%44 = sub nsw i32 %43, 1
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 %45
store i8 0, i8* %46
br label %47
47: ; preds = %41, %32
%48 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%49 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%50 = call i32 (i8*, i8*, i8*, i8*, ...) @sprintf(i8* %48, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %49)
%51 = load i32, i32* %6
%52 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%53 = call i32 @send_msg(i32 %51, i8* %52)
br label %16
54: ; preds = %16
%55 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%56 = call i32 (i8*, i8*, i8*, i8*, ...) @sprintf(i8* %55, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%57 = load i32, i32* %6
%58 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%59 = call i32 @send_msg(i32 %57, i8* %58)
%60 = bitcast [512 x i8]* %4 to i8**
%61 = call i32 @memset(i8** %60, i32 0, i32 512)
%62 = bitcast [512 x i8]* %5 to i8**
%63 = call i32 @memset(i8** %62, i32 0, i32 512)
%64 = call i32 @client_count(i32 -1)
%65 = load i32, i32* @stdout
%66 = load i32, i32* %6
%67 = call i32 (i32, i8*, i8*, i8*, ...) @fprintf(i32 %65, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.7, i64 0, i64 0), i32 %66)
%68 = load i32, i32* %6
%69 = call i32 @close(i32 %68)
%70 = icmp eq i32 %69, -1
br i1 %70, label %71, label %74
71: ; preds = %54
%72 = load i32, i32* @stderr
%73 = call i32 (i32, i8*, i8*, i8*, ...) @fprintf(i32 %72, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.8, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.9, i64 0, i64 0))
store i8* inttoptr (i64 -1 to i8*), i8** %2
br label %75
74: ; preds = %54
store i8* null, i8** %2
br label %75
75: ; preds = %74, %71
%76 = load i8*, i8** %2
ret i8* %76
}

LLVM IR (-O2):
@.str = [41 x i8] c"%s: %s type a message, or /quit to quit\0A\00"
@.str.1 = [5 x i8] c"echo\00"
@.str.2 = [3 x i8] c">>\00"
@.str.3 = [6 x i8] c"/quit\00"
@.str.4 = [21 x i8] c"%s: %s received: %s\0A\00"
@.str.5 = [17 x i8] c"%s: %s goodbye!\0A\00"
@stdout = global i32 0
@.str.6 = [37 x i8] c"%s: %s client disconnected [fd: %d]\0A\00"
@.str.7 = [20 x i8] c"\1B[1;32m OK >>\1B[0m\00"
@stderr = global i32 0
@.str.8 = [36 x i8] c"%s: %s failed to close user socket\0A\00"
@.str.9 = [20 x i8] c"\1B[1;31mERROR >>\1B[0m\00"
define i8* @echo(i8* nocapture %0) {
%2 = alloca [512 x i8]
%3 = alloca [512 x i8]
%4 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%6 = bitcast i8* %0 to i32*
%7 = load i32, i32* %6
%8 = call i32 (i8*, i8*, i8*, i8*, ...) @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%9 = call i32 @send_msg(i32 %7, i8* %5)
%10 = call i64 @strncmp(i8* %4, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32 5)
%11 = icmp eq i64 %10, 0
br i1 %11, label %37, label %12
12: ; preds = %1, %32
%13 = call i32 @recv_msg(i32 %7, i8* %4)
%14 = call i32 @strlen(i8* %4)
%15 = add nsw i32 %14, -1
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 %16
%18 = load i8, i8* %17
%19 = icmp eq i8 %18, 10
br i1 %19, label %27, label %20
20: ; preds = %12
%21 = call i32 @strlen(i8* %4)
%22 = add nsw i32 %21, -1
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 %23
%25 = load i8, i8* %24
%26 = icmp eq i8 %25, 13
br i1 %26, label %27, label %32
27: ; preds = %20, %12
%28 = call i32 @strlen(i8* %4)
%29 = add nsw i32 %28, -1
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 %30
store i8 0, i8* %31
br label %32
32: ; preds = %27, %20
%33 = call i32 (i8*, i8*, i8*, i8*, ...) @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %4)
%34 = call i32 @send_msg(i32 %7, i8* %5)
%35 = call i64 @strncmp(i8* %4, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32 5)
%36 = icmp eq i64 %35, 0
br i1 %36, label %37, label %12
37: ; preds = %32, %1
%38 = call i32 (i8*, i8*, i8*, i8*, ...) @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%39 = call i32 @send_msg(i32 %7, i8* %5)
%40 = bitcast [512 x i8]* %2 to i8**
%41 = call i32 @memset(i8** %40, i32 0, i32 512)
%42 = bitcast [512 x i8]* %3 to i8**
%43 = call i32 @memset(i8** %42, i32 0, i32 512)
%44 = call i32 @client_count(i32 -1)
%45 = load i32, i32* @stdout
%46 = call i32 (i32, i8*, i8*, i8*, ...) @fprintf(i32 %45, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.7, i64 0, i64 0), i32 %7)
%47 = call i32 @close(i32 %7)
%48 = icmp eq i32 %47, -1
br i1 %48, label %49, label %52
49: ; preds = %37
%50 = load i32, i32* @stderr
%51 = call i32 (i32, i8*, i8*, i8*, ...) @fprintf(i32 %50, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.8, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.9, i64 0, i64 0))
br label %52
52: ; preds = %37, %49
%53 = phi i8* [ inttoptr (i64 -1 to i8*), %49 ], [ null, %37 ]
ret i8* %53
}