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

/* Variables and functions */
int atoi(char *);
int /*<<< orphan*/ isdigit(char);
int /*<<< orphan*/ safe_gets(char const *, char *, int);
int safe_geti(const char *prompt, int defval) {
  char line[255];
  do {
    safe_gets(prompt, line, sizeof(line));
    if (defval && !line[0])
      return (defval);
  } while (!line[0] || !isdigit(line[0]));
  return (atoi(line));
}


x86 assembly (clang, -O0):
<safe_geti>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x120(%rbp),%rsi
mov    $0xff,%edx
call   1040 <safe_gets@plt>
cmpl   $0x0,-0x14(%rbp)
je     1179 <safe_geti+0x49>
cmpb   $0x0,-0x120(%rbp)
jne    1179 <safe_geti+0x49>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11ca <safe_geti+0x9a>
jmp    117e <safe_geti+0x4e>
mov    $0x1,%al
cmpb   $0x0,-0x120(%rbp)
mov    %al,-0x121(%rbp)
je     11ad <safe_geti+0x7d>
movsbl -0x120(%rbp),%edi
call   1030 <isdigit@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
mov    %al,-0x121(%rbp)
mov    -0x121(%rbp),%al
test   $0x1,%al
jne    1142 <safe_geti+0x12>
lea    -0x120(%rbp),%rdi
call   1050 <atoi@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<safe_geti>:
push   %rbp
push   %r14
push   %rbx
sub    $0x100,%rsp
mov    %esi,%ebp
mov    %rdi,%rbx
mov    %rsp,%rsi
mov    $0xff,%edx
call   1040 <safe_gets@plt>
mov    (%rsp),%al
test   %al,%al
setne  %cl
test   %ebp,%ebp
je     115d <safe_geti+0x2d>
test   %al,%al
je     119e <safe_geti+0x6e>
mov    %rsp,%r14
test   $0x1,%cl
je     1171 <safe_geti+0x41>
movsbl %al,%edi
call   1030 <isdigit@plt>
test   %eax,%eax
jne    1194 <safe_geti+0x64>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    $0xff,%edx
call   1040 <safe_gets@plt>
movzbl (%rsp),%eax
test   %al,%al
setne  %cl
test   %ebp,%ebp
je     1160 <safe_geti+0x30>
test   %al,%al
jne    1160 <safe_geti+0x30>
jmp    119e <safe_geti+0x6e>
mov    %rsp,%rdi
call   1050 <atoi@plt>
mov    %eax,%ebp
mov    %ebp,%eax
add    $0x100,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @safe_geti(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca [255 x i8]
store i8* %0, i8** %4
store i32 %1, i32* %5
br label %7
7: ; preds = %30, %2
%8 = load i8*, i8** %4
%9 = getelementptr inbounds [255 x i8], [255 x i8]* %6, i64 0, i64 0
%10 = call i32 @safe_gets(i8* %8, i8* %9, i32 255)
%11 = load i32, i32* %5
%12 = icmp ne i32 %11, 0
br i1 %12, label %13, label %19
13: ; preds = %7
%14 = getelementptr inbounds [255 x i8], [255 x i8]* %6, i64 0, i64 0
%15 = load i8, i8* %14
%16 = icmp ne i8 %15, 0
br i1 %16, label %19, label %17
17: ; preds = %13
%18 = load i32, i32* %5
store i32 %18, i32* %3
br label %35
19: ; preds = %13, %7
br label %20
20: ; preds = %19
%21 = getelementptr inbounds [255 x i8], [255 x i8]* %6, i64 0, i64 0
%22 = load i8, i8* %21
%23 = icmp ne i8 %22, 0
br i1 %23, label %24, label %30
24: ; preds = %20
%25 = getelementptr inbounds [255 x i8], [255 x i8]* %6, i64 0, i64 0
%26 = load i8, i8* %25
%27 = call i32 @isdigit(i8 signext %26)
%28 = icmp ne i32 %27, 0
%29 = xor i1 %28, true
br label %30
30: ; preds = %24, %20
%31 = phi i1 [ true, %20 ], [ %29, %24 ]
br i1 %31, label %7, label %32
32: ; preds = %30
%33 = getelementptr inbounds [255 x i8], [255 x i8]* %6, i64 0, i64 0
%34 = call i32 @atoi(i8* %33)
store i32 %34, i32* %3
br label %35
35: ; preds = %32, %17
%36 = load i32, i32* %3
ret i32 %36
}

LLVM IR (-O2):
define i32 @safe_geti(i8* %0, i32 %1) {
%3 = alloca [255 x i8]
%4 = getelementptr inbounds [255 x i8], [255 x i8]* %3, i64 0, i64 0
%5 = call i32 @safe_gets(i8* %0, i8* %4, i32 255)
%6 = icmp eq i32 %1, 0
%7 = load i8, i8* %4
%8 = icmp ne i8 %7, 0
%9 = select i1 %6, i1 true, i1 %8
br i1 %9, label %10, label %23
10: ; preds = %2, %16
%11 = phi i1 [ %19, %16 ], [ %8, %2 ]
%12 = phi i8 [ %18, %16 ], [ %7, %2 ]
br i1 %11, label %13, label %16
13: ; preds = %10
%14 = call i32 @isdigit(i8 signext %12)
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %21
16: ; preds = %13, %10
%17 = call i32 @safe_gets(i8* %0, i8* %4, i32 255)
%18 = load i8, i8* %4
%19 = icmp ne i8 %18, 0
%20 = select i1 %6, i1 true, i1 %19
br i1 %20, label %10, label %23
21: ; preds = %13
%22 = call i32 @atoi(i8* %4)
br label %23
23: ; preds = %16, %2, %21
%24 = phi i32 [ %22, %21 ], [ %1, %2 ], [ %1, %16 ]
ret i32 %24
}