#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 atoi(char *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ stdin;
void pnc_read_int_from_console_optional(char const *message, int *val,
                                        bool optional) {
  char buf[8];
  bool first_try = true;
  do {
    if (!first_try) {
      puts("Invalid input. Try again.");
    }
    printf("Enter %s %s\n", message,
           optional ? " ( Optional input. You can skip by pressing enter )"
                    : "");
    printf("> ");
    fgets(buf, sizeof buf, stdin);
    *val = atoi(buf);
    first_try = false;
  } while (*val < -1 && !optional);
  printf("%s: %d\n", message, *val);
}


x86 assembly (clang, -O0):
<pnc_read_int_from_console_optional>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x1,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jne    1170 <pnc_read_int_from_console_optional+0x30>
lea    0xe95(%rip),%rdi
call   1030 <puts@plt>
mov    -0x8(%rbp),%rsi
mov    -0x14(%rbp),%ecx
lea    0xee7(%rip),%rdx
lea    0xea2(%rip),%rax
cmp    $0x0,%ecx
cmovne %rax,%rdx
lea    0xe87(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xeba(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x1c(%rbp),%rdi
mov    0x2e35(%rip),%rax
mov    (%rax),%edx
mov    $0x8,%esi
call   1050 <fgets@plt>
lea    -0x1c(%rbp),%rdi
call   1060 <atoi@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rcx
xor    %eax,%eax
cmpl   $0xffffffff,(%rcx)
mov    %al,-0x21(%rbp)
jge    11f5 <pnc_read_int_from_console_optional+0xb5>
cmpl   $0x0,-0x14(%rbp)
setne  %al
xor    $0xff,%al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    115a <pnc_read_int_from_console_optional+0x1a>
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rax
mov    (%rax),%edx
lea    0xe4d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pnc_read_int_from_console_optional>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r12d
mov    %rsi,%r15
mov    %rdi,%r14
test   %edx,%edx
lea    0xf08(%rip),%rax
lea    0xec3(%rip),%rbx
cmove  %rax,%rbx
lea    0xeab(%rip),%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xed8(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2e57(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    $0x8,%esi
call   1050 <fgets@plt>
mov    %rbp,%rdi
call   1060 <atoi@plt>
mov    %eax,(%r15)
test   %r12d,%r12d
jne    121c <pnc_read_int_from_console_optional+0xdc>
cmp    $0xfffffffe,%eax
jg     121c <pnc_read_int_from_console_optional+0xdc>
lea    0xe5b(%rip),%r13
lea    0xe95(%rip),%rbp
mov    %rsp,%r12
nopl   0x0(%rax)
lea    0xe29(%rip),%rdi
call   1030 <puts@plt>
mov    %r13,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %rbp,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2deb(%rip),%rax
mov    (%rax),%edx
mov    %r12,%rdi
mov    $0x8,%esi
call   1050 <fgets@plt>
mov    %r12,%rdi
call   1060 <atoi@plt>
mov    %eax,(%r15)
cmp    $0xffffffff,%eax
jl     11d0 <pnc_read_int_from_console_optional+0x90>
lea    0xe3b(%rip),%rdi
mov    %r14,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <printf@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [26 x i8] c"Invalid input. Try again.\00"
@.str.1 = [13 x i8] c"Enter %s %s\0A\00"
@.str.2 = [52 x i8] c" ( Optional input. You can skip by pressing enter )\00"
@.str.3 = [1 x i8] zeroinitializer
@.str.4 = [3 x i8] c"> \00"
@stdin = global i32 0
@.str.5 = [8 x i8] c"%s: %d\0A\00"
define void @pnc_read_int_from_console_optional(i8* %0, i32* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca [8 x i8]
%8 = alloca i32
store i8* %0, i8** %4
store i32* %1, i32** %5
store i32 %2, i32* %6
store i32 1, i32* %8
br label %9
9: ; preds = %36, %3
%10 = load i32, i32* %8
%11 = icmp ne i32 %10, 0
br i1 %11, label %14, label %12
12: ; preds = %9
%13 = call i32 @puts(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
br label %14
14: ; preds = %12, %9
%15 = load i8*, i8** %4
%16 = load i32, i32* %6
%17 = icmp ne i32 %16, 0
%18 = zext i1 %17 to i64
%19 = select i1 %17, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.3, i64 0, i64 0)
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* %15, i8* %19)
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%22 = getelementptr inbounds [8 x i8], [8 x i8]* %7, i64 0, i64 0
%23 = load i32, i32* @stdin
%24 = call i32 @fgets(i8* %22, i32 8, i32 %23)
%25 = getelementptr inbounds [8 x i8], [8 x i8]* %7, i64 0, i64 0
%26 = call i32 @atoi(i8* %25)
%27 = load i32*, i32** %5
store i32 %26, i32* %27
store i32 0, i32* %8
br label %28
28: ; preds = %14
%29 = load i32*, i32** %5
%30 = load i32, i32* %29
%31 = icmp slt i32 %30, -1
br i1 %31, label %32, label %36
32: ; preds = %28
%33 = load i32, i32* %6
%34 = icmp ne i32 %33, 0
%35 = xor i1 %34, true
br label %36
36: ; preds = %32, %28
%37 = phi i1 [ false, %28 ], [ %35, %32 ]
br i1 %37, label %9, label %38
38: ; preds = %36
%39 = load i8*, i8** %4
%40 = load i32*, i32** %5
%41 = load i32, i32* %40
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0), i8* %39, i32 %41)
ret void
}

LLVM IR (-O2):
@.str = [26 x i8] c"Invalid input. Try again.\00"
@.str.1 = [13 x i8] c"Enter %s %s\0A\00"
@.str.2 = [52 x i8] c" ( Optional input. You can skip by pressing enter )\00"
@.str.3 = [1 x i8] zeroinitializer
@.str.4 = [3 x i8] c"> \00"
@stdin = global i32 0
@.str.5 = [8 x i8] c"%s: %d\0A\00"
define void @pnc_read_int_from_console_optional(i8* %0, i32* nocapture writeonly %1, i32 %2) {
%4 = alloca [8 x i8]
%5 = getelementptr inbounds [8 x i8], [8 x i8]* %4, i64 0, i64 0
%6 = icmp eq i32 %2, 0
%7 = select i1 %6, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.2, i64 0, i64 0)
%8 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %7)
%9 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%10 = load i32, i32* @stdin
%11 = call i32 @fgets(i8* %5, i32 8, i32 %10)
%12 = call i32 @atoi(i8* %5)
store i32 %12, i32* %1
%13 = icmp slt i32 %12, -1
%14 = and i1 %6, %13
br i1 %14, label %15, label %23
15: ; preds = %3, %15
%16 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%17 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %7)
%18 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%19 = load i32, i32* @stdin
%20 = call i32 @fgets(i8* %5, i32 8, i32 %19)
%21 = call i32 @atoi(i8* %5)
store i32 %21, i32* %1
%22 = icmp slt i32 %21, -1
br i1 %22, label %15, label %23
23: ; preds = %15, %3
%24 = phi i32 [ %12, %3 ], [ %21, %15 ]
%25 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0), i8* %0, i32 %24)
ret void
}