#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 */
unsigned char kgetc();
int /*<<< orphan*/ kputc(char);
void kgstr(char *s) {
  char *s_ptr = s;
  unsigned char c;
  while ((c = kgetc()) != 0x0d) {
    if (c != 0x7f) {
      *s_ptr++ = c;
      kputc(c);
    } else if (s_ptr != s) {
      s_ptr--;
      kputc(c);
    }
  }
  *s_ptr = 0;
  kputc('\n');
}


x86 assembly (clang, -O0):
<kgstr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    $0x0,%al
call   1030 <kgetc@plt>
mov    %al,-0x11(%rbp)
movzbl %al,%eax
cmp    $0xd,%eax
je     11a6 <kgstr+0x86>
movzbl -0x11(%rbp),%eax
cmp    $0x7f,%eax
je     1179 <kgstr+0x59>
mov    -0x11(%rbp),%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,(%rax)
movsbl -0x11(%rbp),%edi
call   1040 <kputc@plt>
jmp    11a1 <kgstr+0x81>
mov    -0x10(%rbp),%rax
cmp    -0x8(%rbp),%rax
je     119c <kgstr+0x7c>
mov    -0x10(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x10(%rbp)
movsbl -0x11(%rbp),%edi
call   1040 <kputc@plt>
jmp    11a1 <kgstr+0x81>
jmp    1134 <kgstr+0x14>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    $0xa,%edi
call   1040 <kputc@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<kgstr>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
mov    %rdi,%r15
jmp    1149 <kgstr+0x29>
nopl   (%rax)
mov    %r14,%r15
cmp    %r14,%rbx
je     1149 <kgstr+0x29>
add    $0xffffffffffffffff,%rbx
mov    $0x7f,%al
mov    %rbx,%r15
movsbl %al,%edi
call   1040 <kputc@plt>
mov    %r15,%rbx
xor    %eax,%eax
call   1030 <kgetc@plt>
cmp    $0x7f,%al
je     1130 <kgstr+0x10>
cmp    $0xd,%al
je     1163 <kgstr+0x43>
mov    %al,(%rbx)
add    $0x1,%rbx
jmp    113e <kgstr+0x1e>
movb   $0x0,(%rbx)
mov    $0xa,%edi
pop    %rbx
pop    %r14
pop    %r15
jmp    1040 <kputc@plt>

LLVM IR (-O0):
define void @kgstr(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8
store i8* %0, i8** %2
%5 = load i8*, i8** %2
store i8* %5, i8** %3
br label %6
6: ; preds = %30, %1
%7 = call zeroext i8 (...) @kgetc()
store i8 %7, i8* %4
%8 = zext i8 %7 to i32
%9 = icmp ne i32 %8, 13
br i1 %9, label %10, label %31
10: ; preds = %6
%11 = load i8, i8* %4
%12 = zext i8 %11 to i32
%13 = icmp ne i32 %12, 127
br i1 %13, label %14, label %20
14: ; preds = %10
%15 = load i8, i8* %4
%16 = load i8*, i8** %3
%17 = getelementptr inbounds i8, i8* %16, i32 1
store i8* %17, i8** %3
store i8 %15, i8* %16
%18 = load i8, i8* %4
%19 = call i32 @kputc(i8 signext %18)
br label %30
20: ; preds = %10
%21 = load i8*, i8** %3
%22 = load i8*, i8** %2
%23 = icmp ne i8* %21, %22
br i1 %23, label %24, label %29
24: ; preds = %20
%25 = load i8*, i8** %3
%26 = getelementptr inbounds i8, i8* %25, i32 -1
store i8* %26, i8** %3
%27 = load i8, i8* %4
%28 = call i32 @kputc(i8 signext %27)
br label %29
29: ; preds = %24, %20
br label %30
30: ; preds = %29, %14
br label %6
31: ; preds = %6
%32 = load i8*, i8** %3
store i8 0, i8* %32
%33 = call i32 @kputc(i8 signext 10)
ret void
}

LLVM IR (-O2):
define void @kgstr(i8* writeonly %0) {
br label %2
2: ; preds = %15, %1
%3 = phi i8* [ %0, %1 ], [ %16, %15 ]
%4 = tail call zeroext i8 (...) @kgetc()
switch i8 %4, label %5 [
i8 13, label %17
i8 127, label %7
]
5: ; preds = %2
%6 = getelementptr inbounds i8, i8* %3, i64 1
store i8 %4, i8* %3
br label %11
7: ; preds = %2
%8 = icmp eq i8* %3, %0
br i1 %8, label %15, label %9
9: ; preds = %7
%10 = getelementptr inbounds i8, i8* %3, i64 -1
br label %11
11: ; preds = %5, %9
%12 = phi i8 [ 127, %9 ], [ %4, %5 ]
%13 = phi i8* [ %10, %9 ], [ %6, %5 ]
%14 = tail call i32 @kputc(i8 signext %12)
br label %15
15: ; preds = %11, %7
%16 = phi i8* [ %0, %7 ], [ %13, %11 ]
br label %2
17: ; preds = %2
store i8 0, i8* %3
%18 = tail call i32 @kputc(i8 signext 10)
ret void
}