#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 *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char const *, char *);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ stdin;
int strlen(char *);
void pnc_read_string_from_console_optional(char const *message, char *val,
                                           int size, bool optional) {
  bool first_try = true;
  do {
    if (!first_try) {
      puts("Invalid input. Try again.");
    }
    printf("%s %s\n> ", message,
           optional ? " ( Optional input. You can skip by pressing enter )"
                    : "");
    if (!fgets(val, size, stdin)) {
      puts("Failed to get input");
    }
    first_try = false;
  } while ((val == NULL || strlen(val) == 1) && !optional);
  chomp(val);
  printf("%s : %s\n", message, val);
}


x86 assembly (clang, -O0):
<pnc_read_string_from_console_optional>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
movl   $0x1,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    1183 <pnc_read_string_from_console_optional+0x33>
lea    0xe82(%rip),%rdi
call   1030 <puts@plt>
mov    -0x8(%rbp),%rsi
mov    -0x18(%rbp),%ecx
lea    0xee2(%rip),%rdx
lea    0xe8b(%rip),%rax
cmp    $0x0,%ecx
cmovne %rax,%rdx
lea    0xe74(%rip),%rdi
call   1050 <printf@plt>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    0x2e2f(%rip),%rax
mov    (%rax),%edx
call   1060 <fgets@plt>
cmp    $0x0,%eax
jne    11d5 <pnc_read_string_from_console_optional+0x85>
lea    0xe87(%rip),%rdi
call   1030 <puts@plt>
movl   $0x0,-0x1c(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1200 <pnc_read_string_from_console_optional+0xb0>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0x1,%ecx
mov    %al,-0x1d(%rbp)
jne    120c <pnc_read_string_from_console_optional+0xbc>
cmpl   $0x0,-0x18(%rbp)
setne  %al
xor    $0xff,%al
mov    %al,-0x1d(%rbp)
mov    -0x1d(%rbp),%al
test   $0x1,%al
jne    116d <pnc_read_string_from_console_optional+0x1d>
mov    -0x10(%rbp),%rdi
call   1070 <chomp@plt>
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
lea    0xe3c(%rip),%rdi
call   1050 <printf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pnc_read_string_from_console_optional>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %ecx,%r12d
mov    %edx,%r15d
mov    %rsi,%rbx
mov    %rdi,%r14
test   %ecx,%ecx
lea    0xf03(%rip),%rax
lea    0xeac(%rip),%rbp
cmove  %rax,%rbp
lea    0xe7e(%rip),%r13
jmp    119d <pnc_read_string_from_console_optional+0x4d>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
test   %r12d,%r12d
jne    11f3 <pnc_read_string_from_console_optional+0xa3>
mov    %r13,%rdi
call   1030 <puts@plt>
lea    0xe76(%rip),%rdi
mov    %r14,%rsi
mov    %rbp,%rdx
call   1050 <printf@plt>
mov    0x2e32(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    %r15d,%esi
call   1060 <fgets@plt>
test   %eax,%eax
je     11d0 <pnc_read_string_from_console_optional+0x80>
test   %rbx,%rbx
jne    11e1 <pnc_read_string_from_console_optional+0x91>
jmp    1190 <pnc_read_string_from_console_optional+0x40>
xchg   %ax,%ax
lea    0xe80(%rip),%rdi
call   1030 <puts@plt>
test   %rbx,%rbx
je     1190 <pnc_read_string_from_console_optional+0x40>
mov    %rbx,%rdi
call   1040 <strlen@plt>
test   %r12d,%r12d
jne    11f3 <pnc_read_string_from_console_optional+0xa3>
cmp    $0x1,%eax
je     1195 <pnc_read_string_from_console_optional+0x45>
mov    %rbx,%rdi
call   1070 <chomp@plt>
lea    0xe69(%rip),%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <printf@plt>

LLVM IR (-O0):
@.str = [26 x i8] c"Invalid input. Try again.\00"
@.str.1 = [9 x i8] c"%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
@stdin = global i32 0
@.str.4 = [20 x i8] c"Failed to get input\00"
@.str.5 = [9 x i8] c"%s : %s\0A\00"
define void @pnc_read_string_from_console_optional(i8* %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 %3, i32* %8
store i32 1, i32* %9
br label %10
10: ; preds = %41, %4
%11 = load i32, i32* %9
%12 = icmp ne i32 %11, 0
br i1 %12, label %15, label %13
13: ; preds = %10
%14 = call i32 @puts(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
br label %15
15: ; preds = %13, %10
%16 = load i8*, i8** %5
%17 = load i32, i32* %8
%18 = icmp ne i32 %17, 0
%19 = zext i1 %18 to i64
%20 = select i1 %18, 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)
%21 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* %16, i8* %20)
%22 = load i8*, i8** %6
%23 = load i32, i32* %7
%24 = load i32, i32* @stdin
%25 = call i32 @fgets(i8* %22, i32 %23, i32 %24)
%26 = icmp ne i32 %25, 0
br i1 %26, label %29, label %27
27: ; preds = %15
%28 = call i32 @puts(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0))
br label %29
29: ; preds = %27, %15
store i32 0, i32* %9
br label %30
30: ; preds = %29
%31 = load i8*, i8** %6
%32 = icmp eq i8* %31, null
br i1 %32, label %37, label %33
33: ; preds = %30
%34 = load i8*, i8** %6
%35 = call i32 @strlen(i8* %34)
%36 = icmp eq i32 %35, 1
br i1 %36, label %37, label %41
37: ; preds = %33, %30
%38 = load i32, i32* %8
%39 = icmp ne i32 %38, 0
%40 = xor i1 %39, true
br label %41
41: ; preds = %37, %33
%42 = phi i1 [ false, %33 ], [ %40, %37 ]
br i1 %42, label %10, label %43
43: ; preds = %41
%44 = load i8*, i8** %6
%45 = call i32 @chomp(i8* %44)
%46 = load i8*, i8** %5
%47 = load i8*, i8** %6
%48 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0), i8* %46, i8* %47)
ret void
}

LLVM IR (-O2):
@.str = [26 x i8] c"Invalid input. Try again.\00"
@.str.1 = [9 x i8] c"%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
@stdin = global i32 0
@.str.4 = [20 x i8] c"Failed to get input\00"
@.str.5 = [9 x i8] c"%s : %s\0A\00"
define void @pnc_read_string_from_console_optional(i8* %0, i8* %1, i32 %2, i32 %3) {
%5 = icmp eq i32 %3, 0
%6 = select i1 %5, 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)
%7 = icmp eq i8* %1, null
br label %10
8: ; preds = %18, %22
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
br label %10
10: ; preds = %4, %8
%11 = tail call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %6)
%12 = load i32, i32* @stdin
%13 = tail call i32 @fgets(i8* %1, i32 %2, i32 %12)
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %10
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0))
br label %17
17: ; preds = %15, %10
br i1 %7, label %22, label %18
18: ; preds = %17
%19 = tail call i32 @strlen(i8* %1)
%20 = icmp eq i32 %19, 1
%21 = and i1 %5, %20
br i1 %21, label %8, label %23
22: ; preds = %17
br i1 %5, label %8, label %23
23: ; preds = %18, %22
%24 = tail call i32 @chomp(i8* %1)
%25 = tail call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0), i8* %0, i8* %1)
ret void
}