#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*/ EINVAL;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ disableRawMode(int /*<<< orphan*/);
int enableRawMode(int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ *fgets(char *, size_t, int /*<<< orphan*/);
int /*<<< orphan*/ isatty(int /*<<< orphan*/);
int linenoiseEdit(int /*<<< orphan*/, int /*<<< orphan*/, char *, size_t,
                  char const *, int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ stdin;
int strlen(char *);
int linenoiseRaw(char *buf, size_t buflen, const char *prompt, int spaces) {
  int count;
  if (buflen == 0) {
    errno = EINVAL;
    return -1;
  }
  if (!isatty(STDIN_FILENO)) {
    if (fgets(buf, buflen, stdin) == NULL)
      return -1;
    count = strlen(buf);
    if (count && buf[count - 1] == '\n') {
      count--;
      buf[count] = '\0';
    }
  } else {
    if (enableRawMode(STDIN_FILENO) == -1)
      return -1;
    count =
        linenoiseEdit(STDIN_FILENO, STDOUT_FILENO, buf, buflen, prompt, spaces);
    disableRawMode(STDIN_FILENO);
    printf("\n");
  }
  return count;
}


x86 assembly (clang, -O0):
<linenoiseRaw>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11b0 <linenoiseRaw+0x40>
mov    0x2e2f(%rip),%rax
mov    (%rax),%ecx
mov    0x2e2e(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    12aa <linenoiseRaw+0x13a>
mov    0x2e09(%rip),%rax
mov    (%rax),%edi
call   1040 <isatty@plt>
cmp    $0x0,%eax
jne    123d <linenoiseRaw+0xcd>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    0x2e0a(%rip),%rax
mov    (%rax),%edx
call   1080 <fgets@plt>
cmp    $0x0,%rax
jne    11f3 <linenoiseRaw+0x83>
movl   $0xffffffff,-0x4(%rbp)
jmp    12aa <linenoiseRaw+0x13a>
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
je     1238 <linenoiseRaw+0xc8>
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    1238 <linenoiseRaw+0xc8>
mov    -0x28(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    12a4 <linenoiseRaw+0x134>
mov    0x2d7c(%rip),%rax
mov    (%rax),%edi
call   1030 <enableRawMode@plt>
cmp    $0xffffffff,%eax
jne    1260 <linenoiseRaw+0xf0>
movl   $0xffffffff,-0x4(%rbp)
jmp    12aa <linenoiseRaw+0x13a>
mov    0x2d59(%rip),%rax
mov    (%rax),%edi
mov    0x2d80(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    -0x20(%rbp),%r8
mov    -0x24(%rbp),%r9d
call   1090 <linenoiseEdit@plt>
mov    %eax,-0x28(%rbp)
mov    0x2d2f(%rip),%rax
mov    (%rax),%edi
call   1050 <disableRawMode@plt>
lea    0xd61(%rip),%rdi
call   1070 <printf@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<linenoiseRaw>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
test   %rsi,%rsi
je     11e6 <linenoiseRaw+0x76>
mov    %ecx,%r15d
mov    %rdx,%rbp
mov    %rsi,%rbx
mov    %rdi,%r14
mov    0x2e30(%rip),%r12
mov    (%r12),%edi
call   1050 <isatty@plt>
test   %eax,%eax
je     11ff <linenoiseRaw+0x8f>
mov    (%r12),%edi
call   1030 <enableRawMode@plt>
cmp    $0xffffffff,%eax
je     123c <linenoiseRaw+0xcc>
mov    (%r12),%edi
mov    0x2e36(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdx
mov    %rbx,%rcx
mov    %rbp,%r8
mov    %r15d,%r9d
call   1090 <linenoiseEdit@plt>
mov    %eax,%ebx
mov    (%r12),%edi
call   1060 <disableRawMode@plt>
mov    $0xa,%edi
call   1040 <putchar@plt>
mov    %ebx,%eax
jmp    1241 <linenoiseRaw+0xd1>
mov    0x2ddb(%rip),%rax
mov    (%rax),%eax
mov    0x2dda(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
jmp    1241 <linenoiseRaw+0xd1>
mov    0x2dda(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rdi
mov    %rbx,%rsi
call   1080 <fgets@plt>
test   %rax,%rax
je     123c <linenoiseRaw+0xcc>
mov    %r14,%rdi
call   1070 <strlen@plt>
test   %eax,%eax
je     124a <linenoiseRaw+0xda>
movslq %eax,%rcx
cmpb   $0xa,-0x1(%r14,%rcx,1)
jne    1241 <linenoiseRaw+0xd1>
add    $0xffffffffffffffff,%rcx
movb   $0x0,(%r14,%rcx,1)
mov    %ecx,%eax
jmp    1241 <linenoiseRaw+0xd1>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %eax,%eax
jmp    1241 <linenoiseRaw+0xd1>

LLVM IR (-O0):
@EINVAL = global i32 0
@errno = global i32 0
@STDIN_FILENO = global i32 0
@stdin = global i32 0
@STDOUT_FILENO = global i32 0
@.str = [2 x i8] c"\0A\00"
define i32 @linenoiseRaw(i8* %0, i64 %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %6
store i64 %1, i64* %7
store i8* %2, i8** %8
store i32 %3, i32* %9
%11 = load i64, i64* %7
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %4
%14 = load i32, i32* @EINVAL
store i32 %14, i32* @errno
store i32 -1, i32* %5
br label %66
15: ; preds = %4
%16 = load i32, i32* @STDIN_FILENO
%17 = call i32 @isatty(i32 %16)
%18 = icmp ne i32 %17, 0
br i1 %18, label %48, label %19
19: ; preds = %15
%20 = load i8*, i8** %6
%21 = load i64, i64* %7
%22 = load i32, i32* @stdin
%23 = call i32* @fgets(i8* %20, i64 %21, i32 %22)
%24 = icmp eq i32* %23, null
br i1 %24, label %25, label %26
25: ; preds = %19
store i32 -1, i32* %5
br label %66
26: ; preds = %19
%27 = load i8*, i8** %6
%28 = call i32 @strlen(i8* %27)
store i32 %28, i32* %10
%29 = load i32, i32* %10
%30 = icmp ne i32 %29, 0
br i1 %30, label %31, label %47
31: ; preds = %26
%32 = load i8*, i8** %6
%33 = load i32, i32* %10
%34 = sub nsw i32 %33, 1
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %32, i64 %35
%37 = load i8, i8* %36
%38 = sext i8 %37 to i32
%39 = icmp eq i32 %38, 10
br i1 %39, label %40, label %47
40: ; preds = %31
%41 = load i32, i32* %10
%42 = add nsw i32 %41, -1
store i32 %42, i32* %10
%43 = load i8*, i8** %6
%44 = load i32, i32* %10
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds i8, i8* %43, i64 %45
store i8 0, i8* %46
br label %47
47: ; preds = %40, %31, %26
br label %64
48: ; preds = %15
%49 = load i32, i32* @STDIN_FILENO
%50 = call i32 @enableRawMode(i32 %49)
%51 = icmp eq i32 %50, -1
br i1 %51, label %52, label %53
52: ; preds = %48
store i32 -1, i32* %5
br label %66
53: ; preds = %48
%54 = load i32, i32* @STDIN_FILENO
%55 = load i32, i32* @STDOUT_FILENO
%56 = load i8*, i8** %6
%57 = load i64, i64* %7
%58 = load i8*, i8** %8
%59 = load i32, i32* %9
%60 = call i32 @linenoiseEdit(i32 %54, i32 %55, i8* %56, i64 %57, i8* %58, i32 %59)
store i32 %60, i32* %10
%61 = load i32, i32* @STDIN_FILENO
%62 = call i32 @disableRawMode(i32 %61)
%63 = call i32 @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %64
64: ; preds = %53, %47
%65 = load i32, i32* %10
store i32 %65, i32* %5
br label %66
66: ; preds = %64, %52, %25, %13
%67 = load i32, i32* %5
ret i32 %67
}

LLVM IR (-O2):
@EINVAL = global i32 0
@errno = global i32 0
@STDIN_FILENO = global i32 0
@stdin = global i32 0
@STDOUT_FILENO = global i32 0
define i32 @linenoiseRaw(i8* %0, i64 %1, i8* %2, i32 %3) {
%5 = icmp eq i64 %1, 0
br i1 %5, label %6, label %8
6: ; preds = %4
%7 = load i32, i32* @EINVAL
store i32 %7, i32* @errno
br label %37
8: ; preds = %4
%9 = load i32, i32* @STDIN_FILENO
%10 = tail call i32 @isatty(i32 %9)
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %26
12: ; preds = %8
%13 = load i32, i32* @stdin
%14 = tail call i32* @fgets(i8* %0, i64 %1, i32 %13)
%15 = icmp eq i32* %14, null
br i1 %15, label %37, label %16
16: ; preds = %12
%17 = tail call i32 @strlen(i8* %0)
%18 = icmp eq i32 %17, 0
br i1 %18, label %37, label %19
19: ; preds = %16
%20 = add nsw i32 %17, -1
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i8, i8* %0, i64 %21
%23 = load i8, i8* %22
%24 = icmp eq i8 %23, 10
br i1 %24, label %25, label %37
25: ; preds = %19
store i8 0, i8* %22
br label %37
26: ; preds = %8
%27 = load i32, i32* @STDIN_FILENO
%28 = tail call i32 @enableRawMode(i32 %27)
%29 = icmp eq i32 %28, -1
br i1 %29, label %37, label %30
30: ; preds = %26
%31 = load i32, i32* @STDIN_FILENO
%32 = load i32, i32* @STDOUT_FILENO
%33 = tail call i32 @linenoiseEdit(i32 %31, i32 %32, i8* %0, i64 %1, i8* %2, i32 %3)
%34 = load i32, i32* @STDIN_FILENO
%35 = tail call i32 @disableRawMode(i32 %34)
%36 = tail call i32 @putchar(i32 10)
br label %37
37: ; preds = %30, %25, %19, %16, %26, %12, %6
%38 = phi i32 [ -1, %6 ], [ -1, %12 ], [ -1, %26 ], [ %33, %30 ], [ %20, %25 ], [ %17, %19 ], [ 0, %16 ]
ret i32 %38
}