#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 */
int /*<<< orphan*/ _printf(char *, ...);
scalar_t__ read(int /*<<< orphan*/, char *, int);
int main(void) {
  char wmsg[] = "Welcome to Echo program (written in C)!\n";
  char prompt[] = "> ";
  char token[512];
  char key[2];
  int i, p;
  _printf(wmsg);
  _printf(prompt);
  p = 0;
  while (1) {
    if (read(0, key, 1) > 0) {
      switch (key[0]) {
      case '\r':
      case '\n':
        token[p] = '\0';
        _printf("Echo: %s\n", token);
        _printf(prompt);
        p = 0;
        break;
      default:
        token[p++] = key[0];
      }
    }
  }
  for (;;)
    ;
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x250,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x30(%rbp),%rdi
lea    0xeb3(%rip),%rsi
mov    $0x29,%edx
call   1050 <memcpy@plt>
mov    0xecb(%rip),%ax
mov    %ax,-0x33(%rbp)
mov    0xec3(%rip),%al
mov    %al,-0x31(%rbp)
lea    -0x30(%rbp),%rdi
mov    $0x0,%al
call   1030 <_printf@plt>
lea    -0x33(%rbp),%rdi
mov    $0x0,%al
call   1030 <_printf@plt>
movl   $0x0,-0x24c(%rbp)
lea    -0x242(%rbp),%rsi
xor    %edi,%edi
mov    $0x1,%edx
call   1040 <read@plt>
cmp    $0x0,%rax
jle    123a <main+0x10a>
movsbl -0x242(%rbp),%eax
mov    %eax,-0x250(%rbp)
sub    $0xa,%eax
je     11d7 <main+0xa7>
jmp    11c3 <main+0x93>
mov    -0x250(%rbp),%eax
sub    $0xd,%eax
jne    1215 <main+0xe5>
jmp    11d7 <main+0xa7>
movslq -0x24c(%rbp),%rax
movb   $0x0,-0x240(%rbp,%rax,1)
lea    -0x240(%rbp),%rsi
lea    0xe38(%rip),%rdi
mov    $0x0,%al
call   1030 <_printf@plt>
lea    -0x33(%rbp),%rdi
mov    $0x0,%al
call   1030 <_printf@plt>
movl   $0x0,-0x24c(%rbp)
jmp    1235 <main+0x105>
mov    -0x242(%rbp),%cl
mov    -0x24c(%rbp),%eax
mov    %eax,%edx
add    $0x1,%edx
mov    %edx,-0x24c(%rbp)
cltq
mov    %cl,-0x240(%rbp,%rax,1)
jmp    123a <main+0x10a>
jmp    118b <main+0x5b>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x240,%rsp
movups 0xee3(%rip),%xmm0
movups %xmm0,0x29(%rsp)
movaps 0xece(%rip),%xmm0
movaps %xmm0,0x20(%rsp)
movaps 0xeb2(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movb   $0x0,0xc(%rsp)
movw   $0x203e,0xa(%rsp)
xor    %ebp,%ebp
lea    0x10(%rsp),%rdi
xor    %eax,%eax
call   1030 <_printf@plt>
lea    0xa(%rsp),%r14
mov    %r14,%rdi
xor    %eax,%eax
call   1030 <_printf@plt>
lea    0xe(%rsp),%rbx
lea    0xea4(%rip),%r15
lea    0x40(%rsp),%r12
jmp    11b0 <main+0x90>
movslq %ebp,%rax
movb   $0x0,0x40(%rsp,%rax,1)
xor    %ebp,%ebp
mov    %r15,%rdi
mov    %r12,%rsi
xor    %eax,%eax
call   1030 <_printf@plt>
mov    %r14,%rdi
xor    %eax,%eax
call   1030 <_printf@plt>
xor    %edi,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1040 <read@plt>
test   %rax,%rax
jle    11b0 <main+0x90>
movzbl 0xe(%rsp),%eax
movsbl %al,%ecx
cmp    $0xd,%ecx
je     118f <main+0x6f>
cmp    $0xa,%ecx
je     118f <main+0x6f>
movslq %ebp,%rcx
add    $0x1,%ebp
mov    %al,0x40(%rsp,%rcx,1)
jmp    11b0 <main+0x90>

LLVM IR (-O0):
@__const.main.wmsg = [41 x i8] c"Welcome to Echo program (written in C)!\0A\00"
@__const.main.prompt = [3 x i8] c"> \00"
@.str = [10 x i8] c"Echo: %s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [41 x i8]
%3 = alloca [3 x i8]
%4 = alloca [512 x i8]
%5 = alloca [2 x i8]
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %1
%8 = bitcast [41 x i8]* %2 to i8*
%9 = bitcast [3 x i8]* %3 to i8*
%10 = getelementptr inbounds [41 x i8], [41 x i8]* %2, i64 0, i64 0
%11 = call i32 (i8*, ...) @_printf(i8* %10)
%12 = getelementptr inbounds [3 x i8], [3 x i8]* %3, i64 0, i64 0
%13 = call i32 (i8*, ...) @_printf(i8* %12)
store i32 0, i32* %7
br label %14
14: ; preds = %0, %38
%15 = getelementptr inbounds [2 x i8], [2 x i8]* %5, i64 0, i64 0
%16 = call i64 @read(i32 0, i8* %15, i32 1)
%17 = icmp sgt i64 %16, 0
br i1 %17, label %18, label %38
18: ; preds = %14
%19 = getelementptr inbounds [2 x i8], [2 x i8]* %5, i64 0, i64 0
%20 = load i8, i8* %19
%21 = sext i8 %20 to i32
switch i32 %21, label %30 [
i32 13, label %22
i32 10, label %22
]
22: ; preds = %18, %18
%23 = load i32, i32* %7
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 %24
store i8 0, i8* %25
%26 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%27 = call i32 (i8*, ...) @_printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %26)
%28 = getelementptr inbounds [3 x i8], [3 x i8]* %3, i64 0, i64 0
%29 = call i32 (i8*, ...) @_printf(i8* %28)
store i32 0, i32* %7
br label %37
30: ; preds = %18
%31 = getelementptr inbounds [2 x i8], [2 x i8]* %5, i64 0, i64 0
%32 = load i8, i8* %31
%33 = load i32, i32* %7
%34 = add nsw i32 %33, 1
store i32 %34, i32* %7
%35 = sext i32 %33 to i64
%36 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 %35
store i8 %32, i8* %36
br label %37
37: ; preds = %30, %22
br label %38
38: ; preds = %37, %14
br label %14
}

