#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 *, size_t, int /*<<< orphan*/);
int /*<<< orphan*/ isatty(int);
int linenoiseEdit(int, char *, size_t, char const *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ stdin;
int 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, buflen, stdin) == NULL)
      return -1;
    count = strlen(buf);
    if (count && buf[count - 1] == '\n') {
      buf[count] = '\0';
    }
  } else {
    if (enableRawMode(fd) == -1)
      return -1;
    count = linenoiseEdit(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    128b <linenoiseRaw+0x11b>
mov    0x2e08(%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    0x2e09(%rip),%rax
mov    (%rax),%edx
call   1080 <fgets@plt>
cmp    $0x0,%rax
jne    11fc <linenoiseRaw+0x8c>
movl   $0xffffffff,-0x4(%rbp)
jmp    128b <linenoiseRaw+0x11b>
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    -0x10(%rbp),%rax
movslq -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    1285 <linenoiseRaw+0x115>
mov    -0x24(%rbp),%edi
call   1030 <enableRawMode@plt>
cmp    $0xffffffff,%eax
jne    125a <linenoiseRaw+0xea>
movl   $0xffffffff,-0x4(%rbp)
jmp    128b <linenoiseRaw+0x11b>
mov    -0x24(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
call   1090 <linenoiseEdit@plt>
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%edi
call   1050 <disableRawMode@plt>
lea    0xd80(%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   %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     11e5 <linenoiseRaw+0x75>
mov    %ebp,%edi
call   1030 <enableRawMode@plt>
cmp    $0xffffffff,%eax
je     121c <linenoiseRaw+0xac>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %rbx,%rdx
mov    %r15,%rcx
call   1090 <linenoiseEdit@plt>
mov    %eax,%ebx
mov    %ebp,%edi
call   1060 <disableRawMode@plt>
mov    $0xa,%edi
call   1040 <putchar@plt>
mov    %ebx,%eax
jmp    1221 <linenoiseRaw+0xb1>
mov    0x2dfd(%rip),%rax
mov    (%rax),%eax
mov    0x2dfc(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
jmp    1221 <linenoiseRaw+0xb1>
mov    0x2dfc(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rdi
mov    %rbx,%rsi
call   1080 <fgets@plt>
test   %rax,%rax
je     121c <linenoiseRaw+0xac>
mov    %r14,%rdi
call   1070 <strlen@plt>
test   %eax,%eax
je     122c <linenoiseRaw+0xbc>
movslq %eax,%rcx
cmpb   $0xa,-0x1(%rcx,%r14,1)
jne    1221 <linenoiseRaw+0xb1>
movb   $0x0,(%r14,%rcx,1)
jmp    1221 <linenoiseRaw+0xb1>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %eax,%eax
jmp    1221 <linenoiseRaw+0xb1>

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 %62
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 %46, label %19
19: ; preds = %15
%20 = load i8*, i8** %5
%21 = load i64, i64* %6
%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* %4
br label %62
26: ; preds = %19
%27 = load i8*, i8** %5
%28 = call i32 @strlen(i8* %27)
store i32 %28, i32* %9
%29 = load i32, i32* %9
%30 = icmp ne i32 %29, 0
br i1 %30, label %31, label %45
31: ; preds = %26
%32 = load i8*, i8** %5
%33 = load i32, i32* %9
%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 %45
40: ; preds = %31
%41 = load i8*, i8** %5
%42 = load i32, i32* %9
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds i8, i8* %41, i64 %43
store i8 0, i8* %44
br label %45
45: ; preds = %40, %31, %26
br label %60
46: ; preds = %15
%47 = load i32, i32* %8
%48 = call i32 @enableRawMode(i32 %47)
%49 = icmp eq i32 %48, -1
br i1 %49, label %50, label %51
50: ; preds = %46
store i32 -1, i32* %4
br label %62
51: ; preds = %46
%52 = load i32, i32* %8
%53 = load i8*, i8** %5
%54 = load i64, i64* %6
%55 = load i8*, i8** %7
%56 = call i32 @linenoiseEdit(i32 %52, i8* %53, i64 %54, i8* %55)
store i32 %56, i32* %9
%57 = load i32, i32* %8
%58 = call i32 @disableRawMode(i32 %57)
%59 = call i32 @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %60
60: ; preds = %51, %45
%61 = load i32, i32* %9
store i32 %61, i32* %4
br label %62
62: ; preds = %60, %50, %25, %13
%63 = load i32, i32* %4
ret i32 %63
}

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 %34
8: ; preds = %3
%9 = tail call i32 @isatty(i32 %4)
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %27
11: ; preds = %8
%12 = load i32, i32* @stdin
%13 = tail call i32* @fgets(i8* %0, i64 %1, i32 %12)
%14 = icmp eq i32* %13, null
br i1 %14, label %34, label %15
15: ; preds = %11
%16 = tail call i32 @strlen(i8* %0)
%17 = icmp eq i32 %16, 0
br i1 %17, label %34, label %18
18: ; preds = %15
%19 = add nsw i32 %16, -1
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %0, i64 %20
%22 = load i8, i8* %21
%23 = icmp eq i8 %22, 10
br i1 %23, label %24, label %34
24: ; preds = %18
%25 = sext i32 %16 to i64
%26 = getelementptr inbounds i8, i8* %0, i64 %25
store i8 0, i8* %26
br label %34
27: ; preds = %8
%28 = tail call i32 @enableRawMode(i32 %4)
%29 = icmp eq i32 %28, -1
br i1 %29, label %34, label %30
30: ; preds = %27
%31 = tail call i32 @linenoiseEdit(i32 %4, i8* %0, i64 %1, i8* %2)
%32 = tail call i32 @disableRawMode(i32 %4)
%33 = tail call i32 @putchar(i32 10)
br label %34
34: ; preds = %30, %24, %18, %15, %27, %11, %6
%35 = phi i32 [ -1, %6 ], [ -1, %11 ], [ -1, %27 ], [ %31, %30 ], [ %16, %24 ], [ %16, %18 ], [ 0, %15 ]
ret i32 %35
}