#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 readKey();
int /*<<< orphan*/ terminal_putchar(char);
char *terminal_readline() {
  char prev = 0;
  char c = 0;
  char array[32] = {0};
  int i = 0;
  while (c != '\n' && i < 32) {
    c = readKey();
    if (c != prev) {
      i++;
      terminal_putchar(c);
      array[i] = c;
      prev = c;
    }
  }
  return array;
}


x86 assembly (clang, -O0):
<terminal_readline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movb   $0x0,-0x1(%rbp)
movb   $0x0,-0x2(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
movl   $0x0,-0x34(%rbp)
movsbl -0x2(%rbp),%ecx
xor    %eax,%eax
cmp    $0xa,%ecx
mov    %al,-0x35(%rbp)
je     1173 <terminal_readline+0x43>
cmpl   $0x20,-0x34(%rbp)
setl   %al
mov    %al,-0x35(%rbp)
mov    -0x35(%rbp),%al
test   $0x1,%al
jne    1183 <terminal_readline+0x53>
jmp    11c5 <terminal_readline+0x95>
mov    $0x0,%al
call   1030 <readKey@plt>
mov    %al,-0x2(%rbp)
movsbl -0x2(%rbp),%eax
movsbl -0x1(%rbp),%ecx
cmp    %ecx,%eax
je     11c0 <terminal_readline+0x90>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
movsbl -0x2(%rbp),%edi
call   1050 <terminal_putchar@plt>
mov    -0x2(%rbp),%cl
movslq -0x34(%rbp),%rax
mov    %cl,-0x30(%rbp,%rax,1)
mov    -0x2(%rbp),%al
mov    %al,-0x1(%rbp)
jmp    1157 <terminal_readline+0x27>
lea    -0x30(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<terminal_readline>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
xor    %r14d,%r14d
xor    %ebp,%ebp
nopl   (%rax)
xor    %eax,%eax
call   1030 <readKey@plt>
mov    %eax,%ebx
cmp    %bpl,%al
je     1153 <terminal_readline+0x33>
movslq %r14d,%rbp
lea    0x1(%rbp),%r14
movsbl %bl,%edi
call   1040 <terminal_putchar@plt>
mov    %bl,0x1(%rsp,%rbp,1)
mov    %ebx,%ebp
cmp    $0xa,%bl
je     115e <terminal_readline+0x3e>
cmp    $0x20,%r14d
jl     1130 <terminal_readline+0x10>
mov    %rsp,%rax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
define noalias i8* @terminal_readline() {
%1 = alloca [32 x i8]
%2 = getelementptr inbounds [32 x i8], [32 x i8]* %1, i64 0, i64 0
br label %3
3: ; preds = %0, %13
%4 = phi i32 [ 0, %0 ], [ %15, %13 ]
%5 = phi i8 [ 0, %0 ], [ %14, %13 ]
%6 = tail call signext i8 (...) @readKey()
%7 = icmp eq i8 %6, %5
br i1 %7, label %13, label %8
8: ; preds = %3
%9 = add nsw i32 %4, 1
%10 = tail call i32 @terminal_putchar(i8 signext %6)
%11 = sext i32 %9 to i64
%12 = getelementptr inbounds [32 x i8], [32 x i8]* %1, i64 0, i64 %11
store i8 %6, i8* %12
br label %13
13: ; preds = %8, %3
%14 = phi i8 [ %6, %8 ], [ %5, %3 ]
%15 = phi i32 [ %9, %8 ], [ %4, %3 ]
%16 = icmp ne i8 %6, 10
%17 = icmp slt i32 %15, 32
%18 = select i1 %16, i1 %17, i1 false
br i1 %18, label %3, label %19
19: ; preds = %13
ret i8* %2
}