#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_putc(char);
int _write_r(struct _reent *r, int file, char *ptr, int len) {
  r = r;
  file = file;
  ptr = ptr;
  int index;
  for (index = 0; index < len; index++) {
    if (ptr[index] == '\n') {
      uart_putc('\r');
    }
    uart_putc(ptr[index]);
  }
  return len;
}


x86 assembly (clang, -O0):
<_write_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)
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    118d <_write_r+0x7d>
mov    -0x18(%rbp),%rax
movslq -0x20(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    116e <_write_r+0x5e>
mov    $0xd,%edi
call   1030 <uart_putc@plt>
mov    -0x18(%rbp),%rax
movslq -0x20(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1030 <uart_putc@plt>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1143 <_write_r+0x33>
mov    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_write_r>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %ecx,%r14d
test   %ecx,%ecx
jle    1159 <_write_r+0x49>
mov    %rdx,%rbx
mov    %r14d,%r15d
xor    %ebp,%ebp
jmp    1141 <_write_r+0x31>
nopl   0x0(%rax,%rax,1)
movsbl %al,%edi
call   1030 <uart_putc@plt>
add    $0x1,%rbp
cmp    %rbp,%r15
je     1159 <_write_r+0x49>
movzbl (%rbx,%rbp,1),%eax
cmp    $0xa,%al
jne    1130 <_write_r+0x20>
mov    $0xd,%edi
call   1030 <uart_putc@plt>
movzbl (%rbx,%rbp,1),%eax
jmp    1130 <_write_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 @_write_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
store i32 0, i32* %9
br label %13
13: ; preds = %34, %4
%14 = load i32, i32* %9
%15 = load i32, i32* %8
%16 = icmp slt i32 %14, %15
br i1 %16, label %17, label %37
17: ; preds = %13
%18 = load i8*, i8** %7
%19 = load i32, i32* %9
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %18, i64 %20
%22 = load i8, i8* %21
%23 = sext i8 %22 to i32
%24 = icmp eq i32 %23, 10
br i1 %24, label %25, label %27
25: ; preds = %17
%26 = call i32 @uart_putc(i8 signext 13)
br label %27
27: ; preds = %25, %17
%28 = load i8*, i8** %7
%29 = load i32, i32* %9
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %28, i64 %30
%32 = load i8, i8* %31
%33 = call i32 @uart_putc(i8 signext %32)
br label %34
34: ; preds = %27
%35 = load i32, i32* %9
%36 = add nsw i32 %35, 1
store i32 %36, i32* %9
br label %13
37: ; preds = %13
%38 = load i32, i32* %8
ret i32 %38
}

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