#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*/ DieWithError(char *);
int /*<<< orphan*/ printf(char *, int);
int recv(int, char *, int, int /*<<< orphan*/);
void RecvBytes(int sock, char *recvBuffer, int expectedLength) {
  int bytesRcvd, totalBytesRcvd = 0;
  while (totalBytesRcvd < expectedLength) {
    if ((bytesRcvd = recv(sock, recvBuffer, 5 - 1, 0)) <= 0)
      DieWithError("recv() failed or connection closed prematurely");
    totalBytesRcvd += bytesRcvd;
    printf("Total bytes recieved: %i\n", totalBytesRcvd);
  }
}


x86 assembly (clang, -O0):
<RecvBytes>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    119d <RecvBytes+0x6d>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x4,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jg     1180 <RecvBytes+0x50>
lea    0xe85(%rip),%rdi
call   1050 <DieWithError@plt>
mov    -0x18(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%esi
lea    0xe9c(%rip),%rdi
call   1040 <printf@plt>
jmp    1149 <RecvBytes+0x19>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<RecvBytes>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %edx,%edx
jle    1198 <RecvBytes+0x68>
mov    %edx,%ebx
mov    %rsi,%r14
mov    %edi,%r15d
xor    %ebp,%ebp
lea    0xedf(%rip),%r13
jmp    1171 <RecvBytes+0x41>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
add    %r12d,%ebp
mov    %r13,%rdi
mov    %ebp,%esi
call   1040 <printf@plt>
cmp    %ebx,%ebp
jge    1198 <RecvBytes+0x68>
mov    %r15d,%edi
mov    %r14,%rsi
mov    $0x4,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,%r12d
test   %eax,%eax
jg     1160 <RecvBytes+0x30>
lea    0xe6f(%rip),%rdi
call   1050 <DieWithError@plt>
jmp    1160 <RecvBytes+0x30>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [47 x i8] c"recv() failed or connection closed prematurely\00"
@.str.1 = [26 x i8] c"Total bytes recieved: %i\0A\00"
define void @RecvBytes(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* %8
br label %9
9: ; preds = %20, %3
%10 = load i32, i32* %8
%11 = load i32, i32* %6
%12 = icmp slt i32 %10, %11
br i1 %12, label %13, label %26
13: ; preds = %9
%14 = load i32, i32* %4
%15 = load i8*, i8** %5
%16 = call i32 @recv(i32 %14, i8* %15, i32 4, i32 0)
store i32 %16, i32* %7
%17 = icmp sle i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %13
%19 = call i32 @DieWithError(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0))
br label %20
20: ; preds = %18, %13
%21 = load i32, i32* %7
%22 = load i32, i32* %8
%23 = add nsw i32 %22, %21
store i32 %23, i32* %8
%24 = load i32, i32* %8
%25 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i32 %24)
br label %9
26: ; preds = %9
ret void
}

LLVM IR (-O2):
@.str = [47 x i8] c"recv() failed or connection closed prematurely\00"
@.str.1 = [26 x i8] c"Total bytes recieved: %i\0A\00"
define void @RecvBytes(i32 %0, i8* %1, i32 %2) {
%4 = icmp sgt i32 %2, 0
br i1 %4, label %5, label %15
5: ; preds = %3, %11
%6 = phi i32 [ %12, %11 ], [ 0, %3 ]
%7 = tail call i32 @recv(i32 %0, i8* %1, i32 4, i32 0)
%8 = icmp slt i32 %7, 1
br i1 %8, label %9, label %11
9: ; preds = %5
%10 = tail call i32 @DieWithError(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %9, %5
%12 = add nsw i32 %7, %6
%13 = tail call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i32 %12)
%14 = icmp slt i32 %12, %2
br i1 %14, label %5, label %15
15: ; preds = %11, %3
ret void
}