#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*/ MSG_WAITALL;
scalar_t__ recv(int, char *, int, int /*<<< orphan*/);
int readtcp(int sock, char *buff, int size) {
  int bytes_read, to_read;
  bytes_read = 0;
  while (bytes_read < size) {
    to_read = size - bytes_read;
    buff += bytes_read;
    bytes_read += recv(sock, buff, to_read, MSG_WAITALL);
  }
  return bytes_read;
}


x86 assembly (clang, -O0):
<readtcp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1179 <readtcp+0x69>
mov    -0x14(%rbp),%eax
sub    -0x18(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x1c(%rbp),%edx
mov    0x2e78(%rip),%rax
mov    (%rax),%ecx
call   1030 <recv@plt>
mov    %rax,%rcx
movslq -0x18(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x18(%rbp)
jmp    1129 <readtcp+0x19>
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readtcp>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
test   %edx,%edx
jle    1153 <readtcp+0x43>
mov    %edx,%r14d
mov    %rsi,%rbp
mov    %edi,%r15d
xor    %ebx,%ebx
mov    0x2eaa(%rip),%r12
xchg   %ax,%ax
mov    %r14d,%edx
sub    %ebx,%edx
movslq %ebx,%rbx
add    %rbx,%rbp
mov    (%r12),%ecx
mov    %r15d,%edi
mov    %rbp,%rsi
call   1030 <recv@plt>
add    %eax,%ebx
cmp    %r14d,%ebx
jl     1130 <readtcp+0x20>
jmp    1155 <readtcp+0x45>
xor    %ebx,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@MSG_WAITALL = global i32 0
define i32 @readtcp(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
store i32 0, i32* %7
br label %9
9: ; preds = %13, %3
%10 = load i32, i32* %7
%11 = load i32, i32* %6
%12 = icmp slt i32 %10, %11
br i1 %12, label %13, label %30
13: ; preds = %9
%14 = load i32, i32* %6
%15 = load i32, i32* %7
%16 = sub nsw i32 %14, %15
store i32 %16, i32* %8
%17 = load i32, i32* %7
%18 = load i8*, i8** %5
%19 = sext i32 %17 to i64
%20 = getelementptr inbounds i8, i8* %18, i64 %19
store i8* %20, i8** %5
%21 = load i32, i32* %4
%22 = load i8*, i8** %5
%23 = load i32, i32* %8
%24 = load i32, i32* @MSG_WAITALL
%25 = call i64 @recv(i32 %21, i8* %22, i32 %23, i32 %24)
%26 = load i32, i32* %7
%27 = sext i32 %26 to i64
%28 = add nsw i64 %27, %25
%29 = trunc i64 %28 to i32
store i32 %29, i32* %7
br label %9
30: ; preds = %9
%31 = load i32, i32* %7
ret i32 %31
}

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