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

/* Variables and functions */
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ memset(char *, char, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ write(int, char *, int);
void *thread_run(void *arg) {
  int fd = (int)arg;
  char buf[1024];
  int done = 0;
  while (!done) {
    memset(buf, '\0', sizeof(buf));
    int size = read(fd, buf, sizeof(buf) - 1);
    if (size > 0) {
      buf[size - 1] = '\0';
      printf("client : %s\n", buf);
      fflush(stdout);
      write(fd, buf, size);
    } else if (size == 0) {
      printf("client close... read done....\n");
      return NULL;
    } else {
      perror("read");
      return NULL;
    }
  }
  return (void *)0;
}


x86 assembly (clang, -O0):
<thread_run>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %eax,-0x14(%rbp)
movl   $0x0,-0x424(%rbp)
cmpl   $0x0,-0x424(%rbp)
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1199 <thread_run+0x39>
jmp    1269 <thread_run+0x109>
lea    -0x420(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1050 <memset@plt>
mov    -0x14(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x3ff,%edx
call   1060 <read@plt>
mov    %eax,-0x428(%rbp)
cmpl   $0x0,-0x428(%rbp)
jle    1223 <thread_run+0xc3>
mov    -0x428(%rbp),%eax
sub    $0x1,%eax
cltq
movb   $0x0,-0x420(%rbp,%rax,1)
lea    -0x420(%rbp),%rsi
lea    0xe0c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2dde(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
mov    -0x14(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    -0x428(%rbp),%edx
call   1030 <write@plt>
jmp    1264 <thread_run+0x104>
cmpl   $0x0,-0x428(%rbp)
jne    124b <thread_run+0xeb>
lea    0xdd6(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1271 <thread_run+0x111>
lea    0xdda(%rip),%rdi
call   1080 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    1271 <thread_run+0x111>
jmp    1180 <thread_run+0x20>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<thread_run>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x400,%rsp
mov    %rdi,%r15
mov    %rsp,%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1060 <memset@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x3ff,%edx
call   1070 <read@plt>
mov    %eax,%ebx
test   %eax,%eax
jle    120f <thread_run+0x9f>
lea    0xe4e(%rip),%r14
mov    %rsp,%r12
mov    0x2e24(%rip),%r13
nopl   0x0(%rax)
lea    -0x1(%rbx),%eax
movb   $0x0,(%rsp,%rax,1)
mov    %r14,%rdi
mov    %r12,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x0(%r13),%edi
call   1080 <fflush@plt>
mov    %r15d,%edi
mov    %r12,%rsi
mov    %ebx,%edx
call   1040 <write@plt>
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1060 <memset@plt>
mov    %r15d,%edi
mov    %r12,%rsi
mov    $0x3ff,%edx
call   1070 <read@plt>
mov    %eax,%ebx
test   %eax,%eax
jg     11c0 <thread_run+0x50>
test   %ebx,%ebx
jne    1232 <thread_run+0xc2>
lea    0xdf8(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x400,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
lea    0xdd4(%rip),%rdi
call   1090 <perror@plt>
jmp    121f <thread_run+0xaf>

LLVM IR (-O0):
@.str = [13 x i8] c"client : %s\0A\00"
@stdout = global i32 0
@.str.1 = [31 x i8] c"client close... read done....\0A\00"
@.str.2 = [5 x i8] c"read\00"
define i8* @thread_run(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca [1024 x i8]
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = ptrtoint i8* %8 to i32
store i32 %9, i32* %4
store i32 0, i32* %6
br label %10
10: ; preds = %42, %1
%11 = load i32, i32* %6
%12 = icmp ne i32 %11, 0
%13 = xor i1 %12, true
br i1 %13, label %14, label %43
14: ; preds = %10
%15 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%16 = call i32 @memset(i8* %15, i8 signext 0, i32 1024)
%17 = load i32, i32* %4
%18 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%19 = call i32 @read(i32 %17, i8* %18, i32 1023)
store i32 %19, i32* %7
%20 = load i32, i32* %7
%21 = icmp sgt i32 %20, 0
br i1 %21, label %22, label %35
22: ; preds = %14
%23 = load i32, i32* %7
%24 = sub nsw i32 %23, 1
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 %25
store i8 0, i8* %26
%27 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %27)
%29 = load i32, i32* @stdout
%30 = call i32 @fflush(i32 %29)
%31 = load i32, i32* %4
%32 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%33 = load i32, i32* %7
%34 = call i32 @write(i32 %31, i8* %32, i32 %33)
br label %42
35: ; preds = %14
%36 = load i32, i32* %7
%37 = icmp eq i32 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %35
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
store i8* null, i8** %2
br label %44
40: ; preds = %35
%41 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
store i8* null, i8** %2
br label %44
42: ; preds = %22
br label %10
43: ; preds = %10
store i8* null, i8** %2
br label %44
44: ; preds = %43, %40, %38
%45 = load i8*, i8** %2
ret i8* %45
}

LLVM IR (-O2):
@.str = [13 x i8] c"client : %s\0A\00"
@stdout = global i32 0
@.str.2 = [5 x i8] c"read\00"
@str = [30 x i8] c"client close... read done....\00"
define noalias i8* @thread_run(i8* %0) {
%2 = alloca [1024 x i8]
%3 = ptrtoint i8* %0 to i64
%4 = trunc i64 %3 to i32
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%6 = call i32 @memset(i8* %5, i8 signext 0, i32 1024)
%7 = call i32 @read(i32 %4, i8* %5, i32 1023)
%8 = icmp sgt i32 %7, 0
br i1 %8, label %9, label %21
9: ; preds = %1, %9
%10 = phi i32 [ %19, %9 ], [ %7, %1 ]
%11 = add nsw i32 %10, -1
%12 = zext i32 %11 to i64
%13 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 %12
store i8 0, i8* %13
%14 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %5)
%15 = load i32, i32* @stdout
%16 = call i32 @fflush(i32 %15)
%17 = call i32 @write(i32 %4, i8* %5, i32 %10)
%18 = call i32 @memset(i8* %5, i8 signext 0, i32 1024)
%19 = call i32 @read(i32 %4, i8* %5, i32 1023)
%20 = icmp sgt i32 %19, 0
br i1 %20, label %9, label %21
21: ; preds = %9, %1
%22 = phi i32 [ %7, %1 ], [ %19, %9 ]
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %21
%25 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str, i64 0, i64 0))
br label %28
26: ; preds = %21
%27 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %28
28: ; preds = %24, %26
ret i8* null
}