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

/* Variables and functions */
int /*<<< orphan*/ SYS_gettid;
int /*<<< orphan*/ close(int);
int errno;
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
int recv(int, char *, int, int /*<<< orphan*/);
int send(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(char *, char *);
char *strerror(int);
scalar_t__ strncmp(char *, char *, int);
int syscall(int /*<<< orphan*/);
void *client_thread(void *arg) {
  int fd = 0;
  int size = 0;
  int running_flag = 0;
  char sbuff[1024];
  char rbuff[1024];
  fd = (int)(long)arg;
  if (fd < 0)
    return NULL;
  printf("[SERVER] "
         "CLIENT-%d connected, tid=%d\n",
         fd, syscall(SYS_gettid));
  running_flag = 1;
  while (running_flag > 0) {
    printf("[SERVER] "
           "CLIENT-%d loop ...\n",
           fd);
    memset(rbuff, 0, 1024);
    size = recv(fd, rbuff, 1024, 0);
    if (size < 0) {
      printf("[SERVER] "
             "recv msg error: %s(errno: %d)\n",
             strerror(errno), errno);
      running_flag = 0;
      break;
    }
    printf("[SERVER] "
           "<<< CLIENT-%d: (%d) %s\n",
           fd, size, rbuff);
    if (strncmp(rbuff, "exit", 4) == 0)
      running_flag = 0;
    memset(sbuff, 0, 1024);
    strcpy(sbuff, "got it\n");
    size = send(fd, sbuff, 1024, 0);
    if (size < 0) {
      printf("[SERVER] "
             "send msg error: %s(errno: %d)\n",
             strerror(errno), errno);
      running_flag = 0;
      break;
    }
  }
  printf("[SERVER] "
         "CLIENT-%d closed\n",
         fd);
  close(fd);
  return NULL;
}


x86 assembly (clang, -O0):
<client_thread>:
push   %rbp
mov    %rsp,%rbp
sub    $0x830,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11d2 <client_thread+0x42>
movq   $0x0,-0x8(%rbp)
jmp    1372 <client_thread+0x1e2>
mov    -0x14(%rbp),%eax
mov    %eax,-0x824(%rbp)
mov    0x2e06(%rip),%rax
mov    (%rax),%edi
call   10a0 <syscall@plt>
mov    -0x824(%rbp),%esi
mov    %eax,%edx
lea    0xe08(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x1,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jle    1351 <client_thread+0x1c1>
mov    -0x14(%rbp),%esi
lea    0xe0c(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
lea    -0x820(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1080 <memset@plt>
mov    -0x14(%rbp),%edi
lea    -0x820(%rbp),%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    128b <client_thread+0xfb>
mov    0x2d7a(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    %rax,%rsi
mov    0x2d69(%rip),%rax
mov    (%rax),%edx
lea    0xdcb(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x0,-0x1c(%rbp)
jmp    1351 <client_thread+0x1c1>
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
lea    -0x820(%rbp),%rcx
lea    0xdcc(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
lea    -0x820(%rbp),%rdi
lea    0xdd8(%rip),%rsi
mov    $0x4,%edx
call   1040 <strncmp@plt>
cmp    $0x0,%rax
jne    12cf <client_thread+0x13f>
movl   $0x0,-0x1c(%rbp)
lea    -0x420(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1080 <memset@plt>
lea    -0x420(%rbp),%rdi
lea    0xda1(%rip),%rsi
call   1050 <strcpy@plt>
mov    -0x14(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    134c <client_thread+0x1bc>
mov    0x2cb9(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    %rax,%rsi
mov    0x2ca8(%rip),%rax
mov    (%rax),%edx
lea    0xd60(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x0,-0x1c(%rbp)
jmp    1351 <client_thread+0x1c1>
jmp    1206 <client_thread+0x76>
mov    -0x14(%rbp),%esi
lea    0xd66(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x14(%rbp),%edi
call   1090 <close@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x830,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<client_thread>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x808,%rsp
mov    %rdi,%rbx
test   %ebx,%ebx
js     12d5 <client_thread+0x145>
mov    0x2e35(%rip),%rax
mov    (%rax),%edi
call   10a0 <syscall@plt>
lea    0xe3f(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0x1,%r13d
mov    %rsp,%r12
lea    0xeb0(%rip),%rbp
lea    0x400(%rsp),%r14
lea    0xea6(%rip),%r15
nopl   0x0(%rax,%rax,1)
test   %r13d,%r13d
je     12be <client_thread+0x12e>
lea    0xe26(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1080 <memset@plt>
mov    %ebx,%edi
mov    %r12,%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
js     1298 <client_thread+0x108>
lea    0xe37(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
mov    %r12,%rcx
xor    %eax,%eax
call   1070 <printf@plt>
mov    %r12,%rdi
mov    %rbp,%rsi
mov    $0x4,%edx
call   1040 <strncmp@plt>
xor    %r13d,%r13d
test   %rax,%rax
setne  %r13b
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1080 <memset@plt>
mov    %r14,%rdi
mov    %r15,%rsi
call   1050 <strcpy@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
test   %eax,%eax
jns    11f0 <client_thread+0x60>
lea    0xe03(%rip),%rbp
jmp    129f <client_thread+0x10f>
lea    0xda4(%rip),%rbp
mov    0x2d32(%rip),%r14
mov    (%r14),%edi
call   10b0 <strerror@plt>
mov    (%r14),%edx
mov    %rbp,%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
lea    0xdfc(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %ebx,%edi
call   1090 <close@plt>
xor    %eax,%eax
add    $0x808,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [38 x i8] c"[SERVER] CLIENT-%d connected, tid=%d\0A\00"
@SYS_gettid = global i32 0
@.str.1 = [29 x i8] c"[SERVER] CLIENT-%d loop ...\0A\00"
@.str.2 = [40 x i8] c"[SERVER] recv msg error: %s(errno: %d)\0A\00"
@errno = global i32 0
@.str.3 = [33 x i8] c"[SERVER] <<< CLIENT-%d: (%d) %s\0A\00"
@.str.4 = [5 x i8] c"exit\00"
@.str.5 = [8 x i8] c"got it\0A\00"
@.str.6 = [40 x i8] c"[SERVER] send msg error: %s(errno: %d)\0A\00"
@.str.7 = [27 x i8] c"[SERVER] CLIENT-%d closed\0A\00"
define i8* @client_thread(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca [1024 x i8]
%8 = alloca [1024 x i8]
store i8* %0, i8** %3
store i32 0, i32* %4
store i32 0, i32* %5
store i32 0, i32* %6
%9 = load i8*, i8** %3
%10 = ptrtoint i8* %9 to i64
%11 = trunc i64 %10 to i32
store i32 %11, i32* %4
%12 = load i32, i32* %4
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %1
store i8* null, i8** %2
br label %68
15: ; preds = %1
%16 = load i32, i32* %4
%17 = load i32, i32* @SYS_gettid
%18 = call i32 @syscall(i32 %17)
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i32 %16, i32 %18)
store i32 1, i32* %6
br label %20
20: ; preds = %62, %15
%21 = load i32, i32* %6
%22 = icmp sgt i32 %21, 0
br i1 %22, label %23, label %63
23: ; preds = %20
%24 = load i32, i32* %4
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i32 %24)
%26 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%27 = call i32 @memset(i8* %26, i32 0, i32 1024)
%28 = load i32, i32* %4
%29 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%30 = call i32 @recv(i32 %28, i8* %29, i32 1024, i32 0)
store i32 %30, i32* %5
%31 = load i32, i32* %5
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %38
33: ; preds = %23
%34 = load i32, i32* @errno
%35 = call i8* @strerror(i32 %34)
%36 = load i32, i32* @errno
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0), i8* %35, i32 %36)
store i32 0, i32* %6
br label %63
38: ; preds = %23
%39 = load i32, i32* %4
%40 = load i32, i32* %5
%41 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i32 %39, i32 %40, i8* %41)
%43 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%44 = call i64 @strncmp(i8* %43, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i32 4)
%45 = icmp eq i64 %44, 0
br i1 %45, label %46, label %47
46: ; preds = %38
store i32 0, i32* %6
br label %47
47: ; preds = %46, %38
%48 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%49 = call i32 @memset(i8* %48, i32 0, i32 1024)
%50 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%51 = call i32 @strcpy(i8* %50, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0))
%52 = load i32, i32* %4
%53 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%54 = call i32 @send(i32 %52, i8* %53, i32 1024, i32 0)
store i32 %54, i32* %5
%55 = load i32, i32* %5
%56 = icmp slt i32 %55, 0
br i1 %56, label %57, label %62
57: ; preds = %47
%58 = load i32, i32* @errno
%59 = call i8* @strerror(i32 %58)
%60 = load i32, i32* @errno
%61 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.6, i64 0, i64 0), i8* %59, i32 %60)
store i32 0, i32* %6
br label %63
62: ; preds = %47
br label %20
63: ; preds = %57, %33, %20
%64 = load i32, i32* %4
%65 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.7, i64 0, i64 0), i32 %64)
%66 = load i32, i32* %4
%67 = call i32 @close(i32 %66)
store i8* null, i8** %2
br label %68
68: ; preds = %63, %14
%69 = load i8*, i8** %2
ret i8* %69
}

LLVM IR (-O2):
@.str = [38 x i8] c"[SERVER] CLIENT-%d connected, tid=%d\0A\00"
@SYS_gettid = global i32 0
@.str.1 = [29 x i8] c"[SERVER] CLIENT-%d loop ...\0A\00"
@.str.2 = [40 x i8] c"[SERVER] recv msg error: %s(errno: %d)\0A\00"
@errno = global i32 0
@.str.3 = [33 x i8] c"[SERVER] <<< CLIENT-%d: (%d) %s\0A\00"
@.str.4 = [5 x i8] c"exit\00"
@.str.5 = [8 x i8] c"got it\0A\00"
@.str.6 = [40 x i8] c"[SERVER] send msg error: %s(errno: %d)\0A\00"
@.str.7 = [27 x i8] c"[SERVER] CLIENT-%d closed\0A\00"
define noalias i8* @client_thread(i8* %0) {
%2 = alloca [1024 x i8]
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%6 = ptrtoint i8* %0 to i64
%7 = trunc i64 %6 to i32
%8 = icmp slt i32 %7, 0
br i1 %8, label %39, label %9
9: ; preds = %1
%10 = load i32, i32* @SYS_gettid
%11 = tail call i32 @syscall(i32 %10)
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i32 %7, i32 %11)
br label %13
13: ; preds = %21, %9
%14 = phi i32 [ 1, %9 ], [ %25, %21 ]
%15 = icmp eq i32 %14, 0
br i1 %15, label %36, label %16
16: ; preds = %13
%17 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i32 %7)
%18 = call i32 @memset(i8* %5, i32 0, i32 1024)
%19 = call i32 @recv(i32 %7, i8* %5, i32 1024, i32 0)
%20 = icmp slt i32 %19, 0
br i1 %20, label %30, label %21
21: ; preds = %16
%22 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i32 %7, i32 %19, i8* %5)
%23 = call i64 @strncmp(i8* %5, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i32 4)
%24 = icmp ne i64 %23, 0
%25 = zext i1 %24 to i32
%26 = call i32 @memset(i8* %4, i32 0, i32 1024)
%27 = call i32 @strcpy(i8* %4, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0))
%28 = call i32 @send(i32 %7, i8* %4, i32 1024, i32 0)
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %13
30: ; preds = %21, %16
%31 = phi i8* [ getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0), %16 ], [ getelementptr inbounds ([40 x i8], [40 x i8]* @.str.6, i64 0, i64 0), %21 ]
%32 = load i32, i32* @errno
%33 = call i8* @strerror(i32 %32)
%34 = load i32, i32* @errno
%35 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) %31, i8* %33, i32 %34)
br label %36
36: ; preds = %13, %30
%37 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.7, i64 0, i64 0), i32 %7)
%38 = call i32 @close(i32 %7)
br label %39
39: ; preds = %1, %36
ret i8* null
}