#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 EOF;
int errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, ...);
int scanf(char *, int *);
int /*<<< orphan*/ stderr;
char *strerror(int);
int scanf_int_safe(char *error_string) {
  errno = 0;
  int val;
  int scanf_count;
  scanf_count = scanf("%d", &val);
  if (scanf_count == 1) {
    return val;
  }
  if (scanf_count == EOF) {
    fprintf(stderr, "Mismatch when reading %s.\n", error_string);
    exit(4);
  }
  fprintf(stderr, "Error reading %s: %s\n", error_string, strerror(errno));
  exit(errno);
}


x86 assembly (clang, -O0):
<scanf_int_safe>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e7d(%rip),%rax
movl   $0x0,(%rax)
lea    0xea0(%rip),%rdi
lea    -0xc(%rbp),%rsi
call   1040 <scanf@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x1,-0x10(%rbp)
jne    117f <scanf_int_safe+0x3f>
mov    -0xc(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret
mov    -0x10(%rbp),%eax
mov    0x2e5f(%rip),%rcx
cmp    (%rcx),%eax
jne    11b6 <scanf_int_safe+0x76>
mov    0x2e40(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe5e(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    $0x4,%edi
call   1050 <exit@plt>
mov    0x2e1b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    0x2dff(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%rdx
mov    %rax,%rcx
lea    0xe35(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    0x2dd9(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>

x86 assembly (clang, -O2):
<scanf_int_safe>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
mov    0x2e7e(%rip),%rbp
movl   $0x0,0x0(%rbp)
lea    0xea0(%rip),%rdi
lea    0xc(%rsp),%rsi
call   1040 <scanf@plt>
cmp    $0x1,%eax
jne    117c <scanf_int_safe+0x3c>
mov    0xc(%rsp),%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2e65(%rip),%rcx
mov    0x2e4e(%rip),%rdx
mov    (%rdx),%r14d
cmp    (%rcx),%eax
jne    11af <scanf_int_safe+0x6f>
lea    0xe6b(%rip),%rsi
mov    %r14d,%edi
mov    %rbx,%rdx
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    $0x4,%edi
call   1050 <exit@plt>
mov    0x0(%rbp),%edi
call   1060 <strerror@plt>
lea    0xe60(%rip),%rsi
mov    %r14d,%edi
mov    %rbx,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    0x0(%rbp),%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@errno = global i32 0
@.str = [3 x i8] c"%d\00"
@EOF = global i32 0
@stderr = global i32 0
@.str.1 = [27 x i8] c"Mismatch when reading %s.\0A\00"
@.str.2 = [22 x i8] c"Error reading %s: %s\0A\00"
define i32 @scanf_int_safe(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
store i8* %0, i8** %2
store i32 0, i32* @errno
%5 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %3)
store i32 %5, i32* %4
%6 = load i32, i32* %4
%7 = icmp eq i32 %6, 1
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = load i32, i32* %3
ret i32 %9
10: ; preds = %1
%11 = load i32, i32* %4
%12 = load i32, i32* @EOF
%13 = icmp eq i32 %11, %12
br i1 %13, label %14, label %19
14: ; preds = %10
%15 = load i32, i32* @stderr
%16 = load i8*, i8** %2
%17 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %16)
%18 = call i32 @exit(i32 4)
unreachable
19: ; preds = %10
%20 = load i32, i32* @stderr
%21 = load i8*, i8** %2
%22 = load i32, i32* @errno
%23 = call i8* @strerror(i32 %22)
%24 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %21, i8* %23)
%25 = load i32, i32* @errno
%26 = call i32 @exit(i32 %25)
unreachable
}

LLVM IR (-O2):
@errno = global i32 0
@.str = [3 x i8] c"%d\00"
@EOF = global i32 0
@stderr = global i32 0
@.str.1 = [27 x i8] c"Mismatch when reading %s.\0A\00"
@.str.2 = [22 x i8] c"Error reading %s: %s\0A\00"
define i32 @scanf_int_safe(i8* %0) {
%2 = alloca i32
store i32 0, i32* @errno
%3 = bitcast i32* %2 to i8*
%4 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %2)
%5 = icmp eq i32 %4, 1
br i1 %5, label %6, label %8
6: ; preds = %1
%7 = load i32, i32* %2
ret i32 %7
8: ; preds = %1
%9 = load i32, i32* @EOF
%10 = icmp eq i32 %4, %9
%11 = load i32, i32* @stderr
br i1 %10, label %12, label %15
12: ; preds = %8
%13 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%14 = call i32 @exit(i32 4)
unreachable
15: ; preds = %8
%16 = load i32, i32* @errno
%17 = call i8* @strerror(i32 %16)
%18 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %0, i8* %17)
%19 = load i32, i32* @errno
%20 = call i32 @exit(i32 %19)
unreachable
}