#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 tty_getc(int);
int /*<<< orphan*/ tty_putc(int, char);
int tty_getline(int chan, char *buf) {
  char c;
  int len = 0;
  while (((c = tty_getc(chan)) != '\n') && (c != '\r')) {
    tty_putc(chan, c);
    *buf++ = c;
    len++;
  }
  *buf = '\0';
  return len;
}


x86 assembly (clang, -O0):
<tty_getline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x4(%rbp),%edi
call   1040 <tty_getc@plt>
mov    %al,-0x11(%rbp)
movsbl %al,%ecx
xor    %eax,%eax
cmp    $0xa,%ecx
mov    %al,-0x19(%rbp)
je     115f <tty_getline+0x3f>
movsbl -0x11(%rbp),%eax
cmp    $0xd,%eax
setne  %al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
test   $0x1,%al
jne    116f <tty_getline+0x4f>
jmp    119d <tty_getline+0x7d>
mov    -0x4(%rbp),%edi
movsbl -0x11(%rbp),%esi
call   1030 <tty_putc@plt>
mov    -0x11(%rbp),%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,(%rax)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1136 <tty_getline+0x16>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tty_getline>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %edi,%r15d
xor    %ebp,%ebp
nop
mov    %r15d,%edi
call   1040 <tty_getc@plt>
cmp    $0xa,%al
je     1157 <tty_getline+0x37>
mov    %eax,%ebx
cmp    $0xd,%al
je     1157 <tty_getline+0x37>
movsbl %bl,%esi
mov    %r15d,%edi
call   1030 <tty_putc@plt>
mov    %bl,(%r14,%rbp,1)
add    $0x1,%rbp
jmp    1130 <tty_getline+0x10>
movb   $0x0,(%r14,%rbp,1)
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @tty_getline(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8
%6 = alloca i32
store i32 %0, i32* %3
store i8* %1, i8** %4
store i32 0, i32* %6
br label %7
7: ; preds = %18, %2
%8 = load i32, i32* %3
%9 = call signext i8 @tty_getc(i32 %8)
store i8 %9, i8* %5
%10 = sext i8 %9 to i32
%11 = icmp ne i32 %10, 10
br i1 %11, label %12, label %16
12: ; preds = %7
%13 = load i8, i8* %5
%14 = sext i8 %13 to i32
%15 = icmp ne i32 %14, 13
br label %16
16: ; preds = %12, %7
%17 = phi i1 [ false, %7 ], [ %15, %12 ]
br i1 %17, label %18, label %27
18: ; preds = %16
%19 = load i32, i32* %3
%20 = load i8, i8* %5
%21 = call i32 @tty_putc(i32 %19, i8 signext %20)
%22 = load i8, i8* %5
%23 = load i8*, i8** %4
%24 = getelementptr inbounds i8, i8* %23, i32 1
store i8* %24, i8** %4
store i8 %22, i8* %23
%25 = load i32, i32* %6
%26 = add nsw i32 %25, 1
store i32 %26, i32* %6
br label %7
27: ; preds = %16
%28 = load i8*, i8** %4
store i8 0, i8* %28
%29 = load i32, i32* %6
ret i32 %29
}

LLVM IR (-O2):
define i32 @tty_getline(i32 %0, i8* nocapture writeonly %1) {
br label %3
3: ; preds = %7, %2
%4 = phi i8* [ %1, %2 ], [ %9, %7 ]
%5 = phi i32 [ 0, %2 ], [ %10, %7 ]
%6 = tail call signext i8 @tty_getc(i32 %0)
switch i8 %6, label %7 [
i8 13, label %11
i8 10, label %11
]
7: ; preds = %3
%8 = tail call i32 @tty_putc(i32 %0, i8 signext %6)
%9 = getelementptr inbounds i8, i8* %4, i64 1
store i8 %6, i8* %4
%10 = add nuw nsw i32 %5, 1
br label %3
11: ; preds = %3, %3
store i8 0, i8* %4
ret i32 %5
}