#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 *, unsigned int, int /*<<< orphan*/);
int read_from_socket(int sockfd, char *buf, unsigned int total,
                     unsigned int remaining) {
  int data_read = 0;
  if ((data_read = recv(sockfd, &buf[total - remaining], remaining, 0)) < 0) {
    perror("recv");
    return -1;
  }
  return data_read;
}


x86 assembly (clang, -O0):
<read_from_socket>:
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)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%eax
sub    -0x18(%rbp),%eax
mov    %eax,%eax
add    %rax,%rsi
mov    -0x18(%rbp),%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    117c <read_from_socket+0x5c>
lea    0xe95(%rip),%rdi
call   1040 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1182 <read_from_socket+0x62>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_from_socket>:
push   %rax
sub    %ecx,%edx
add    %rdx,%rsi
mov    %ecx,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
js     1135 <read_from_socket+0x15>
pop    %rcx
ret
lea    0xec4(%rip),%rdi
call   1040 <perror@plt>
mov    $0xffffffff,%eax
pop    %rcx
ret

LLVM IR (-O0):
@.str = [5 x i8] c"recv\00"
define i32 @read_from_socket(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
%11 = load i32, i32* %6
%12 = load i8*, i8** %7
%13 = load i32, i32* %8
%14 = load i32, i32* %9
%15 = sub i32 %13, %14
%16 = zext i32 %15 to i64
%17 = getelementptr inbounds i8, i8* %12, i64 %16
%18 = load i32, i32* %9
%19 = call i32 @recv(i32 %11, i8* %17, i32 %18, i32 0)
store i32 %19, i32* %10
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %4
%22 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %5
br label %25
23: ; preds = %4
%24 = load i32, i32* %10
store i32 %24, i32* %5
br label %25
25: ; preds = %23, %21
%26 = load i32, i32* %5
ret i32 %26
}

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