#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 getch();
int /*<<< orphan*/ printf(char const *);
int /*<<< orphan*/ putchar(int);
char *PlatformGetLine(char *Buf, int MaxLen, const char *Prompt) {
  unsigned int ix;
  char ch;
  printf(Prompt);
  ix = 0;
  ch = getch();
  if (ch == '\n' || ch == '\r') {
    ch = getch();
  }
  while (ix < MaxLen) {
    if (ch == 0x1B) {
      printf("LLeaving PicoC\n");
      return 0;
    } else if (ch == 0x03) {
      printf("Leaving PicoC\n");
      return 0;
    } else if (ch == 0x08) {
      if (ix > 0) {
        putchar(' ');
        putchar(0x08);
        Buf[ix] = 0x00;
        ix--;
      }
    } else if (ch == '\n') {
      Buf[ix] = ch;
      ix++;
      Buf[ix] = 0;
      return Buf;
    } else {
      Buf[ix] = ch;
      ix++;
    }
    ch = getch();
  }
  return 0;
}


x86 assembly (clang, -O0):
<PlatformGetLine>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x24(%rbp)
mov    $0x0,%al
call   1050 <getch@plt>
mov    %al,-0x25(%rbp)
movsbl -0x25(%rbp),%eax
cmp    $0xa,%eax
je     1177 <PlatformGetLine+0x47>
movsbl -0x25(%rbp),%eax
cmp    $0xd,%eax
jne    1181 <PlatformGetLine+0x51>
mov    $0x0,%al
call   1050 <getch@plt>
mov    %al,-0x25(%rbp)
jmp    1186 <PlatformGetLine+0x56>
mov    -0x24(%rbp),%eax
cmp    -0x14(%rbp),%eax
jae    1291 <PlatformGetLine+0x161>
movsbl -0x25(%rbp),%eax
cmp    $0x1b,%eax
jne    11b8 <PlatformGetLine+0x88>
lea    0xe5a(%rip),%rdi
call   1040 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1299 <PlatformGetLine+0x169>
movsbl -0x25(%rbp),%eax
cmp    $0x3,%eax
jne    11de <PlatformGetLine+0xae>
lea    0xe35(%rip),%rdi
call   1040 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1299 <PlatformGetLine+0x169>
movsbl -0x25(%rbp),%eax
cmp    $0x8,%eax
jne    1222 <PlatformGetLine+0xf2>
cmpl   $0x0,-0x24(%rbp)
jbe    121d <PlatformGetLine+0xed>
mov    $0x20,%edi
call   1030 <putchar@plt>
mov    $0x8,%edi
call   1030 <putchar@plt>
mov    -0x10(%rbp),%rax
mov    -0x24(%rbp),%ecx
movb   $0x0,(%rax,%rcx,1)
mov    -0x24(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x24(%rbp)
jmp    1278 <PlatformGetLine+0x148>
movsbl -0x25(%rbp),%eax
cmp    $0xa,%eax
jne    125d <PlatformGetLine+0x12d>
mov    -0x25(%rbp),%dl
mov    -0x10(%rbp),%rax
mov    -0x24(%rbp),%ecx
mov    %dl,(%rax,%rcx,1)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x24(%rbp),%ecx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1299 <PlatformGetLine+0x169>
mov    -0x25(%rbp),%dl
mov    -0x10(%rbp),%rax
mov    -0x24(%rbp),%ecx
mov    %dl,(%rax,%rcx,1)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1278 <PlatformGetLine+0x148>
jmp    127d <PlatformGetLine+0x14d>
jmp    1282 <PlatformGetLine+0x152>
mov    $0x0,%al
call   1050 <getch@plt>
mov    %al,-0x25(%rbp)
jmp    1186 <PlatformGetLine+0x56>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<PlatformGetLine>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %esi,%r12d
mov    %rdi,%r14
mov    %rdx,%rdi
call   1050 <printf@plt>
xor    %eax,%eax
call   1060 <getch@plt>
cmp    $0xd,%al
je     1165 <PlatformGetLine+0x25>
cmp    $0xa,%al
jne    116c <PlatformGetLine+0x2c>
xor    %eax,%eax
call   1060 <getch@plt>
test   %r12d,%r12d
je     11f2 <PlatformGetLine+0xb2>
lea    0xe84(%rip),%r15
xor    %ebp,%ebp
jmp    11b1 <PlatformGetLine+0x71>
mov    $0x20,%edi
call   1030 <putchar@plt>
mov    $0x8,%edi
call   1030 <putchar@plt>
mov    $0xffffffff,%ebx
xor    %eax,%eax
mov    %ebp,%ecx
mov    %al,(%r14,%rcx,1)
add    %ebp,%ebx
xor    %eax,%eax
call   1060 <getch@plt>
mov    %ebx,%ebp
cmp    %r12d,%ebx
jae    11f2 <PlatformGetLine+0xb2>
mov    $0x1,%ebx
lea    -0x3(%rax),%ecx
cmp    $0x18,%cl
ja     119b <PlatformGetLine+0x5b>
movzbl %cl,%ecx
movslq (%r15,%rcx,4),%rcx
add    %r15,%rcx
jmp    *%rcx
test   %ebp,%ebp
jne    1180 <PlatformGetLine+0x40>
xor    %ebx,%ebx
jmp    11a3 <PlatformGetLine+0x63>
lea    0xe8c(%rip),%rdi
jmp    11ed <PlatformGetLine+0xad>
mov    %ebp,%eax
movw   $0xa,(%r14,%rax,1)
jmp    11f5 <PlatformGetLine+0xb5>
lea    0xe77(%rip),%rdi
call   1040 <puts@plt>
xor    %r14d,%r14d
mov    %r14,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [16 x i8] c"LLeaving PicoC\0A\00"
@.str.1 = [15 x i8] c"Leaving PicoC\0A\00"
define i8* @PlatformGetLine(i8* %0, i32 %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i8
store i8* %0, i8** %5
store i32 %1, i32* %6
store i8* %2, i8** %7
%10 = load i8*, i8** %7
%11 = call i32 @printf(i8* %10)
store i32 0, i32* %8
%12 = call signext i8 (...) @getch()
store i8 %12, i8* %9
%13 = load i8, i8* %9
%14 = sext i8 %13 to i32
%15 = icmp eq i32 %14, 10
br i1 %15, label %20, label %16
16: ; preds = %3
%17 = load i8, i8* %9
%18 = sext i8 %17 to i32
%19 = icmp eq i32 %18, 13
br i1 %19, label %20, label %22
20: ; preds = %16, %3
%21 = call signext i8 (...) @getch()
store i8 %21, i8* %9
br label %22
22: ; preds = %20, %16
br label %23
23: ; preds = %84, %22
%24 = load i32, i32* %8
%25 = load i32, i32* %6
%26 = icmp ult i32 %24, %25
br i1 %26, label %27, label %86
27: ; preds = %23
%28 = load i8, i8* %9
%29 = sext i8 %28 to i32
%30 = icmp eq i32 %29, 27
br i1 %30, label %31, label %33
31: ; preds = %27
%32 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
store i8* null, i8** %4
br label %87
33: ; preds = %27
%34 = load i8, i8* %9
%35 = sext i8 %34 to i32
%36 = icmp eq i32 %35, 3
br i1 %36, label %37, label %39
37: ; preds = %33
%38 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
store i8* null, i8** %4
br label %87
39: ; preds = %33
%40 = load i8, i8* %9
%41 = sext i8 %40 to i32
%42 = icmp eq i32 %41, 8
br i1 %42, label %43, label %56
43: ; preds = %39
%44 = load i32, i32* %8
%45 = icmp ugt i32 %44, 0
br i1 %45, label %46, label %55
46: ; preds = %43
%47 = call i32 @putchar(i32 32)
%48 = call i32 @putchar(i32 8)
%49 = load i8*, i8** %5
%50 = load i32, i32* %8
%51 = zext i32 %50 to i64
%52 = getelementptr inbounds i8, i8* %49, i64 %51
store i8 0, i8* %52
%53 = load i32, i32* %8
%54 = add i32 %53, -1
store i32 %54, i32* %8
br label %55
55: ; preds = %46, %43
br label %82
56: ; preds = %39
%57 = load i8, i8* %9
%58 = sext i8 %57 to i32
%59 = icmp eq i32 %58, 10
br i1 %59, label %60, label %73
60: ; preds = %56
%61 = load i8, i8* %9
%62 = load i8*, i8** %5
%63 = load i32, i32* %8
%64 = zext i32 %63 to i64
%65 = getelementptr inbounds i8, i8* %62, i64 %64
store i8 %61, i8* %65
%66 = load i32, i32* %8
%67 = add i32 %66, 1
store i32 %67, i32* %8
%68 = load i8*, i8** %5
%69 = load i32, i32* %8
%70 = zext i32 %69 to i64
%71 = getelementptr inbounds i8, i8* %68, i64 %70
store i8 0, i8* %71
%72 = load i8*, i8** %5
store i8* %72, i8** %4
br label %87
73: ; preds = %56
%74 = load i8, i8* %9
%75 = load i8*, i8** %5
%76 = load i32, i32* %8
%77 = zext i32 %76 to i64
%78 = getelementptr inbounds i8, i8* %75, i64 %77
store i8 %74, i8* %78
%79 = load i32, i32* %8
%80 = add i32 %79, 1
store i32 %80, i32* %8
br label %81
81: ; preds = %73
br label %82
82: ; preds = %81, %55
br label %83
83: ; preds = %82
br label %84
84: ; preds = %83
%85 = call signext i8 (...) @getch()
store i8 %85, i8* %9
br label %23
86: ; preds = %23
store i8* null, i8** %4
br label %87
87: ; preds = %86, %60, %37, %31
%88 = load i8*, i8** %4
ret i8* %88
}

LLVM IR (-O2):
@str = [14 x i8] c"Leaving PicoC\00"
@str.2 = [15 x i8] c"LLeaving PicoC\00"
define i8* @PlatformGetLine(i8* writeonly %0, i32 %1, i8* nocapture %2) {
%4 = tail call i32 @printf(i8* dereferenceable(1) %2)
%5 = tail call signext i8 (...) @getch()
switch i8 %5, label %8 [
i8 13, label %6
i8 10, label %6
]
6: ; preds = %3, %3
%7 = tail call signext i8 (...) @getch()
br label %8
8: ; preds = %3, %6
%9 = phi i8 [ %7, %6 ], [ %5, %3 ]
%10 = icmp eq i32 %1, 0
br i1 %10, label %39, label %11
11: ; preds = %8, %35
%12 = phi i8 [ %37, %35 ], [ %9, %8 ]
%13 = phi i32 [ %36, %35 ], [ 0, %8 ]
switch i8 %12, label %29 [
i8 27, label %14
i8 3, label %16
i8 8, label %18
i8 10, label %23
]
14: ; preds = %11
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.2, i64 0, i64 0))
br label %39
16: ; preds = %11
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str, i64 0, i64 0))
br label %39
18: ; preds = %11
%19 = icmp eq i32 %13, 0
br i1 %19, label %35, label %20
20: ; preds = %18
%21 = tail call i32 @putchar(i32 32)
%22 = tail call i32 @putchar(i32 8)
br label %29
23: ; preds = %11
%24 = zext i32 %13 to i64
%25 = getelementptr inbounds i8, i8* %0, i64 %24
store i8 10, i8* %25
%26 = add nuw i32 %13, 1
%27 = zext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %0, i64 %27
store i8 0, i8* %28
br label %39
29: ; preds = %11, %20
%30 = phi i8 [ 0, %20 ], [ %12, %11 ]
%31 = phi i32 [ -1, %20 ], [ 1, %11 ]
%32 = zext i32 %13 to i64
%33 = getelementptr inbounds i8, i8* %0, i64 %32
store i8 %30, i8* %33
%34 = add i32 %13, %31
br label %35
35: ; preds = %29, %18
%36 = phi i32 [ 0, %18 ], [ %34, %29 ]
%37 = tail call signext i8 (...) @getch()
%38 = icmp ult i32 %36, %1
br i1 %38, label %11, label %39
39: ; preds = %35, %8, %23, %16, %14
%40 = phi i8* [ null, %14 ], [ null, %16 ], [ %0, %23 ], [ null, %8 ], [ null, %35 ]
ret i8* %40
}