#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 */
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ call_gui_input_string_draw(char const *, char *, long);
int /*<<< orphan*/ call_gui_input_string_wipe();
long get_key_buf_char();
long min_l(long, int);
scalar_t__ read_char(long *);
int /*<<< orphan*/ str_nz_cpy(char *, char *, long);
void game_gets_gui_sub(const char *ttl, char *s, long len) {
  static char buf[1023 + 1];
  long c;
  long n;
  len = min_l(len, (sizeof(buf) / sizeof(buf[0]) - 1 - 1));
  buf[0] = '\0';
  c = '\0';
  n = 0;
  call_gui_input_string_draw(ttl, s, len);
  while (1) {
    c = get_key_buf_char();
    if (c == '\0')
      if (read_char(&c) <= 0)
        continue;
    if (c == '\0')
      continue;
    if (c == ('H' - '@')) {
      n--;
      if (n < 0)
        n = 0;
      continue;
    }
    if (c == '\n')
      ;
    if (c == '\r')
      break;
    if (n < len)
      buf[n] = c;
    n++;
  }
  if (n > (len - 1))
    n = len - 1;
  if (n >= 0)
    buf[n] = '\n';
  n++;
  if (n > len)
    n = len;
  buf[n] = '\0';
  str_nz_cpy(s, buf, len);
  call_gui_input_string_wipe();
}


