#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 getc();
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ putc(char);
int getsecret(char *buf, int size) {
  int i = 0, done = 0;
  char c;
  if (!size) {
    size = -1;
  }
  while (!done) {
    c = getc();
    if (i == size && c != '\b') {
      continue;
    }
    switch (c) {
    case ('\r'):
    case ('\n'):
      printf("\n");
      *(buf + i) = '\0';
      done = 1;
      break;
    case ('\b'):
      if (i > 0) {
        printf("\b \b");
        *(buf + i) = '\0';
        i--;
      }
      i--;
      break;
    default:
      putc('*');
      *(buf + i) = c;
      break;
    }
    i++;
  }
  --i;
  return (i);
}


x86 assembly (clang, -O0):
<getsecret>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0x0,-0xc(%rbp)
jne    115e <getsecret+0x2e>
movl   $0xffffffff,-0xc(%rbp)
jmp    1163 <getsecret+0x33>
cmpl   $0x0,-0x14(%rbp)
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1179 <getsecret+0x49>
jmp    125b <getsecret+0x12b>
mov    $0x0,%al
call   1050 <getc@plt>
mov    %al,-0x15(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jne    11a1 <getsecret+0x71>
movsbl -0x15(%rbp),%eax
cmp    $0x8,%eax
je     11a1 <getsecret+0x71>
jmp    1163 <getsecret+0x33>
movsbl -0x15(%rbp),%eax
mov    %eax,-0x1c(%rbp)
sub    $0x8,%eax
je     11fc <getsecret+0xcc>
jmp    11b6 <getsecret+0x86>
mov    -0x1c(%rbp),%eax
sub    $0xa,%eax
je     11d8 <getsecret+0xa8>
jmp    11c7 <getsecret+0x97>
mov    -0x1c(%rbp),%eax
sub    $0xd,%eax
jne    1235 <getsecret+0x105>
jmp    11d8 <getsecret+0xa8>
lea    0xe21(%rip),%rdi
call   1030 <printf@plt>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0x1,-0x14(%rbp)
jmp    124d <getsecret+0x11d>
cmpl   $0x0,-0x10(%rbp)
jle    1227 <getsecret+0xf7>
lea    0xdf5(%rip),%rdi
call   1030 <printf@plt>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
jmp    124d <getsecret+0x11d>
mov    $0x2a,%edi
call   1040 <putc@plt>
mov    -0x15(%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)
jmp    1163 <getsecret+0x33>
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getsecret>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r15
cmp    $0x1,%esi
sbb    %r12d,%r12d
or     %esi,%r12d
xor    %r13d,%r13d
lea    0xe9f(%rip),%r14
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
test   %ebp,%ebp
jne    11f0 <getsecret+0xb0>
xor    %ebp,%ebp
xor    %eax,%eax
call   1060 <getc@plt>
mov    %eax,%ebx
cmp    %r12d,%r13d
jne    1189 <getsecret+0x49>
cmp    $0x8,%bl
jne    1170 <getsecret+0x30>
movsbl %bl,%eax
cmp    $0x8,%eax
je     11b8 <getsecret+0x78>
cmp    $0xd,%eax
je     119b <getsecret+0x5b>
cmp    $0xa,%eax
jne    11d7 <getsecret+0x97>
mov    $0xa,%edi
call   1030 <putchar@plt>
movslq %r13d,%rax
movb   $0x0,(%r15,%rax,1)
mov    $0x1,%ebp
add    $0x1,%r13d
jmp    1170 <getsecret+0x30>
test   %r13d,%r13d
jle    11d1 <getsecret+0x91>
mov    %r14,%rdi
call   1040 <printf@plt>
mov    %r13d,%eax
movb   $0x0,(%r15,%rax,1)
add    $0xffffffff,%r13d
add    $0xffffffff,%r13d
jmp    11e8 <getsecret+0xa8>
mov    $0x2a,%edi
call   1050 <putc@plt>
movslq %r13d,%rax
mov    %bl,(%r15,%rax,1)
xor    %ebp,%ebp
add    $0x1,%r13d
jmp    1170 <getsecret+0x30>
add    $0xffffffff,%r13d
mov    %r13d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"\0A\00"
@.str.1 = [4 x i8] c"\08 \08\00"
define i32 @getsecret(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8
store i8* %0, i8** %3
store i32 %1, i32* %4
store i32 0, i32* %5
store i32 0, i32* %6
%8 = load i32, i32* %4
%9 = icmp ne i32 %8, 0
br i1 %9, label %11, label %10
10: ; preds = %2
store i32 -1, i32* %4
br label %11
11: ; preds = %10, %2
br label %12
12: ; preds = %56, %25, %11
%13 = load i32, i32* %6
%14 = icmp ne i32 %13, 0
%15 = xor i1 %14, true
br i1 %15, label %16, label %59
16: ; preds = %12
%17 = call signext i8 (...) @getc()
store i8 %17, i8* %7
%18 = load i32, i32* %5
%19 = load i32, i32* %4
%20 = icmp eq i32 %18, %19
br i1 %20, label %21, label %26
21: ; preds = %16
%22 = load i8, i8* %7
%23 = sext i8 %22 to i32
%24 = icmp ne i32 %23, 8
br i1 %24, label %25, label %26
25: ; preds = %21
br label %12
26: ; preds = %21, %16
%27 = load i8, i8* %7
%28 = sext i8 %27 to i32
switch i32 %28, label %49 [
i32 13, label %29
i32 10, label %29
i32 8, label %35
]
29: ; preds = %26, %26
%30 = call i32 @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%31 = load i8*, i8** %3
%32 = load i32, i32* %5
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %31, i64 %33
store i8 0, i8* %34
store i32 1, i32* %6
br label %56
35: ; preds = %26
%36 = load i32, i32* %5
%37 = icmp sgt i32 %36, 0
br i1 %37, label %38, label %46
38: ; preds = %35
%39 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%40 = load i8*, i8** %3
%41 = load i32, i32* %5
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i8, i8* %40, i64 %42
store i8 0, i8* %43
%44 = load i32, i32* %5
%45 = add nsw i32 %44, -1
store i32 %45, i32* %5
br label %46
46: ; preds = %38, %35
%47 = load i32, i32* %5
%48 = add nsw i32 %47, -1
store i32 %48, i32* %5
br label %56
49: ; preds = %26
%50 = call i32 @putc(i8 signext 42)
%51 = load i8, i8* %7
%52 = load i8*, i8** %3
%53 = load i32, i32* %5
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds i8, i8* %52, i64 %54
store i8 %51, i8* %55
br label %56
56: ; preds = %49, %46, %29
%57 = load i32, i32* %5
%58 = add nsw i32 %57, 1
store i32 %58, i32* %5
br label %12
59: ; preds = %12
%60 = load i32, i32* %5
%61 = add nsw i32 %60, -1
store i32 %61, i32* %5
%62 = load i32, i32* %5
ret i32 %62
}

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