#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*/ perror(char *);
int recv(int, char *, int, int /*<<< orphan*/);
void receive(int sock, char *buf) {
  int received = 0;
  char *p = buf;
  while (received < 512) {
    int ret_val = recv(sock, p, 512 - received, 0);
    if (ret_val == -1) {
      perror("ERROR: could not receive. Trying again...");
      continue;
    }
    received += ret_val;
  }
}


x86 assembly (clang, -O0):
<receive>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpl   $0x200,-0x14(%rbp)
jge    118d <receive+0x6d>
mov    -0x4(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    $0x200,%edx
sub    -0x14(%rbp),%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    117f <receive+0x5f>
lea    0xe8b(%rip),%rdi
call   1040 <perror@plt>
jmp    113e <receive+0x1e>
mov    -0x24(%rbp),%eax
add    -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
jmp    113e <receive+0x1e>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<receive>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %edi,%ebp
xor    %ebx,%ebx
lea    0xecb(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
nop
mov    $0x200,%edx
sub    %ebx,%edx
mov    %ebp,%edi
mov    %r15,%rsi
xor    %ecx,%ecx
call   1030 <recv@plt>
cmp    $0xffffffff,%eax
je     1164 <receive+0x44>
add    %eax,%ebx
cmp    $0x200,%ebx
jl     1140 <receive+0x20>
jmp    1174 <receive+0x54>
mov    %r14,%rdi
call   1040 <perror@plt>
cmp    $0x200,%ebx
jl     1140 <receive+0x20>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [42 x i8] c"ERROR: could not receive. Trying again...\00"
define void @receive(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
store i32 %0, i32* %3
store i8* %1, i8** %4
store i32 0, i32* %5
%8 = load i8*, i8** %4
store i8* %8, i8** %6
br label %9
9: ; preds = %22, %20, %2
%10 = load i32, i32* %5
%11 = icmp slt i32 %10, 512
br i1 %11, label %12, label %26
12: ; preds = %9
%13 = load i32, i32* %3
%14 = load i8*, i8** %6
%15 = load i32, i32* %5
%16 = sub nsw i32 512, %15
%17 = call i32 @recv(i32 %13, i8* %14, i32 %16, i32 0)
store i32 %17, i32* %7
%18 = load i32, i32* %7
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %22
20: ; preds = %12
%21 = call i32 @perror(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0))
br label %9
22: ; preds = %12
%23 = load i32, i32* %7
%24 = load i32, i32* %5
%25 = add nsw i32 %24, %23
store i32 %25, i32* %5
br label %9
26: ; preds = %9
ret void
}

LLVM IR (-O2):
@.str = [42 x i8] c"ERROR: could not receive. Trying again...\00"
define void @receive(i32 %0, i8* %1) {
br label %3
3: ; preds = %2, %12
%4 = phi i32 [ 0, %2 ], [ %13, %12 ]
%5 = sub nsw i32 512, %4
%6 = tail call i32 @recv(i32 %0, i8* %1, i32 %5, i32 0)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %10
8: ; preds = %3
%9 = tail call i32 @perror(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0))
br label %12
10: ; preds = %3
%11 = add nsw i32 %6, %4
br label %12
12: ; preds = %10, %8
%13 = phi i32 [ %4, %8 ], [ %11, %10 ]
%14 = icmp slt i32 %13, 512
br i1 %14, label %3, label %15
15: ; preds = %12
ret void
}