#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 ferror(int /*<<< orphan*/);
int /*<<< orphan*/ flush_to_eol(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int fscanf(int /*<<< orphan*/, char *, int *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
char *strerror(int);
void read_int(int *ptr) {
  int result;
  printf("new value: ");
  result = fscanf(stdin, "%i", ptr);
  if (result != 1) {
    fprintf(stderr, "Input value must be integer");
    if (EOF == result) {
      int stdin_err = ferror(stdin);
      if (stdin_err != 0) {
        fprintf(stderr, ", error: %d (%s)", errno, strerror(errno));
      } else {
        fprintf(stderr, ", result: %d (Reached end of input)", result);
      }
    }
    fprintf(stderr, "\n");
  }
  flush_to_eol(0);
}


x86 assembly (clang, -O0):
<read_int>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
lea    0xe8d(%rip),%rdi
call   1050 <printf@plt>
mov    0x2e69(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe80(%rip),%rsi
call   1060 <fscanf@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x1,-0xc(%rbp)
je     125a <read_int+0xfa>
mov    0x2e2b(%rip),%rax
mov    (%rax),%edi
lea    0xe61(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    0x2e24(%rip),%rax
mov    (%rax),%eax
cmp    -0xc(%rbp),%eax
jne    1243 <read_int+0xe3>
mov    0x2e1a(%rip),%rax
mov    (%rax),%edi
call   1030 <ferror@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
je     1224 <read_int+0xc4>
mov    0x2de7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2dd3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    0x2dc7(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x18(%rbp),%edi
mov    -0x14(%rbp),%edx
mov    %rax,%rcx
lea    0xe13(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
jmp    123e <read_int+0xde>
mov    0x2da5(%rip),%rax
mov    (%rax),%edi
mov    -0xc(%rbp),%edx
lea    0xe05(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
jmp    1243 <read_int+0xe3>
mov    0x2d86(%rip),%rax
mov    (%rax),%edi
lea    0xe0d(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
xor    %edi,%edi
call   1040 <flush_to_eol@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_int>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%rbx
lea    0xe92(%rip),%rdi
call   1050 <printf@plt>
mov    0x2e6e(%rip),%rbp
mov    0x0(%rbp),%edi
lea    0xe88(%rip),%rsi
mov    %rbx,%rdx
call   1060 <fscanf@plt>
cmp    $0x1,%eax
je     120f <read_int+0xaf>
mov    %eax,%ebx
mov    0x2e36(%rip),%r14
mov    (%r14),%edi
lea    0xe6b(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    0x2e2e(%rip),%rax
cmp    %ebx,(%rax)
jne    11fe <read_int+0x9e>
mov    0x0(%rbp),%edi
call   1030 <ferror@plt>
mov    (%r14),%ebp
test   %eax,%eax
je     11ec <read_int+0x8c>
mov    0x2dfc(%rip),%rax
mov    (%rax),%ebx
mov    %ebx,%edi
call   1080 <strerror@plt>
lea    0xe4f(%rip),%rsi
mov    %ebp,%edi
mov    %ebx,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    11fe <read_int+0x9e>
lea    0xe49(%rip),%rsi
mov    %ebp,%edi
mov    %ebx,%edx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    (%r14),%edi
lea    0xe58(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
xor    %edi,%edi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1040 <flush_to_eol@plt>

LLVM IR (-O0):
@.str = [12 x i8] c"new value: \00"
@stdin = global i32 0
@.str.1 = [3 x i8] c"%i\00"
@stderr = global i32 0
@.str.2 = [28 x i8] c"Input value must be integer\00"
@EOF = global i32 0
@.str.3 = [17 x i8] c", error: %d (%s)\00"
@errno = global i32 0
@.str.4 = [36 x i8] c", result: %d (Reached end of input)\00"
@.str.5 = [2 x i8] c"\0A\00"
define void @read_int(i32* %0) {
%2 = alloca i32*
%3 = alloca i32
%4 = alloca i32
store i32* %0, i32** %2
%5 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%6 = load i32, i32* @stdin
%7 = load i32*, i32** %2
%8 = call i32 @fscanf(i32 %6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %7)
store i32 %8, i32* %3
%9 = load i32, i32* %3
%10 = icmp ne i32 %9, 1
br i1 %10, label %11, label %36
11: ; preds = %1
%12 = load i32, i32* @stderr
%13 = call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
%14 = load i32, i32* @EOF
%15 = load i32, i32* %3
%16 = icmp eq i32 %14, %15
br i1 %16, label %17, label %33
17: ; preds = %11
%18 = load i32, i32* @stdin
%19 = call i32 @ferror(i32 %18)
store i32 %19, i32* %4
%20 = load i32, i32* %4
%21 = icmp ne i32 %20, 0
br i1 %21, label %22, label %28
22: ; preds = %17
%23 = load i32, i32* @stderr
%24 = load i32, i32* @errno
%25 = load i32, i32* @errno
%26 = call i8* @strerror(i32 %25)
%27 = call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i32 %24, i8* %26)
br label %32
28: ; preds = %17
%29 = load i32, i32* @stderr
%30 = load i32, i32* %3
%31 = call i32 (i32, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0), i32 %30)
br label %32
32: ; preds = %28, %22
br label %33
33: ; preds = %32, %11
%34 = load i32, i32* @stderr
%35 = call i32 (i32, i8*, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
br label %36
36: ; preds = %33, %1
%37 = call i32 @flush_to_eol(i32 0)
ret void
}

LLVM IR (-O2):
@.str = [12 x i8] c"new value: \00"
@stdin = global i32 0
@.str.1 = [3 x i8] c"%i\00"
@stderr = global i32 0
@.str.2 = [28 x i8] c"Input value must be integer\00"
@EOF = global i32 0
@.str.3 = [17 x i8] c", error: %d (%s)\00"
@errno = global i32 0
@.str.4 = [36 x i8] c", result: %d (Reached end of input)\00"
@.str.5 = [2 x i8] c"\0A\00"
define void @read_int(i32* %0) {
%2 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%3 = load i32, i32* @stdin
%4 = tail call i32 @fscanf(i32 %3, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %0)
%5 = icmp eq i32 %4, 1
br i1 %5, label %25, label %6
6: ; preds = %1
%7 = load i32, i32* @stderr
%8 = tail call i32 (i32, i8*, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
%9 = load i32, i32* @EOF
%10 = icmp eq i32 %9, %4
br i1 %10, label %11, label %22
11: ; preds = %6
%12 = load i32, i32* @stdin
%13 = tail call i32 @ferror(i32 %12)
%14 = icmp eq i32 %13, 0
%15 = load i32, i32* @stderr
br i1 %14, label %20, label %16
16: ; preds = %11
%17 = load i32, i32* @errno
%18 = tail call i8* @strerror(i32 %17)
%19 = tail call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i32 %17, i8* %18)
br label %22
20: ; preds = %11
%21 = tail call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0), i32 %4)
br label %22
22: ; preds = %16, %20, %6
%23 = load i32, i32* @stderr
%24 = tail call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
br label %25
25: ; preds = %22, %1
%26 = tail call i32 @flush_to_eol(i32 0)
ret void
}