#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*/ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ stdin;
char *strchr(char *, char);
int strlen(char *);
int safegets(char *buffer, int n) {
  char *ptr;
  buffer[0] = '\0';
  fgets(buffer, n, stdin);
  buffer[n + 1] = '\0';
  ptr = strchr(buffer, '\n');
  if (ptr != NULL) {
    ptr[0] = '\0';
    if (buffer[0] == '\0')
      printf("\n ... no entry ...\n");
  }
  return strlen(buffer);
}


x86 assembly (clang, -O0):
<safegets>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    0x2e84(%rip),%rax
mov    (%rax),%edx
call   1060 <fgets@plt>
mov    -0x8(%rbp),%rax
mov    -0xc(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x8(%rbp),%rdi
mov    $0xa,%esi
call   1040 <strchr@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11c1 <safegets+0x81>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    11bc <safegets+0x7c>
lea    0xe49(%rip),%rdi
call   1050 <printf@plt>
jmp    11c1 <safegets+0x81>
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<safegets>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
movb   $0x0,(%rdi)
mov    0x2e96(%rip),%rax
mov    (%rax),%edx
call   1060 <fgets@plt>
movslq %ebp,%rax
movb   $0x0,0x1(%rax,%rbx,1)
mov    %rbx,%rdi
mov    $0xa,%esi
call   1050 <strchr@plt>
test   %rax,%rax
je     1187 <safegets+0x47>
movb   $0x0,(%rax)
cmpb   $0x0,(%rbx)
jne    1187 <safegets+0x47>
lea    0xe7e(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1040 <strlen@plt>

LLVM IR (-O0):
@stdin = global i32 0
@.str = [20 x i8] c"\0A ... no entry ...\0A\00"
define i32 @safegets(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
store i8* %0, i8** %3
store i32 %1, i32* %4
%6 = load i8*, i8** %3
%7 = getelementptr inbounds i8, i8* %6, i64 0
store i8 0, i8* %7
%8 = load i8*, i8** %3
%9 = load i32, i32* %4
%10 = load i32, i32* @stdin
%11 = call i32 @fgets(i8* %8, i32 %9, i32 %10)
%12 = load i8*, i8** %3
%13 = load i32, i32* %4
%14 = add nsw i32 %13, 1
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds i8, i8* %12, i64 %15
store i8 0, i8* %16
%17 = load i8*, i8** %3
%18 = call i8* @strchr(i8* %17, i8 signext 10)
store i8* %18, i8** %5
%19 = load i8*, i8** %5
%20 = icmp ne i8* %19, null
br i1 %20, label %21, label %32
21: ; preds = %2
%22 = load i8*, i8** %5
%23 = getelementptr inbounds i8, i8* %22, i64 0
store i8 0, i8* %23
%24 = load i8*, i8** %3
%25 = getelementptr inbounds i8, i8* %24, i64 0
%26 = load i8, i8* %25
%27 = sext i8 %26 to i32
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %21
%30 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %31
31: ; preds = %29, %21
br label %32
32: ; preds = %31, %2
%33 = load i8*, i8** %3
%34 = call i32 @strlen(i8* %33)
ret i32 %34
}

LLVM IR (-O2):
@stdin = global i32 0
@str = [19 x i8] c"\0A ... no entry ...\00"
define i32 @safegets(i8* %0, i32 %1) {
store i8 0, i8* %0
%3 = load i32, i32* @stdin
%4 = tail call i32 @fgets(i8* %0, i32 %1, i32 %3)
%5 = add nsw i32 %1, 1
%6 = sext i32 %5 to i64
%7 = getelementptr inbounds i8, i8* %0, i64 %6
store i8 0, i8* %7
%8 = tail call i8* @strchr(i8* %0, i8 signext 10)
%9 = icmp eq i8* %8, null
br i1 %9, label %15, label %10
10: ; preds = %2
store i8 0, i8* %8
%11 = load i8, i8* %0
%12 = icmp eq i8 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
br label %15
15: ; preds = %10, %13, %2
%16 = tail call i32 @strlen(i8* %0)
ret i32 %16
}