#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 getkeystroke();
scalar_t__ key_abort();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sprintf(char *, char *, char *);
int strlen(char *);
int EnterString(char *msg, char *buf, int min, int max) {
  int len, deflen;
  char ch, defbuf[80];
  if (key_abort())
    return -1;
  deflen = strlen(buf);
  if (deflen < 80)
    sprintf(defbuf, "%s", buf);
  else
    defbuf[0] = 0;
  if (deflen < 30)
    printf("%s (%s): ", msg, buf);
  else
    printf("%s (%s):\n", msg, buf);
  len = 0;
  for (;;) {
    ch = getkeystroke();
    if (key_abort())
      return -1;
    if (ch == 0x08) {
      if (len > 0) {
        len--;
        printf("\x008 \x008");
      }
    } else if (ch == 0x1B) {
      printf("\n");
      return -1;
    }
    if (ch == 0x0A) {
      if ((len == 0) && (deflen > min)) {
        sprintf(buf, "%s", defbuf);
        printf("\n");
        return deflen;
      } else if (len < min)
        continue;
      else {
        printf("\n");
        return len;
      }
    } else if (len < max) {
      printf("%c", ch);
      buf[len++] = ch;
    }
  }
}


x86 assembly (clang, -O0):
<EnterString>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %ecx,-0x20(%rbp)
mov    $0x0,%al
call   1060 <key_abort@plt>
cmp    $0x0,%rax
je     1186 <EnterString+0x36>
movl   $0xffffffff,-0x4(%rbp)
jmp    133b <EnterString+0x1eb>
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x50,-0x28(%rbp)
jge    11b5 <EnterString+0x65>
lea    -0x80(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xe55(%rip),%rsi
call   1070 <sprintf@plt>
jmp    11b9 <EnterString+0x69>
movb   $0x0,-0x80(%rbp)
cmpl   $0x1e,-0x28(%rbp)
jge    11de <EnterString+0x8e>
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    0xe31(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    11f4 <EnterString+0xa4>
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    0xe20(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x24(%rbp)
mov    $0x0,%al
call   1030 <getkeystroke@plt>
mov    %al,-0x29(%rbp)
mov    $0x0,%al
call   1060 <key_abort@plt>
cmp    $0x0,%rax
je     1222 <EnterString+0xd2>
movl   $0xffffffff,-0x4(%rbp)
jmp    133b <EnterString+0x1eb>
movsbl -0x29(%rbp),%eax
cmp    $0x8,%eax
jne    1255 <EnterString+0x105>
cmpl   $0x0,-0x24(%rbp)
jle    1250 <EnterString+0x100>
mov    -0x24(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x24(%rbp)
lea    0xdce(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1281 <EnterString+0x131>
movsbl -0x29(%rbp),%eax
cmp    $0x1b,%eax
jne    127c <EnterString+0x12c>
lea    0xdac(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    133b <EnterString+0x1eb>
jmp    1281 <EnterString+0x131>
movsbl -0x29(%rbp),%eax
cmp    $0xa,%eax
jne    12fb <EnterString+0x1ab>
cmpl   $0x0,-0x24(%rbp)
jne    12d1 <EnterString+0x181>
mov    -0x28(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jle    12d1 <EnterString+0x181>
mov    -0x18(%rbp),%rdi
lea    -0x80(%rbp),%rdx
lea    0xd4d(%rip),%rsi
call   1070 <sprintf@plt>
lea    0xd56(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    133b <EnterString+0x1eb>
mov    -0x24(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    12e2 <EnterString+0x192>
jmp    11fb <EnterString+0xab>
lea    0xd2c(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    133b <EnterString+0x1eb>
mov    -0x24(%rbp),%eax
cmp    -0x20(%rbp),%eax
jge    1331 <EnterString+0x1e1>
movsbl -0x29(%rbp),%esi
lea    0xd09(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x29(%rbp),%dl
mov    -0x18(%rbp),%rax
mov    -0x24(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x24(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
jmp    1336 <EnterString+0x1e6>
jmp    11fb <EnterString+0xab>
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<EnterString>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x68,%rsp
mov    %ecx,%ebx
mov    %edx,%ebp
mov    %rsi,%r12
mov    %rdi,%r13
xor    %eax,%eax
call   1080 <key_abort@plt>
mov    $0xffffffff,%r15d
test   %rax,%rax
jne    1294 <EnterString+0x134>
mov    %ebx,0xc(%rsp)
mov    %r12,%rdi
call   1060 <strlen@plt>
mov    %eax,%r14d
cmp    $0x50,%eax
jge    11be <EnterString+0x5e>
lea    0x10(%rsp),%rdi
mov    %r12,%rsi
call   1040 <strcpy@plt>
cmp    $0x1e,%r14d
jge    11c3 <EnterString+0x63>
lea    0xe44(%rip),%rdi
jmp    11ca <EnterString+0x6a>
movb   $0x0,0x10(%rsp)
lea    0xe40(%rip),%rdi
mov    %r13,%rsi
mov    %r12,%rdx
xor    %eax,%eax
call   1070 <printf@plt>
xor    %ebx,%ebx
mov    %r14d,0x8(%rsp)
cmp    %ebp,%r14d
setg   %al
test   %ebx,%ebx
sete   %r14b
and    %al,%r14b
nopl   (%rax)
xor    %eax,%eax
call   1050 <getkeystroke@plt>
mov    %eax,%r13d
xor    %eax,%eax
call   1080 <key_abort@plt>
test   %rax,%rax
jne    1294 <EnterString+0x134>
cmp    $0xa,%r13b
jne    1220 <EnterString+0xc0>
test   %r14b,%r14b
jne    126b <EnterString+0x10b>
cmp    %ebp,%ebx
jl     11f0 <EnterString+0x90>
jmp    127f <EnterString+0x11f>
nopl   0x0(%rax,%rax,1)
cmp    $0x1b,%r13b
je     1284 <EnterString+0x124>
cmp    $0x8,%r13b
jne    1248 <EnterString+0xe8>
test   %ebx,%ebx
mov    0x8(%rsp),%r14d
jle    124d <EnterString+0xed>
add    $0xffffffff,%ebx
lea    0xdd5(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
jmp    124d <EnterString+0xed>
mov    0x8(%rsp),%r14d
cmp    0xc(%rsp),%ebx
jge    11de <EnterString+0x7e>
movsbl %r13b,%edi
call   1030 <putchar@plt>
movslq %ebx,%rax
add    $0x1,%ebx
mov    %r13b,(%r12,%rax,1)
jmp    11de <EnterString+0x7e>
lea    0x10(%rsp),%rsi
mov    %r12,%rdi
call   1040 <strcpy@plt>
mov    0x8(%rsp),%r15d
jmp    128a <EnterString+0x12a>
mov    %ebx,%r15d
jmp    128a <EnterString+0x12a>
mov    $0xffffffff,%r15d
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    %r15d,%eax
add    $0x68,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%s\00"
@.str.1 = [10 x i8] c"%s (%s): \00"
@.str.2 = [10 x i8] c"%s (%s):\0A\00"
@.str.3 = [4 x i8] c"\08 \08\00"
@.str.4 = [2 x i8] c"\0A\00"
@.str.5 = [3 x i8] c"%c\00"
define i32 @EnterString(i8* %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i8
%13 = alloca [80 x i8]
store i8* %0, i8** %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%14 = call i64 (...) @key_abort()
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %4
store i32 -1, i32* %5
br label %105
17: ; preds = %4
%18 = load i8*, i8** %7
%19 = call i32 @strlen(i8* %18)
store i32 %19, i32* %11
%20 = load i32, i32* %11
%21 = icmp slt i32 %20, 80
br i1 %21, label %22, label %26
22: ; preds = %17
%23 = getelementptr inbounds [80 x i8], [80 x i8]* %13, i64 0, i64 0
%24 = load i8*, i8** %7
%25 = call i32 @sprintf(i8* %23, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %24)
br label %28
26: ; preds = %17
%27 = getelementptr inbounds [80 x i8], [80 x i8]* %13, i64 0, i64 0
store i8 0, i8* %27
br label %28
28: ; preds = %26, %22
%29 = load i32, i32* %11
%30 = icmp slt i32 %29, 30
br i1 %30, label %31, label %35
31: ; preds = %28
%32 = load i8*, i8** %6
%33 = load i8*, i8** %7
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %32, i8* %33)
br label %39
35: ; preds = %28
%36 = load i8*, i8** %6
%37 = load i8*, i8** %7
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* %36, i8* %37)
br label %39
39: ; preds = %35, %31
store i32 0, i32* %10
br label %40
40: ; preds = %104, %85, %39
%41 = call signext i8 (...) @getkeystroke()
store i8 %41, i8* %12
%42 = call i64 (...) @key_abort()
%43 = icmp ne i64 %42, 0
br i1 %43, label %44, label %45
44: ; preds = %40
store i32 -1, i32* %5
br label %105
45: ; preds = %40
%46 = load i8, i8* %12
%47 = sext i8 %46 to i32
%48 = icmp eq i32 %47, 8
br i1 %48, label %49, label %57
49: ; preds = %45
%50 = load i32, i32* %10
%51 = icmp sgt i32 %50, 0
br i1 %51, label %52, label %56
52: ; preds = %49
%53 = load i32, i32* %10
%54 = add nsw i32 %53, -1
store i32 %54, i32* %10
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
br label %56
56: ; preds = %52, %49
br label %64
57: ; preds = %45
%58 = load i8, i8* %12
%59 = sext i8 %58 to i32
%60 = icmp eq i32 %59, 27
br i1 %60, label %61, label %63
61: ; preds = %57
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %5
br label %105
63: ; preds = %57
br label %64
64: ; preds = %63, %56
%65 = load i8, i8* %12
%66 = sext i8 %65 to i32
%67 = icmp eq i32 %66, 10
br i1 %67, label %68, label %89
68: ; preds = %64
%69 = load i32, i32* %10
%70 = icmp eq i32 %69, 0
br i1 %70, label %71, label %81
71: ; preds = %68
%72 = load i32, i32* %11
%73 = load i32, i32* %8
%74 = icmp sgt i32 %72, %73
br i1 %74, label %75, label %81
75: ; preds = %71
%76 = load i8*, i8** %7
%77 = getelementptr inbounds [80 x i8], [80 x i8]* %13, i64 0, i64 0
%78 = call i32 @sprintf(i8* %76, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %77)
%79 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%80 = load i32, i32* %11
store i32 %80, i32* %5
br label %105
81: ; preds = %71, %68
%82 = load i32, i32* %10
%83 = load i32, i32* %8
%84 = icmp slt i32 %82, %83
br i1 %84, label %85, label %86
85: ; preds = %81
br label %40
86: ; preds = %81
%87 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%88 = load i32, i32* %10
store i32 %88, i32* %5
br label %105
89: ; preds = %64
%90 = load i32, i32* %10
%91 = load i32, i32* %9
%92 = icmp slt i32 %90, %91
br i1 %92, label %93, label %103
93: ; preds = %89
%94 = load i8, i8* %12
%95 = sext i8 %94 to i32
%96 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32 %95)
%97 = load i8, i8* %12
%98 = load i8*, i8** %7
%99 = load i32, i32* %10
%100 = add nsw i32 %99, 1
store i32 %100, i32* %10
%101 = sext i32 %99 to i64
%102 = getelementptr inbounds i8, i8* %98, i64 %101
store i8 %97, i8* %102
br label %103
103: ; preds = %93, %89
br label %104
104: ; preds = %103
br label %40
105: ; preds = %86, %75, %61, %44, %16
%106 = load i32, i32* %5
ret i32 %106
}

LLVM IR (-O2):
@.str.1 = [10 x i8] c"%s (%s): \00"
@.str.2 = [10 x i8] c"%s (%s):\0A\00"
@.str.3 = [4 x i8] c"\08 \08\00"
define i32 @EnterString(i8* %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca [80 x i8]
%6 = getelementptr inbounds [80 x i8], [80 x i8]* %5, i64 0, i64 0
%7 = tail call i64 (...) @key_abort()
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %54
9: ; preds = %4
%10 = tail call i32 @strlen(i8* %1)
%11 = icmp slt i32 %10, 80
br i1 %11, label %13, label %12
12: ; preds = %9
store i8 0, i8* %6
br label %16
13: ; preds = %9
%14 = call i8* @strcpy(i8* %6, i8* dereferenceable(1) %1)
%15 = icmp slt i32 %10, 30
br i1 %15, label %17, label %16
16: ; preds = %12, %13
br label %17
17: ; preds = %13, %16
%18 = phi i8* [ getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), %16 ], [ getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), %13 ]
%19 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) %18, i8* %0, i8* %1)
%20 = icmp sgt i32 %10, %2
br label %21
21: ; preds = %43, %17
%22 = phi i32 [ 0, %17 ], [ %44, %43 ]
%23 = icmp eq i32 %22, 0
%24 = select i1 %23, i1 %20, i1 false
%25 = icmp slt i32 %22, %2
br label %26
26: ; preds = %21, %39
%27 = call signext i8 (...) @getkeystroke()
%28 = call i64 (...) @key_abort()
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %54
30: ; preds = %26
switch i8 %27, label %40 [
i8 8, label %31
i8 27, label %51
i8 10, label %36
]
31: ; preds = %30
%32 = icmp sgt i32 %22, 0
br i1 %32, label %33, label %40
33: ; preds = %31
%34 = add nsw i32 %22, -1
%35 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
br label %40
36: ; preds = %30
br i1 %24, label %37, label %39
37: ; preds = %36
%38 = call i8* @strcpy(i8* dereferenceable(1) %1, i8* %6)
br label %51
39: ; preds = %36
br i1 %25, label %26, label %51
40: ; preds = %30, %33, %31
%41 = phi i32 [ %22, %31 ], [ %34, %33 ], [ %22, %30 ]
%42 = icmp slt i32 %41, %3
br i1 %42, label %45, label %43
43: ; preds = %40, %45
%44 = phi i32 [ %48, %45 ], [ %41, %40 ]
br label %21
45: ; preds = %40
%46 = sext i8 %27 to i32
%47 = call i32 @putchar(i32 %46)
%48 = add nsw i32 %41, 1
%49 = sext i32 %41 to i64
%50 = getelementptr inbounds i8, i8* %1, i64 %49
store i8 %27, i8* %50
br label %43
51: ; preds = %39, %30, %37
%52 = phi i32 [ %10, %37 ], [ -1, %30 ], [ %22, %39 ]
%53 = call i32 @putchar(i32 10)
br label %54
54: ; preds = %26, %51, %4
%55 = phi i32 [ -1, %4 ], [ %52, %51 ], [ -1, %26 ]
ret i32 %55
}