#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 */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int EOF;
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, unsigned int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int input(FILE *f, unsigned int count, char begin, char end) {
  unsigned int i;
  int c;
  char expected = begin;
  for (i = 0; i < count; ++i) {
    c = fgetc(f);
    if (c == EOF)
      goto exit_bad_fgetc;
    if ((unsigned char)c != expected)
      goto exit_mismatch;
    if (expected == end)
      expected = begin;
    else
      ++expected;
  }
  if (fgetc(f) != EOF)
    goto exit_bad_eof;
  return 0;
exit_bad_fgetc:
  perror("error reading data");
  goto exit_fail;
exit_mismatch:
  fprintf(stderr, "failed to match with what was expected (at %uth data)", i);
  goto exit_fail;
exit_bad_eof:
  perror("error detecting eof after data were supposed to finish");
  goto exit_fail;
exit_fail:
  return -1;
}


x86 assembly (clang, -O0):
<input>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %cl,%al
mov    %dl,%cl
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %cl,-0x15(%rbp)
mov    %al,-0x16(%rbp)
mov    -0x15(%rbp),%al
mov    %al,-0x21(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jae    11d2 <input+0xa2>
mov    -0x10(%rbp),%rdi
call   1030 <fgetc@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    0x2e70(%rip),%rcx
cmp    (%rcx),%eax
jne    1185 <input+0x55>
jmp    11fb <input+0xcb>
mov    -0x20(%rbp),%eax
movzbl %al,%eax
movsbl -0x21(%rbp),%ecx
cmp    %ecx,%eax
je     119c <input+0x6c>
jmp    120c <input+0xdc>
movsbl -0x21(%rbp),%eax
movsbl -0x16(%rbp),%ecx
cmp    %ecx,%eax
jne    11b7 <input+0x87>
mov    -0x15(%rbp),%al
mov    %al,-0x21(%rbp)
jmp    11bf <input+0x8f>
mov    -0x21(%rbp),%al
add    $0x1,%al
mov    %al,-0x21(%rbp)
jmp    11c4 <input+0x94>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1156 <input+0x26>
mov    -0x10(%rbp),%rdi
call   1030 <fgetc@plt>
mov    0x2e06(%rip),%rcx
cmp    (%rcx),%eax
je     11ef <input+0xbf>
jmp    1229 <input+0xf9>
movl   $0x0,-0x4(%rbp)
jmp    123c <input+0x10c>
lea    0xdfe(%rip),%rdi
call   1050 <perror@plt>
jmp    1235 <input+0x105>
mov    0x2dc5(%rip),%rax
mov    (%rax),%edi
mov    -0x1c(%rbp),%edx
lea    0xdf4(%rip),%rsi
call   1040 <fprintf@plt>
jmp    1235 <input+0x105>
lea    0xe19(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<input>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r15
test   %esi,%esi
je     1182 <input+0x52>
mov    %ecx,%r14d
mov    %edx,%ebx
mov    %esi,%r12d
xor    %ebp,%ebp
movzbl %bl,%r13d
mov    %r15,%rdi
call   1030 <fgetc@plt>
mov    0x2e89(%rip),%rcx
cmp    (%rcx),%eax
je     11d2 <input+0xa2>
movzbl %al,%eax
movsbl %bl,%ecx
cmp    %ecx,%eax
jne    11a7 <input+0x77>
lea    0x1(%rbx),%eax
cmp    %r14b,%bl
movzbl %al,%ebx
cmove  %r13d,%ebx
add    $0x1,%ebp
cmp    %ebp,%r12d
jne    1150 <input+0x20>
mov    %r15,%rdi
call   1030 <fgetc@plt>
mov    %eax,%ecx
mov    0x2e55(%rip),%rdx
xor    %eax,%eax
cmp    (%rdx),%ecx
je     11c3 <input+0x93>
lea    0xea9(%rip),%rdi
call   1050 <perror@plt>
jmp    11be <input+0x8e>
mov    0x2e2a(%rip),%rax
mov    (%rax),%edi
lea    0xe5c(%rip),%rsi
mov    %ebp,%edx
call   1040 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe27(%rip),%rdi
call   1050 <perror@plt>
jmp    11be <input+0x8e>

LLVM IR (-O0):
@EOF = global i32 0
@.str = [19 x i8] c"error reading data\00"
@stderr = global i32 0
@.str.1 = [54 x i8] c"failed to match with what was expected (at %uth data)\00"
@.str.2 = [55 x i8] c"error detecting eof after data were supposed to finish\00"
define i32 @input(i32* %0, i32 %1, i8 signext %2, i8 signext %3) {
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i8
%9 = alloca i8
%10 = alloca i32
%11 = alloca i32
%12 = alloca i8
store i32* %0, i32** %6
store i32 %1, i32* %7
store i8 %2, i8* %8
store i8 %3, i8* %9
%13 = load i8, i8* %8
store i8 %13, i8* %12
store i32 0, i32* %10
br label %14
14: ; preds = %45, %4
%15 = load i32, i32* %10
%16 = load i32, i32* %7
%17 = icmp ult i32 %15, %16
br i1 %17, label %18, label %48
18: ; preds = %14
%19 = load i32*, i32** %6
%20 = call i32 @fgetc(i32* %19)
store i32 %20, i32* %11
%21 = load i32, i32* %11
%22 = load i32, i32* @EOF
%23 = icmp eq i32 %21, %22
br i1 %23, label %24, label %25
24: ; preds = %18
br label %55
25: ; preds = %18
%26 = load i32, i32* %11
%27 = trunc i32 %26 to i8
%28 = zext i8 %27 to i32
%29 = load i8, i8* %12
%30 = sext i8 %29 to i32
%31 = icmp ne i32 %28, %30
br i1 %31, label %32, label %33
32: ; preds = %25
br label %57
33: ; preds = %25
%34 = load i8, i8* %12
%35 = sext i8 %34 to i32
%36 = load i8, i8* %9
%37 = sext i8 %36 to i32
%38 = icmp eq i32 %35, %37
br i1 %38, label %39, label %41
39: ; preds = %33
%40 = load i8, i8* %8
store i8 %40, i8* %12
br label %44
41: ; preds = %33
%42 = load i8, i8* %12
%43 = add i8 %42, 1
store i8 %43, i8* %12
br label %44
44: ; preds = %41, %39
br label %45
45: ; preds = %44
%46 = load i32, i32* %10
%47 = add i32 %46, 1
store i32 %47, i32* %10
br label %14
48: ; preds = %14
%49 = load i32*, i32** %6
%50 = call i32 @fgetc(i32* %49)
%51 = load i32, i32* @EOF
%52 = icmp ne i32 %50, %51
br i1 %52, label %53, label %54
53: ; preds = %48
br label %61
54: ; preds = %48
store i32 0, i32* %5
br label %64
55: ; preds = %24
%56 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
br label %63
57: ; preds = %32
%58 = load i32, i32* @stderr
%59 = load i32, i32* %10
%60 = call i32 @fprintf(i32 %58, i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.1, i64 0, i64 0), i32 %59)
br label %63
61: ; preds = %53
%62 = call i32 @perror(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.2, i64 0, i64 0))
br label %63
63: ; preds = %61, %57, %55
store i32 -1, i32* %5
br label %64
64: ; preds = %63, %54
%65 = load i32, i32* %5
ret i32 %65
}

