#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*/ abort();
int /*<<< orphan*/ perror(char *);
int read(int, void *, int);
void internal_read(int socket, void *buffer, size_t size) {
  int notread = size, recv_return = 0, received = 0;
  while (notread > 0 &&
         (recv_return = read(socket, buffer + received, notread)) > 0) {
    notread -= recv_return;
    received += recv_return;
  }
  if (recv_return == -1) {
    perror("read: ");
    abort();
  }
}


x86 assembly (clang, -O0):
<internal_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %eax,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x1c(%rbp)
mov    %al,-0x25(%rbp)
jle    1189 <internal_read+0x59>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x24(%rbp),%rax
add    %rax,%rsi
mov    -0x1c(%rbp),%edx
call   1040 <read@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
setg   %al
mov    %al,-0x25(%rbp)
mov    -0x25(%rbp),%al
test   $0x1,%al
jne    1199 <internal_read+0x69>
jmp    11b2 <internal_read+0x82>
mov    -0x20(%rbp),%ecx
mov    -0x1c(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%eax
add    -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
jmp    1158 <internal_read+0x28>
cmpl   $0xffffffff,-0x20(%rbp)
jne    11cf <internal_read+0x9f>
lea    0xe3d(%rip),%rdi
call   1050 <perror@plt>
mov    $0x0,%al
call   1030 <abort@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<internal_read>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%rbx
test   %ebx,%ebx
jle    1171 <internal_read+0x41>
mov    %rsi,%r14
mov    %edi,%r15d
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
movslq %ebp,%rsi
add    %r14,%rsi
mov    %r15d,%edi
mov    %ebx,%edx
call   1040 <read@plt>
test   %eax,%eax
jle    116c <internal_read+0x3c>
add    %eax,%ebp
sub    %eax,%ebx
jg     1150 <internal_read+0x20>
jmp    1171 <internal_read+0x41>
cmp    $0xffffffff,%eax
je     117c <internal_read+0x4c>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe7d(%rip),%rdi
call   1050 <perror@plt>
xor    %eax,%eax
call   1030 <abort@plt>

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

LLVM IR (-O2):
@.str = [7 x i8] c"read: \00"
define void @internal_read(i32 %0, i8* nocapture %1, i64 %2) {
%4 = trunc i64 %2 to i32
%5 = icmp sgt i32 %4, 0
br i1 %5, label %6, label %22
6: ; preds = %3, %13
%7 = phi i32 [ %15, %13 ], [ 0, %3 ]
%8 = phi i32 [ %14, %13 ], [ %4, %3 ]
%9 = sext i32 %7 to i64
%10 = getelementptr i8, i8* %1, i64 %9
%11 = tail call i32 @read(i32 %0, i8* %10, i32 %8)
%12 = icmp sgt i32 %11, 0
br i1 %12, label %13, label %17
13: ; preds = %6
%14 = sub nsw i32 %8, %11
%15 = add nsw i32 %11, %7
%16 = icmp sgt i32 %14, 0
br i1 %16, label %6, label %22
17: ; preds = %6
%18 = icmp eq i32 %11, -1
br i1 %18, label %19, label %22
19: ; preds = %17
%20 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%21 = tail call i32 (...) @abort()
unreachable
22: ; preds = %13, %3, %17
ret void
}