#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 */
char __uart_getc();
int /*<<< orphan*/ __uart_putc(char);
int readline(char *ptr, int max) {
  int count;
  for (count = 0; max > 1; max--, ptr++, count++) {
    *ptr = __uart_getc();
    __uart_putc(*ptr);
    if (*ptr == '\n' || *ptr == '\r') {
      *ptr = 0;
      return count;
    }
  }
  *ptr = 0;
  {
    char c;
    do {
      c = __uart_getc();
      __uart_putc(c);
    } while (c != '\n' && c != '\r');
  }
  return count;
}


x86 assembly (clang, -O0):
<readline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
cmpl   $0x1,-0x14(%rbp)
jle    11b5 <readline+0x95>
mov    $0x0,%al
call   1030 <__uart_getc@plt>
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,(%rax)
mov    -0x10(%rbp),%rax
movsbl (%rax),%edi
call   1040 <__uart_putc@plt>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0xa,%eax
je     117b <readline+0x5b>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0xd,%eax
jne    118d <readline+0x6d>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11ff <readline+0xdf>
jmp    1192 <readline+0x72>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1136 <readline+0x16>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    $0x0,%al
call   1030 <__uart_getc@plt>
mov    %al,-0x19(%rbp)
movsbl -0x19(%rbp),%edi
call   1040 <__uart_putc@plt>
movsbl -0x19(%rbp),%ecx
xor    %eax,%eax
cmp    $0xa,%ecx
mov    %al,-0x1a(%rbp)
je     11ee <readline+0xce>
movsbl -0x19(%rbp),%eax
cmp    $0xd,%eax
setne  %al
mov    %al,-0x1a(%rbp)
mov    -0x1a(%rbp),%al
test   $0x1,%al
jne    11bc <readline+0x9c>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readline>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%rbx
cmp    $0x2,%esi
jl     116c <readline+0x4c>
mov    %esi,%r14d
add    $0xffffffff,%r14d
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nop
xor    %eax,%eax
call   1030 <__uart_getc@plt>
mov    %al,(%rbx,%rbp,1)
movsbl %al,%edi
call   1040 <__uart_putc@plt>
movzbl (%rbx,%rbp,1),%eax
cmp    $0xd,%al
je     11a0 <readline+0x80>
cmp    $0xa,%al
je     11a0 <readline+0x80>
add    $0x1,%rbp
cmp    %ebp,%r14d
jne    1140 <readline+0x20>
add    %rbp,%rbx
jmp    116f <readline+0x4f>
xor    %r14d,%r14d
movb   $0x0,(%rbx)
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
xor    %eax,%eax
call   1030 <__uart_getc@plt>
mov    %eax,%ebx
movsbl %al,%ebp
mov    %ebp,%edi
call   1040 <__uart_putc@plt>
cmp    $0xd,%bpl
je     11a7 <readline+0x87>
cmp    $0xa,%bl
jne    1180 <readline+0x60>
jmp    11a7 <readline+0x87>
movb   $0x0,(%rbx,%rbp,1)
mov    %ebp,%r14d
mov    %r14d,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @readline(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 0, i32* %6
br label %8
8: ; preds = %30, %2
%9 = load i32, i32* %5
%10 = icmp sgt i32 %9, 1
br i1 %10, label %11, label %37
11: ; preds = %8
%12 = call signext i8 (...) @__uart_getc()
%13 = load i8*, i8** %4
store i8 %12, i8* %13
%14 = load i8*, i8** %4
%15 = load i8, i8* %14
%16 = call i32 @__uart_putc(i8 signext %15)
%17 = load i8*, i8** %4
%18 = load i8, i8* %17
%19 = sext i8 %18 to i32
%20 = icmp eq i32 %19, 10
br i1 %20, label %26, label %21
21: ; preds = %11
%22 = load i8*, i8** %4
%23 = load i8, i8* %22
%24 = sext i8 %23 to i32
%25 = icmp eq i32 %24, 13
br i1 %25, label %26, label %29
26: ; preds = %21, %11
%27 = load i8*, i8** %4
store i8 0, i8* %27
%28 = load i32, i32* %6
store i32 %28, i32* %3
br label %55
29: ; preds = %21
br label %30
30: ; preds = %29
%31 = load i32, i32* %5
%32 = add nsw i32 %31, -1
store i32 %32, i32* %5
%33 = load i8*, i8** %4
%34 = getelementptr inbounds i8, i8* %33, i32 1
store i8* %34, i8** %4
%35 = load i32, i32* %6
%36 = add nsw i32 %35, 1
store i32 %36, i32* %6
br label %8
37: ; preds = %8
%38 = load i8*, i8** %4
store i8 0, i8* %38
br label %39
39: ; preds = %51, %37
%40 = call signext i8 (...) @__uart_getc()
store i8 %40, i8* %7
%41 = load i8, i8* %7
%42 = call i32 @__uart_putc(i8 signext %41)
br label %43
43: ; preds = %39
%44 = load i8, i8* %7
%45 = sext i8 %44 to i32
%46 = icmp ne i32 %45, 10
br i1 %46, label %47, label %51
47: ; preds = %43
%48 = load i8, i8* %7
%49 = sext i8 %48 to i32
%50 = icmp ne i32 %49, 13
br label %51
51: ; preds = %47, %43
%52 = phi i1 [ false, %43 ], [ %50, %47 ]
br i1 %52, label %39, label %53
53: ; preds = %51
%54 = load i32, i32* %6
store i32 %54, i32* %3
br label %55
55: ; preds = %53, %26
%56 = load i32, i32* %3
ret i32 %56
}

LLVM IR (-O2):
define i32 @readline(i8* nocapture %0, i32 %1) {
%3 = icmp sgt i32 %1, 1
br i1 %3, label %4, label %17
4: ; preds = %2
%5 = add nsw i32 %1, -1
br label %6
6: ; preds = %4, %13
%7 = phi i32 [ %15, %13 ], [ 0, %4 ]
%8 = phi i8* [ %14, %13 ], [ %0, %4 ]
%9 = tail call signext i8 (...) @__uart_getc()
store i8 %9, i8* %8
%10 = tail call i32 @__uart_putc(i8 signext %9)
%11 = load i8, i8* %8
switch i8 %11, label %13 [
i8 10, label %12
i8 13, label %12
]
12: ; preds = %6, %6
store i8 0, i8* %8
br label %23
13: ; preds = %6
%14 = getelementptr inbounds i8, i8* %8, i64 1
%15 = add nuw nsw i32 %7, 1
%16 = icmp eq i32 %15, %5
br i1 %16, label %17, label %6
17: ; preds = %13, %2
%18 = phi i8* [ %0, %2 ], [ %14, %13 ]
%19 = phi i32 [ 0, %2 ], [ %5, %13 ]
store i8 0, i8* %18
br label %20
20: ; preds = %20, %17
%21 = tail call signext i8 (...) @__uart_getc()
%22 = tail call i32 @__uart_putc(i8 signext %21)
switch i8 %21, label %20 [
i8 13, label %23
i8 10, label %23
]
23: ; preds = %20, %20, %12
%24 = phi i32 [ %7, %12 ], [ %19, %20 ], [ %19, %20 ]
ret i32 %24
}