#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 read(int, unsigned char *, int);
int wkd_read_loop(int fd, unsigned char *buf, int len) {
  int ret, idx = 0;
  while (idx < len) {
    ret = read(fd, buf + idx, len);
    if (ret < 0)
      break;
    idx += ret;
  }
  return idx;
}


x86 assembly (clang, -O0):
<wkd_read_loop>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    116b <wkd_read_loop+0x5b>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x1c(%rbp),%rax
add    %rax,%rsi
mov    -0x14(%rbp),%edx
call   1030 <read@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    115d <wkd_read_loop+0x4d>
jmp    116b <wkd_read_loop+0x5b>
mov    -0x18(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    1129 <wkd_read_loop+0x19>
mov    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wkd_read_loop>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %edx,%edx
jle    114c <wkd_read_loop+0x3c>
mov    %edx,%ebp
mov    %rsi,%r14
mov    %edi,%r15d
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
movslq %ebx,%rsi
add    %r14,%rsi
mov    %r15d,%edi
mov    %ebp,%edx
call   1030 <read@plt>
test   %eax,%eax
js     114e <wkd_read_loop+0x3e>
add    %eax,%ebx
cmp    %ebp,%ebx
jl     1130 <wkd_read_loop+0x20>
jmp    114e <wkd_read_loop+0x3e>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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