#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 recv(int, char *, int const, int /*<<< orphan*/);
int /*<<< orphan*/ strcat(char *, char *);
int request_receive(int sock, char message[], int max) {
  int total = 0;
  const int bsize = 1024;
  char buffer[bsize + 1];
  int read = bsize;
  message[0] = 0;
  while (read == bsize) {
    read = recv(sock, buffer, bsize, 0);
    if (read < 0)
      return -1;
    total += read;
    if (total > max)
      return -2;
    buffer[read] = 0;
    strcat(message, buffer);
  }
  return total;
}


x86 assembly (clang, -O0):
<request_receive>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x400,-0x1c(%rbp)
mov    %rsp,%rax
mov    %rax,-0x28(%rbp)
movl   $0x400,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
cmpl   $0x400,-0x2c(%rbp)
jne    11e4 <request_receive+0xc4>
mov    -0x8(%rbp),%edi
lea    -0x440(%rbp),%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    119b <request_receive+0x7b>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x30(%rbp)
jmp    11f1 <request_receive+0xd1>
mov    -0x2c(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jle    11c3 <request_receive+0xa3>
movl   $0xfffffffe,-0x4(%rbp)
movl   $0x1,-0x30(%rbp)
jmp    11f1 <request_receive+0xd1>
movslq -0x2c(%rbp),%rax
movb   $0x0,-0x440(%rbp,%rax,1)
mov    -0x10(%rbp),%rdi
lea    -0x440(%rbp),%rsi
call   1040 <strcat@plt>
jmp    1158 <request_receive+0x38>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x30(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<request_receive>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x408,%rsp
mov    %edx,%r14d
mov    %rsi,%r13
mov    %edi,%r15d
movb   $0x0,(%rsi)
xor    %ebx,%ebx
mov    %rsp,%r12
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r15d,%edi
mov    %r12,%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
js     118a <request_receive+0x6a>
mov    %eax,%ebp
add    %eax,%ebx
cmp    %r14d,%ebx
jg     1191 <request_receive+0x71>
mov    %ebp,%eax
movb   $0x0,(%rsp,%rax,1)
mov    %r13,%rdi
mov    %r12,%rsi
call   1040 <strcat@plt>
cmp    $0x400,%ebp
je     1150 <request_receive+0x30>
jmp    1196 <request_receive+0x76>
mov    $0xffffffff,%ebx
jmp    1196 <request_receive+0x76>
mov    $0xfffffffe,%ebx
mov    %ebx,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @request_receive(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 0, i32* %8
store i32 1024, i32* %9
%13 = call i8* @llvm.stacksave()
store i8* %13, i8** %10
%14 = alloca i8, i64 1025
store i32 1024, i32* %11
%15 = load i8*, i8** %6
%16 = getelementptr inbounds i8, i8* %15, i64 0
store i8 0, i8* %16
br label %17
17: ; preds = %34, %3
%18 = load i32, i32* %11
%19 = icmp eq i32 %18, 1024
br i1 %19, label %20, label %40
20: ; preds = %17
%21 = load i32, i32* %5
%22 = call i32 @recv(i32 %21, i8* %14, i32 1024, i32 0)
store i32 %22, i32* %11
%23 = load i32, i32* %11
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %20
store i32 -1, i32* %4
store i32 1, i32* %12
br label %42
26: ; preds = %20
%27 = load i32, i32* %11
%28 = load i32, i32* %8
%29 = add nsw i32 %28, %27
store i32 %29, i32* %8
%30 = load i32, i32* %8
%31 = load i32, i32* %7
%32 = icmp sgt i32 %30, %31
br i1 %32, label %33, label %34
33: ; preds = %26
store i32 -2, i32* %4
store i32 1, i32* %12
br label %42
34: ; preds = %26
%35 = load i32, i32* %11
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i8, i8* %14, i64 %36
store i8 0, i8* %37
%38 = load i8*, i8** %6
%39 = call i32 @strcat(i8* %38, i8* %14)
br label %17
40: ; preds = %17
%41 = load i32, i32* %8
store i32 %41, i32* %4
store i32 1, i32* %12
br label %42
42: ; preds = %40, %33, %25
%43 = load i8*, i8** %10
%44 = load i32, i32* %4
ret i32 %44
}

LLVM IR (-O2):
define i32 @request_receive(i32 %0, i8* %1, i32 %2) {
%4 = alloca [1025 x i8]
%5 = getelementptr inbounds [1025 x i8], [1025 x i8]* %4, i64 0, i64 0
store i8 0, i8* %1
br label %6
6: ; preds = %3, %13
%7 = phi i32 [ 0, %3 ], [ %11, %13 ]
%8 = call i32 @recv(i32 %0, i8* %5, i32 1024, i32 0)
%9 = icmp slt i32 %8, 0
br i1 %9, label %18, label %10
10: ; preds = %6
%11 = add nsw i32 %8, %7
%12 = icmp sgt i32 %11, %2
br i1 %12, label %18, label %13
13: ; preds = %10
%14 = zext i32 %8 to i64
%15 = getelementptr inbounds [1025 x i8], [1025 x i8]* %4, i64 0, i64 %14
store i8 0, i8* %15
%16 = call i32 @strcat(i8* %1, i8* %5)
%17 = icmp eq i32 %8, 1024
br i1 %17, label %6, label %18
18: ; preds = %13, %10, %6
%19 = phi i32 [ -1, %6 ], [ -2, %10 ], [ %11, %13 ]
ret i32 %19
}