#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*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int recv(int, char *, int, int /*<<< orphan*/);
int send(int, char *, scalar_t__, int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
scalar_t__ strlen(char *);
int handle_client(int conn_fd) {
  char buf[4096] = {0};
  char msg[4096] = {0};
  int n;
  n = recv(conn_fd, buf, 4096 - 1, 0);
  if (n < 0) {
    perror("recv");
  } else if (n == 0) {
    printf("connect close by peer\n");
    close(conn_fd);
  } else {
    printf("recv:%s\n", buf);
  }
  snprintf(msg, 4096 - 1, "i am server, your conn_id is %d.", conn_fd);
  n = send(conn_fd, msg, strlen(msg) + 1, 0);
  printf("send: %s to id %d\n", msg, conn_fd);
  return 0;
err:
  close(conn_fd);
  return -1;
}


x86 assembly (clang, -O0):
<handle_client>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2030,%rsp
mov    %edi,-0x8(%rbp)
lea    -0x1010(%rbp),%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1080 <memset@plt>
lea    -0x2010(%rbp),%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1080 <memset@plt>
mov    -0x8(%rbp),%edi
lea    -0x1010(%rbp),%rsi
mov    $0xfff,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x2014(%rbp)
cmpl   $0x0,-0x2014(%rbp)
jge    11ee <handle_client+0x6e>
lea    0xe1c(%rip),%rdi
call   10a0 <perror@plt>
jmp    1230 <handle_client+0xb0>
cmpl   $0x0,-0x2014(%rbp)
jne    1216 <handle_client+0x96>
lea    0xe03(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x8(%rbp),%edi
call   1090 <close@plt>
jmp    122b <handle_client+0xab>
lea    -0x1010(%rbp),%rsi
lea    0xdf8(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    1230 <handle_client+0xb0>
lea    -0x2010(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0xfff,%esi
lea    0xddf(%rip),%rdx
call   1070 <snprintf@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x2024(%rbp)
lea    -0x2010(%rbp),%rax
mov    %rax,-0x2020(%rbp)
lea    -0x2010(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x2024(%rbp),%edi
mov    -0x2020(%rbp),%rsi
mov    %rax,%rdx
add    $0x1,%rdx
xor    %ecx,%ecx
call   1050 <send@plt>
mov    %eax,-0x2014(%rbp)
lea    -0x2010(%rbp),%rsi
mov    -0x8(%rbp),%edx
lea    0xda6(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x2030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<handle_client>:
push   %rbp
push   %r14
push   %rbx
sub    $0x2000,%rsp
mov    %edi,%ebp
lea    0x1000(%rsp),%r14
mov    $0x1000,%edx
mov    %r14,%rdi
xor    %esi,%esi
call   1090 <memset@plt>
mov    %rsp,%rdi
mov    $0x1000,%edx
xor    %esi,%esi
call   1090 <memset@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0xfff,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
js     1257 <handle_client+0xc7>
je     11f2 <handle_client+0x62>
lea    0xe24(%rip),%rdi
lea    0x1000(%rsp),%rsi
xor    %eax,%eax
call   1070 <printf@plt>
jmp    1205 <handle_client+0x75>
lea    0xe49(%rip),%rdi
call   1040 <puts@plt>
mov    %ebp,%edi
call   10a0 <close@plt>
lea    0xe02(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0xfff,%esi
mov    %ebp,%ecx
call   1080 <snprintf@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
lea    0x1(%rax),%rdx
mov    %ebp,%edi
mov    %rbx,%rsi
xor    %ecx,%ecx
call   1060 <send@plt>
lea    0xdf2(%rip),%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
xor    %eax,%eax
call   1070 <printf@plt>
xor    %eax,%eax
add    $0x2000,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xda2(%rip),%rdi
call   10b0 <perror@plt>
jmp    1205 <handle_client+0x75>

LLVM IR (-O0):
@.str = [5 x i8] c"recv\00"
@.str.1 = [23 x i8] c"connect close by peer\0A\00"
@.str.2 = [9 x i8] c"recv:%s\0A\00"
@.str.3 = [33 x i8] c"i am server, your conn_id is %d.\00"
@.str.4 = [19 x i8] c"send: %s to id %d\0A\00"
define i32 @handle_client(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [4096 x i8]
%5 = alloca [4096 x i8]
%6 = alloca i32
store i32 %0, i32* %3
%7 = bitcast [4096 x i8]* %4 to i8*
%8 = bitcast [4096 x i8]* %5 to i8*
%9 = load i32, i32* %3
%10 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%11 = call i32 @recv(i32 %9, i8* %10, i32 4095, i32 0)
store i32 %11, i32* %6
%12 = load i32, i32* %6
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %1
%15 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %27
16: ; preds = %1
%17 = load i32, i32* %6
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %23
19: ; preds = %16
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
%21 = load i32, i32* %3
%22 = call i32 @close(i32 %21)
br label %26
23: ; preds = %16
%24 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* %24)
br label %26
26: ; preds = %23, %19
br label %27
27: ; preds = %26, %14
%28 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%29 = load i32, i32* %3
%30 = call i32 @snprintf(i8* %28, i32 4095, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i32 %29)
%31 = load i32, i32* %3
%32 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%33 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%34 = call i64 @strlen(i8* %33)
%35 = add nsw i64 %34, 1
%36 = call i32 @send(i32 %31, i8* %32, i64 %35, i32 0)
store i32 %36, i32* %6
%37 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%38 = load i32, i32* %3
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i8* %37, i32 %38)
store i32 0, i32* %2
br label %43
40: ; No predecessors!
%41 = load i32, i32* %3
%42 = call i32 @close(i32 %41)
store i32 -1, i32* %2
br label %43
43: ; preds = %40, %27
%44 = load i32, i32* %2
ret i32 %44
}

LLVM IR (-O2):
@.str = [5 x i8] c"recv\00"
@.str.2 = [9 x i8] c"recv:%s\0A\00"
@.str.3 = [33 x i8] c"i am server, your conn_id is %d.\00"
@.str.4 = [19 x i8] c"send: %s to id %d\0A\00"
@str = [22 x i8] c"connect close by peer\00"
define i32 @handle_client(i32 %0) {
%2 = alloca [4096 x i8]
%3 = alloca [4096 x i8]
%4 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%6 = call i32 @recv(i32 %0, i8* %4, i32 4095, i32 0)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %17
10: ; preds = %1
%11 = icmp eq i32 %6, 0
br i1 %11, label %12, label %15
12: ; preds = %10
%13 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
%14 = call i32 @close(i32 %0)
br label %17
15: ; preds = %10
%16 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* %4)
br label %17
17: ; preds = %12, %15, %8
%18 = call i32 @snprintf(i8* %5, i32 4095, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i32 %0)
%19 = call i64 @strlen(i8* %5)
%20 = add nsw i64 %19, 1
%21 = call i32 @send(i32 %0, i8* %5, i64 %20, i32 0)
%22 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i8* %5, i32 %0)
ret i32 0
}