#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 getChar();
int /*<<< orphan*/ putChar(char);
int scanf(char *buffer) {
  char c;
  int count = 0;
  while ((c = getChar()) != '\n') {
    if (c == '\b') {
      if (count != 0) {
        --count;
        *(buffer + count) = 0;
        putChar(c);
      }
    } else {
      *(buffer + count) = c;
      count++;
      putChar(c);
    }
  }
  if (count != 0)
    putChar('\n');
  return count;
}


x86 assembly (clang, -O0):
<scanf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0x10(%rbp)
mov    $0x0,%al
call   1030 <getChar@plt>
mov    %al,-0x9(%rbp)
movsbl %al,%eax
cmp    $0xa,%eax
je     11a8 <scanf+0x88>
movsbl -0x9(%rbp),%eax
cmp    $0x8,%eax
jne    1183 <scanf+0x63>
cmpl   $0x0,-0x10(%rbp)
je     117e <scanf+0x5e>
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
movsbl -0x9(%rbp),%edi
call   1040 <putChar@plt>
jmp    11a3 <scanf+0x83>
mov    -0x9(%rbp),%dl
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
movsbl -0x9(%rbp),%edi
call   1040 <putChar@plt>
jmp    1133 <scanf+0x13>
cmpl   $0x0,-0x10(%rbp)
je     11bc <scanf+0x9c>
mov    $0xa,%edi
call   1040 <putChar@plt>
mov    -0x10(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<scanf>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%r14
xor    %ebx,%ebx
jmp    1150 <scanf+0x30>
nopl   0x0(%rax,%rax,1)
mov    $0x0,%ebx
test   %ebp,%ebp
je     1150 <scanf+0x30>
movslq %ebp,%rbx
movb   $0x0,-0x1(%r14,%rbx,1)
add    $0xffffffffffffffff,%rbx
mov    $0x8,%al
movsbl %al,%edi
call   1040 <putChar@plt>
mov    %ebx,%ebp
xor    %eax,%eax
call   1030 <getChar@plt>
cmp    $0x8,%al
je     1130 <scanf+0x10>
cmp    $0xa,%al
je     116f <scanf+0x4f>
movslq %ebp,%rcx
mov    %al,(%r14,%rcx,1)
add    $0x1,%ebp
mov    %ebp,%ebx
jmp    1148 <scanf+0x28>
test   %ebp,%ebp
je     117d <scanf+0x5d>
mov    $0xa,%edi
call   1040 <putChar@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @scanf(i8* %0) {
%2 = alloca i8*
%3 = alloca i8
%4 = alloca i32
store i8* %0, i8** %2
store i32 0, i32* %4
br label %5
5: ; preds = %36, %1
%6 = call signext i8 (...) @getChar()
store i8 %6, i8* %3
%7 = sext i8 %6 to i32
%8 = icmp ne i32 %7, 10
br i1 %8, label %9, label %37
9: ; preds = %5
%10 = load i8, i8* %3
%11 = sext i8 %10 to i32
%12 = icmp eq i32 %11, 8
br i1 %12, label %13, label %26
13: ; preds = %9
%14 = load i32, i32* %4
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %25
16: ; preds = %13
%17 = load i32, i32* %4
%18 = add nsw i32 %17, -1
store i32 %18, i32* %4
%19 = load i8*, i8** %2
%20 = load i32, i32* %4
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i8, i8* %19, i64 %21
store i8 0, i8* %22
%23 = load i8, i8* %3
%24 = call i32 @putChar(i8 signext %23)
br label %25
25: ; preds = %16, %13
br label %36
26: ; preds = %9
%27 = load i8, i8* %3
%28 = load i8*, i8** %2
%29 = load i32, i32* %4
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %28, i64 %30
store i8 %27, i8* %31
%32 = load i32, i32* %4
%33 = add nsw i32 %32, 1
store i32 %33, i32* %4
%34 = load i8, i8* %3
%35 = call i32 @putChar(i8 signext %34)
br label %36
36: ; preds = %26, %25
br label %5
37: ; preds = %5
%38 = load i32, i32* %4
%39 = icmp ne i32 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %37
%41 = call i32 @putChar(i8 signext 10)
br label %42
42: ; preds = %40, %37
%43 = load i32, i32* %4
ret i32 %43
}

LLVM IR (-O2):
define i32 @scanf(i8* nocapture writeonly %0) {
br label %2
2: ; preds = %19, %1
%3 = phi i32 [ 0, %1 ], [ %20, %19 ]
%4 = tail call signext i8 (...) @getChar()
switch i8 %4, label %11 [
i8 10, label %21
i8 8, label %5
]
5: ; preds = %2
%6 = icmp eq i32 %3, 0
br i1 %6, label %19, label %7
7: ; preds = %5
%8 = add nsw i32 %3, -1
%9 = sext i32 %8 to i64
%10 = getelementptr inbounds i8, i8* %0, i64 %9
store i8 0, i8* %10
br label %15
11: ; preds = %2
%12 = sext i32 %3 to i64
%13 = getelementptr inbounds i8, i8* %0, i64 %12
store i8 %4, i8* %13
%14 = add nsw i32 %3, 1
br label %15
15: ; preds = %11, %7
%16 = phi i8 [ 8, %7 ], [ %4, %11 ]
%17 = phi i32 [ %8, %7 ], [ %14, %11 ]
%18 = tail call i32 @putChar(i8 signext %16)
br label %19
19: ; preds = %15, %5
%20 = phi i32 [ 0, %5 ], [ %17, %15 ]
br label %2
21: ; preds = %2
%22 = icmp eq i32 %3, 0
br i1 %22, label %25, label %23
23: ; preds = %21
%24 = tail call i32 @putChar(i8 signext 10)
br label %25
25: ; preds = %23, %21
ret i32 %3
}