#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_NOSIGNAL;
int recv(int, void *, int, int /*<<< orphan*/);
int recv_socket(int sockfd, void *buffer, int size) {
  int received = 0;
  while (buffer && received < size) {
    int result = recv(sockfd, buffer + received, size - received, MSG_NOSIGNAL);
    if (result > 0) {
      received += result;
    } else {
      received = result;
      break;
    }
  }
  return received;
}


x86 assembly (clang, -O0):
<recv_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x10(%rbp)
mov    %al,-0x1d(%rbp)
je     1145 <recv_socket+0x35>
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
setl   %al
mov    %al,-0x1d(%rbp)
mov    -0x1d(%rbp),%al
test   $0x1,%al
jne    1155 <recv_socket+0x45>
jmp    11a2 <recv_socket+0x92>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x18(%rbp),%rax
add    %rax,%rsi
mov    -0x14(%rbp),%edx
sub    -0x18(%rbp),%edx
mov    0x2e70(%rip),%rax
mov    (%rax),%ecx
call   1030 <recv@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jle    1192 <recv_socket+0x82>
mov    -0x1c(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    119d <recv_socket+0x8d>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    11a2 <recv_socket+0x92>
jmp    1129 <recv_socket+0x19>
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<recv_socket>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%r14d
mov    %rsi,%rbp
mov    %edi,%r15d
xor    %ebx,%ebx
mov    0x2eb6(%rip),%r12
nopw   0x0(%rax,%rax,1)
test   %rbp,%rbp
je     1159 <recv_socket+0x49>
cmp    %r14d,%ebx
jge    1159 <recv_socket+0x49>
movslq %ebx,%rsi
add    %rbp,%rsi
mov    %r14d,%edx
sub    %ebx,%edx
mov    (%r12),%ecx
mov    %r15d,%edi
call   1030 <recv@plt>
add    %eax,%ebx
test   %eax,%eax
jg     1130 <recv_socket+0x20>
mov    %eax,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@MSG_NOSIGNAL = global i32 0
define i32 @recv_socket(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 = %37, %3
%10 = load i8*, i8** %5
%11 = icmp ne i8* %10, null
br i1 %11, label %12, label %16
12: ; preds = %9
%13 = load i32, i32* %7
%14 = load i32, i32* %6
%15 = icmp slt i32 %13, %14
br label %16
16: ; preds = %12, %9
%17 = phi i1 [ false, %9 ], [ %15, %12 ]
br i1 %17, label %18, label %38
18: ; preds = %16
%19 = load i32, i32* %4
%20 = load i8*, i8** %5
%21 = load i32, i32* %7
%22 = sext i32 %21 to i64
%23 = getelementptr i8, i8* %20, i64 %22
%24 = load i32, i32* %6
%25 = load i32, i32* %7
%26 = sub nsw i32 %24, %25
%27 = load i32, i32* @MSG_NOSIGNAL
%28 = call i32 @recv(i32 %19, i8* %23, i32 %26, i32 %27)
store i32 %28, i32* %8
%29 = load i32, i32* %8
%30 = icmp sgt i32 %29, 0
br i1 %30, label %31, label %35
31: ; preds = %18
%32 = load i32, i32* %8
%33 = load i32, i32* %7
%34 = add nsw i32 %33, %32
store i32 %34, i32* %7
br label %37
35: ; preds = %18
%36 = load i32, i32* %8
store i32 %36, i32* %7
br label %38
37: ; preds = %31
br label %9
38: ; preds = %35, %16
%39 = load i32, i32* %7
ret i32 %39
}

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