#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 */
struct term_t {
  int dummy;
};

/* Variables and functions */
int term_read_dev(struct term_t *, char *, int);
int term_read_nbytes(struct term_t *info, char *buf, int len) {
  int count = 0;
  int nleft = len;
  char *ptr = buf;
  while (nleft > 0) {
    if ((count = term_read_dev(info, ptr, nleft)) < 0) {
      goto err;
    } else if (count == 0) {
      break;
    }
    ptr += count;
    nleft -= count;
  }
  return len - nleft;
err:
  return count;
}


x86 assembly (clang, -O0):
<term_read_nbytes>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
cmpl   $0x0,-0x24(%rbp)
jle    1198 <term_read_nbytes+0x88>
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1030 <term_read_dev@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jge    1163 <term_read_nbytes+0x53>
jmp    11a6 <term_read_nbytes+0x96>
cmpl   $0x0,-0x20(%rbp)
jne    1172 <term_read_nbytes+0x62>
jmp    1198 <term_read_nbytes+0x88>
jmp    1177 <term_read_nbytes+0x67>
mov    -0x20(%rbp),%ecx
mov    -0x30(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%ecx
mov    -0x24(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x24(%rbp)
jmp    1138 <term_read_nbytes+0x28>
mov    -0x1c(%rbp),%eax
sub    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11ac <term_read_nbytes+0x9c>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<term_read_nbytes>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %edx,%ebp
test   %edx,%edx
jle    114c <term_read_nbytes+0x3c>
mov    %rsi,%rbx
mov    %rdi,%r15
mov    %r14d,%ebp
nopl   0x0(%rax)
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <term_read_dev@plt>
test   %eax,%eax
js     1151 <term_read_nbytes+0x41>
je     114c <term_read_nbytes+0x3c>
mov    %eax,%ecx
add    %rcx,%rbx
sub    %eax,%ebp
jg     1130 <term_read_nbytes+0x20>
sub    %ebp,%r14d
jmp    1154 <term_read_nbytes+0x44>
mov    %eax,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.term_t = type { i32 }
define i32 @term_read_nbytes(%struct.term_t* %0, i8* %1, i32 %2) {
%4 = icmp sgt i32 %2, 0
br i1 %4, label %5, label %17
5: ; preds = %3, %12
%6 = phi i8* [ %14, %12 ], [ %1, %3 ]
%7 = phi i32 [ %15, %12 ], [ %2, %3 ]
%8 = tail call i32 @term_read_dev(%struct.term_t* %0, i8* %6, i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %20, label %10
10: ; preds = %5
%11 = icmp eq i32 %8, 0
br i1 %11, label %17, label %12
12: ; preds = %10
%13 = zext i32 %8 to i64
%14 = getelementptr inbounds i8, i8* %6, i64 %13
%15 = sub nsw i32 %7, %8
%16 = icmp sgt i32 %15, 0
br i1 %16, label %5, label %17
17: ; preds = %12, %10, %3
%18 = phi i32 [ %2, %3 ], [ %7, %10 ], [ %15, %12 ]
%19 = sub nsw i32 %2, %18
br label %20
20: ; preds = %5, %17
%21 = phi i32 [ %19, %17 ], [ %8, %5 ]
ret i32 %21
}