#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*/ clearerr(int /*<<< orphan*/);
int /*<<< orphan*/ errmsg(char *);
scalar_t__ feof(int /*<<< orphan*/);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sscanf(char *, char *, int *);
int /*<<< orphan*/ stdin;
int select_window() {
  char termbuf[80];
  int win = 1;
  while (1) {
    printf(" Enter window number (1-%d) [%1d] : ", 4, win);
    fgets(termbuf, 80, stdin);
    if (feof(stdin)) {
      clearerr(stdin);
      printf("\n");
      return (-1);
    } else if ((termbuf[0] == '^') && termbuf[1] == 'd' || termbuf[1] == 'D')
      break;
    else {
      sscanf(termbuf, "%d", &win);
      if (win > 0 && win <= 4)
        return (--win);
      else
        errmsg("Error: Invalid window number selection");
    }
  }
}


x86 assembly (clang, -O0):
<select_window>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x1,-0x64(%rbp)
mov    -0x64(%rbp),%edx
lea    0xe87(%rip),%rdi
mov    $0x4,%esi
mov    $0x0,%al
call   1030 <printf@plt>
lea    -0x60(%rbp),%rdi
mov    0x2e58(%rip),%rax
mov    (%rax),%edx
mov    $0x50,%esi
call   1040 <fgets@plt>
mov    0x2e45(%rip),%rax
mov    (%rax),%edi
call   1050 <feof@plt>
cmp    $0x0,%rax
je     11dc <select_window+0x7c>
mov    0x2e2d(%rip),%rax
mov    (%rax),%edi
call   1060 <clearerr@plt>
lea    0xe5c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    125c <select_window+0xfc>
movsbl -0x60(%rbp),%eax
cmp    $0x5e,%eax
jne    11f6 <select_window+0x96>
movsbl -0x5f(%rbp),%eax
cmp    $0x64,%eax
je     1203 <select_window+0xa3>
movsbl -0x5f(%rbp),%eax
cmp    $0x44,%eax
jne    1208 <select_window+0xa8>
jmp    125c <select_window+0xfc>
lea    -0x60(%rbp),%rdi
lea    0xe14(%rip),%rsi
lea    -0x64(%rbp),%rdx
call   1070 <sscanf@plt>
cmpl   $0x0,-0x64(%rbp)
jle    1241 <select_window+0xe1>
cmpl   $0x4,-0x64(%rbp)
jg     1241 <select_window+0xe1>
mov    -0x64(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x64(%rbp)
mov    %eax,-0x4(%rbp)
jmp    125c <select_window+0xfc>
lea    0xde2(%rip),%rdi
call   1080 <errmsg@plt>
jmp    1252 <select_window+0xf2>
jmp    1257 <select_window+0xf7>
jmp    116f <select_window+0xf>
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<select_window>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x68,%rsp
movl   $0x1,0xc(%rsp)
lea    0xe73(%rip),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2e43(%rip),%rbp
mov    0x0(%rbp),%edx
lea    0x10(%rsp),%rdi
mov    $0x50,%esi
call   1050 <fgets@plt>
mov    0x0(%rbp),%edi
call   1060 <feof@plt>
test   %rax,%rax
je     11ea <select_window+0x7a>
mov    0x0(%rbp),%edi
call   1070 <clearerr@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    $0xffffffff,%eax
add    $0x68,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe34(%rip),%r14
lea    0x10(%rsp),%rbx
lea    0xc(%rsp),%r15
lea    0xe26(%rip),%r12
lea    0xdf7(%rip),%r13
nopl   0x0(%rax)
movzbl 0x10(%rsp),%eax
movzbl 0x11(%rsp),%ecx
xor    $0x5e,%al
mov    %ecx,%edx
xor    $0x64,%dl
or     %al,%dl
je     11db <select_window+0x6b>
cmp    $0x44,%cl
je     11db <select_window+0x6b>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1080 <sscanf@plt>
mov    0xc(%rsp),%eax
add    $0xffffffff,%eax
cmp    $0x4,%eax
jb     11db <select_window+0x6b>
mov    %r12,%rdi
call   1090 <errmsg@plt>
mov    0xc(%rsp),%edx
mov    %r13,%rdi
mov    $0x4,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x0(%rbp),%edx
mov    %rbx,%rdi
mov    $0x50,%esi
call   1050 <fgets@plt>
mov    0x0(%rbp),%edi
call   1060 <feof@plt>
test   %rax,%rax
je     1210 <select_window+0xa0>
jmp    11c4 <select_window+0x54>

LLVM IR (-O0):
@.str = [37 x i8] c" Enter window number (1-%d) [%1d] : \00"
@stdin = global i32 0
@.str.1 = [2 x i8] c"\0A\00"
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [39 x i8] c"Error: Invalid window number selection\00"
define i32 @select_window() {
%1 = alloca i32
%2 = alloca [80 x i8]
%3 = alloca i32
store i32 1, i32* %3
br label %4
4: ; preds = %0, %48
%5 = load i32, i32* %3
%6 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i32 4, i32 %5)
%7 = getelementptr inbounds [80 x i8], [80 x i8]* %2, i64 0, i64 0
%8 = load i32, i32* @stdin
%9 = call i32 @fgets(i8* %7, i32 80, i32 %8)
%10 = load i32, i32* @stdin
%11 = call i64 @feof(i32 %10)
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %17
13: ; preds = %4
%14 = load i32, i32* @stdin
%15 = call i32 @clearerr(i32 %14)
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %1
br label %49
17: ; preds = %4
%18 = getelementptr inbounds [80 x i8], [80 x i8]* %2, i64 0, i64 0
%19 = load i8, i8* %18
%20 = sext i8 %19 to i32
%21 = icmp eq i32 %20, 94
br i1 %21, label %22, label %27
22: ; preds = %17
%23 = getelementptr inbounds [80 x i8], [80 x i8]* %2, i64 0, i64 1
%24 = load i8, i8* %23
%25 = sext i8 %24 to i32
%26 = icmp eq i32 %25, 100
br i1 %26, label %32, label %27
27: ; preds = %22, %17
%28 = getelementptr inbounds [80 x i8], [80 x i8]* %2, i64 0, i64 1
%29 = load i8, i8* %28
%30 = sext i8 %29 to i32
%31 = icmp eq i32 %30, 68
br i1 %31, label %32, label %33
32: ; preds = %27, %22
br label %49
33: ; preds = %27
%34 = getelementptr inbounds [80 x i8], [80 x i8]* %2, i64 0, i64 0
%35 = call i32 @sscanf(i8* %34, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %3)
%36 = load i32, i32* %3
%37 = icmp sgt i32 %36, 0
br i1 %37, label %38, label %44
38: ; preds = %33
%39 = load i32, i32* %3
%40 = icmp sle i32 %39, 4
br i1 %40, label %41, label %44
41: ; preds = %38
%42 = load i32, i32* %3
%43 = add nsw i32 %42, -1
store i32 %43, i32* %3
store i32 %43, i32* %1
br label %49
44: ; preds = %38, %33
%45 = call i32 @errmsg(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0))
br label %46
46: ; preds = %44
br label %47
47: ; preds = %46
br label %48
48: ; preds = %47
br label %4
49: ; preds = %13, %41, %32
%50 = load i32, i32* %1
ret i32 %50
}

