#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 */
int KEY_BACKSPACE;
int /*<<< orphan*/ adjust_scroll(int);
int /*<<< orphan*/ bufdelete(char *, char *);
char *bufinsert(int, char *);
int /*<<< orphan*/ clrscreen();
int /*<<< orphan*/ draw_eof();
int /*<<< orphan*/ drawmodeline(char *, char *);
int /*<<< orphan*/ drawtext();
char *getbufstart();
int getch();
int /*<<< orphan*/ isgraph(int);
int /*<<< orphan*/ isspace(int);
int /*<<< orphan*/ movecursor(char *);
int /*<<< orphan*/ present();
scalar_t__ recdelete(char *, char *);
scalar_t__ recinsert(char *, char *);
int /*<<< orphan*/ refresh_bounds();
scalar_t__ ruboutword(char **);
char *winend();
int winrows();
int insertmode(char *filename, char *t) {
  int c;
  for (;;) {
    refresh_bounds();
    if (t > winend())
      adjust_scroll(winrows() / 2);
    clrscreen();
    drawtext();
    draw_eof();
    drawmodeline(filename, "INSERT");
    movecursor(t);
    present();
    c = getch();
    if (c == '\r')
      c = '\n';
    if (c == 4 || c == 27)
      break;
    if (c == KEY_BACKSPACE || c == 127) {
      if (t <= getbufstart())
        continue;
      t--;
      if (recdelete(t, t + 1) < 0)
        return -1;
      bufdelete(t, t + 1);
      continue;
    }
    if (c == 23) {
      if (t <= getbufstart())
        continue;
      t--;
      if (ruboutword(&t) < 0)
        return -1;
      continue;
    }
    if (!isgraph(c) && !isspace(c)) {
      continue;
    }
    if (!(t = bufinsert(c, t)))
      return -1;
    if (recinsert(t, t + 1) < 0)
      return -1;
    else
      t++;
  }
  return 0;
}


