#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 A_BOLD;
int COLOR_PAIR(int);
int /*<<< orphan*/ attrset(int);
char *calloc(int, int);
int /*<<< orphan*/ getnstr(char *, int);
int strlen(char *);
char *ngets(char *s, int n) {
  if (s == NULL) {
    s = calloc(255, sizeof(char));
    n = 255;
  }
  attrset(COLOR_PAIR(1) | A_BOLD);
  do {
    getnstr(s, n);
    if (s[strlen(s) - 1] == '\n')
      s[strlen(s) - 1] = 0;
  } while (s[0] == 0);
  return s;
}


x86 assembly (clang, -O0):
<ngets>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    1184 <ngets+0x34>
mov    $0xff,%edi
mov    $0x1,%esi
call   1050 <calloc@plt>
mov    %rax,-0x8(%rbp)
movl   $0xff,-0xc(%rbp)
mov    $0x1,%edi
call   1060 <COLOR_PAIR@plt>
mov    %eax,%edi
mov    0x2e49(%rip),%rax
or     (%rax),%edi
call   1070 <attrset@plt>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1030 <getnstr@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    11f5 <ngets+0xa5>
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    11fa <ngets+0xaa>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     119e <ngets+0x4e>
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ngets>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
test   %rdi,%rdi
jne    1174 <ngets+0x24>
mov    $0xff,%ebp
mov    $0xff,%edi
mov    $0x1,%esi
call   1050 <calloc@plt>
mov    %rax,%rbx
mov    $0x1,%edi
call   1060 <COLOR_PAIR@plt>
mov    0x2e5b(%rip),%rcx
or     (%rcx),%eax
mov    %eax,%edi
call   1070 <attrset@plt>
jmp    1195 <ngets+0x45>
cmpb   $0x0,(%rbx)
jne    11c1 <ngets+0x71>
mov    %rbx,%rdi
mov    %ebp,%esi
call   1030 <getnstr@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
cltq
cmpb   $0xa,-0x1(%rax,%rbx,1)
jne    1190 <ngets+0x40>
mov    %rbx,%rdi
call   1040 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%rbx,1)
jmp    1190 <ngets+0x40>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@A_BOLD = global i32 0
define i8* @ngets(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%5 = load i8*, i8** %3
%6 = icmp eq i8* %5, null
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = call i8* @calloc(i32 255, i32 1)
store i8* %8, i8** %3
store i32 255, i32* %4
br label %9
9: ; preds = %7, %2
%10 = call i32 @COLOR_PAIR(i32 1)
%11 = load i32, i32* @A_BOLD
%12 = or i32 %10, %11
%13 = call i32 @attrset(i32 %12)
br label %14
14: ; preds = %35, %9
%15 = load i8*, i8** %3
%16 = load i32, i32* %4
%17 = call i32 @getnstr(i8* %15, i32 %16)
%18 = load i8*, i8** %3
%19 = load i8*, i8** %3
%20 = call i32 @strlen(i8* %19)
%21 = sub nsw i32 %20, 1
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i8, i8* %18, i64 %22
%24 = load i8, i8* %23
%25 = sext i8 %24 to i32
%26 = icmp eq i32 %25, 10
br i1 %26, label %27, label %34
27: ; preds = %14
%28 = load i8*, i8** %3
%29 = load i8*, i8** %3
%30 = call i32 @strlen(i8* %29)
%31 = sub nsw i32 %30, 1
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %28, i64 %32
store i8 0, i8* %33
br label %34
34: ; preds = %27, %14
br label %35
35: ; preds = %34
%36 = load i8*, i8** %3
%37 = getelementptr inbounds i8, i8* %36, i64 0
%38 = load i8, i8* %37
%39 = sext i8 %38 to i32
%40 = icmp eq i32 %39, 0
br i1 %40, label %14, label %41
41: ; preds = %35
%42 = load i8*, i8** %3
ret i8* %42
}

LLVM IR (-O2):
@A_BOLD = global i32 0
define i8* @ngets(i8* %0, i32 %1) {
%3 = icmp eq i8* %0, null
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = tail call dereferenceable_or_null(255) i8* @calloc(i32 255, i32 1)
br label %6
6: ; preds = %4, %2
%7 = phi i8* [ %5, %4 ], [ %0, %2 ]
%8 = phi i32 [ 255, %4 ], [ %1, %2 ]
%9 = tail call i32 @COLOR_PAIR(i32 1)
%10 = load i32, i32* @A_BOLD
%11 = or i32 %10, %9
%12 = tail call i32 @attrset(i32 %11)
br label %13
13: ; preds = %26, %6
%14 = tail call i32 @getnstr(i8* %7, i32 %8)
%15 = tail call i32 @strlen(i8* %7)
%16 = add nsw i32 %15, -1
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds i8, i8* %7, i64 %17
%19 = load i8, i8* %18
%20 = icmp eq i8 %19, 10
br i1 %20, label %21, label %26
21: ; preds = %13
%22 = tail call i32 @strlen(i8* %7)
%23 = add nsw i32 %22, -1
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %7, i64 %24
store i8 0, i8* %25
br label %26
26: ; preds = %13, %21
%27 = load i8, i8* %7
%28 = icmp eq i8 %27, 0
br i1 %28, label %13, label %29
29: ; preds = %26
ret i8* %7
}