#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 /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ stdin;
int strlen(char *);
int linenoiseRaw(char *buf, size_t buflen, const 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);
  } 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    126a <linenoiseRaw+0xfa>
mov    0x2e0c(%rip),%rax
mov    (%rax),%edi
call   1040 <isatty@plt>
cmp    $0x0,%eax
jne    1201 <linenoiseRaw+0x91>
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    126a <linenoiseRaw+0xfa>
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,-0x24(%rbp)
jmp    1264 <linenoiseRaw+0xf4>
mov    0x2db8(%rip),%rax
mov    (%rax),%edi
call   1030 <enableRawMode@plt>
cmp    $0xffffffff,%eax
jne    1224 <linenoiseRaw+0xb4>
movl   $0xffffffff,-0x4(%rbp)
jmp    126a <linenoiseRaw+0xfa>
mov    0x2d95(%rip),%rax
mov    (%rax),%edi
mov    0x2dbc(%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    0x2d6f(%rip),%rax
mov    (%rax),%edi
call   1050 <disableRawMode@plt>
lea    0xda1(%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     11dc <linenoiseRaw+0x6c>
mov    %rdx,%r15
mov    %rsi,%rbx
mov    %rdi,%r14
mov    0x2e33(%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     11ee <linenoiseRaw+0x7e>
mov    (%r12),%edi
mov    0x2e3d(%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    11f3 <linenoiseRaw+0x83>
mov    0x2de5(%rip),%rax
mov    (%rax),%eax
mov    0x2de4(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    0x2dda(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rdi
mov    %rbx,%rsi
call   1080 <fgets@plt>
test   %rax,%rax
je     11ee <linenoiseRaw+0x7e>
mov    %r14,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1070 <strlen@plt>

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 %44
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 %27, 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 %44
24: ; preds = %17
%25 = load i8*, i8** %5
%26 = call i32 @strlen(i8* %25)
store i32 %26, i32* %8
br label %42
27: ; preds = %13
%28 = load i32, i32* @STDIN_FILENO
%29 = call i32 @enableRawMode(i32 %28)
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %32
31: ; preds = %27
store i32 -1, i32* %4
br label %44
32: ; preds = %27
%33 = load i32, i32* @STDIN_FILENO
%34 = load i32, i32* @STDOUT_FILENO
%35 = load i8*, i8** %5
%36 = load i64, i64* %6
%37 = load i8*, i8** %7
%38 = call i32 @linenoiseEdit(i32 %33, i32 %34, i8* %35, i64 %36, i8* %37)
store i32 %38, i32* %8
%39 = load i32, i32* @STDIN_FILENO
%40 = call i32 @disableRawMode(i32 %39)
%41 = call i32 @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %42
42: ; preds = %32, %24
%43 = load i32, i32* %8
store i32 %43, i32* %4
br label %44
44: ; preds = %42, %31, %23, %11
%45 = load i32, i32* %4
ret i32 %45
}

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 %28
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 %17
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 %28, label %15
15: ; preds = %11
%16 = tail call i32 @strlen(i8* %0)
br label %28
17: ; preds = %7
%18 = load i32, i32* @STDIN_FILENO
%19 = tail call i32 @enableRawMode(i32 %18)
%20 = icmp eq i32 %19, -1
br i1 %20, label %28, label %21
21: ; preds = %17
%22 = load i32, i32* @STDIN_FILENO
%23 = load i32, i32* @STDOUT_FILENO
%24 = tail call i32 @linenoiseEdit(i32 %22, i32 %23, i8* %0, i64 %1, i8* %2)
%25 = load i32, i32* @STDIN_FILENO
%26 = tail call i32 @disableRawMode(i32 %25)
%27 = tail call i32 @putchar(i32 10)
br label %28
28: ; preds = %15, %21, %17, %11, %5
%29 = phi i32 [ -1, %5 ], [ -1, %11 ], [ -1, %17 ], [ %24, %21 ], [ %16, %15 ]
ret i32 %29
}