#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*/ Die(char *);
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ ftruncate(int, long long);
char *malloc(int);
int /*<<< orphan*/ memset(char *, int, int);
int mkstemp(char *);
int recv(int, char *, int, int /*<<< orphan*/);
long long sendfile(int, int, int /*<<< orphan*/, long long);
int /*<<< orphan*/ stderr;
long long strtoll(char *, char **, int);
int /*<<< orphan*/ unlink(char *);
void HandleClient(int sock) {
  char buffer[32];
  char *tailptr;
  int received = -1;
  long long int respsize, sendsize;
  char *blockptr = malloc(0x10000);
  memset(blockptr, 0xaa, 0x10000);
  if ((received = recv(sock, buffer, 32 - 1, 0)) < 0) {
    Die("Failed to receive initial bytes from client");
  }
  buffer[received] = 0;
  respsize = strtoll(buffer, &tailptr, 10);
  fprintf(stderr, "client request (\"%s\")", buffer);
  if (errno) {
    Die("\nFailed to parse client request");
  } else {
    switch (*tailptr) {
    case 'k':
    case 'K':
      respsize <<= 10;
      break;
    case 'm':
    case 'M':
      respsize <<= 20;
      break;
    case 'g':
    case 'G':
      respsize <<= 30;
      break;
    };
    fprintf(stderr, ", parsed request as %lld bytes\n", respsize);
  }
  char tmpn[] = "/tmp/XXXXXX";
  int tmpfd = mkstemp(tmpn);
  ftruncate(tmpfd, respsize);
  while (respsize > 0) {
    sendsize = respsize > 2147479552 ? 2147479552 : respsize;
    respsize -= sendsize;
    if (sendfile(sock, tmpfd, 0, sendsize) != sendsize) {
      Die("Failed to send bytes to client");
    }
  }
  close(tmpfd);
  unlink(tmpn);
  close(sock);
}


