#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 */
scalar_t__ getkeystroke();
scalar_t__ key_abort();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sprintf(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, buf);
  else
    defbuf[0] = 0;
  if (deflen < 30)
    printf("%s (%s): ", msg, buf);
  else
    printf("%s (%s):\n", msg, buf);
  len = 0;
  for (;;) {
    ch = (char)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    1338 <EnterString+0x1e8>
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x50,-0x28(%rbp)
jge    11b0 <EnterString+0x60>
lea    -0x80(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    $0x0,%al
call   1070 <sprintf@plt>
jmp    11b4 <EnterString+0x64>
movb   $0x0,-0x80(%rbp)
cmpl   $0x1e,-0x28(%rbp)
jge    11d9 <EnterString+0x89>
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    0xe33(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    11ef <EnterString+0x9f>
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    0xe22(%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     121d <EnterString+0xcd>
movl   $0xffffffff,-0x4(%rbp)
jmp    1338 <EnterString+0x1e8>
movsbl -0x29(%rbp),%eax
cmp    $0x8,%eax
jne    1250 <EnterString+0x100>
cmpl   $0x0,-0x24(%rbp)
jle    124b <EnterString+0xfb>
mov    -0x24(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x24(%rbp)
lea    0xdd0(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    127c <EnterString+0x12c>
movsbl -0x29(%rbp),%eax
cmp    $0x1b,%eax
jne    1277 <EnterString+0x127>
lea    0xdae(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1338 <EnterString+0x1e8>
jmp    127c <EnterString+0x12c>
movsbl -0x29(%rbp),%eax
cmp    $0xa,%eax
jne    12f8 <EnterString+0x1a8>
cmpl   $0x0,-0x24(%rbp)
jne    12ce <EnterString+0x17e>
mov    -0x28(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jle    12ce <EnterString+0x17e>
mov    -0x18(%rbp),%rdi
lea    -0x80(%rbp),%rdx
lea    0xd6a(%rip),%rsi
mov    $0x0,%al
call   1070 <sprintf@plt>
lea    0xd56(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1338 <EnterString+0x1e8>
mov    -0x24(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    12df <EnterString+0x18f>
jmp    11f6 <EnterString+0xa6>
lea    0xd2c(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1338 <EnterString+0x1e8>
mov    -0x24(%rbp),%eax
cmp    -0x20(%rbp),%eax
jge    132e <EnterString+0x1de>
movsbl -0x29(%rbp),%esi
lea    0xd0c(%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    1333 <EnterString+0x1e3>
jmp    11f6 <EnterString+0xa6>
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    12bb <EnterString+0x14b>
mov    %ebx,0xc(%rsp)
mov    %r12,%rdi
call   1060 <strlen@plt>
mov    %eax,%r14d
cmp    $0x50,%eax
jge    11d0 <EnterString+0x60>
lea    0x10(%rsp),%rdi
mov    %r12,%rsi
xor    %eax,%eax
call   1090 <sprintf@plt>
cmp    $0x1e,%r14d
jge    11d5 <EnterString+0x65>
lea    0xe32(%rip),%rdi
jmp    11dc <EnterString+0x6c>
movb   $0x0,0x10(%rsp)
lea    0xe2e(%rip),%rdi
mov    %r13,%rsi
mov    %r12,%rdx
xor    %eax,%eax
call   1070 <printf@plt>
xor    %r13d,%r13d
mov    %r14d,0x8(%rsp)
cmp    %ebp,%r14d
setg   %al
test   %r13d,%r13d
sete   %r14b
and    %al,%r14b
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1050 <getkeystroke@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1080 <key_abort@plt>
test   %rax,%rax
jne    12bb <EnterString+0x14b>
cmp    $0xa,%bl
jne    1240 <EnterString+0xd0>
test   %r14b,%r14b
jne    1292 <EnterString+0x122>
cmp    %ebp,%r13d
jl     1210 <EnterString+0xa0>
jmp    12a6 <EnterString+0x136>
nopl   0x0(%rax,%rax,1)
cmp    $0x1b,%bl
je     12ab <EnterString+0x13b>
cmp    $0x8,%bl
jne    1268 <EnterString+0xf8>
test   %r13d,%r13d
mov    0x8(%rsp),%r14d
jle    126d <EnterString+0xfd>
add    $0xffffffff,%r13d
lea    0xdb5(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
jmp    126d <EnterString+0xfd>
mov    0x8(%rsp),%r14d
cmp    0xc(%rsp),%r13d
jge    11f1 <EnterString+0x81>
movsbl %bl,%ebx
mov    %ebx,%edi
call   1030 <putchar@plt>
movslq %r13d,%rax
add    $0x1,%r13d
mov    %bl,(%r12,%rax,1)
jmp    11f1 <EnterString+0x81>
lea    0x10(%rsp),%rsi
mov    %r12,%rdi
call   1040 <strcpy@plt>
mov    0x8(%rsp),%r15d
jmp    12b1 <EnterString+0x141>
mov    %r13d,%r15d
jmp    12b1 <EnterString+0x141>
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 = [10 x i8] c"%s (%s): \00"
@.str.1 = [10 x i8] c"%s (%s):\0A\00"
@.str.2 = [4 x i8] c"\08 \08\00"
@.str.3 = [2 x i8] c"\0A\00"
@.str.4 = [3 x i8] c"%s\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 %106
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 (i8*, i8*, ...) @sprintf(i8* %23, 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, 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.1, 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 = %105, %86, %39
%41 = call i64 (...) @getkeystroke()
%42 = trunc i64 %41 to i8
store i8 %42, i8* %12
%43 = call i64 (...) @key_abort()
%44 = icmp ne i64 %43, 0
br i1 %44, label %45, label %46
45: ; preds = %40
store i32 -1, i32* %5
br label %106
46: ; preds = %40
%47 = load i8, i8* %12
%48 = sext i8 %47 to i32
%49 = icmp eq i32 %48, 8
br i1 %49, label %50, label %58
50: ; preds = %46
%51 = load i32, i32* %10
%52 = icmp sgt i32 %51, 0
br i1 %52, label %53, label %57
53: ; preds = %50
%54 = load i32, i32* %10
%55 = add nsw i32 %54, -1
store i32 %55, i32* %10
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
br label %57
57: ; preds = %53, %50
br label %65
58: ; preds = %46
%59 = load i8, i8* %12
%60 = sext i8 %59 to i32
%61 = icmp eq i32 %60, 27
br i1 %61, label %62, label %64
62: ; preds = %58
%63 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %5
br label %106
64: ; preds = %58
br label %65
65: ; preds = %64, %57
%66 = load i8, i8* %12
%67 = sext i8 %66 to i32
%68 = icmp eq i32 %67, 10
br i1 %68, label %69, label %90
69: ; preds = %65
%70 = load i32, i32* %10
%71 = icmp eq i32 %70, 0
br i1 %71, label %72, label %82
72: ; preds = %69
%73 = load i32, i32* %11
%74 = load i32, i32* %8
%75 = icmp sgt i32 %73, %74
br i1 %75, label %76, label %82
76: ; preds = %72
%77 = load i8*, i8** %7
%78 = getelementptr inbounds [80 x i8], [80 x i8]* %13, i64 0, i64 0
%79 = call i32 (i8*, i8*, ...) @sprintf(i8* %77, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %78)
%80 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%81 = load i32, i32* %11
store i32 %81, i32* %5
br label %106
82: ; preds = %72, %69
%83 = load i32, i32* %10
%84 = load i32, i32* %8
%85 = icmp slt i32 %83, %84
br i1 %85, label %86, label %87
86: ; preds = %82
br label %40
87: ; preds = %82
%88 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%89 = load i32, i32* %10
store i32 %89, i32* %5
br label %106
90: ; preds = %65
%91 = load i32, i32* %10
%92 = load i32, i32* %9
%93 = icmp slt i32 %91, %92
br i1 %93, label %94, label %104
94: ; preds = %90
%95 = load i8, i8* %12
%96 = sext i8 %95 to i32
%97 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32 %96)
%98 = load i8, i8* %12
%99 = load i8*, i8** %7
%100 = load i32, i32* %10
%101 = add nsw i32 %100, 1
store i32 %101, i32* %10
%102 = sext i32 %100 to i64
%103 = getelementptr inbounds i8, i8* %99, i64 %102
store i8 %98, i8* %103
br label %104
104: ; preds = %94, %90
br label %105
105: ; preds = %104
br label %40
106: ; preds = %87, %76, %62, %45, %16
%107 = load i32, i32* %5
ret i32 %107
}

LLVM IR (-O2):
@.str = [10 x i8] c"%s (%s): \00"
@.str.1 = [10 x i8] c"%s (%s):\0A\00"
@.str.2 = [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 %56
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 i32 (i8*, i8*, ...) @sprintf(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.1, i64 0, i64 0), %16 ], [ getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), %13 ]
%19 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) %18, i8* %0, i8* %1)
%20 = icmp sgt i32 %10, %2
br label %21
21: ; preds = %45, %17
%22 = phi i32 [ 0, %17 ], [ %46, %45 ]
%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, %40
%27 = tail call i64 (...) @getkeystroke()
%28 = tail call i64 (...) @key_abort()
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %56
30: ; preds = %26
%31 = trunc i64 %27 to i8
switch i8 %31, label %41 [
i8 8, label %32
i8 27, label %53
i8 10, label %37
]
32: ; preds = %30
%33 = icmp sgt i32 %22, 0
br i1 %33, label %34, label %41
34: ; preds = %32
%35 = add nsw i32 %22, -1
%36 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
br label %41
37: ; preds = %30
br i1 %24, label %38, label %40
38: ; preds = %37
%39 = call i8* @strcpy(i8* dereferenceable(1) %1, i8* %6)
br label %53
40: ; preds = %37
br i1 %25, label %26, label %53
41: ; preds = %30, %34, %32
%42 = phi i32 [ %22, %32 ], [ %35, %34 ], [ %22, %30 ]
%43 = trunc i64 %27 to i8
%44 = icmp slt i32 %42, %3
br i1 %44, label %47, label %45
45: ; preds = %41, %47
%46 = phi i32 [ %50, %47 ], [ %42, %41 ]
br label %21
47: ; preds = %41
%48 = sext i8 %43 to i32
%49 = tail call i32 @putchar(i32 %48)
%50 = add nsw i32 %42, 1
%51 = sext i32 %42 to i64
%52 = getelementptr inbounds i8, i8* %1, i64 %51
store i8 %43, i8* %52
br label %45
53: ; preds = %40, %30, %38
%54 = phi i32 [ %10, %38 ], [ -1, %30 ], [ %22, %40 ]
%55 = tail call i32 @putchar(i32 10)
br label %56
56: ; preds = %26, %53, %4
%57 = phi i32 [ -1, %4 ], [ %54, %53 ], [ -1, %26 ]
ret i32 %57
}