#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*/ u_char;
typedef scalar_t__ ssize_t;

/* Variables and functions */
int accept(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ close(int);
scalar_t__ recv(int, int /*<<< orphan*/ *, int, int /*<<< orphan*/);
scalar_t__ send(int, int /*<<< orphan*/ *, scalar_t__, int /*<<< orphan*/);
int /*<<< orphan*/ warn(char *);
void handle_connection(int accept_sock) {
  u_char buffer[128 * 1024];
  ssize_t len, recvlen, sofar;
  int s;
  s = accept(accept_sock, NULL, NULL);
  if (s < 0) {
    warn("accept");
    return;
  }
  while (1) {
    recvlen = recv(s, buffer, 128 * 1024, 0);
    if (recvlen < 0 || recvlen == 0) {
      close(s);
      return;
    }
    sofar = 0;
    while (sofar < recvlen) {
      len = send(s, buffer + sofar, recvlen - sofar, 0);
      if (len < 0) {
        close(s);
        return;
      }
      sofar += len;
    }
  }
}


x86 assembly (clang, -O0):
<handle_connection>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80030,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rsi
call   1070 <accept@plt>
mov    %eax,-0x8002c(%rbp)
cmpl   $0x0,-0x8002c(%rbp)
jge    1191 <handle_connection+0x41>
lea    0xe79(%rip),%rdi
call   1040 <warn@plt>
jmp    1275 <handle_connection+0x125>
jmp    1196 <handle_connection+0x46>
mov    -0x8002c(%rbp),%edi
lea    -0x80010(%rbp),%rsi
mov    $0x20000,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %rax,-0x80020(%rbp)
cmpq   $0x0,-0x80020(%rbp)
jl     11d2 <handle_connection+0x82>
cmpq   $0x0,-0x80020(%rbp)
jne    11e2 <handle_connection+0x92>
mov    -0x8002c(%rbp),%edi
call   1060 <close@plt>
jmp    1275 <handle_connection+0x125>
movq   $0x0,-0x80028(%rbp)
mov    -0x80028(%rbp),%rax
cmp    -0x80020(%rbp),%rax
jge    1270 <handle_connection+0x120>
mov    -0x8002c(%rbp),%edi
lea    -0x80010(%rbp),%rsi
mov    -0x80028(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
mov    -0x80020(%rbp),%rdx
sub    -0x80028(%rbp),%rdx
xor    %ecx,%ecx
call   1050 <send@plt>
mov    %rax,-0x80018(%rbp)
cmpq   $0x0,-0x80018(%rbp)
jge    1256 <handle_connection+0x106>
mov    -0x8002c(%rbp),%edi
call   1060 <close@plt>
jmp    1275 <handle_connection+0x125>
mov    -0x80018(%rbp),%rax
add    -0x80028(%rbp),%rax
mov    %rax,-0x80028(%rbp)
jmp    11ed <handle_connection+0x9d>
jmp    1196 <handle_connection+0x46>
add    $0x80030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<handle_connection>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x80008,%rsp
xor    %esi,%esi
xor    %edx,%edx
call   1070 <accept@plt>
test   %eax,%eax
js     11e5 <handle_connection+0x95>
mov    %eax,%r15d
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x20000,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %rax,%rax
jle    11cf <handle_connection+0x7f>
mov    %rax,%rbx
mov    %rsp,%r14
xor    %ebp,%ebp
nopl   0x0(%rax,%rax,1)
lea    (%rsp,%rbp,4),%rsi
mov    %rbx,%rdx
sub    %rbp,%rdx
mov    %r15d,%edi
xor    %ecx,%ecx
call   1050 <send@plt>
test   %rax,%rax
js     11cf <handle_connection+0x7f>
add    %rax,%rbp
cmp    %rbp,%rbx
jg     1190 <handle_connection+0x40>
mov    %r15d,%edi
mov    %r14,%rsi
mov    $0x20000,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %rax,%rbx
test   %rax,%rax
jle    11cf <handle_connection+0x7f>
xor    %ebp,%ebp
jmp    1190 <handle_connection+0x40>
mov    %r15d,%edi
call   1060 <close@plt>
add    $0x80008,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe14(%rip),%rdi
add    $0x80008,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <warn@plt>

LLVM IR (-O0):
@.str = [7 x i8] c"accept\00"
define void @handle_connection(i32 %0) {
%2 = alloca i32
%3 = alloca [131072 x i32]
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64
%7 = alloca i32
store i32 %0, i32* %2
%8 = load i32, i32* %2
%9 = call i32 @accept(i32 %8, i32* null, i32* null)
store i32 %9, i32* %7
%10 = load i32, i32* %7
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %1
%13 = call i32 @warn(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %51
14: ; preds = %1
br label %15
15: ; preds = %14, %50
%16 = load i32, i32* %7
%17 = getelementptr inbounds [131072 x i32], [131072 x i32]* %3, i64 0, i64 0
%18 = call i64 @recv(i32 %16, i32* %17, i32 131072, i32 0)
store i64 %18, i64* %5
%19 = load i64, i64* %5
%20 = icmp slt i64 %19, 0
br i1 %20, label %24, label %21
21: ; preds = %15
%22 = load i64, i64* %5
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %21, %15
%25 = load i32, i32* %7
%26 = call i32 @close(i32 %25)
br label %51
27: ; preds = %21
store i64 0, i64* %6
br label %28
28: ; preds = %46, %27
%29 = load i64, i64* %6
%30 = load i64, i64* %5
%31 = icmp slt i64 %29, %30
br i1 %31, label %32, label %50
32: ; preds = %28
%33 = load i32, i32* %7
%34 = getelementptr inbounds [131072 x i32], [131072 x i32]* %3, i64 0, i64 0
%35 = load i64, i64* %6
%36 = getelementptr inbounds i32, i32* %34, i64 %35
%37 = load i64, i64* %5
%38 = load i64, i64* %6
%39 = sub nsw i64 %37, %38
%40 = call i64 @send(i32 %33, i32* %36, i64 %39, i32 0)
store i64 %40, i64* %4
%41 = load i64, i64* %4
%42 = icmp slt i64 %41, 0
br i1 %42, label %43, label %46
43: ; preds = %32
%44 = load i32, i32* %7
%45 = call i32 @close(i32 %44)
br label %51
46: ; preds = %32
%47 = load i64, i64* %4
%48 = load i64, i64* %6
%49 = add nsw i64 %48, %47
store i64 %49, i64* %6
br label %28
50: ; preds = %28
br label %15
51: ; preds = %43, %24, %12
ret void
}

LLVM IR (-O2):
@.str = [7 x i8] c"accept\00"
define void @handle_connection(i32 %0) {
%2 = alloca [131072 x i32]
%3 = bitcast [131072 x i32]* %2 to i8*
%4 = tail call i32 @accept(i32 %0, i32* null, i32* null)
%5 = icmp slt i32 %4, 0
br i1 %5, label %10, label %6
6: ; preds = %1
%7 = getelementptr inbounds [131072 x i32], [131072 x i32]* %2, i64 0, i64 0
%8 = call i64 @recv(i32 %4, i32* %7, i32 131072, i32 0)
%9 = icmp slt i64 %8, 1
br i1 %9, label %17, label %15
10: ; preds = %1
%11 = tail call i32 @warn(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %30
12: ; preds = %19
%13 = call i64 @recv(i32 %4, i32* %7, i32 131072, i32 0)
%14 = icmp slt i64 %13, 1
br i1 %14, label %17, label %15
15: ; preds = %6, %12
%16 = phi i64 [ %13, %12 ], [ %8, %6 ]
br label %21
17: ; preds = %12, %6
%18 = call i32 @close(i32 %4)
br label %30
19: ; preds = %21
%20 = icmp sgt i64 %16, %27
br i1 %20, label %21, label %12
21: ; preds = %15, %19
%22 = phi i64 [ %27, %19 ], [ 0, %15 ]
%23 = getelementptr inbounds [131072 x i32], [131072 x i32]* %2, i64 0, i64 %22
%24 = sub nsw i64 %16, %22
%25 = call i64 @send(i32 %4, i32* %23, i64 %24, i32 0)
%26 = icmp slt i64 %25, 0
%27 = add nsw i64 %25, %22
br i1 %26, label %28, label %19
28: ; preds = %21
%29 = call i32 @close(i32 %4)
br label %30
30: ; preds = %28, %17, %10
ret void
}