#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 *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ stdin;
int strlen(char *);
int linenoiseRaw(char *buf, size_t buflen, char *prompt) {
  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);
    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)
cmpq   $0x0,-0x18(%rbp)
jne    11ad <linenoiseRaw+0x3d>
mov    0x2e32(%rip),%rax
mov    (%rax),%ecx
mov    0x2e31(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    12a3 <linenoiseRaw+0x133>
mov    0x2e0c(%rip),%rax
mov    (%rax),%edi
call   1040 <isatty@plt>
cmp    $0x0,%eax
jne    123a <linenoiseRaw+0xca>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    0x2e0d(%rip),%rax
mov    (%rax),%edx
call   1080 <fgets@plt>
cmp    $0x0,%rax
jne    11f0 <linenoiseRaw+0x80>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a3 <linenoiseRaw+0x133>
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     1235 <linenoiseRaw+0xc5>
mov    -0x10(%rbp),%rax
mov    -0x24(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    1235 <linenoiseRaw+0xc5>
mov    -0x24(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    129d <linenoiseRaw+0x12d>
mov    0x2d7f(%rip),%rax
mov    (%rax),%edi
call   1030 <enableRawMode@plt>
cmp    $0xffffffff,%eax
jne    125d <linenoiseRaw+0xed>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a3 <linenoiseRaw+0x133>
mov    0x2d5c(%rip),%rax
mov    (%rax),%edi
mov    0x2d83(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    -0x20(%rbp),%r8
call   1090 <linenoiseEdit@plt>
mov    %eax,-0x24(%rbp)
mov    0x2d36(%rip),%rax
mov    (%rax),%edi
call   1050 <disableRawMode@plt>
lea    0xd68(%rip),%rdi
call   1070 <printf@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<linenoiseRaw>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
test   %rsi,%rsi
je     11e0 <linenoiseRaw+0x70>
mov    %rdx,%r15
mov    %rsi,%rbx
mov    %rdi,%r14
mov    0x2e33(%rip),%r12
mov    (%r12),%edi
call   1050 <isatty@plt>
test   %eax,%eax
je     11f9 <linenoiseRaw+0x89>
mov    (%r12),%edi
call   1030 <enableRawMode@plt>
cmp    $0xffffffff,%eax
je     1236 <linenoiseRaw+0xc6>
mov    (%r12),%edi
mov    0x2e39(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdx
mov    %rbx,%rcx
mov    %r15,%r8
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    123b <linenoiseRaw+0xcb>
mov    0x2de1(%rip),%rax
mov    (%rax),%eax
mov    0x2de0(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
jmp    123b <linenoiseRaw+0xcb>
mov    0x2de0(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rdi
mov    %rbx,%rsi
call   1080 <fgets@plt>
test   %rax,%rax
je     1236 <linenoiseRaw+0xc6>
mov    %r14,%rdi
call   1070 <strlen@plt>
test   %eax,%eax
je     1247 <linenoiseRaw+0xd7>
movslq %eax,%rcx
cmpb   $0xa,-0x1(%r14,%rcx,1)
jne    123b <linenoiseRaw+0xcb>
add    $0xffffffffffffffff,%rcx
movb   $0x0,(%r14,%rcx,1)
mov    %ecx,%eax
jmp    123b <linenoiseRaw+0xcb>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
xor    %eax,%eax
jmp    123b <linenoiseRaw+0xcb>

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

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