LLVM IR (-O2):
@EOF = global i32 0
@.str = [19 x i8] c"error reading data\00"
@stderr = global i32 0
@.str.1 = [54 x i8] c"failed to match with what was expected (at %uth data)\00"
@.str.2 = [55 x i8] c"error detecting eof after data were supposed to finish\00"
define i32 @input(i32* nocapture %0, i32 %1, i8 signext %2, i8 signext %3) {
%5 = icmp eq i32 %1, 0
br i1 %5, label %22, label %6
6: ; preds = %4, %16
%7 = phi i8 [ %19, %16 ], [ %2, %4 ]
%8 = phi i32 [ %20, %16 ], [ 0, %4 ]
%9 = tail call i32 @fgetc(i32* %0)
%10 = load i32, i32* @EOF
%11 = icmp eq i32 %9, %10
br i1 %11, label %26, label %12
12: ; preds = %6
%13 = and i32 %9, 255
%14 = sext i8 %7 to i32
%15 = icmp eq i32 %13, %14
br i1 %15, label %16, label %28
16: ; preds = %12
%17 = icmp eq i8 %7, %3
%18 = add i8 %7, 1
%19 = select i1 %17, i8 %2, i8 %18
%20 = add nuw i32 %8, 1
%21 = icmp eq i32 %20, %1
br i1 %21, label %22, label %6
22: ; preds = %16, %4
%23 = tail call i32 @fgetc(i32* %0)
%24 = load i32, i32* @EOF
%25 = icmp eq i32 %23, %24
br i1 %25, label %33, label %31
26: ; preds = %6
%27 = tail call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
br label %33
28: ; preds = %12
%29 = load i32, i32* @stderr
%30 = tail call i32 @fprintf(i32 %29, i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.1, i64 0, i64 0), i32 %8)
br label %33
31: ; preds = %22
%32 = tail call i32 @perror(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.2, i64 0, i64 0))
br label %33
33: ; preds = %26, %28, %31, %22
%34 = phi i32 [ 0, %22 ], [ -1, %31 ], [ -1, %28 ], [ -1, %26 ]
ret i32 %34
}