#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 *, size_t, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ stdin;
char *strchr(char *, char);
void read_input(char *s, size_t size) {
  s[size - 2] = '\0';
  if (fgets(s, size, stdin) == NULL) {
    printf("error: failed reading input\n");
    s[0] = '\0';
  } else if (s[size - 2] != '\0') {
    printf("error: too much input\n");
    s[0] = '\0';
  }
  char *new_line = strchr(s, '\0');
  if (new_line)
    *new_line = '\0';
}


x86 assembly (clang, -O0):
<read_input>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    $0x2,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    0x2e89(%rip),%rax
mov    (%rax),%edx
call   1050 <fgets@plt>
cmp    $0x0,%rax
jne    1188 <read_input+0x58>
lea    0xe89(%rip),%rdi
call   1040 <printf@plt>
mov    -0x8(%rbp),%rax
movb   $0x0,(%rax)
jmp    11b9 <read_input+0x89>
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    $0x2,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
je     11b4 <read_input+0x84>
lea    0xe75(%rip),%rdi
call   1040 <printf@plt>
mov    -0x8(%rbp),%rax
movb   $0x0,(%rax)
jmp    11b9 <read_input+0x89>
mov    -0x8(%rbp),%rdi
xor    %esi,%esi
call   1030 <strchr@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11da <read_input+0xaa>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_input>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
movb   $0x0,-0x2(%rsi,%rdi,1)
mov    0x2ea2(%rip),%rax
mov    (%rax),%edx
call   1050 <fgets@plt>
test   %rax,%rax
je     1163 <read_input+0x33>
cmpb   $0x0,-0x2(%rbx,%r14,1)
je     1172 <read_input+0x42>
lea    0xe9f(%rip),%rdi
jmp    116a <read_input+0x3a>
lea    0xeac(%rip),%rdi
call   1030 <puts@plt>
movb   $0x0,(%rbx)
mov    %rbx,%rdi
call   1040 <strlen@plt>
add    %rbx,%rax
je     1182 <read_input+0x52>
movb   $0x0,(%rax)
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
@stdin = global i32 0
@str = [22 x i8] c"error: too much input\00"
@str.2 = [28 x i8] c"error: failed reading input\00"
define void @read_input(i8* %0, i64 %1) {
%3 = add i64 %1, -2
%4 = getelementptr inbounds i8, i8* %0, i64 %3
store i8 0, i8* %4
%5 = load i32, i32* @stdin
%6 = tail call i32* @fgets(i8* %0, i64 %1, i32 %5)
%7 = icmp eq i32* %6, null
br i1 %7, label %11, label %8
8: ; preds = %2
%9 = load i8, i8* %4
%10 = icmp eq i8 %9, 0
br i1 %10, label %14, label %11
11: ; preds = %8, %2
%12 = phi i8* [ getelementptr inbounds ([28 x i8], [28 x i8]* @str.2, i64 0, i64 0), %2 ], [ getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0), %8 ]
%13 = tail call i32 @puts(i8* dereferenceable(1) %12)
store i8 0, i8* %0
br label %14
14: ; preds = %11, %8
%15 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%16 = getelementptr i8, i8* %0, i64 %15
%17 = icmp eq i8* %16, null
br i1 %17, label %19, label %18
18: ; preds = %14
store i8 0, i8* %16
br label %19
19: ; preds = %18, %14
ret void
}