#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 /*<<< orphan*/ exit(int);
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int fscanf(int /*<<< orphan*/ *, char *, int *);
int /*<<< orphan*/ stderr;
int readInt(FILE *fp) {
  int x, result;
  result = fscanf(fp, "%d", &x);
  if (result == EOF) {
    return 0;
  }
  if (result == 0) {
    fprintf(stderr, "SCAN ERROR: attempt to read an integer failed\n");
    fprintf(stderr, "offending character was <%c>\n", fgetc(fp));
    exit(1);
  }
  return x;
}


x86 assembly (clang, -O0):
<readInt>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xea9(%rip),%rsi
lea    -0x14(%rbp),%rdx
call   1030 <fscanf@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    0x2e7b(%rip),%rcx
cmp    (%rcx),%eax
jne    1181 <readInt+0x41>
movl   $0x0,-0x4(%rbp)
jmp    11da <readInt+0x9a>
cmpl   $0x0,-0x18(%rbp)
jne    11d4 <readInt+0x94>
mov    0x2e46(%rip),%rax
mov    (%rax),%edi
lea    0xe68(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2e2f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <fgetc@plt>
mov    -0x1c(%rbp),%edi
mov    %eax,%edx
lea    0xe6f(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readInt>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
lea    0xeb3(%rip),%rsi
lea    0x4(%rsp),%rdx
call   1030 <fscanf@plt>
mov    %eax,%ecx
mov    0x2e88(%rip),%rdx
xor    %eax,%eax
cmp    (%rdx),%ecx
je     116e <readInt+0x2e>
test   %ecx,%ecx
je     1175 <readInt+0x35>
mov    0x4(%rsp),%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2e5c(%rip),%rbp
mov    0x0(%rbp),%edi
lea    0xe7d(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    0x0(%rbp),%ebp
mov    %rbx,%rdi
call   1040 <fgetc@plt>
lea    0xe93(%rip),%rsi
mov    %ebp,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"%d\00"
@EOF = global i32 0
@stderr = global i32 0
@.str.1 = [47 x i8] c"SCAN ERROR: attempt to read an integer failed\0A\00"
@.str.2 = [30 x i8] c"offending character was <%c>\0A\00"
define i32 @readInt(i32* %0) {
%2 = alloca i32
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
store i32* %0, i32** %3
%6 = load i32*, i32** %3
%7 = call i32 @fscanf(i32* %6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %4)
store i32 %7, i32* %5
%8 = load i32, i32* %5
%9 = load i32, i32* @EOF
%10 = icmp eq i32 %8, %9
br i1 %10, label %11, label %12
11: ; preds = %1
store i32 0, i32* %2
br label %25
12: ; preds = %1
%13 = load i32, i32* %5
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %23
15: ; preds = %12
%16 = load i32, i32* @stderr
%17 = call i32 (i32, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.1, i64 0, i64 0))
%18 = load i32, i32* @stderr
%19 = load i32*, i32** %3
%20 = call i32 @fgetc(i32* %19)
%21 = call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i32 %20)
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %12
%24 = load i32, i32* %4
store i32 %24, i32* %2
br label %25
25: ; preds = %23, %11
%26 = load i32, i32* %2
ret i32 %26
}

LLVM IR (-O2):
@.str = [3 x i8] c"%d\00"
@EOF = global i32 0
@stderr = global i32 0
@.str.1 = [47 x i8] c"SCAN ERROR: attempt to read an integer failed\0A\00"
@.str.2 = [30 x i8] c"offending character was <%c>\0A\00"
define i32 @readInt(i32* nocapture %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = call i32 @fscanf(i32* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %2)
%5 = load i32, i32* @EOF
%6 = icmp eq i32 %4, %5
br i1 %6, label %18, label %7
7: ; preds = %1
%8 = icmp eq i32 %4, 0
br i1 %8, label %9, label %16
9: ; preds = %7
%10 = load i32, i32* @stderr
%11 = call i32 (i32, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.1, i64 0, i64 0))
%12 = load i32, i32* @stderr
%13 = call i32 @fgetc(i32* %0)
%14 = call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i32 %13)
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %7
%17 = load i32, i32* %2
br label %18
18: ; preds = %1, %16
%19 = phi i32 [ %17, %16 ], [ 0, %1 ]
ret i32 %19
}