#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 *);
scalar_t__ recv(int, char *, int, int);
int recv_msg(int t, char *buf, int bufsize, int flags) {
  int bytes_recv = 0;
  while (bytes_recv < 1)
    if ((bytes_recv += recv(t, buf + bytes_recv, bufsize, flags)) < 0) {
      perror("recv");
      return -1;
    }
  return bytes_recv;
}


x86 assembly (clang, -O0):
<recv_msg>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpl   $0x1,-0x1c(%rbp)
jge    1192 <recv_msg+0x72>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x1c(%rbp),%rax
add    %rax,%rsi
mov    -0x14(%rbp),%edx
mov    -0x18(%rbp),%ecx
call   1030 <recv@plt>
mov    %rax,%rcx
movslq -0x1c(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    118d <recv_msg+0x6d>
lea    0xe84(%rip),%rdi
call   1040 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1198 <recv_msg+0x78>
jmp    113c <recv_msg+0x1c>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<recv_msg>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %ecx,%r14d
mov    %edx,%r15d
mov    %rsi,%rbx
mov    %edi,%ebp
xor    %eax,%eax
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
test   %eax,%eax
jne    1169 <recv_msg+0x49>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r15d,%edx
mov    %r14d,%ecx
call   1030 <recv@plt>
test   %eax,%eax
jns    1140 <recv_msg+0x20>
lea    0xea1(%rip),%rdi
call   1040 <perror@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"recv\00"
define i32 @recv_msg(i32 %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
store i32 0, i32* %10
br label %11
11: ; preds = %30, %4
%12 = load i32, i32* %10
%13 = icmp slt i32 %12, 1
br i1 %13, label %14, label %31
14: ; preds = %11
%15 = load i32, i32* %6
%16 = load i8*, i8** %7
%17 = load i32, i32* %10
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i8, i8* %16, i64 %18
%20 = load i32, i32* %8
%21 = load i32, i32* %9
%22 = call i64 @recv(i32 %15, i8* %19, i32 %20, i32 %21)
%23 = load i32, i32* %10
%24 = sext i32 %23 to i64
%25 = add nsw i64 %24, %22
%26 = trunc i64 %25 to i32
store i32 %26, i32* %10
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %14
%29 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %5
br label %33
30: ; preds = %14
br label %11
31: ; preds = %11
%32 = load i32, i32* %10
store i32 %32, i32* %5
br label %33
33: ; preds = %31, %28
%34 = load i32, i32* %5
ret i32 %34
}

LLVM IR (-O2):
@.str = [5 x i8] c"recv\00"
define i32 @recv_msg(i32 %0, i8* %1, i32 %2, i32 %3) {
br label %5
5: ; preds = %8, %4
%6 = phi i32 [ 0, %4 ], [ %10, %8 ]
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %14
8: ; preds = %5
%9 = tail call i64 @recv(i32 %0, i8* %1, i32 %2, i32 %3)
%10 = trunc i64 %9 to i32
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %5
12: ; preds = %8
%13 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %14
14: ; preds = %5, %12
%15 = phi i32 [ -1, %12 ], [ %6, %5 ]
ret i32 %15
}