#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_read();
int /*<<< orphan*/ UART_write_str(char *);
int /*<<< orphan*/ printf(char *, char *);
int main() {
  char str[100];
  char *c = str;
  UART_write_str("Send me a string and I'll echo it back!\n");
  c--;
  do {
    c++;
    *c = UART_read();
  } while ((*c != '\n') && (c < (str + 99)));
  *c = '\0';
  printf("%s\n", str);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x70(%rbp),%rax
mov    %rax,-0x78(%rbp)
lea    0xeaf(%rip),%rdi
call   1050 <UART_write_str@plt>
mov    -0x78(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x78(%rbp)
mov    -0x78(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x78(%rbp)
mov    $0x0,%al
call   1040 <UART_read@plt>
mov    %al,%cl
mov    -0x78(%rbp),%rax
mov    %cl,(%rax)
mov    -0x78(%rbp),%rax
movsbl (%rax),%ecx
xor    %eax,%eax
cmp    $0xa,%ecx
mov    %al,-0x79(%rbp)
je     11a7 <main+0x77>
mov    -0x78(%rbp),%rax
lea    -0x70(%rbp),%rcx
add    $0x63,%rcx
cmp    %rcx,%rax
setb   %al
mov    %al,-0x79(%rbp)
mov    -0x79(%rbp),%al
test   $0x1,%al
jne    1162 <main+0x32>
mov    -0x78(%rbp),%rax
movb   $0x0,(%rax)
lea    -0x70(%rbp),%rsi
lea    0xe65(%rip),%rdi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x68,%rsp
lea    0xec2(%rip),%rdi
call   1050 <UART_write_str@plt>
mov    %rsp,%rbx
lea    0x63(%rsp),%r14
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1040 <UART_read@plt>
mov    %al,(%rbx)
lea    0x1(%rbx),%rcx
cmp    $0xa,%al
je     1169 <main+0x39>
cmp    %r14,%rbx
mov    %rcx,%rbx
jb     1150 <main+0x20>
movb   $0x0,-0x1(%rcx)
lea    0xeb5(%rip),%rdi
mov    %rsp,%rsi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x68,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [41 x i8] c"Send me a string and I'll echo it back!\0A\00"
@.str.1 = [4 x i8] c"%s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [100 x i8]
%3 = alloca i8*
store i32 0, i32* %1
%4 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 0
store i8* %4, i8** %3
%5 = call i32 @UART_write_str(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0))
%6 = load i8*, i8** %3
%7 = getelementptr inbounds i8, i8* %6, i32 -1
store i8* %7, i8** %3
br label %8
8: ; preds = %23, %0
%9 = load i8*, i8** %3
%10 = getelementptr inbounds i8, i8* %9, i32 1
store i8* %10, i8** %3
%11 = call signext i8 (...) @UART_read()
%12 = load i8*, i8** %3
store i8 %11, i8* %12
br label %13
13: ; preds = %8
%14 = load i8*, i8** %3
%15 = load i8, i8* %14
%16 = sext i8 %15 to i32
%17 = icmp ne i32 %16, 10
br i1 %17, label %18, label %23
18: ; preds = %13
%19 = load i8*, i8** %3
%20 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 0
%21 = getelementptr inbounds i8, i8* %20, i64 99
%22 = icmp ult i8* %19, %21
br label %23
23: ; preds = %18, %13
%24 = phi i1 [ false, %13 ], [ %22, %18 ]
br i1 %24, label %8, label %25
25: ; preds = %23
%26 = load i8*, i8** %3
store i8 0, i8* %26
%27 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 0
%28 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %27)
ret i32 0
}

LLVM IR (-O2):
@.str = [41 x i8] c"Send me a string and I'll echo it back!\0A\00"
@.str.1 = [4 x i8] c"%s\0A\00"
define i32 @main() {
%1 = alloca [100 x i8]
%2 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 0
%3 = tail call i32 @UART_write_str(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0))
%4 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 -1
%5 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 99
br label %6
6: ; preds = %6, %0
%7 = phi i8* [ %4, %0 ], [ %8, %6 ]
%8 = getelementptr inbounds i8, i8* %7, i64 1
%9 = call signext i8 (...) @UART_read()
store i8 %9, i8* %8
%10 = icmp ne i8 %9, 10
%11 = icmp ult i8* %8, %5
%12 = select i1 %10, i1 %11, i1 false
br i1 %12, label %6, label %13
13: ; preds = %6
store i8 0, i8* %8
%14 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %2)
ret i32 0
}