#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 rw_get(int, char *, int, int);
int rw_read(int fsock, char *buffer, int buflen, int timeout) {
  int rc, nb = 0;
  do {
    rc = rw_get(fsock, &buffer[nb], buflen - nb, timeout);
    if (rc < 0)
      return rc;
    nb += rc;
  } while (nb < buflen);
  return nb;
}


x86 assembly (clang, -O0):
<rw_read>:
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,-0x20(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x20(%rbp),%rax
add    %rax,%rsi
mov    -0x14(%rbp),%edx
sub    -0x20(%rbp),%edx
mov    -0x18(%rbp),%ecx
call   1030 <rw_get@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1160 <rw_read+0x50>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    117b <rw_read+0x6b>
mov    -0x1c(%rbp),%eax
add    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x14(%rbp),%eax
jl     112c <rw_read+0x1c>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rw_read>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %ecx,%r14d
mov    %edx,%ebx
mov    %rsi,%r15
mov    %edi,%r12d
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nop
movslq %ebp,%rax
lea    (%r15,%rax,1),%rsi
mov    %ebx,%edx
sub    %eax,%edx
mov    %r12d,%edi
mov    %r14d,%ecx
call   1030 <rw_get@plt>
test   %eax,%eax
js     1152 <rw_read+0x42>
add    %eax,%ebp
cmp    %ebx,%ebp
jl     1130 <rw_read+0x20>
jmp    1154 <rw_read+0x44>
mov    %eax,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @rw_read(i32 %0, i8* %1, i32 %2, i32 %3) {
br label %5
5: ; preds = %12, %4
%6 = phi i32 [ 0, %4 ], [ %13, %12 ]
%7 = sext i32 %6 to i64
%8 = getelementptr inbounds i8, i8* %1, i64 %7
%9 = sub nsw i32 %2, %6
%10 = tail call i32 @rw_get(i32 %0, i8* %8, i32 %9, i32 %3)
%11 = icmp slt i32 %10, 0
br i1 %11, label %15, label %12
12: ; preds = %5
%13 = add nsw i32 %10, %6
%14 = icmp slt i32 %13, %2
br i1 %14, label %5, label %15
15: ; preds = %12, %5
%16 = phi i32 [ %10, %5 ], [ %13, %12 ]
ret i32 %16
}