#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 */
char *calloc(int, int);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ stdin;
int strlen(char *);
char *ngets(char *s, int n) {
  if (s == NULL) {
    s = calloc(255, sizeof(char));
    n = 255;
  }
  do {
    fgets(s, n, stdin);
    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    1164 <ngets+0x34>
mov    $0xff,%edi
mov    $0x1,%esi
call   1050 <calloc@plt>
mov    %rax,-0x8(%rbp)
movl   $0xff,-0xc(%rbp)
jmp    1169 <ngets+0x39>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    0x2e71(%rip),%rax
mov    (%rax),%edx
call   1040 <fgets@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    11c9 <ngets+0x99>
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    11ce <ngets+0x9e>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     1169 <ngets+0x39>
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ngets>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%ebp
mov    %rdi,%rbx
test   %rdi,%rdi
jne    1155 <ngets+0x25>
mov    $0xff,%ebp
mov    $0xff,%edi
mov    $0x1,%esi
call   1050 <calloc@plt>
mov    %rax,%rbx
mov    0x2e8c(%rip),%r14
jmp    1165 <ngets+0x35>
xchg   %ax,%ax
cmpb   $0x0,(%rbx)
jne    1194 <ngets+0x64>
mov    (%r14),%edx
mov    %rbx,%rdi
mov    %ebp,%esi
call   1040 <fgets@plt>
mov    %rbx,%rdi
call   1030 <strlen@plt>
cltq
cmpb   $0xa,-0x1(%rax,%rbx,1)
jne    1160 <ngets+0x30>
mov    %rbx,%rdi
call   1030 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%rbx,1)
jmp    1160 <ngets+0x30>
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@stdin = 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
br label %10
10: ; preds = %32, %9
%11 = load i8*, i8** %3
%12 = load i32, i32* %4
%13 = load i32, i32* @stdin
%14 = call i32 @fgets(i8* %11, i32 %12, i32 %13)
%15 = load i8*, i8** %3
%16 = load i8*, i8** %3
%17 = call i32 @strlen(i8* %16)
%18 = sub nsw i32 %17, 1
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %15, i64 %19
%21 = load i8, i8* %20
%22 = sext i8 %21 to i32
%23 = icmp eq i32 %22, 10
br i1 %23, label %24, label %31
24: ; preds = %10
%25 = load i8*, i8** %3
%26 = load i8*, i8** %3
%27 = call i32 @strlen(i8* %26)
%28 = sub nsw i32 %27, 1
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %25, i64 %29
store i8 0, i8* %30
br label %31
31: ; preds = %24, %10
br label %32
32: ; preds = %31
%33 = load i8*, i8** %3
%34 = getelementptr inbounds i8, i8* %33, i64 0
%35 = load i8, i8* %34
%36 = sext i8 %35 to i32
%37 = icmp eq i32 %36, 0
br i1 %37, label %10, label %38
38: ; preds = %32
%39 = load i8*, i8** %3
ret i8* %39
}

LLVM IR (-O2):
@stdin = 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 ]
br label %9
9: ; preds = %23, %6
%10 = load i32, i32* @stdin
%11 = tail call i32 @fgets(i8* %7, i32 %8, i32 %10)
%12 = tail call i32 @strlen(i8* %7)
%13 = add nsw i32 %12, -1
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds i8, i8* %7, i64 %14
%16 = load i8, i8* %15
%17 = icmp eq i8 %16, 10
br i1 %17, label %18, label %23
18: ; preds = %9
%19 = tail call i32 @strlen(i8* %7)
%20 = add nsw i32 %19, -1
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i8, i8* %7, i64 %21
store i8 0, i8* %22
br label %23
23: ; preds = %9, %18
%24 = load i8, i8* %7
%25 = icmp eq i8 %24, 0
br i1 %25, label %9, label %26
26: ; preds = %23
ret i8* %7
}