x86 assembly (clang, -O0):
<game_gets_gui_sub>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    $0x3fe,%esi
call   1030 <min_l@plt>
mov    %rax,-0x18(%rbp)
movb   $0x0,0x2ed3(%rip)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <call_gui_input_string_draw@plt>
mov    $0x0,%al
call   1070 <get_key_buf_char@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11e1 <game_gets_gui_sub+0x81>
lea    -0x20(%rbp),%rdi
call   1060 <read_char@plt>
cmp    $0x0,%rax
jg     11dc <game_gets_gui_sub+0x7c>
jmp    11ae <game_gets_gui_sub+0x4e>
jmp    11e1 <game_gets_gui_sub+0x81>
cmpq   $0x0,-0x20(%rbp)
jne    11f1 <game_gets_gui_sub+0x91>
jmp    11ae <game_gets_gui_sub+0x4e>
cmpq   $0x8,-0x20(%rbp)
jne    1220 <game_gets_gui_sub+0xc0>
mov    -0x28(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jge    121b <game_gets_gui_sub+0xbb>
movq   $0x0,-0x28(%rbp)
jmp    11ae <game_gets_gui_sub+0x4e>
cmpq   $0xa,-0x20(%rbp)
jne    1230 <game_gets_gui_sub+0xd0>
jmp    1230 <game_gets_gui_sub+0xd0>
cmpq   $0xd,-0x20(%rbp)
jne    1240 <game_gets_gui_sub+0xe0>
jmp    1273 <game_gets_gui_sub+0x113>
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
jge    1262 <game_gets_gui_sub+0x102>
mov    -0x20(%rbp),%rax
mov    %al,%dl
mov    -0x28(%rbp),%rcx
lea    0x2e01(%rip),%rax
mov    %dl,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    11ae <game_gets_gui_sub+0x4e>
mov    -0x28(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    $0x1,%rcx
cmp    %rcx,%rax
jle    1294 <game_gets_gui_sub+0x134>
mov    -0x18(%rbp),%rax
sub    $0x1,%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jl     12ae <game_gets_gui_sub+0x14e>
mov    -0x28(%rbp),%rcx
lea    0x2db6(%rip),%rax
movb   $0xa,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
jle    12d0 <game_gets_gui_sub+0x170>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rcx
lea    0x2d85(%rip),%rax
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0x2d72(%rip),%rsi
call   1050 <str_nz_cpy@plt>
mov    $0x0,%al
call   1080 <call_gui_input_string_wipe@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<game_gets_gui_sub>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
mov    %rdx,%rdi
mov    $0x3fe,%esi
call   1030 <min_l@plt>
mov    %rax,%r12
movb   $0x0,0x2ed8(%rip)
movq   $0x0,(%rsp)
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %rax,%rdx
call   1040 <call_gui_input_string_draw@plt>
mov    %rsp,%rbx
mov    $0x1,%ebp
lea    0x2eb3(%rip),%r15
xor    %r13d,%r13d
jmp    11c0 <game_gets_gui_sub+0x60>
add    $0x1,%r13
cs nopw 0x0(%rax,%rax,1)
xor    %eax,%eax
call   1070 <get_key_buf_char@plt>
mov    %rax,(%rsp)
test   %rax,%rax
jne    11e6 <game_gets_gui_sub+0x86>
mov    %rbx,%rdi
call   1060 <read_char@plt>
test   %rax,%rax
jle    11c0 <game_gets_gui_sub+0x60>
mov    (%rsp),%rax
test   %rax,%rax
je     11c0 <game_gets_gui_sub+0x60>
cmp    $0x8,%rax
je     11fe <game_gets_gui_sub+0x9e>
cmp    $0xd,%rax
je     120b <game_gets_gui_sub+0xab>
cmp    %r12,%r13
jge    11b2 <game_gets_gui_sub+0x52>
mov    %al,0x0(%r13,%r15,1)
jmp    11b2 <game_gets_gui_sub+0x52>
test   %r13,%r13
cmovle %rbp,%r13
add    $0xffffffffffffffff,%r13
jmp    11c0 <game_gets_gui_sub+0x60>
lea    -0x1(%r12),%rax
cmp    %r12,%r13
cmovl  %r13,%rax
test   %rax,%rax
js     1221 <game_gets_gui_sub+0xc1>
movb   $0xa,(%rax,%r15,1)
movb   $0x0,0x1(%rax,%r15,1)
mov    %r14,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
call   1050 <str_nz_cpy@plt>
xor    %eax,%eax
call   1080 <call_gui_input_string_wipe@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@game_gets_gui_sub.buf = internal global [1024 x i8] zeroinitializer
define void @game_gets_gui_sub(i8* %0, i8* %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
store i8* %0, i8** %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%9 = load i64, i64* %6
%10 = call i64 @min_l(i64 %9, i32 1022)
store i64 %10, i64* %6
store i8 0, i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @game_gets_gui_sub.buf, i64 0, i64 0)
store i64 0, i64* %7
store i64 0, i64* %8
%11 = load i8*, i8** %4
%12 = load i8*, i8** %5
%13 = load i64, i64* %6
%14 = call i32 @call_gui_input_string_draw(i8* %11, i8* %12, i64 %13)
br label %15
15: ; preds = %3, %22, %27, %37, %55
%16 = call i64 (...) @get_key_buf_char()
store i64 %16, i64* %7
%17 = load i64, i64* %7
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %24
19: ; preds = %15
%20 = call i64 @read_char(i64* %7)
%21 = icmp sle i64 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %19
br label %15
23: ; preds = %19
br label %24
24: ; preds = %23, %15
%25 = load i64, i64* %7
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %24
br label %15
28: ; preds = %24
%29 = load i64, i64* %7
%30 = icmp eq i64 %29, 8
br i1 %30, label %31, label %38
31: ; preds = %28
%32 = load i64, i64* %8
%33 = add nsw i64 %32, -1
store i64 %33, i64* %8
%34 = load i64, i64* %8
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %37
36: ; preds = %31
store i64 0, i64* %8
br label %37
37: ; preds = %36, %31
br label %15
38: ; preds = %28
%39 = load i64, i64* %7
%40 = icmp eq i64 %39, 10
br i1 %40, label %41, label %42
41: ; preds = %38
br label %42
42: ; preds = %41, %38
%43 = load i64, i64* %7
%44 = icmp eq i64 %43, 13
br i1 %44, label %45, label %46
45: ; preds = %42
br label %58
46: ; preds = %42
%47 = load i64, i64* %8
%48 = load i64, i64* %6
%49 = icmp slt i64 %47, %48
br i1 %49, label %50, label %55
50: ; preds = %46
%51 = load i64, i64* %7
%52 = trunc i64 %51 to i8
%53 = load i64, i64* %8
%54 = getelementptr inbounds [1024 x i8], [1024 x i8]* @game_gets_gui_sub.buf, i64 0, i64 %53
store i8 %52, i8* %54
br label %55
55: ; preds = %50, %46
%56 = load i64, i64* %8
%57 = add nsw i64 %56, 1
store i64 %57, i64* %8
br label %15
58: ; preds = %45
%59 = load i64, i64* %8
%60 = load i64, i64* %6
%61 = sub nsw i64 %60, 1
%62 = icmp sgt i64 %59, %61
br i1 %62, label %63, label %66
63: ; preds = %58
%64 = load i64, i64* %6
%65 = sub nsw i64 %64, 1
store i64 %65, i64* %8
br label %66
66: ; preds = %63, %58
%67 = load i64, i64* %8
%68 = icmp sge i64 %67, 0
br i1 %68, label %69, label %72
69: ; preds = %66
%70 = load i64, i64* %8
%71 = getelementptr inbounds [1024 x i8], [1024 x i8]* @game_gets_gui_sub.buf, i64 0, i64 %70
store i8 10, i8* %71
br label %72
72: ; preds = %69, %66
%73 = load i64, i64* %8
%74 = add nsw i64 %73, 1
store i64 %74, i64* %8
%75 = load i64, i64* %8
%76 = load i64, i64* %6
%77 = icmp sgt i64 %75, %76
br i1 %77, label %78, label %80
78: ; preds = %72
%79 = load i64, i64* %6
store i64 %79, i64* %8
br label %80
80: ; preds = %78, %72
%81 = load i64, i64* %8
%82 = getelementptr inbounds [1024 x i8], [1024 x i8]* @game_gets_gui_sub.buf, i64 0, i64 %81
store i8 0, i8* %82
%83 = load i8*, i8** %5
%84 = load i64, i64* %6
%85 = call i32 @str_nz_cpy(i8* %83, i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @game_gets_gui_sub.buf, i64 0, i64 0), i64 %84)
%86 = call i32 (...) @call_gui_input_string_wipe()
ret void
}

