#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*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
char *fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int getchar();
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
char *strrchr(char *, char);
void read_string_from_stdin(char buffer[1000]) {
  if (fgets(buffer, 1000, stdin) != buffer) {
    fprintf(stderr, "Fatal error: your input is invalid!\nExiting\n");
    exit(EXIT_FAILURE);
  }
  char *newline = strrchr(buffer, '\n');
  if (!newline) {
    int c;
    while ((c = getchar()) != '\n')
      ;
    fprintf(stderr, "Error: your input is too long!\nPlease shorten it up.\n");
    memset(buffer, 0, 1000);
    read_string_from_stdin(buffer);
  } else {
    if (*newline == buffer[0]) {
      fprintf(stderr, "Error: please input something!\n");
      *newline = 0;
      read_string_from_stdin(buffer);
    }
    *newline = '\0';
  }
}


x86 assembly (clang, -O0):
<read_string_from_stdin>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e61(%rip),%rax
mov    (%rax),%edx
mov    $0x3e8,%esi
call   1050 <fgets@plt>
cmp    -0x8(%rbp),%rax
je     11c0 <read_string_from_stdin+0x50>
mov    0x2e34(%rip),%rax
mov    (%rax),%edi
lea    0xe53(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2e17(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x8(%rbp),%rdi
mov    $0xa,%esi
call   1030 <strrchr@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    122d <read_string_from_stdin+0xbd>
jmp    11e2 <read_string_from_stdin+0x72>
mov    $0x0,%al
call   1060 <getchar@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xa,%eax
je     11fa <read_string_from_stdin+0x8a>
jmp    11e2 <read_string_from_stdin+0x72>
mov    0x2dd7(%rip),%rax
mov    (%rax),%edi
lea    0xe23(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x8(%rbp),%rdi
xor    %esi,%esi
mov    $0x3e8,%edx
call   1040 <memset@plt>
mov    -0x8(%rbp),%rdi
call   1080 <read_string_from_stdin@plt>
jmp    126f <read_string_from_stdin+0xff>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
mov    -0x8(%rbp),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
jne    1268 <read_string_from_stdin+0xf8>
mov    0x2d8e(%rip),%rax
mov    (%rax),%edi
lea    0xe10(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x8(%rbp),%rdi
call   1080 <read_string_from_stdin@plt>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_string_from_stdin>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdi,%r15
mov    0x2e65(%rip),%r12
mov    (%r12),%edx
mov    $0x3e8,%esi
call   1050 <fgets@plt>
cmp    %r15,%rax
jne    120d <read_string_from_stdin+0x9d>
mov    0x2e3b(%rip),%r13
lea    0xe89(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r15,%rdi
mov    $0xa,%esi
call   1030 <strrchr@plt>
test   %rax,%rax
jne    1230 <read_string_from_stdin+0xc0>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
xor    %eax,%eax
call   1060 <getchar@plt>
cmp    $0xa,%eax
jne    11d0 <read_string_from_stdin+0x60>
mov    0x0(%r13),%edi
mov    %r14,%rsi
call   1070 <fprintf@plt>
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x3e8,%edx
call   1040 <memset@plt>
mov    (%r12),%edx
mov    %r15,%rdi
mov    $0x3e8,%esi
call   1050 <fgets@plt>
cmp    %r15,%rax
je     11b0 <read_string_from_stdin+0x40>
mov    0x2dc4(%rip),%rax
mov    (%rax),%edi
lea    0xde3(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2da7(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    %rax,%rbx
mov    (%rax),%al
cmp    (%r15),%al
jne    1255 <read_string_from_stdin+0xe5>
mov    0x0(%r13),%edi
lea    0xe1e(%rip),%rsi
call   1070 <fprintf@plt>
movb   $0x0,(%rbx)
mov    %r15,%rdi
call   1080 <read_string_from_stdin@plt>
movb   $0x0,(%rbx)
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@stdin = global i32 0
@stderr = global i32 0
@.str = [45 x i8] c"Fatal error: your input is invalid!\0AExiting\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [54 x i8] c"Error: your input is too long!\0APlease shorten it up.\0A\00"
@.str.2 = [32 x i8] c"Error: please input something!\0A\00"
define void @read_string_from_stdin(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
store i8* %0, i8** %2
%5 = load i8*, i8** %2
%6 = load i32, i32* @stdin
%7 = call i8* @fgets(i8* %5, i32 1000, i32 %6)
%8 = load i8*, i8** %2
%9 = icmp ne i8* %7, %8
br i1 %9, label %10, label %15
10: ; preds = %1
%11 = load i32, i32* @stderr
%12 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0))
%13 = load i32, i32* @EXIT_FAILURE
%14 = call i32 @exit(i32 %13)
unreachable
15: ; preds = %1
%16 = load i8*, i8** %2
%17 = call i8* @strrchr(i8* %16, i8 signext 10)
store i8* %17, i8** %3
%18 = load i8*, i8** %3
%19 = icmp ne i8* %18, null
br i1 %19, label %31, label %20
20: ; preds = %15
br label %21
21: ; preds = %24, %20
%22 = call i32 (...) @getchar()
store i32 %22, i32* %4
%23 = icmp ne i32 %22, 10
br i1 %23, label %24, label %25
24: ; preds = %21
br label %21
25: ; preds = %21
%26 = load i32, i32* @stderr
%27 = call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.1, i64 0, i64 0))
%28 = load i8*, i8** %2
%29 = call i32 @memset(i8* %28, i32 0, i32 1000)
%30 = load i8*, i8** %2
br label %47
31: ; preds = %15
%32 = load i8*, i8** %3
%33 = load i8, i8* %32
%34 = sext i8 %33 to i32
%35 = load i8*, i8** %2
%36 = getelementptr inbounds i8, i8* %35, i64 0
%37 = load i8, i8* %36
%38 = sext i8 %37 to i32
%39 = icmp eq i32 %34, %38
br i1 %39, label %40, label %45
40: ; preds = %31
%41 = load i32, i32* @stderr
%42 = call i32 @fprintf(i32 %41, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0))
%43 = load i8*, i8** %3
store i8 0, i8* %43
%44 = load i8*, i8** %2
br label %45
45: ; preds = %40, %31
%46 = load i8*, i8** %3
store i8 0, i8* %46
br label %47
47: ; preds = %45, %25
ret void
}

LLVM IR (-O2):
@stdin = global i32 0
@stderr = global i32 0
@.str = [45 x i8] c"Fatal error: your input is invalid!\0AExiting\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [54 x i8] c"Error: your input is too long!\0APlease shorten it up.\0A\00"
@.str.2 = [32 x i8] c"Error: please input something!\0A\00"
define void @read_string_from_stdin(i8* %0) {
%2 = load i32, i32* @stdin
%3 = tail call i8* @fgets(i8* %0, i32 1000, i32 %2)
%4 = icmp eq i8* %3, %0
br i1 %4, label %10, label %5
5: ; preds = %16, %1
%6 = load i32, i32* @stderr
%7 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0))
%8 = load i32, i32* @EXIT_FAILURE
%9 = tail call i32 @exit(i32 %8)
unreachable
10: ; preds = %1, %16
%11 = tail call i8* @strrchr(i8* dereferenceable(1) %0, i8 signext 10)
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %23
13: ; preds = %10, %13
%14 = tail call i32 (...) @getchar()
%15 = icmp eq i32 %14, 10
br i1 %15, label %16, label %13
16: ; preds = %13
%17 = load i32, i32* @stderr
%18 = tail call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.1, i64 0, i64 0))
%19 = tail call i32 @memset(i8* %0, i32 0, i32 1000)
%20 = load i32, i32* @stdin
%21 = tail call i8* @fgets(i8* %0, i32 1000, i32 %20)
%22 = icmp eq i8* %21, %0
br i1 %22, label %10, label %5
23: ; preds = %10
%24 = load i8, i8* %11
%25 = load i8, i8* %0
%26 = icmp eq i8 %24, %25
br i1 %26, label %27, label %30
27: ; preds = %23
%28 = load i32, i32* @stderr
%29 = tail call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0))
store i8 0, i8* %11
tail call void @read_string_from_stdin(i8* %0)
br label %30
30: ; preds = %27, %23
store i8 0, i8* %11
ret void
}