#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 ssize_t;
typedef int /*<<< orphan*/ buffer;

/* Variables and functions */
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ die_errno_msg(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ printf(char *, ...);
int recv(int, char *, int, int /*<<< orphan*/);
int send(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ write(int, char *, int);
void handle_client(int client_fd) {
  char buffer[128];
  ssize_t size, sent_size;
  printf("Handling fd: %d\n", client_fd);
  for (;;) {
    size = recv(client_fd, buffer, sizeof(buffer), 0);
    if (size == -1) {
      die_errno_msg(errno, "recv failed (%d)", client_fd);
    }
    if (size == 0)
      break;
    printf("%d: Recv'd %ld bytes:\n<<<<<<\n", client_fd, (long)size);
    (void)write(1, buffer, size);
    printf(">>>>>>\n");
    char *p = buffer;
    while (size) {
      sent_size = send(client_fd, p, size, 0);
      if (sent_size == -1) {
        die_errno_msg(errno, "send failed (%d)", client_fd);
      }
      p += sent_size;
      size -= sent_size;
    }
  }
  close(client_fd);
  printf("Client disconnected (%d)\n", client_fd);
}


x86 assembly (clang, -O0):
<handle_client>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%esi
lea    0xe88(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x4(%rbp),%edi
lea    -0x90(%rbp),%rsi
mov    $0x80,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x94(%rbp)
cmpl   $0xffffffff,-0x94(%rbp)
jne    11c0 <handle_client+0x60>
mov    0x2e31(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%edx
lea    0xe56(%rip),%rsi
call   1080 <die_errno_msg@plt>
cmpl   $0x0,-0x94(%rbp)
jne    11d2 <handle_client+0x72>
jmp    12a4 <handle_client+0x144>
mov    -0x4(%rbp),%esi
movslq -0x94(%rbp),%rdx
lea    0xe3f(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    -0x90(%rbp),%rsi
mov    -0x94(%rbp),%edx
mov    $0x1,%edi
call   1040 <write@plt>
lea    0xe38(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    -0x90(%rbp),%rax
mov    %rax,-0xa0(%rbp)
cmpl   $0x0,-0x94(%rbp)
je     129f <handle_client+0x13f>
mov    -0x4(%rbp),%edi
mov    -0xa0(%rbp),%rsi
mov    -0x94(%rbp),%edx
xor    %ecx,%ecx
call   1050 <send@plt>
mov    %eax,-0x98(%rbp)
cmpl   $0xffffffff,-0x98(%rbp)
jne    126c <handle_client+0x10c>
mov    0x2d85(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%edx
lea    0xde1(%rip),%rsi
call   1080 <die_errno_msg@plt>
mov    -0x98(%rbp),%ecx
mov    -0xa0(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0xa0(%rbp)
mov    -0x98(%rbp),%ecx
mov    -0x94(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x94(%rbp)
jmp    121d <handle_client+0xbd>
jmp    117f <handle_client+0x1f>
mov    -0x4(%rbp),%edi
call   1070 <close@plt>
mov    -0x4(%rbp),%esi
lea    0xda3(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<handle_client>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x88,%rsp
mov    %edi,%ebx
lea    0xe76(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1070 <printf@plt>
mov    0x2e46(%rip),%r12
lea    0xe9f(%rip),%rbp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %ebx,%edi
mov    %rsp,%rsi
mov    $0x80,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,%r15d
cmp    $0xffffffff,%eax
je     11e0 <handle_client+0x70>
test   %r15d,%r15d
jne    11f2 <handle_client+0x82>
jmp    1266 <handle_client+0xf6>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    (%r12),%edi
lea    0xe26(%rip),%rsi
mov    %ebx,%edx
call   1090 <die_errno_msg@plt>
movslq %r15d,%rdx
lea    0xe26(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0x1,%edi
mov    %rsp,%r14
mov    %r14,%rsi
mov    %r15d,%edx
call   1050 <write@plt>
lea    0xe4c(%rip),%rdi
call   1040 <puts@plt>
jmp    123f <handle_client+0xcf>
cs nopw 0x0(%rax,%rax,1)
movslq %r13d,%rax
add    %rax,%r14
sub    %r13d,%r15d
je     11b0 <handle_client+0x40>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %r15d,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
mov    %eax,%r13d
cmp    $0xffffffff,%eax
jne    1230 <handle_client+0xc0>
mov    (%r12),%edi
mov    %rbp,%rsi
mov    %ebx,%edx
call   1090 <die_errno_msg@plt>
jmp    1230 <handle_client+0xc0>
mov    %ebx,%edi
call   1080 <close@plt>
lea    0xddd(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1070 <printf@plt>
add    $0x88,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [17 x i8] c"Handling fd: %d\0A\00"
@errno = global i32 0
@.str.1 = [17 x i8] c"recv failed (%d)\00"
@.str.2 = [30 x i8] c"%d: Recv'd %ld bytes:\0A<<<<<<\0A\00"
@.str.3 = [8 x i8] c">>>>>>\0A\00"
@.str.4 = [17 x i8] c"send failed (%d)\00"
@.str.5 = [26 x i8] c"Client disconnected (%d)\0A\00"
define void @handle_client(i32 %0) {
%2 = alloca i32
%3 = alloca [128 x i8]
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
store i32 %0, i32* %2
%7 = load i32, i32* %2
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %7)
br label %9
9: ; preds = %55, %1
%10 = load i32, i32* %2
%11 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%12 = call i32 @recv(i32 %10, i8* %11, i32 128, i32 0)
store i32 %12, i32* %4
%13 = load i32, i32* %4
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %19
15: ; preds = %9
%16 = load i32, i32* @errno
%17 = load i32, i32* %2
%18 = call i32 @die_errno_msg(i32 %16, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %17)
br label %19
19: ; preds = %15, %9
%20 = load i32, i32* %4
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %19
br label %56
23: ; preds = %19
%24 = load i32, i32* %2
%25 = load i32, i32* %4
%26 = sext i32 %25 to i64
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i32 %24, i64 %26)
%28 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%29 = load i32, i32* %4
%30 = call i32 @write(i32 1, i8* %28, i32 %29)
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%32 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
store i8* %32, i8** %6
br label %33
33: ; preds = %47, %23
%34 = load i32, i32* %4
%35 = icmp ne i32 %34, 0
br i1 %35, label %36, label %55
36: ; preds = %33
%37 = load i32, i32* %2
%38 = load i8*, i8** %6
%39 = load i32, i32* %4
%40 = call i32 @send(i32 %37, i8* %38, i32 %39, i32 0)
store i32 %40, i32* %5
%41 = load i32, i32* %5
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %47
43: ; preds = %36
%44 = load i32, i32* @errno
%45 = load i32, i32* %2
%46 = call i32 @die_errno_msg(i32 %44, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i32 %45)
br label %47
47: ; preds = %43, %36
%48 = load i32, i32* %5
%49 = load i8*, i8** %6
%50 = sext i32 %48 to i64
%51 = getelementptr inbounds i8, i8* %49, i64 %50
store i8* %51, i8** %6
%52 = load i32, i32* %5
%53 = load i32, i32* %4
%54 = sub nsw i32 %53, %52
store i32 %54, i32* %4
br label %33
55: ; preds = %33
br label %9
56: ; preds = %22
%57 = load i32, i32* %2
%58 = call i32 @close(i32 %57)
%59 = load i32, i32* %2
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0), i32 %59)
ret void
}

LLVM IR (-O2):
@.str = [17 x i8] c"Handling fd: %d\0A\00"
@errno = global i32 0
@.str.1 = [17 x i8] c"recv failed (%d)\00"
@.str.2 = [30 x i8] c"%d: Recv'd %ld bytes:\0A<<<<<<\0A\00"
@.str.4 = [17 x i8] c"send failed (%d)\00"
@.str.5 = [26 x i8] c"Client disconnected (%d)\0A\00"
@str = [7 x i8] c">>>>>>\00"
define void @handle_client(i32 %0) {
%2 = alloca [128 x i8]
%3 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%4 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %0)
br label %5
5: ; preds = %23, %1
%6 = call i32 @recv(i32 %0, i8* %3, i32 128, i32 0)
switch i32 %6, label %10 [
i32 -1, label %7
i32 0, label %28
]
7: ; preds = %5
%8 = load i32, i32* @errno
%9 = call i32 @die_errno_msg(i32 %8, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %0)
br label %10
10: ; preds = %5, %7
%11 = sext i32 %6 to i64
%12 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i32 %0, i64 %11)
%13 = call i32 @write(i32 1, i8* %3, i32 %6)
%14 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @str, i64 0, i64 0))
br label %15
15: ; preds = %10, %23
%16 = phi i8* [ %25, %23 ], [ %3, %10 ]
%17 = phi i32 [ %26, %23 ], [ %6, %10 ]
%18 = call i32 @send(i32 %0, i8* %16, i32 %17, i32 0)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %23
20: ; preds = %15
%21 = load i32, i32* @errno
%22 = call i32 @die_errno_msg(i32 %21, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i32 %0)
br label %23
23: ; preds = %20, %15
%24 = sext i32 %18 to i64
%25 = getelementptr inbounds i8, i8* %16, i64 %24
%26 = sub nsw i32 %17, %18
%27 = icmp eq i32 %26, 0
br i1 %27, label %5, label %15
28: ; preds = %5
%29 = call i32 @close(i32 %0)
%30 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0), i32 %0)
ret void
}