#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 STDIN_FILENO;
int /*<<< orphan*/ disableRawMode(int);
int enableRawMode(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ isatty(int);
int linenoisePrompt(int, char *, size_t, char const *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ stdin;
scalar_t__ strlen(char *);
int linenoiseRaw(char *buf, size_t buflen, const char *prompt) {
  int fd = STDIN_FILENO;
  int count;
  if (buflen == 0) {
    errno = EINVAL;
    return -1;
  }
  if (!isatty(STDIN_FILENO)) {
    if (fgets(buf, (int)buflen, stdin) == NULL)
      return -1;
    count = (int)strlen(buf);
    if (count && buf[count - 1] == '\n') {
      count--;
      buf[count] = '\0';
    }
  } else {
    if (enableRawMode(fd) == -1)
      return -1;
    count = linenoisePrompt(fd, buf, buflen, prompt);
    disableRawMode(fd);
    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    0x2e3d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11b9 <linenoiseRaw+0x49>
mov    0x2e2e(%rip),%rax
mov    (%rax),%ecx
mov    0x2e2d(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1296 <linenoiseRaw+0x126>
mov    0x2e08(%rip),%rax
mov    (%rax),%edi
call   1040 <isatty@plt>
cmp    $0x0,%eax
jne    1248 <linenoiseRaw+0xd8>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    %eax,%esi
mov    0x2e07(%rip),%rax
mov    (%rax),%edx
call   1090 <fgets@plt>
cmp    $0x0,%rax
jne    11fe <linenoiseRaw+0x8e>
movl   $0xffffffff,-0x4(%rbp)
jmp    1296 <linenoiseRaw+0x126>
mov    -0x10(%rbp),%rdi
call   1070 <strlen@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
je     1243 <linenoiseRaw+0xd3>
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    1243 <linenoiseRaw+0xd3>
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    1290 <linenoiseRaw+0x120>
mov    -0x24(%rbp),%edi
call   1030 <enableRawMode@plt>
cmp    $0xffffffff,%eax
jne    1265 <linenoiseRaw+0xf5>
movl   $0xffffffff,-0x4(%rbp)
jmp    1296 <linenoiseRaw+0x126>
mov    -0x24(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
call   1060 <linenoisePrompt@plt>
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%edi
call   1050 <disableRawMode@plt>
lea    0xd75(%rip),%rdi
call   1080 <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   %rbx
push   %rax
test   %rsi,%rsi
je     11cc <linenoiseRaw+0x5c>
mov    %rdx,%r15
mov    %rsi,%rbx
mov    %rdi,%r14
mov    0x2e3c(%rip),%rax
mov    (%rax),%ebp
mov    %ebp,%edi
call   1050 <isatty@plt>
test   %eax,%eax
je     11ee <linenoiseRaw+0x7e>
mov    %ebp,%edi
call   1030 <enableRawMode@plt>
cmp    $0xffffffff,%eax
je     11de <linenoiseRaw+0x6e>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %rbx,%rdx
mov    %r15,%rcx
call   1070 <linenoisePrompt@plt>
mov    %eax,%ebx
mov    %ebp,%edi
call   1060 <disableRawMode@plt>
mov    $0xa,%edi
call   1040 <putchar@plt>
mov    %ebx,%eax
jmp    11e3 <linenoiseRaw+0x73>
mov    0x2dfd(%rip),%rax
mov    (%rax),%eax
mov    0x2dfc(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2df3(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rdi
mov    %ebx,%esi
call   1090 <fgets@plt>
test   %rax,%rax
je     11de <linenoiseRaw+0x6e>
mov    %r14,%rdi
call   1080 <strlen@plt>
test   %eax,%eax
je     122f <linenoiseRaw+0xbf>
mov    %eax,%ecx
add    $0xffffffff,%ecx
movslq %ecx,%rcx
cmpb   $0xa,(%r14,%rcx,1)
jne    11e3 <linenoiseRaw+0x73>
cltq
movb   $0x0,-0x1(%r14,%rax,1)
add    $0xffffffffffffffff,%rax
jmp    11e3 <linenoiseRaw+0x73>
xor    %eax,%eax
jmp    11e3 <linenoiseRaw+0x73>

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

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