#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*/ buffer;

/* Variables and functions */
scalar_t__ errno;
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
int sscanf(char *, char *, int *, int *);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
int getint(int *i) {
  int nope = 0;
  int end;
  char buffer[100];
  do {
    if (nope == 1)
      fputs("Invalid input, try again.\n", stdout);
    nope = 1;
    if (NULL == fgets(buffer, sizeof(buffer), stdin))
      return -1;
    errno = 0;
  } while ((1 != sscanf(buffer, "%d %n", i, &end)) || buffer[end] || errno);
  return 0;
}


x86 assembly (clang, -O0):
<getint>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0x1,-0x14(%rbp)
jne    1165 <getint+0x35>
mov    0x2e79(%rip),%rax
mov    (%rax),%esi
lea    0xea0(%rip),%rdi
call   1030 <fputs@plt>
movl   $0x1,-0x14(%rbp)
lea    -0x80(%rbp),%rdi
mov    0x2e71(%rip),%rax
mov    (%rax),%edx
mov    $0x64,%esi
call   1040 <fgets@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    119d <getint+0x6d>
movl   $0xffffffff,-0x4(%rbp)
jmp    121d <getint+0xed>
mov    0x2e34(%rip),%rax
movq   $0x0,(%rax)
lea    -0x80(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xe61(%rip),%rsi
lea    -0x18(%rbp),%rcx
call   1050 <sscanf@plt>
mov    %eax,%edx
mov    $0x1,%al
mov    $0x1,%ecx
cmp    %edx,%ecx
mov    %al,-0x81(%rbp)
jne    1208 <getint+0xd8>
movslq -0x18(%rbp),%rax
movsbl -0x80(%rbp,%rax,1),%ecx
mov    $0x1,%al
cmp    $0x0,%ecx
mov    %al,-0x81(%rbp)
jne    1208 <getint+0xd8>
mov    0x2ddd(%rip),%rax
cmpq   $0x0,(%rax)
setne  %al
mov    %al,-0x81(%rbp)
mov    -0x81(%rbp),%al
test   $0x1,%al
jne    1146 <getint+0x16>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getint>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x78,%rsp
mov    %rdi,%rbx
mov    0x2ea0(%rip),%rax
mov    (%rax),%edx
lea    0x10(%rsp),%rdi
mov    $0x64,%esi
call   1040 <fgets@plt>
mov    $0xffffffff,%ecx
test   %rax,%rax
je     11f2 <getint+0xc2>
mov    0x2e6a(%rip),%r14
lea    0x10(%rsp),%rbp
lea    0xc(%rsp),%r12
mov    0x2e51(%rip),%r15
lea    0xe7a(%rip),%r13
jmp    11b6 <getint+0x86>
nopl   0x0(%rax,%rax,1)
mov    (%r15),%esi
mov    %r13,%rdi
call   1030 <fputs@plt>
mov    0x2e46(%rip),%rax
mov    (%rax),%edx
mov    %rbp,%rdi
mov    $0x64,%esi
call   1040 <fgets@plt>
test   %rax,%rax
je     11ed <getint+0xbd>
movq   $0x0,(%r14)
mov    %rbp,%rdi
lea    0xe54(%rip),%rsi
mov    %rbx,%rdx
mov    %r12,%rcx
call   1050 <sscanf@plt>
cmp    $0x1,%eax
jne    1190 <getint+0x60>
movslq 0xc(%rsp),%rax
cmpb   $0x0,0x10(%rsp,%rax,1)
jne    1190 <getint+0x60>
cmpq   $0x0,(%r14)
jne    1190 <getint+0x60>
xor    %ecx,%ecx
jmp    11f2 <getint+0xc2>
mov    $0xffffffff,%ecx
mov    %ecx,%eax
add    $0x78,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [27 x i8] c"Invalid input, try again.\0A\00"
@stdout = global i32 0
@stdin = global i32 0
@errno = global i64 0
@.str.1 = [6 x i8] c"%d %n\00"
define i32 @getint(i32* %0) {
%2 = alloca i32
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
%6 = alloca [100 x i8]
store i32* %0, i32** %3
store i32 0, i32* %4
br label %7
7: ; preds = %35, %1
%8 = load i32, i32* %4
%9 = icmp eq i32 %8, 1
br i1 %9, label %10, label %13
10: ; preds = %7
%11 = load i32, i32* @stdout
%12 = call i32 @fputs(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i32 %11)
br label %13
13: ; preds = %10, %7
store i32 1, i32* %4
%14 = getelementptr inbounds [100 x i8], [100 x i8]* %6, i64 0, i64 0
%15 = load i32, i32* @stdin
%16 = call i32* @fgets(i8* %14, i32 100, i32 %15)
%17 = icmp eq i32* null, %16
br i1 %17, label %18, label %19
18: ; preds = %13
store i32 -1, i32* %2
br label %38
19: ; preds = %13
store i64 0, i64* @errno
br label %20
20: ; preds = %19
%21 = getelementptr inbounds [100 x i8], [100 x i8]* %6, i64 0, i64 0
%22 = load i32*, i32** %3
%23 = call i32 @sscanf(i8* %21, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32* %22, i32* %5)
%24 = icmp ne i32 1, %23
br i1 %24, label %35, label %25
25: ; preds = %20
%26 = load i32, i32* %5
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds [100 x i8], [100 x i8]* %6, i64 0, i64 %27
%29 = load i8, i8* %28
%30 = sext i8 %29 to i32
%31 = icmp ne i32 %30, 0
br i1 %31, label %35, label %32
32: ; preds = %25
%33 = load i64, i64* @errno
%34 = icmp ne i64 %33, 0
br label %35
35: ; preds = %32, %25, %20
%36 = phi i1 [ true, %25 ], [ true, %20 ], [ %34, %32 ]
br i1 %36, label %7, label %37
37: ; preds = %35
store i32 0, i32* %2
br label %38
38: ; preds = %37, %18
%39 = load i32, i32* %2
ret i32 %39
}

LLVM IR (-O2):
@.str = [27 x i8] c"Invalid input, try again.\0A\00"
@stdout = global i32 0
@stdin = global i32 0
@errno = global i64 0
@.str.1 = [6 x i8] c"%d %n\00"
define i32 @getint(i32* %0) {
%2 = alloca i32
%3 = alloca [100 x i8]
%4 = bitcast i32* %2 to i8*
%5 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%6 = load i32, i32* @stdin
%7 = call i32* @fgets(i8* %5, i32 100, i32 %6)
%8 = icmp eq i32* %7, null
br i1 %8, label %27, label %15
9: ; preds = %18, %15
%10 = load i32, i32* @stdout
%11 = call i32 @fputs(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = load i32, i32* @stdin
%13 = call i32* @fgets(i8* %5, i32 100, i32 %12)
%14 = icmp eq i32* %13, null
br i1 %14, label %27, label %15
15: ; preds = %1, %9
store i64 0, i64* @errno
%16 = call i32 @sscanf(i8* %5, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32* %0, i32* %2)
%17 = icmp eq i32 %16, 1
br i1 %17, label %18, label %9
18: ; preds = %15
%19 = load i32, i32* %2
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 %20
%22 = load i8, i8* %21
%23 = icmp ne i8 %22, 0
%24 = load i64, i64* @errno
%25 = icmp ne i64 %24, 0
%26 = select i1 %23, i1 true, i1 %25
br i1 %26, label %9, label %27
27: ; preds = %9, %18, %1
%28 = phi i32 [ -1, %1 ], [ 0, %18 ], [ -1, %9 ]
ret i32 %28
}