LLVM IR (-O2):
@__const.main.wmsg = [41 x i8] c"Welcome to Echo program (written in C)!\0A\00"
@__const.main.prompt = [3 x i8] c"> \00"
@.str = [10 x i8] c"Echo: %s\0A\00"
define i32 @main() {
%1 = alloca [41 x i8]
%2 = alloca [3 x i8]
%3 = alloca [512 x i8]
%4 = alloca [2 x i8]
%5 = getelementptr inbounds [41 x i8], [41 x i8]* %1, i64 0, i64 0
%6 = getelementptr inbounds [3 x i8], [3 x i8]* %2, i64 0, i64 0
%7 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%8 = getelementptr inbounds [2 x i8], [2 x i8]* %4, i64 0, i64 0
%9 = call i32 (i8*, ...) @_printf(i8* %5)
%10 = call i32 (i8*, ...) @_printf(i8* %6)
br label %11
11: ; preds = %28, %0
%12 = phi i32 [ 0, %0 ], [ %29, %28 ]
br label %13
13: ; preds = %11, %13
%14 = call i64 @read(i32 0, i8* %8, i32 1)
%15 = icmp sgt i64 %14, 0
br i1 %15, label %16, label %13
16: ; preds = %13
%17 = load i8, i8* %8
%18 = sext i8 %17 to i32
switch i32 %18, label %24 [
i32 13, label %19
i32 10, label %19
]
19: ; preds = %16, %16
%20 = sext i32 %12 to i64
%21 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 %20
store i8 0, i8* %21
%22 = call i32 (i8*, ...) @_printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %7)
%23 = call i32 (i8*, ...) @_printf(i8* %6)
br label %28
24: ; preds = %16
%25 = add nsw i32 %12, 1
%26 = sext i32 %12 to i64
%27 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 %26
store i8 %17, i8* %27
br label %28
28: ; preds = %24, %19
%29 = phi i32 [ 0, %19 ], [ %25, %24 ]
br label %11
}