int readbuffer(int file, char *buffer, int len, int offset) {
  int ret = 0;
  while (len > 0) {
    ret = pread(file, buffer, len, offset);
    if (ret < 0) {
      printf("%s: failed %d\n", __FUNCTION__, ret);
      goto out;
    } else if (ret == 0)
      goto out;
    len -= ret;
    buffer += ret;
  }
out:
  return ret;
}


x86 assembly (clang, -O0):
<readbuffer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    11b9 <readbuffer+0x99>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
mov    -0x18(%rbp),%ecx
mov    $0x0,%al
call   1040 <pread@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1184 <readbuffer+0x64>
mov    -0x1c(%rbp),%edx
lea    0xe8f(%rip),%rdi
lea    0xe97(%rip),%rsi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11be <readbuffer+0x9e>
cmpl   $0x0,-0x1c(%rbp)
jne    1193 <readbuffer+0x73>
jmp    11be <readbuffer+0x9e>
jmp    1198 <readbuffer+0x78>
mov    -0x1c(%rbp),%ecx
mov    -0x14(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x14(%rbp)
mov    -0x1c(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
jmp    113c <readbuffer+0x1c>
jmp    11be <readbuffer+0x9e>
mov    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readbuffer>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
test   %edx,%edx
jle    1168 <readbuffer+0x48>
mov    %ecx,%r14d
mov    %edx,%ebx
mov    %rsi,%rbp
mov    %edi,%r12d
nopw   0x0(%rax,%rax,1)
mov    %r12d,%edi
mov    %rbp,%rsi
mov    %ebx,%edx
mov    %r14d,%ecx
xor    %eax,%eax
call   1040 <pread@plt>
mov    %eax,%r15d
test   %eax,%eax
js     116d <readbuffer+0x4d>
je     1168 <readbuffer+0x48>
mov    %r15d,%eax
add    %rax,%rbp
sub    %r15d,%ebx
jg     1140 <readbuffer+0x20>
jmp    1185 <readbuffer+0x65>
xor    %r15d,%r15d
jmp    1185 <readbuffer+0x65>
lea    0xe8c(%rip),%rdi
lea    0xe94(%rip),%rsi
mov    %r15d,%edx
xor    %eax,%eax
call   1030 <printf@plt>
mov    %r15d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [15 x i8] c"%s: failed %d\0A\00"
@__FUNCTION__.readbuffer = [11 x i8] c"readbuffer\00"
define i32 @readbuffer(i32 %0, i8* %1, i32 %2, i32 %3) {
%5 = icmp sgt i32 %2, 0
br i1 %5, label %6, label %20
6: ; preds = %4, %15
%7 = phi i8* [ %18, %15 ], [ %1, %4 ]
%8 = phi i32 [ %16, %15 ], [ %2, %4 ]
%9 = tail call i32 (i32, i8*, i32, i32, ...) bitcast (i32 (...)* @pread to i32 (i32, i8*, i32, i32, ...)*)(i32 %0, i8* %7, i32 %8, i32 %3)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %6
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @__FUNCTION__.readbuffer, i64 0, i64 0), i32 %9)
br label %20
13: ; preds = %6
%14 = icmp eq i32 %9, 0
br i1 %14, label %20, label %15
15: ; preds = %13
%16 = sub nsw i32 %8, %9
%17 = zext i32 %9 to i64
%18 = getelementptr inbounds i8, i8* %7, i64 %17
%19 = icmp sgt i32 %16, 0
br i1 %19, label %6, label %20
20: ; preds = %13, %15, %4, %11
%21 = phi i32 [ %9, %11 ], [ 0, %4 ], [ 0, %13 ], [ %9, %15 ]
ret i32 %21
}