LLVM IR (-O2):
@.str = [37 x i8] c" Enter window number (1-%d) [%1d] : \00"
@stdin = global i32 0
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [39 x i8] c"Error: Invalid window number selection\00"
define i32 @select_window() {
%1 = alloca [80 x i8]
%2 = alloca i32
%3 = getelementptr inbounds [80 x i8], [80 x i8]* %1, i64 0, i64 0
%4 = bitcast i32* %2 to i8*
store i32 1, i32* %2
%5 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i32 4, i32 1)
%6 = load i32, i32* @stdin
%7 = call i32 @fgets(i8* %3, i32 80, i32 %6)
%8 = load i32, i32* @stdin
%9 = call i64 @feof(i32 %8)
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = getelementptr inbounds [80 x i8], [80 x i8]* %1, i64 0, i64 1
br label %17
13: ; preds = %30, %0
%14 = load i32, i32* @stdin
%15 = call i32 @clearerr(i32 %14)
%16 = call i32 @putchar(i32 10)
br label %39
17: ; preds = %11, %30
%18 = load i8, i8* %3
%19 = icmp eq i8 %18, 94
%20 = load i8, i8* %12
%21 = icmp eq i8 %20, 100
%22 = select i1 %19, i1 %21, i1 false
%23 = icmp eq i8 %20, 68
%24 = select i1 %22, i1 true, i1 %23
br i1 %24, label %39, label %25
25: ; preds = %17
%26 = call i32 @sscanf(i8* %3, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %2)
%27 = load i32, i32* %2
%28 = add i32 %27, -1
%29 = icmp ult i32 %28, 4
br i1 %29, label %39, label %30
30: ; preds = %25
%31 = call i32 @errmsg(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0))
%32 = load i32, i32* %2
%33 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i32 4, i32 %32)
%34 = load i32, i32* @stdin
%35 = call i32 @fgets(i8* %3, i32 80, i32 %34)
%36 = load i32, i32* @stdin
%37 = call i64 @feof(i32 %36)
%38 = icmp eq i64 %37, 0
br i1 %38, label %17, label %13
39: ; preds = %17, %25, %13
%40 = phi i32 [ -1, %13 ], [ %28, %25 ], [ undef, %17 ]
ret i32 %40
}