#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, int /*<<< orphan*/);
int receive_data(int connection, char *buffer, int buffer_size) {
  int total = 0;
  int n = 0;
  int recv_size;
  int left = buffer_size;
  while (total < buffer_size) {
    recv_size = left > 1024 ? 1024 : left;
    n = recv(connection, buffer + total, recv_size, 0);
    if (!n) {
      break;
    }
    total += n;
    left -= n;
  }
  return n < 0 ? 1 : 0;
}


x86 assembly (clang, -O0):
<receive_data>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11ab <receive_data+0x9b>
cmpl   $0x400,-0x24(%rbp)
jle    115c <receive_data+0x4c>
mov    $0x400,%eax
mov    %eax,-0x28(%rbp)
jmp    1162 <receive_data+0x52>
mov    -0x24(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x18(%rbp),%rax
add    %rax,%rsi
mov    -0x20(%rbp),%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    1192 <receive_data+0x82>
jmp    11ab <receive_data+0x9b>
mov    -0x1c(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x1c(%rbp),%ecx
mov    -0x24(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x24(%rbp)
jmp    1136 <receive_data+0x26>
mov    -0x1c(%rbp),%edx
xor    %eax,%eax
mov    $0x1,%ecx
cmp    $0x0,%edx
cmovl  %ecx,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<receive_data>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %edx,%edx
jle    1160 <receive_data+0x50>
mov    %edx,%r13d
mov    %rsi,%r15
mov    %edi,%r12d
xor    %r14d,%r14d
mov    %edx,%ebp
xor    %ebx,%ebx
nop
cmp    $0x400,%ebp
mov    $0x400,%edx
cmovl  %ebp,%edx
movslq %ebx,%rsi
add    %r15,%rsi
mov    %r12d,%edi
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
je     1163 <receive_data+0x53>
add    %eax,%ebx
sub    %eax,%ebp
cmp    %r13d,%ebx
jl     1130 <receive_data+0x20>
mov    %eax,%r14d
jmp    1163 <receive_data+0x53>
xor    %r14d,%r14d
shr    $0x1f,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @receive_data(i32 %0, i8* %1, i32 %2) {
%4 = icmp sgt i32 %2, 0
br i1 %4, label %5, label %18
5: ; preds = %3, %14
%6 = phi i32 [ %16, %14 ], [ %2, %3 ]
%7 = phi i32 [ %15, %14 ], [ 0, %3 ]
%8 = icmp slt i32 %6, 1024
%9 = select i1 %8, i32 %6, i32 1024
%10 = sext i32 %7 to i64
%11 = getelementptr inbounds i8, i8* %1, i64 %10
%12 = tail call i32 @recv(i32 %0, i8* %11, i32 %9, i32 0)
%13 = icmp eq i32 %12, 0
br i1 %13, label %18, label %14
14: ; preds = %5
%15 = add nsw i32 %12, %7
%16 = sub nsw i32 %6, %12
%17 = icmp slt i32 %15, %2
br i1 %17, label %5, label %18
18: ; preds = %14, %5, %3
%19 = phi i32 [ 0, %3 ], [ 0, %5 ], [ %12, %14 ]
%20 = lshr i32 %19, 31
ret i32 %20
}