#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') {
      count--;
      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    1294 <linenoiseRaw+0x124>
mov    0x2e08(%rip),%rax
mov    (%rax),%edi
call   1040 <isatty@plt>
cmp    $0x0,%eax
jne    1246 <linenoiseRaw+0xd6>
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    1294 <linenoiseRaw+0x124>
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
je     1241 <linenoiseRaw+0xd1>
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    1241 <linenoiseRaw+0xd1>
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    128e <linenoiseRaw+0x11e>
mov    -0x24(%rbp),%edi
call   1030 <enableRawMode@plt>
cmp    $0xffffffff,%eax
jne    1263 <linenoiseRaw+0xf3>
movl   $0xffffffff,-0x4(%rbp)
jmp    1294 <linenoiseRaw+0x124>
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    0xd77(%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     1222 <linenoiseRaw+0xb2>
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    1227 <linenoiseRaw+0xb7>
mov    0x2dfd(%rip),%rax
mov    (%rax),%eax
mov    0x2dfc(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
jmp    1227 <linenoiseRaw+0xb7>
mov    0x2dfc(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rdi
mov    %rbx,%rsi
call   1080 <fgets@plt>
test   %rax,%rax
je     1222 <linenoiseRaw+0xb2>
mov    %r14,%rdi
call   1070 <strlen@plt>
test   %eax,%eax
je     1232 <linenoiseRaw+0xc2>
movslq %eax,%rcx
cmpb   $0xa,-0x1(%r14,%rcx,1)
jne    1227 <linenoiseRaw+0xb7>
add    $0xffffffffffffffff,%rcx
movb   $0x0,(%r14,%rcx,1)
mov    %ecx,%eax
jmp    1227 <linenoiseRaw+0xb7>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %eax,%eax
jmp    1227 <linenoiseRaw+0xb7>

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 %64
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 %48, 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 %64
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 %47
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 %47
40: ; preds = %31
%41 = load i32, i32* %9
%42 = add nsw i32 %41, -1
store i32 %42, i32* %9
%43 = load i8*, i8** %5
%44 = load i32, i32* %9
%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 %62
48: ; preds = %15
%49 = load i32, i32* %8
%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* %4
br label %64
53: ; preds = %48
%54 = load i32, i32* %8
%55 = load i8*, i8** %5
%56 = load i64, i64* %6
%57 = load i8*, i8** %7
%58 = call i32 @linenoiseEdit(i32 %54, i8* %55, i64 %56, i8* %57)
store i32 %58, i32* %9
%59 = load i32, i32* %8
%60 = call i32 @disableRawMode(i32 %59)
%61 = call i32 @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %62
62: ; preds = %53, %47
%63 = load i32, i32* %9
store i32 %63, i32* %4
br label %64
64: ; preds = %62, %52, %25, %13
%65 = load i32, i32* %4
ret i32 %65
}

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 %32
8: ; preds = %3
%9 = tail call i32 @isatty(i32 %4)
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %25
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 %32, label %15
15: ; preds = %11
%16 = tail call i32 @strlen(i8* %0)
%17 = icmp eq i32 %16, 0
br i1 %17, label %32, 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 %32
24: ; preds = %18
store i8 0, i8* %21
br label %32
25: ; preds = %8
%26 = tail call i32 @enableRawMode(i32 %4)
%27 = icmp eq i32 %26, -1
br i1 %27, label %32, label %28
28: ; preds = %25
%29 = tail call i32 @linenoiseEdit(i32 %4, i8* %0, i64 %1, i8* %2)
%30 = tail call i32 @disableRawMode(i32 %4)
%31 = tail call i32 @putchar(i32 10)
br label %32
32: ; preds = %28, %24, %18, %15, %25, %11, %6
%33 = phi i32 [ -1, %6 ], [ -1, %11 ], [ -1, %25 ], [ %29, %28 ], [ %19, %24 ], [ %16, %18 ], [ 0, %15 ]
ret i32 %33
}