x86 assembly (clang, -O0):
<insertmode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    $0x0,%al
call   10b0 <refresh_bounds@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    $0x0,%al
call   1140 <winend@plt>
mov    %rax,%rcx
mov    -0x28(%rbp),%rax
cmp    %rcx,%rax
jbe    127c <insertmode+0x4c>
mov    $0x0,%al
call   1130 <winrows@plt>
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%edi
call   1110 <adjust_scroll@plt>
mov    $0x0,%al
call   1070 <clrscreen@plt>
mov    $0x0,%al
call   1060 <drawtext@plt>
mov    $0x0,%al
call   1050 <draw_eof@plt>
mov    -0x10(%rbp),%rdi
lea    0xd64(%rip),%rsi
call   1040 <drawmodeline@plt>
mov    -0x18(%rbp),%rdi
call   1030 <movecursor@plt>
mov    $0x0,%al
call   10e0 <present@plt>
mov    $0x0,%al
call   1100 <getch@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xd,-0x1c(%rbp)
jne    12cc <insertmode+0x9c>
movl   $0xa,-0x1c(%rbp)
cmpl   $0x4,-0x1c(%rbp)
je     12e0 <insertmode+0xb0>
cmpl   $0x1b,-0x1c(%rbp)
jne    12e5 <insertmode+0xb5>
jmp    1451 <insertmode+0x221>
mov    -0x1c(%rbp),%eax
mov    0x2cf9(%rip),%rcx
cmp    (%rcx),%eax
je     1301 <insertmode+0xd1>
cmpl   $0x7f,-0x1c(%rbp)
jne    136e <insertmode+0x13e>
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    $0x0,%al
call   10a0 <getbufstart@plt>
mov    %rax,%rcx
mov    -0x30(%rbp),%rax
cmp    %rcx,%rax
ja     1325 <insertmode+0xf5>
jmp    1240 <insertmode+0x10>
mov    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x18(%rbp),%rsi
add    $0x1,%rsi
call   1080 <recdelete@plt>
cmp    $0x0,%rax
jge    1358 <insertmode+0x128>
movl   $0xffffffff,-0x4(%rbp)
jmp    1458 <insertmode+0x228>
mov    -0x18(%rbp),%rdi
mov    -0x18(%rbp),%rsi
add    $0x1,%rsi
call   10c0 <bufdelete@plt>
jmp    1240 <insertmode+0x10>
cmpl   $0x17,-0x1c(%rbp)
jne    13cc <insertmode+0x19c>
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    $0x0,%al
call   10a0 <getbufstart@plt>
mov    %rax,%rcx
mov    -0x38(%rbp),%rax
cmp    %rcx,%rax
ja     139c <insertmode+0x16c>
jmp    1240 <insertmode+0x10>
mov    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
lea    -0x18(%rbp),%rdi
call   10d0 <ruboutword@plt>
cmp    $0x0,%rax
jge    13c7 <insertmode+0x197>
movl   $0xffffffff,-0x4(%rbp)
jmp    1458 <insertmode+0x228>
jmp    1240 <insertmode+0x10>
mov    -0x1c(%rbp),%edi
call   1150 <isgraph@plt>
cmp    $0x0,%eax
jne    13f3 <insertmode+0x1c3>
mov    -0x1c(%rbp),%edi
call   1090 <isspace@plt>
cmp    $0x0,%eax
jne    13f3 <insertmode+0x1c3>
jmp    1240 <insertmode+0x10>
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%rsi
call   1120 <bufinsert@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1419 <insertmode+0x1e9>
movl   $0xffffffff,-0x4(%rbp)
jmp    1458 <insertmode+0x228>
mov    -0x18(%rbp),%rdi
mov    -0x18(%rbp),%rsi
add    $0x1,%rsi
call   10f0 <recinsert@plt>
cmp    $0x0,%rax
jge    1440 <insertmode+0x210>
movl   $0xffffffff,-0x4(%rbp)
jmp    1458 <insertmode+0x228>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1240 <insertmode+0x10>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<insertmode>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r12
mov    %rsi,(%rsp)
lea    0xdb7(%rip),%r14
mov    0x2d98(%rip),%r13
mov    %rsp,%r15
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
xor    %eax,%eax
call   10b0 <refresh_bounds@plt>
mov    (%rsp),%rbx
xor    %eax,%eax
call   1140 <winend@plt>
cmp    %rax,%rbx
jbe    128c <insertmode+0x5c>
xor    %eax,%eax
call   1130 <winrows@plt>
mov    %eax,%edi
shr    $0x1f,%edi
add    %eax,%edi
sar    %edi
call   1110 <adjust_scroll@plt>
xor    %ebp,%ebp
xor    %eax,%eax
call   1070 <clrscreen@plt>
xor    %eax,%eax
call   1060 <drawtext@plt>
xor    %eax,%eax
call   1050 <draw_eof@plt>
mov    %r12,%rdi
mov    %r14,%rsi
call   1040 <drawmodeline@plt>
mov    (%rsp),%rdi
call   1030 <movecursor@plt>
xor    %eax,%eax
call   10e0 <present@plt>
xor    %eax,%eax
call   1100 <getch@plt>
mov    $0xa,%ebx
cmp    $0xd,%eax
je     12d1 <insertmode+0xa1>
mov    %eax,%ebx
cmp    $0x4,%ebx
je     13c6 <insertmode+0x196>
cmp    $0x1b,%ebx
je     13c6 <insertmode+0x196>
cmp    $0x7f,%ebx
je     1330 <insertmode+0x100>
cmp    0x0(%r13),%ebx
je     1330 <insertmode+0x100>
cmp    $0x17,%ebx
jne    1370 <insertmode+0x140>
mov    (%rsp),%rbx
xor    %eax,%eax
call   10a0 <getbufstart@plt>
cmp    %rax,%rbx
jbe    1260 <insertmode+0x30>
addq   $0xffffffffffffffff,(%rsp)
mov    %r15,%rdi
call   10d0 <ruboutword@plt>
test   %rax,%rax
jns    1260 <insertmode+0x30>
jmp    13c1 <insertmode+0x191>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    (%rsp),%rbx
xor    %eax,%eax
call   10a0 <getbufstart@plt>
cmp    %rax,%rbx
jbe    1260 <insertmode+0x30>
mov    (%rsp),%rsi
lea    -0x1(%rsi),%rdi
mov    %rdi,(%rsp)
call   1080 <recdelete@plt>
test   %rax,%rax
js     13c1 <insertmode+0x191>
mov    (%rsp),%rdi
lea    0x1(%rdi),%rsi
call   10c0 <bufdelete@plt>
jmp    1260 <insertmode+0x30>
nopl   0x0(%rax)
mov    %ebx,%edi
call   1150 <isgraph@plt>
test   %eax,%eax
jne    138a <insertmode+0x15a>
mov    %ebx,%edi
call   1090 <isspace@plt>
test   %eax,%eax
je     1260 <insertmode+0x30>
mov    (%rsp),%rsi
mov    %ebx,%edi
call   1120 <bufinsert@plt>
mov    %rax,(%rsp)
mov    $0xffffffff,%ebp
test   %rax,%rax
je     13c6 <insertmode+0x196>
mov    %rax,%rsi
add    $0x1,%rsi
mov    %rax,%rdi
call   10f0 <recinsert@plt>
test   %rax,%rax
js     13c6 <insertmode+0x196>
addq   $0x1,(%rsp)
jmp    1260 <insertmode+0x30>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [7 x i8] c"INSERT\00"
@KEY_BACKSPACE = global i32 0
define i32 @insertmode(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
br label %7
7: ; preds = %102, %85, %76, %69, %57, %47, %2
%8 = call i32 (...) @refresh_bounds()
%9 = load i8*, i8** %5
%10 = call i8* (...) @winend()
%11 = icmp ugt i8* %9, %10
br i1 %11, label %12, label %16
12: ; preds = %7
%13 = call i32 (...) @winrows()
%14 = sdiv i32 %13, 2
%15 = call i32 @adjust_scroll(i32 %14)
br label %16
16: ; preds = %12, %7
%17 = call i32 (...) @clrscreen()
%18 = call i32 (...) @drawtext()
%19 = call i32 (...) @draw_eof()
%20 = load i8*, i8** %4
%21 = call i32 @drawmodeline(i8* %20, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%22 = load i8*, i8** %5
%23 = call i32 @movecursor(i8* %22)
%24 = call i32 (...) @present()
%25 = call i32 (...) @getch()
store i32 %25, i32* %6
%26 = load i32, i32* %6
%27 = icmp eq i32 %26, 13
br i1 %27, label %28, label %29
28: ; preds = %16
store i32 10, i32* %6
br label %29
29: ; preds = %28, %16
%30 = load i32, i32* %6
%31 = icmp eq i32 %30, 4
br i1 %31, label %35, label %32
32: ; preds = %29
%33 = load i32, i32* %6
%34 = icmp eq i32 %33, 27
br i1 %34, label %35, label %36
35: ; preds = %32, %29
br label %103
36: ; preds = %32
%37 = load i32, i32* %6
%38 = load i32, i32* @KEY_BACKSPACE
%39 = icmp eq i32 %37, %38
br i1 %39, label %43, label %40
40: ; preds = %36
%41 = load i32, i32* %6
%42 = icmp eq i32 %41, 127
br i1 %42, label %43, label %62
43: ; preds = %40, %36
%44 = load i8*, i8** %5
%45 = call i8* (...) @getbufstart()
%46 = icmp ule i8* %44, %45
br i1 %46, label %47, label %48
47: ; preds = %43
br label %7
48: ; preds = %43
%49 = load i8*, i8** %5
%50 = getelementptr inbounds i8, i8* %49, i32 -1
store i8* %50, i8** %5
%51 = load i8*, i8** %5
%52 = load i8*, i8** %5
%53 = getelementptr inbounds i8, i8* %52, i64 1
%54 = call i64 @recdelete(i8* %51, i8* %53)
%55 = icmp slt i64 %54, 0
br i1 %55, label %56, label %57
56: ; preds = %48
store i32 -1, i32* %3
br label %104
57: ; preds = %48
%58 = load i8*, i8** %5
%59 = load i8*, i8** %5
%60 = getelementptr inbounds i8, i8* %59, i64 1
%61 = call i32 @bufdelete(i8* %58, i8* %60)
br label %7
62: ; preds = %40
%63 = load i32, i32* %6
%64 = icmp eq i32 %63, 23
br i1 %64, label %65, label %77
65: ; preds = %62
%66 = load i8*, i8** %5
%67 = call i8* (...) @getbufstart()
%68 = icmp ule i8* %66, %67
br i1 %68, label %69, label %70
69: ; preds = %65
br label %7
70: ; preds = %65
%71 = load i8*, i8** %5
%72 = getelementptr inbounds i8, i8* %71, i32 -1
store i8* %72, i8** %5
%73 = call i64 @ruboutword(i8** %5)
%74 = icmp slt i64 %73, 0
br i1 %74, label %75, label %76
75: ; preds = %70
store i32 -1, i32* %3
br label %104
76: ; preds = %70
br label %7
77: ; preds = %62
%78 = load i32, i32* %6
%79 = call i32 @isgraph(i32 %78)
%80 = icmp ne i32 %79, 0
br i1 %80, label %86, label %81
81: ; preds = %77
%82 = load i32, i32* %6
%83 = call i32 @isspace(i32 %82)
%84 = icmp ne i32 %83, 0
br i1 %84, label %86, label %85
85: ; preds = %81
br label %7
86: ; preds = %81, %77
%87 = load i32, i32* %6
%88 = load i8*, i8** %5
%89 = call i8* @bufinsert(i32 %87, i8* %88)
store i8* %89, i8** %5
%90 = icmp ne i8* %89, null
br i1 %90, label %92, label %91
91: ; preds = %86
store i32 -1, i32* %3
br label %104
92: ; preds = %86
%93 = load i8*, i8** %5
%94 = load i8*, i8** %5
%95 = getelementptr inbounds i8, i8* %94, i64 1
%96 = call i64 @recinsert(i8* %93, i8* %95)
%97 = icmp slt i64 %96, 0
br i1 %97, label %98, label %99
98: ; preds = %92
store i32 -1, i32* %3
br label %104
99: ; preds = %92
%100 = load i8*, i8** %5
%101 = getelementptr inbounds i8, i8* %100, i32 1
store i8* %101, i8** %5
br label %102
102: ; preds = %99
br label %7
103: ; preds = %35
store i32 0, i32* %3
br label %104
104: ; preds = %103, %98, %91, %75, %56
%105 = load i32, i32* %3
ret i32 %105
}

LLVM IR (-O2):
@.str = [7 x i8] c"INSERT\00"
@KEY_BACKSPACE = global i32 0
define i32 @insertmode(i8* %0, i8* %1) {
%3 = alloca i8*
store i8* %1, i8** %3
br label %4
4: ; preds = %42, %2
%5 = call i32 (...) @refresh_bounds()
%6 = load i8*, i8** %3
%7 = call i8* (...) @winend()
%8 = icmp ugt i8* %6, %7
br i1 %8, label %9, label %13
9: ; preds = %4
%10 = call i32 (...) @winrows()
%11 = sdiv i32 %10, 2
%12 = call i32 @adjust_scroll(i32 %11)
br label %13
13: ; preds = %9, %4
%14 = call i32 (...) @clrscreen()
%15 = call i32 (...) @drawtext()
%16 = call i32 (...) @draw_eof()
%17 = call i32 @drawmodeline(i8* %0, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%18 = load i8*, i8** %3
%19 = call i32 @movecursor(i8* %18)
%20 = call i32 (...) @present()
%21 = call i32 (...) @getch()
%22 = icmp eq i32 %21, 13
%23 = select i1 %22, i32 10, i32 %21
switch i32 %23, label %24 [
i32 27, label %71
i32 4, label %71
]
24: ; preds = %13
%25 = load i32, i32* @KEY_BACKSPACE
%26 = icmp eq i32 %23, %25
%27 = icmp eq i32 %23, 127
%28 = or i1 %27, %26
br i1 %28, label %29, label %43
29: ; preds = %24
%30 = load i8*, i8** %3
%31 = call i8* (...) @getbufstart()
%32 = icmp ugt i8* %30, %31
br i1 %32, label %33, label %42
33: ; preds = %29
%34 = load i8*, i8** %3
%35 = getelementptr inbounds i8, i8* %34, i64 -1
store i8* %35, i8** %3
%36 = call i64 @recdelete(i8* %35, i8* %34)
%37 = icmp slt i64 %36, 0
br i1 %37, label %71, label %38
38: ; preds = %33
%39 = load i8*, i8** %3
%40 = getelementptr inbounds i8, i8* %39, i64 1
%41 = call i32 @bufdelete(i8* %39, i8* %40)
br label %42
42: ; preds = %38, %68, %29, %45, %49, %57
br label %4
43: ; preds = %24
%44 = icmp eq i32 %23, 23
br i1 %44, label %45, label %54
45: ; preds = %43
%46 = load i8*, i8** %3
%47 = call i8* (...) @getbufstart()
%48 = icmp ugt i8* %46, %47
br i1 %48, label %49, label %42
49: ; preds = %45
%50 = load i8*, i8** %3
%51 = getelementptr inbounds i8, i8* %50, i64 -1
store i8* %51, i8** %3
%52 = call i64 @ruboutword(i8** %3)
%53 = icmp slt i64 %52, 0
br i1 %53, label %71, label %42
54: ; preds = %43
%55 = call i32 @isgraph(i32 %23)
%56 = icmp eq i32 %55, 0
br i1 %56, label %57, label %60
57: ; preds = %54
%58 = call i32 @isspace(i32 %23)
%59 = icmp eq i32 %58, 0
br i1 %59, label %42, label %60
60: ; preds = %57, %54
%61 = load i8*, i8** %3
%62 = call i8* @bufinsert(i32 %23, i8* %61)
store i8* %62, i8** %3
%63 = icmp eq i8* %62, null
br i1 %63, label %71, label %64
64: ; preds = %60
%65 = getelementptr inbounds i8, i8* %62, i64 1
%66 = call i64 @recinsert(i8* %62, i8* %65)
%67 = icmp slt i64 %66, 0
br i1 %67, label %71, label %68
68: ; preds = %64
%69 = load i8*, i8** %3
%70 = getelementptr inbounds i8, i8* %69, i64 1
store i8* %70, i8** %3
br label %42
71: ; preds = %13, %13, %64, %60, %49, %33
%72 = phi i32 [ -1, %33 ], [ -1, %49 ], [ -1, %60 ], [ -1, %64 ], [ 0, %13 ], [ 0, %13 ]
ret i32 %72
}