#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 /*<<< orphan*/ chomp(char *);
scalar_t__ equals_ic(char *, char *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char const *, char *);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ valid_bool_input(char *);
void pnc_read_bool_from_console_optional(char const *message, bool *val,
                                         bool optional) {
  char input[4];
  bool first_try = true;
  do {
    if (!first_try) {
      puts("Invalid input. Try again.");
    }
    printf("%s? (Enter Yes/No or Y/N ) %s\n> ", message,
           optional ? " (Optional input. You can skip by pressing enter)" : "");
    fgets(input, sizeof input, stdin);
    chomp(input);
    first_try = false;
  } while (!valid_bool_input(input) && !optional);
  *val = (equals_ic(input, "Y") || equals_ic(input, "yes"));
  printf("%s: %s\n", message, *val ? "true" : "false");
}


x86 assembly (clang, -O0):
<pnc_read_bool_from_console_optional>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x1,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    1190 <pnc_read_bool_from_console_optional+0x30>
lea    0xe75(%rip),%rdi
call   1030 <puts@plt>
mov    -0x8(%rbp),%rsi
mov    -0x14(%rbp),%ecx
lea    0xedc(%rip),%rdx
lea    0xe96(%rip),%rax
cmp    $0x0,%ecx
cmovne %rax,%rdx
lea    0xe67(%rip),%rdi
call   1050 <printf@plt>
lea    -0x18(%rbp),%rdi
mov    0x2e25(%rip),%rax
mov    (%rax),%edx
mov    $0x4,%esi
call   1060 <fgets@plt>
lea    -0x18(%rbp),%rdi
call   1070 <chomp@plt>
movl   $0x0,-0x1c(%rbp)
lea    -0x18(%rbp),%rdi
call   1080 <valid_bool_input@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x1d(%rbp)
jne    1204 <pnc_read_bool_from_console_optional+0xa4>
cmpl   $0x0,-0x14(%rbp)
setne  %al
xor    $0xff,%al
mov    %al,-0x1d(%rbp)
mov    -0x1d(%rbp),%al
test   $0x1,%al
jne    117a <pnc_read_bool_from_console_optional+0x1a>
lea    -0x18(%rbp),%rdi
lea    0xe53(%rip),%rsi
call   1040 <equals_ic@plt>
mov    %rax,%rcx
mov    $0x1,%al
cmp    $0x0,%rcx
mov    %al,-0x1e(%rbp)
jne    124b <pnc_read_bool_from_console_optional+0xeb>
lea    -0x18(%rbp),%rdi
lea    0xe33(%rip),%rsi
call   1040 <equals_ic@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,-0x1e(%rbp)
mov    -0x1e(%rbp),%al
and    $0x1,%al
movzbl %al,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rax
mov    (%rax),%ecx
lea    0xe16(%rip),%rdx
lea    0xe0a(%rip),%rax
cmp    $0x0,%ecx
cmovne %rax,%rdx
lea    0xdf4(%rip),%rdi
call   1050 <printf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pnc_read_bool_from_console_optional>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edx,%ebp
mov    %rsi,0x10(%rsp)
mov    %rdi,%r15
test   %edx,%edx
lea    0xef9(%rip),%rax
lea    0xeb3(%rip),%rbx
cmove  %rax,%rbx
lea    0xe87(%rip),%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1050 <printf@plt>
mov    0x2e43(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%r14
mov    %r14,%rdi
mov    $0x4,%esi
call   1060 <fgets@plt>
mov    %r14,%rdi
call   1070 <chomp@plt>
mov    %r14,%rdi
call   1080 <valid_bool_input@plt>
or     %ebp,%eax
jne    1220 <pnc_read_bool_from_console_optional+0xc0>
lea    0xe2c(%rip),%r12
lea    0xe3f(%rip),%r13
lea    0xc(%rsp),%r14
mov    %r12,%rdi
call   1030 <puts@plt>
mov    %r13,%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1050 <printf@plt>
mov    0x2deb(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rdi
mov    $0x4,%esi
call   1060 <fgets@plt>
mov    %r14,%rdi
call   1070 <chomp@plt>
mov    %r14,%rdi
call   1080 <valid_bool_input@plt>
or     %ebp,%eax
je     11e0 <pnc_read_bool_from_console_optional+0x80>
lea    0xe46(%rip),%rsi
lea    0xc(%rsp),%rdi
call   1040 <equals_ic@plt>
test   %rax,%rax
je     1243 <pnc_read_bool_from_console_optional+0xe3>
mov    0x10(%rsp),%rax
movl   $0x1,(%rax)
jmp    1265 <pnc_read_bool_from_console_optional+0x105>
lea    0xe25(%rip),%rsi
lea    0xc(%rsp),%rdi
call   1040 <equals_ic@plt>
xor    %ecx,%ecx
test   %rax,%rax
setne  %cl
mov    0x10(%rsp),%rax
mov    %ecx,(%rax)
je     128a <pnc_read_bool_from_console_optional+0x12a>
lea    0xe0f(%rip),%rdx
lea    0xe00(%rip),%rdi
mov    %r15,%rsi
call   1050 <printf@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdef(%rip),%rdx
jmp    126c <pnc_read_bool_from_console_optional+0x10c>

LLVM IR (-O0):
@.str = [26 x i8] c"Invalid input. Try again.\00"
@.str.1 = [33 x i8] c"%s? (Enter Yes/No or Y/N ) %s\0A> \00"
@.str.2 = [50 x i8] c" (Optional input. You can skip by pressing enter)\00"
@.str.3 = [1 x i8] zeroinitializer
@stdin = global i32 0
@.str.4 = [2 x i8] c"Y\00"
@.str.5 = [4 x i8] c"yes\00"
@.str.6 = [8 x i8] c"%s: %s\0A\00"
@.str.7 = [5 x i8] c"true\00"
@.str.8 = [6 x i8] c"false\00"
define void @pnc_read_bool_from_console_optional(i8* %0, i32* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca [4 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 = %34, %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 ([50 x i8], [50 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 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %15, i8* %19)
%21 = getelementptr inbounds [4 x i8], [4 x i8]* %7, i64 0, i64 0
%22 = load i32, i32* @stdin
%23 = call i32 @fgets(i8* %21, i32 4, i32 %22)
%24 = getelementptr inbounds [4 x i8], [4 x i8]* %7, i64 0, i64 0
%25 = call i32 @chomp(i8* %24)
store i32 0, i32* %8
br label %26
26: ; preds = %14
%27 = getelementptr inbounds [4 x i8], [4 x i8]* %7, i64 0, i64 0
%28 = call i32 @valid_bool_input(i8* %27)
%29 = icmp ne i32 %28, 0
br i1 %29, label %34, label %30
30: ; preds = %26
%31 = load i32, i32* %6
%32 = icmp ne i32 %31, 0
%33 = xor i1 %32, true
br label %34
34: ; preds = %30, %26
%35 = phi i1 [ false, %26 ], [ %33, %30 ]
br i1 %35, label %9, label %36
36: ; preds = %34
%37 = getelementptr inbounds [4 x i8], [4 x i8]* %7, i64 0, i64 0
%38 = call i64 @equals_ic(i8* %37, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%39 = icmp ne i64 %38, 0
br i1 %39, label %44, label %40
40: ; preds = %36
%41 = getelementptr inbounds [4 x i8], [4 x i8]* %7, i64 0, i64 0
%42 = call i64 @equals_ic(i8* %41, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0))
%43 = icmp ne i64 %42, 0
br label %44
44: ; preds = %40, %36
%45 = phi i1 [ true, %36 ], [ %43, %40 ]
%46 = zext i1 %45 to i32
%47 = load i32*, i32** %5
store i32 %46, i32* %47
%48 = load i8*, i8** %4
%49 = load i32*, i32** %5
%50 = load i32, i32* %49
%51 = icmp ne i32 %50, 0
%52 = zext i1 %51 to i64
%53 = select i1 %51, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.8, i64 0, i64 0)
%54 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0), i8* %48, i8* %53)
ret void
}

LLVM IR (-O2):
@.str = [26 x i8] c"Invalid input. Try again.\00"
@.str.1 = [33 x i8] c"%s? (Enter Yes/No or Y/N ) %s\0A> \00"
@.str.2 = [50 x i8] c" (Optional input. You can skip by pressing enter)\00"
@.str.3 = [1 x i8] zeroinitializer
@stdin = global i32 0
@.str.4 = [2 x i8] c"Y\00"
@.str.5 = [4 x i8] c"yes\00"
@.str.6 = [8 x i8] c"%s: %s\0A\00"
@.str.7 = [5 x i8] c"true\00"
@.str.8 = [6 x i8] c"false\00"
define void @pnc_read_bool_from_console_optional(i8* %0, i32* nocapture writeonly %1, i32 %2) {
%4 = alloca [4 x i8]
%5 = getelementptr inbounds [4 x i8], [4 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 ([50 x i8], [50 x i8]* @.str.2, i64 0, i64 0)
%8 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %7)
%9 = load i32, i32* @stdin
%10 = call i32 @fgets(i8* %5, i32 4, i32 %9)
%11 = call i32 @chomp(i8* %5)
%12 = call i32 @valid_bool_input(i8* %5)
%13 = or i32 %12, %2
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %24
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 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %7)
%18 = load i32, i32* @stdin
%19 = call i32 @fgets(i8* %5, i32 4, i32 %18)
%20 = call i32 @chomp(i8* %5)
%21 = call i32 @valid_bool_input(i8* %5)
%22 = or i32 %21, %2
%23 = icmp eq i32 %22, 0
br i1 %23, label %15, label %24
24: ; preds = %15, %3
%25 = call i64 @equals_ic(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%26 = icmp eq i64 %25, 0
br i1 %26, label %28, label %27
27: ; preds = %24
store i32 1, i32* %1
br label %32
28: ; preds = %24
%29 = call i64 @equals_ic(i8* %5, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0))
%30 = icmp ne i64 %29, 0
%31 = zext i1 %30 to i32
store i32 %31, i32* %1
br i1 %30, label %32, label %33
32: ; preds = %27, %28
br label %33
33: ; preds = %28, %32
%34 = phi i8* [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0), %32 ], [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str.8, i64 0, i64 0), %28 ]
%35 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0), i8* %0, i8* %34)
ret void
}