LLVM IR (-O2):
@game_gets_gui_sub.buf = internal global [1024 x i8] zeroinitializer
define void @game_gets_gui_sub(i8* %0, i8* %1, i64 %2) {
%4 = alloca i64
%5 = bitcast i64* %4 to i8*
%6 = tail call i64 @min_l(i64 %2, i32 1022)
store i8 0, i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @game_gets_gui_sub.buf, i64 0, i64 0)
store i64 0, i64* %4
%7 = tail call i32 @call_gui_input_string_draw(i8* %0, i8* %1, i64 %6)
br label %8
8: ; preds = %25, %3
%9 = phi i64 [ 0, %3 ], [ %26, %25 ]
br label %10
10: ; preds = %8, %13
%11 = call i64 (...) @get_key_buf_char()
store i64 %11, i64* %4
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %19
13: ; preds = %10
%14 = call i64 @read_char(i64* %4)
%15 = icmp slt i64 %14, 1
%16 = load i64, i64* %4
%17 = icmp eq i64 %16, 0
%18 = select i1 %15, i1 true, i1 %17
br i1 %18, label %10, label %19
19: ; preds = %10, %13
%20 = phi i64 [ %11, %10 ], [ %16, %13 ]
switch i64 %20, label %27 [
i64 8, label %21
i64 13, label %34
]
21: ; preds = %19
%22 = icmp sgt i64 %9, 1
%23 = select i1 %22, i64 %9, i64 1
%24 = add nsw i64 %23, -1
br label %25
25: ; preds = %21, %32
%26 = phi i64 [ %33, %32 ], [ %24, %21 ]
br label %8
27: ; preds = %19
%28 = icmp slt i64 %9, %6
br i1 %28, label %29, label %32
29: ; preds = %27
%30 = trunc i64 %20 to i8
%31 = getelementptr inbounds [1024 x i8], [1024 x i8]* @game_gets_gui_sub.buf, i64 0, i64 %9
store i8 %30, i8* %31
br label %32
32: ; preds = %29, %27
%33 = add nsw i64 %9, 1
br label %25
34: ; preds = %19
%35 = icmp slt i64 %9, %6
%36 = add nsw i64 %6, -1
%37 = select i1 %35, i64 %9, i64 %36
%38 = icmp sgt i64 %37, -1
br i1 %38, label %39, label %41
39: ; preds = %34
%40 = getelementptr inbounds [1024 x i8], [1024 x i8]* @game_gets_gui_sub.buf, i64 0, i64 %37
store i8 10, i8* %40
br label %41
41: ; preds = %39, %34
%42 = add nsw i64 %37, 1
%43 = getelementptr inbounds [1024 x i8], [1024 x i8]* @game_gets_gui_sub.buf, i64 0, i64 %42
store i8 0, i8* %43
%44 = call i32 @str_nz_cpy(i8* %1, i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @game_gets_gui_sub.buf, i64 0, i64 0), i64 %6)
%45 = call i32 (...) @call_gui_input_string_wipe()
ret void
}