#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 read_at_least(int fd, unsigned char buf[], int nr) {
  int remaining = nr;
  int nr_read = 0;
  while (remaining > 0) {
    int read_this_time;
    read_this_time = read(fd, &buf[nr_read], remaining);
    if (read_this_time == 0)
      return 0;
    nr_read += read_this_time;
    remaining -= read_this_time;
  }
  return nr_read;
}


x86 assembly (clang, -O0):
<read_at_least>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpl   $0x0,-0x18(%rbp)
jle    1181 <read_at_least+0x71>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x1c(%rbp),%rax
add    %rax,%rsi
mov    -0x18(%rbp),%edx
call   1030 <read@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jne    1168 <read_at_least+0x58>
movl   $0x0,-0x4(%rbp)
jmp    1187 <read_at_least+0x77>
mov    -0x20(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%ecx
mov    -0x18(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x18(%rbp)
jmp    112f <read_at_least+0x1f>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_at_least>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %rsi,%r14
mov    %edi,%r15d
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
test   %ebp,%ebp
jle    114f <read_at_least+0x3f>
movslq %ebx,%rbx
lea    (%r14,%rbx,1),%rsi
mov    %r15d,%edi
mov    %ebp,%edx
call   1030 <read@plt>
add    %eax,%ebx
sub    %eax,%ebp
test   %eax,%eax
jne    1130 <read_at_least+0x20>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @read_at_least(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%11 = load i32, i32* %7
store i32 %11, i32* %8
store i32 0, i32* %9
br label %12
12: ; preds = %26, %3
%13 = load i32, i32* %8
%14 = icmp sgt i32 %13, 0
br i1 %14, label %15, label %33
15: ; preds = %12
%16 = load i32, i32* %5
%17 = load i8*, i8** %6
%18 = load i32, i32* %9
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %17, i64 %19
%21 = load i32, i32* %8
%22 = call i32 @read(i32 %16, i8* %20, i32 %21)
store i32 %22, i32* %10
%23 = load i32, i32* %10
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %15
store i32 0, i32* %4
br label %35
26: ; preds = %15
%27 = load i32, i32* %10
%28 = load i32, i32* %9
%29 = add nsw i32 %28, %27
store i32 %29, i32* %9
%30 = load i32, i32* %10
%31 = load i32, i32* %8
%32 = sub nsw i32 %31, %30
store i32 %32, i32* %8
br label %12
33: ; preds = %12
%34 = load i32, i32* %9
store i32 %34, i32* %4
br label %35
35: ; preds = %33, %25
%36 = load i32, i32* %4
ret i32 %36
}

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