#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 _reent {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ uart_getc();
int _read_r(struct _reent *r, int file, char *ptr, int len) {
  r = r;
  file = file;
  ptr = ptr;
  len = len;
  int index;
  for (index = 0; index < len; index++) {
    *ptr++ = uart_getc();
  }
  return len;
}


x86 assembly (clang, -O0):
<_read_r>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    117d <_read_r+0x6d>
mov    $0x0,%al
call   1030 <uart_getc@plt>
mov    %al,%cl
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x18(%rbp)
mov    %cl,(%rax)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1149 <_read_r+0x39>
mov    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_read_r>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %ecx,%r14d
test   %ecx,%ecx
jle    1143 <_read_r+0x33>
mov    %rdx,%r15
mov    %r14d,%ebp
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
xor    %eax,%eax
call   1030 <uart_getc@plt>
mov    %al,(%r15,%rbx,1)
add    $0x1,%rbx
cmp    %ebx,%ebp
jne    1130 <_read_r+0x20>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct._reent = type { i32 }
define i32 @_read_r(%struct._reent* %0, i32 %1, i8* %2, i32 %3) {
%5 = alloca %struct._reent*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
store %struct._reent* %0, %struct._reent** %5
store i32 %1, i32* %6
store i8* %2, i8** %7
store i32 %3, i32* %8
%10 = load %struct._reent*, %struct._reent** %5
store %struct._reent* %10, %struct._reent** %5
%11 = load i32, i32* %6
store i32 %11, i32* %6
%12 = load i8*, i8** %7
store i8* %12, i8** %7
%13 = load i32, i32* %8
store i32 %13, i32* %8
store i32 0, i32* %9
br label %14
14: ; preds = %23, %4
%15 = load i32, i32* %9
%16 = load i32, i32* %8
%17 = icmp slt i32 %15, %16
br i1 %17, label %18, label %26
18: ; preds = %14
%19 = call i32 (...) @uart_getc()
%20 = trunc i32 %19 to i8
%21 = load i8*, i8** %7
%22 = getelementptr inbounds i8, i8* %21, i32 1
store i8* %22, i8** %7
store i8 %20, i8* %21
br label %23
23: ; preds = %18
%24 = load i32, i32* %9
%25 = add nsw i32 %24, 1
store i32 %25, i32* %9
br label %14
26: ; preds = %14
%27 = load i32, i32* %8
ret i32 %27
}

LLVM IR (-O2):
%struct._reent = type { i32 }
define i32 @_read_r(%struct._reent* nocapture readnone %0, i32 %1, i8* nocapture writeonly %2, i32 returned %3) {
%5 = icmp sgt i32 %3, 0
br i1 %5, label %6, label %14
6: ; preds = %4, %6
%7 = phi i32 [ %12, %6 ], [ 0, %4 ]
%8 = phi i8* [ %11, %6 ], [ %2, %4 ]
%9 = tail call i32 (...) @uart_getc()
%10 = trunc i32 %9 to i8
%11 = getelementptr inbounds i8, i8* %8, i64 1
store i8 %10, i8* %8
%12 = add nuw nsw i32 %7, 1
%13 = icmp eq i32 %12, %3
br i1 %13, label %14, label %6
14: ; preds = %6, %4
ret i32 %3
}