x86 assembly (clang, -O0):
<HandleClient>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %edi,-0x4(%rbp)
movl   $0xffffffff,-0x3c(%rbp)
mov    $0x10000,%edi
call   10a0 <malloc@plt>
mov    %rax,-0x58(%rbp)
mov    -0x58(%rbp),%rdi
mov    $0xaa,%esi
mov    $0x10000,%edx
call   1060 <memset@plt>
mov    -0x4(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x1f,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x3c(%rbp)
cmp    $0x0,%eax
jge    1211 <HandleClient+0x61>
lea    0xdf4(%rip),%rdi
call   10c0 <Die@plt>
movslq -0x3c(%rbp),%rax
movb   $0x0,-0x30(%rbp,%rax,1)
lea    -0x30(%rbp),%rdi
lea    -0x38(%rbp),%rsi
mov    $0xa,%edx
call   1080 <strtoll@plt>
mov    %rax,-0x48(%rbp)
mov    0x2da9(%rip),%rax
mov    (%rax),%edi
lea    -0x30(%rbp),%rdx
lea    0xde8(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    0x2d86(%rip),%rax
cmpq   $0x0,(%rax)
je     126d <HandleClient+0xbd>
lea    0xddf(%rip),%rdi
call   10c0 <Die@plt>
jmp    1323 <HandleClient+0x173>
mov    -0x38(%rbp),%rax
movsbl (%rax),%eax
mov    %eax,-0x6c(%rbp)
sub    $0x47,%eax
je     12fc <HandleClient+0x14c>
jmp    1285 <HandleClient+0xd5>
mov    -0x6c(%rbp),%eax
sub    $0x4b,%eax
je     12da <HandleClient+0x12a>
jmp    1296 <HandleClient+0xe6>
mov    -0x6c(%rbp),%eax
sub    $0x4d,%eax
je     12eb <HandleClient+0x13b>
jmp    12a7 <HandleClient+0xf7>
mov    -0x6c(%rbp),%eax
sub    $0x67,%eax
je     12fc <HandleClient+0x14c>
jmp    12b8 <HandleClient+0x108>
mov    -0x6c(%rbp),%eax
sub    $0x6b,%eax
je     12da <HandleClient+0x12a>
jmp    12c9 <HandleClient+0x119>
mov    -0x6c(%rbp),%eax
sub    $0x6d,%eax
je     12eb <HandleClient+0x13b>
jmp    1308 <HandleClient+0x158>
mov    -0x48(%rbp),%rax
shl    $0xa,%rax
mov    %rax,-0x48(%rbp)
jmp    1308 <HandleClient+0x158>
mov    -0x48(%rbp),%rax
shl    $0x14,%rax
mov    %rax,-0x48(%rbp)
jmp    1308 <HandleClient+0x158>
mov    -0x48(%rbp),%rax
shl    $0x1e,%rax
mov    %rax,-0x48(%rbp)
mov    0x2cd1(%rip),%rax
mov    (%rax),%edi
mov    -0x48(%rbp),%rdx
lea    0xd46(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    0xd58(%rip),%rax
mov    %rax,-0x64(%rbp)
mov    0xd56(%rip),%eax
mov    %eax,-0x5c(%rbp)
lea    -0x64(%rbp),%rdi
call   10b0 <mkstemp@plt>
mov    %eax,-0x68(%rbp)
mov    -0x68(%rbp),%edi
mov    -0x48(%rbp),%rsi
call   1050 <ftruncate@plt>
cmpq   $0x0,-0x48(%rbp)
jle    13c1 <HandleClient+0x211>
cmpq   $0x7ffff000,-0x48(%rbp)
7f
jle    1376 <HandleClient+0x1c6>
mov    $0x7ffff000,%eax
mov    %rax,-0x78(%rbp)
jmp    137e <HandleClient+0x1ce>
mov    -0x48(%rbp),%rax
mov    %rax,-0x78(%rbp)
mov    -0x78(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x50(%rbp),%rcx
mov    -0x48(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x48(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x68(%rbp),%esi
mov    -0x50(%rbp),%rcx
xor    %edx,%edx
call   10d0 <sendfile@plt>
cmp    -0x50(%rbp),%rax
je     13bc <HandleClient+0x20c>
lea    0xcd7(%rip),%rdi
call   10c0 <Die@plt>
jmp    134f <HandleClient+0x19f>
mov    -0x68(%rbp),%edi
call   1070 <close@plt>
lea    -0x64(%rbp),%rdi
call   1040 <unlink@plt>
mov    -0x4(%rbp),%edi
call   1070 <close@plt>
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<HandleClient>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x40,%rsp
mov    %edi,%r14d
mov    $0x10000,%edi
call   10a0 <malloc@plt>
mov    %rax,%rdi
mov    $0xaa,%esi
mov    $0x10000,%edx
call   1060 <memset@plt>
lea    0x20(%rsp),%rsi
mov    %r14d,%edi
mov    $0x1f,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    1201 <HandleClient+0x51>
lea    0xe04(%rip),%rdi
call   10c0 <Die@plt>
movslq %ebx,%rax
movb   $0x0,0x20(%rsp,%rax,1)
lea    0x20(%rsp),%rbp
lea    0x18(%rsp),%rsi
mov    %rbp,%rdi
mov    $0xa,%edx
call   1080 <strtoll@plt>
mov    %rax,%rbx
mov    0x2db6(%rip),%r15
mov    (%r15),%edi
lea    0xdf8(%rip),%rsi
mov    %rbp,%rdx
xor    %eax,%eax
call   1090 <fprintf@plt>
mov    0x2d93(%rip),%rax
cmpq   $0x0,(%rax)
je     1259 <HandleClient+0xa9>
lea    0xdf0(%rip),%rdi
call   10c0 <Die@plt>
jmp    12bd <HandleClient+0x10d>
mov    0x18(%rsp),%rax
movsbl (%rax),%eax
add    $0xffffffb9,%eax
cmp    $0x26,%eax
ja     12a9 <HandleClient+0xf9>
movabs $0x100000001,%rcx
bt     %rax,%rcx
jb     129f <HandleClient+0xef>
movabs $0x1000000010,%rcx
bt     %rax,%rcx
jb     12a5 <HandleClient+0xf5>
movabs $0x4000000040,%rcx
bt     %rax,%rcx
jae    12a9 <HandleClient+0xf9>
shl    $0x14,%rbx
jmp    12a9 <HandleClient+0xf9>
shl    $0x1e,%rbx
jmp    12a9 <HandleClient+0xf9>
shl    $0xa,%rbx
mov    (%r15),%edi
lea    0xdaf(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1090 <fprintf@plt>
movabs $0x5858582f706d742f,%rax
58 58 58
mov    %rax,0x8(%rsp)
movl   $0x585858,0x10(%rsp)
lea    0x8(%rsp),%rdi
call   10b0 <mkstemp@plt>
mov    %eax,%r12d
mov    %eax,%edi
mov    %rbx,%rsi
call   1050 <ftruncate@plt>
test   %rbx,%rbx
jle    1337 <HandleClient+0x187>
lea    0xd97(%rip),%r15
jmp    1305 <HandleClient+0x155>
nopl   0x0(%rax)
test   %rbx,%rbx
jle    1337 <HandleClient+0x187>
cmp    $0x7ffff000,%rbx
mov    $0x7ffff000,%ebp
cmovb  %rbx,%rbp
sub    %rbp,%rbx
mov    %r14d,%edi
mov    %r12d,%esi
xor    %edx,%edx
mov    %rbp,%rcx
call   10d0 <sendfile@plt>
cmp    %rbp,%rax
je     1300 <HandleClient+0x150>
mov    %r15,%rdi
call   10c0 <Die@plt>
jmp    1300 <HandleClient+0x150>
mov    %r12d,%edi
call   1070 <close@plt>
lea    0x8(%rsp),%rdi
call   1040 <unlink@plt>
mov    %r14d,%edi
call   1070 <close@plt>
add    $0x40,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [44 x i8] c"Failed to receive initial bytes from client\00"
@stderr = global i32 0
@.str.1 = [22 x i8] c"client request (\22%s\22)\00"
@errno = global i64 0
@.str.2 = [32 x i8] c"\0AFailed to parse client request\00"
@.str.3 = [32 x i8] c", parsed request as %lld bytes\0A\00"
@__const.HandleClient.tmpn = [12 x i8] c"/tmp/XXXXXX\00"
@.str.4 = [31 x i8] c"Failed to send bytes to client\00"
define void @HandleClient(i32 %0) {
%2 = alloca i32
%3 = alloca [32 x i8]
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i64
%7 = alloca i64
%8 = alloca i8*
%9 = alloca [12 x i8]
%10 = alloca i32
store i32 %0, i32* %2
store i32 -1, i32* %5
%11 = call i8* @malloc(i32 65536)
store i8* %11, i8** %8
%12 = load i8*, i8** %8
%13 = call i32 @memset(i8* %12, i32 170, i32 65536)
%14 = load i32, i32* %2
%15 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 0
%16 = call i32 @recv(i32 %14, i8* %15, i32 31, i32 0)
store i32 %16, i32* %5
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %1
%19 = call i32 @Die(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0))
br label %20
20: ; preds = %18, %1
%21 = load i32, i32* %5
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 %22
store i8 0, i8* %23
%24 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 0
%25 = call i64 @strtoll(i8* %24, i8** %4, i32 10)
store i64 %25, i64* %6
%26 = load i32, i32* @stderr
%27 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 0
%28 = call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %27)
%29 = load i64, i64* @errno
%30 = icmp ne i64 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %20
%32 = call i32 @Die(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0))
br label %50
33: ; preds = %20
%34 = load i8*, i8** %4
%35 = load i8, i8* %34
%36 = sext i8 %35 to i32
switch i32 %36, label %46 [
i32 107, label %37
i32 75, label %37
i32 109, label %40
i32 77, label %40
i32 103, label %43
i32 71, label %43
]
37: ; preds = %33, %33
%38 = load i64, i64* %6
%39 = shl i64 %38, 10
store i64 %39, i64* %6
br label %46
40: ; preds = %33, %33
%41 = load i64, i64* %6
%42 = shl i64 %41, 20
store i64 %42, i64* %6
br label %46
43: ; preds = %33, %33
%44 = load i64, i64* %6
%45 = shl i64 %44, 30
store i64 %45, i64* %6
br label %46
46: ; preds = %33, %43, %40, %37
%47 = load i32, i32* @stderr
%48 = load i64, i64* %6
%49 = call i32 (i32, i8*, ...) @fprintf(i32 %47, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i64 %48)
br label %50
50: ; preds = %46, %31
%51 = bitcast [12 x i8]* %9 to i8*
%52 = getelementptr inbounds [12 x i8], [12 x i8]* %9, i64 0, i64 0
%53 = call i32 @mkstemp(i8* %52)
store i32 %53, i32* %10
%54 = load i32, i32* %10
%55 = load i64, i64* %6
%56 = call i32 @ftruncate(i32 %54, i64 %55)
br label %57
57: ; preds = %79, %50
%58 = load i64, i64* %6
%59 = icmp sgt i64 %58, 0
br i1 %59, label %60, label %80
60: ; preds = %57
%61 = load i64, i64* %6
%62 = icmp sgt i64 %61, 2147479552
br i1 %62, label %63, label %64
63: ; preds = %60
br label %66
64: ; preds = %60
%65 = load i64, i64* %6
br label %66
66: ; preds = %64, %63
%67 = phi i64 [ 2147479552, %63 ], [ %65, %64 ]
store i64 %67, i64* %7
%68 = load i64, i64* %7
%69 = load i64, i64* %6
%70 = sub nsw i64 %69, %68
store i64 %70, i64* %6
%71 = load i32, i32* %2
%72 = load i32, i32* %10
%73 = load i64, i64* %7
%74 = call i64 @sendfile(i32 %71, i32 %72, i32 0, i64 %73)
%75 = load i64, i64* %7
%76 = icmp ne i64 %74, %75
br i1 %76, label %77, label %79
77: ; preds = %66
%78 = call i32 @Die(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0))
br label %79
79: ; preds = %77, %66
br label %57
80: ; preds = %57
%81 = load i32, i32* %10
%82 = call i32 @close(i32 %81)
%83 = getelementptr inbounds [12 x i8], [12 x i8]* %9, i64 0, i64 0
%84 = call i32 @unlink(i8* %83)
%85 = load i32, i32* %2
%86 = call i32 @close(i32 %85)
ret void
}

LLVM IR (-O2):
@.str = [44 x i8] c"Failed to receive initial bytes from client\00"
@stderr = global i32 0
@.str.1 = [22 x i8] c"client request (\22%s\22)\00"
@errno = global i64 0
@.str.2 = [32 x i8] c"\0AFailed to parse client request\00"
@.str.3 = [32 x i8] c", parsed request as %lld bytes\0A\00"
@__const.HandleClient.tmpn = [12 x i8] c"/tmp/XXXXXX\00"
@.str.4 = [31 x i8] c"Failed to send bytes to client\00"
define void @HandleClient(i32 %0) {
%2 = alloca [32 x i8]
%3 = alloca i8*
%4 = alloca [12 x i8]
%5 = getelementptr inbounds [32 x i8], [32 x i8]* %2, i64 0, i64 0
%6 = bitcast i8** %3 to i8*
%7 = tail call dereferenceable_or_null(65536) i8* @malloc(i32 65536)
%8 = tail call i32 @memset(i8* %7, i32 170, i32 65536)
%9 = call i32 @recv(i32 %0, i8* %5, i32 31, i32 0)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %1
%12 = call i32 @Die(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0))
br label %13
13: ; preds = %11, %1
%14 = sext i32 %9 to i64
%15 = getelementptr inbounds [32 x i8], [32 x i8]* %2, i64 0, i64 %14
store i8 0, i8* %15
%16 = call i64 @strtoll(i8* %5, i8** %3, i32 10)
%17 = load i32, i32* @stderr
%18 = call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %5)
%19 = load i64, i64* @errno
%20 = icmp eq i64 %19, 0
br i1 %20, label %23, label %21
21: ; preds = %13
%22 = call i32 @Die(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0))
br label %37
23: ; preds = %13
%24 = load i8*, i8** %3
%25 = load i8, i8* %24
%26 = sext i8 %25 to i32
switch i32 %26, label %33 [
i32 107, label %27
i32 75, label %27
i32 109, label %29
i32 77, label %29
i32 103, label %31
i32 71, label %31
]
27: ; preds = %23, %23
%28 = shl i64 %16, 10
br label %33
29: ; preds = %23, %23
%30 = shl i64 %16, 20
br label %33
31: ; preds = %23, %23
%32 = shl i64 %16, 30
br label %33
33: ; preds = %23, %31, %29, %27
%34 = phi i64 [ %16, %23 ], [ %32, %31 ], [ %30, %29 ], [ %28, %27 ]
%35 = load i32, i32* @stderr
%36 = call i32 (i32, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i64 %34)
br label %37
37: ; preds = %33, %21
%38 = phi i64 [ %16, %21 ], [ %34, %33 ]
%39 = getelementptr inbounds [12 x i8], [12 x i8]* %4, i64 0, i64 0
%40 = call i32 @mkstemp(i8* %39)
%41 = call i32 @ftruncate(i32 %40, i64 %38)
%42 = icmp sgt i64 %38, 0
br i1 %42, label %43, label %54
43: ; preds = %37, %52
%44 = phi i64 [ %47, %52 ], [ %38, %37 ]
%45 = icmp ult i64 %44, 2147479552
%46 = select i1 %45, i64 %44, i64 2147479552
%47 = sub nsw i64 %44, %46
%48 = call i64 @sendfile(i32 %0, i32 %40, i32 0, i64 %46)
%49 = icmp eq i64 %48, %46
br i1 %49, label %52, label %50
50: ; preds = %43
%51 = call i32 @Die(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0))
br label %52
52: ; preds = %50, %43
%53 = icmp sgt i64 %47, 0
br i1 %53, label %43, label %54
54: ; preds = %52, %37
%55 = call i32 @close(i32 %40)
%56 = call i32 @unlink(i8* %39)
%57 = call i32 @close(i32 %0)
ret void
}