#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*/ close(int);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
void handle_client(int client_fd) {
  char buf[1024];
  for (;;) {
    int retn = read(client_fd, buf, sizeof(buf - 1));
    if (0 > retn) {
      printf("read error\n");
      return;
    } else if (0 == retn) {
      printf("client exit\n");
      close(client_fd);
      break;
    }
    buf[retn] = '\0';
    printf("recv: %s\n", buf);
  }
}


x86 assembly (clang, -O0):
<handle_client>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x8,%edx
call   1050 <read@plt>
mov    %eax,-0x414(%rbp)
xor    %eax,%eax
cmp    -0x414(%rbp),%eax
jle    1179 <handle_client+0x49>
lea    0xe93(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11d0 <handle_client+0xa0>
xor    %eax,%eax
cmp    -0x414(%rbp),%eax
jne    11a2 <handle_client+0x72>
lea    0xe7e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x4(%rbp),%edi
call   1040 <close@plt>
jmp    11d0 <handle_client+0xa0>
jmp    11a7 <handle_client+0x77>
movslq -0x414(%rbp),%rax
movb   $0x0,-0x410(%rbp,%rax,1)
lea    -0x410(%rbp),%rsi
lea    0xe55(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    113e <handle_client+0xe>
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<handle_client>:
push   %rbp
push   %r14
push   %rbx
sub    $0x400,%rsp
mov    %edi,%ebp
mov    %rsp,%rsi
mov    $0x8,%edx
call   1060 <read@plt>
test   %eax,%eax
js     119a <handle_client+0x5a>
lea    0xe9b(%rip),%r14
mov    %rsp,%rbx
nopl   0x0(%rax,%rax,1)
test   %eax,%eax
je     11a8 <handle_client+0x68>
mov    %eax,%eax
movb   $0x0,(%rsp,%rax,1)
mov    %r14,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x8,%edx
call   1060 <read@plt>
test   %eax,%eax
jns    1170 <handle_client+0x30>
lea    0xe75(%rip),%rdi
call   1030 <puts@plt>
jmp    11bb <handle_client+0x7b>
lea    0xe5b(%rip),%rdi
call   1030 <puts@plt>
mov    %ebp,%edi
call   1050 <close@plt>
add    $0x400,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [12 x i8] c"read error\0A\00"
@.str.1 = [13 x i8] c"client exit\0A\00"
@.str.2 = [10 x i8] c"recv: %s\0A\00"
define void @handle_client(i32 %0) {
%2 = alloca i32
%3 = alloca [1024 x i8]
%4 = alloca i32
store i32 %0, i32* %2
br label %5
5: ; preds = %21, %1
%6 = load i32, i32* %2
%7 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%8 = call i32 @read(i32 %6, i8* %7, i32 8)
store i32 %8, i32* %4
%9 = load i32, i32* %4
%10 = icmp sgt i32 0, %9
br i1 %10, label %11, label %13
11: ; preds = %5
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %27
13: ; preds = %5
%14 = load i32, i32* %4
%15 = icmp eq i32 0, %14
br i1 %15, label %16, label %20
16: ; preds = %13
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%18 = load i32, i32* %2
%19 = call i32 @close(i32 %18)
br label %27
20: ; preds = %13
br label %21
21: ; preds = %20
%22 = load i32, i32* %4
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 %23
store i8 0, i8* %24
%25 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* %25)
br label %5
27: ; preds = %11, %16
ret void
}

LLVM IR (-O2):
@.str.2 = [10 x i8] c"recv: %s\0A\00"
@str = [12 x i8] c"client exit\00"
@str.3 = [11 x i8] c"read error\00"
define void @handle_client(i32 %0) {
%2 = alloca [1024 x i8]
%3 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%4 = call i32 @read(i32 %0, i8* %3, i32 8)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %14, %1
%7 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str.3, i64 0, i64 0))
br label %20
8: ; preds = %1, %14
%9 = phi i32 [ %18, %14 ], [ %4, %1 ]
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %8
%12 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
%13 = call i32 @close(i32 %0)
br label %20
14: ; preds = %8
%15 = zext i32 %9 to i64
%16 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 %15
store i8 0, i8* %16
%17 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* %3)
%18 = call i32 @read(i32 %0, i8* %3, i32 8)
%19 = icmp slt i32 %18, 0
br i1 %19, label %6, label %8
20: ; preds = %6, %11
ret void
}