#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 /*<<< orphan*/ echo();
int /*<<< orphan*/ endwin();
char getch();
int /*<<< orphan*/ initscr();
scalar_t__ malloc(int);
int /*<<< orphan*/ noecho();
int /*<<< orphan*/ raw();
char *getstring(int length, int mode) {
  char c;
  char *buffer = (char *)malloc(255);
  initscr();
  raw();
  if (mode == 1) {
    echo();
  } else {
    noecho();
  }
  for (int i = 0; i < length; i++) {
    c = getch();
    buffer[i] = c;
    if (c == '\n') {
      buffer[i + 1] = '\0';
      endwin();
      return buffer;
    }
  }
  buffer[length] = '\0';
  endwin();
  return buffer;
}


x86 assembly (clang, -O0):
<getstring>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
mov    %esi,-0x10(%rbp)
mov    $0xff,%edi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   1040 <initscr@plt>
mov    $0x0,%al
call   1050 <raw@plt>
cmpl   $0x1,-0x10(%rbp)
jne    11b0 <getstring+0x40>
mov    $0x0,%al
call   1030 <echo@plt>
jmp    11b7 <getstring+0x47>
mov    $0x0,%al
call   1090 <noecho@plt>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    1227 <getstring+0xb7>
mov    $0x0,%al
call   1070 <getch@plt>
mov    %al,-0x11(%rbp)
mov    -0x11(%rbp),%dl
mov    -0x20(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
movsbl -0x11(%rbp),%eax
cmp    $0xa,%eax
jne    1214 <getstring+0xa4>
mov    -0x20(%rbp),%rax
mov    -0x24(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    $0x0,%al
call   1080 <endwin@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1242 <getstring+0xd2>
jmp    1219 <getstring+0xa9>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11be <getstring+0x4e>
mov    -0x20(%rbp),%rax
movslq -0xc(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    $0x0,%al
call   1080 <endwin@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getstring>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %edi,%r14d
mov    $0xff,%edi
call   1060 <malloc@plt>
mov    %rax,%r15
xor    %eax,%eax
call   1040 <initscr@plt>
xor    %eax,%eax
call   1050 <raw@plt>
xor    %eax,%eax
cmp    $0x1,%ebp
jne    11aa <getstring+0x3a>
call   1030 <echo@plt>
test   %r14d,%r14d
jg     11b4 <getstring+0x44>
jmp    11e2 <getstring+0x72>
call   1090 <noecho@plt>
test   %r14d,%r14d
jle    11e2 <getstring+0x72>
mov    %r14d,%ebx
neg    %rbx
mov    $0x1,%ebp
nop
xor    %eax,%eax
call   1070 <getch@plt>
mov    %al,-0x1(%r15,%rbp,1)
cmp    $0xa,%al
je     11e5 <getstring+0x75>
lea    (%rbx,%rbp,1),%rax
add    $0x1,%rax
add    $0x1,%rbp
cmp    $0x1,%rax
jne    11c0 <getstring+0x50>
movslq %r14d,%rbp
movb   $0x0,(%r15,%rbp,1)
xor    %eax,%eax
call   1080 <endwin@plt>
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @getstring(i32 %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8
%7 = alloca i8*
%8 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%9 = call i64 @malloc(i32 255)
%10 = inttoptr i64 %9 to i8*
store i8* %10, i8** %7
%11 = call i32 (...) @initscr()
%12 = call i32 (...) @raw()
%13 = load i32, i32* %5
%14 = icmp eq i32 %13, 1
br i1 %14, label %15, label %17
15: ; preds = %2
%16 = call i32 (...) @echo()
br label %19
17: ; preds = %2
%18 = call i32 (...) @noecho()
br label %19
19: ; preds = %17, %15
store i32 0, i32* %8
br label %20
20: ; preds = %43, %19
%21 = load i32, i32* %8
%22 = load i32, i32* %4
%23 = icmp slt i32 %21, %22
br i1 %23, label %24, label %46
24: ; preds = %20
%25 = call signext i8 (...) @getch()
store i8 %25, i8* %6
%26 = load i8, i8* %6
%27 = load i8*, i8** %7
%28 = load i32, i32* %8
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %27, i64 %29
store i8 %26, i8* %30
%31 = load i8, i8* %6
%32 = sext i8 %31 to i32
%33 = icmp eq i32 %32, 10
br i1 %33, label %34, label %42
34: ; preds = %24
%35 = load i8*, i8** %7
%36 = load i32, i32* %8
%37 = add nsw i32 %36, 1
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %35, i64 %38
store i8 0, i8* %39
%40 = call i32 (...) @endwin()
%41 = load i8*, i8** %7
store i8* %41, i8** %3
br label %53
42: ; preds = %24
br label %43
43: ; preds = %42
%44 = load i32, i32* %8
%45 = add nsw i32 %44, 1
store i32 %45, i32* %8
br label %20
46: ; preds = %20
%47 = load i8*, i8** %7
%48 = load i32, i32* %4
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds i8, i8* %47, i64 %49
store i8 0, i8* %50
%51 = call i32 (...) @endwin()
%52 = load i8*, i8** %7
store i8* %52, i8** %3
br label %53
53: ; preds = %46, %34
%54 = load i8*, i8** %3
ret i8* %54
}

LLVM IR (-O2):
define i8* @getstring(i32 %0, i32 %1) {
%3 = tail call i64 @malloc(i32 255)
%4 = inttoptr i64 %3 to i8*
%5 = tail call i32 (...) @initscr()
%6 = tail call i32 (...) @raw()
%7 = icmp eq i32 %1, 1
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = tail call i32 (...) @echo()
br label %12
10: ; preds = %2
%11 = tail call i32 (...) @noecho()
br label %12
12: ; preds = %10, %8
%13 = icmp sgt i32 %0, 0
br i1 %13, label %14, label %26
14: ; preds = %12
%15 = zext i32 %0 to i64
br label %16
16: ; preds = %14, %24
%17 = phi i64 [ 0, %14 ], [ %21, %24 ]
%18 = tail call signext i8 (...) @getch()
%19 = getelementptr inbounds i8, i8* %4, i64 %17
store i8 %18, i8* %19
%20 = icmp eq i8 %18, 10
%21 = add nuw i64 %17, 1
br i1 %20, label %22, label %24
22: ; preds = %16
%23 = and i64 %21, 4294967295
br label %28
24: ; preds = %16
%25 = icmp eq i64 %21, %15
br i1 %25, label %26, label %16
26: ; preds = %24, %12
%27 = sext i32 %0 to i64
br label %28
28: ; preds = %22, %26
%29 = phi i64 [ %23, %22 ], [ %27, %26 ]
%30 = getelementptr inbounds i8, i8* %4, i64 %29
store i8 0, i8* %30
%31 = tail call i32 (...) @endwin()
ret i8* %4
}