#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*/ free(char *);
int /*<<< orphan*/ printf(char *, int);
char *read_csi();
int sscanf(char *, char *, int *, int *, char *);
bool query_dec_mode(int mode) {
  printf("\e[?%d$p", mode);
  char *s = NULL;
  do {
    if (s)
      free(s);
    s = read_csi();
    int reply_mode, reply_value;
    char reply_cmd;
    if (sscanf(s, "?%d;%d$%c", &reply_mode, &reply_value, &reply_cmd) < 3)
      continue;
    if (reply_cmd != 'y')
      continue;
    if (reply_mode != mode)
      continue;
    free(s);
    if (reply_value == 1 || reply_value == 3)
      return true;
    if (reply_value == 2 || reply_value == 4)
      return false;
    printf("Unrecognised reply to DECRQM: %d\n", reply_value);
    return false;
  } while (1);
}


x86 assembly (clang, -O0):
<query_dec_mode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%esi
lea    0xeab(%rip),%rdi
call   1040 <printf@plt>
movq   $0x0,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1176 <query_dec_mode+0x36>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    $0x0,%al
call   1060 <read_csi@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe7c(%rip),%rsi
lea    -0x14(%rbp),%rdx
lea    -0x18(%rbp),%rcx
lea    -0x19(%rbp),%r8
call   1050 <sscanf@plt>
cmp    $0x3,%eax
jge    11ab <query_dec_mode+0x6b>
jmp    1232 <query_dec_mode+0xf2>
movsbl -0x19(%rbp),%eax
cmp    $0x79,%eax
je     11bd <query_dec_mode+0x7d>
jmp    1232 <query_dec_mode+0xf2>
mov    -0x14(%rbp),%eax
cmp    -0x8(%rbp),%eax
je     11ce <query_dec_mode+0x8e>
jmp    1232 <query_dec_mode+0xf2>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
cmpl   $0x1,-0x18(%rbp)
je     11eb <query_dec_mode+0xab>
cmpl   $0x3,-0x18(%rbp)
jne    11f7 <query_dec_mode+0xb7>
movl   $0x1,-0x4(%rbp)
jmp    1241 <query_dec_mode+0x101>
cmpl   $0x2,-0x18(%rbp)
je     120b <query_dec_mode+0xcb>
cmpl   $0x4,-0x18(%rbp)
jne    1217 <query_dec_mode+0xd7>
movl   $0x0,-0x4(%rbp)
jmp    1241 <query_dec_mode+0x101>
mov    -0x18(%rbp),%esi
lea    0xdf1(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1241 <query_dec_mode+0x101>
mov    $0x1,%al
test   $0x1,%al
jne    1162 <query_dec_mode+0x22>
jmp    1241 <query_dec_mode+0x101>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<query_dec_mode>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edi,%r14d
lea    0xea8(%rip),%rdi
mov    %r14d,%esi
call   1040 <printf@plt>
lea    0xea1(%rip),%r15
lea    0x14(%rsp),%r12
lea    0x10(%rsp),%r13
lea    0xf(%rsp),%rbp
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
test   %rbx,%rbx
je     118d <query_dec_mode+0x4d>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %eax,%eax
call   1060 <read_csi@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
mov    %r13,%rcx
mov    %rbp,%r8
call   1050 <sscanf@plt>
cmp    $0x3,%eax
jl     1180 <query_dec_mode+0x40>
cmpb   $0x79,0xf(%rsp)
jne    1180 <query_dec_mode+0x40>
cmp    %r14d,0x14(%rsp)
jne    1180 <query_dec_mode+0x40>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    0x10(%rsp),%esi
mov    %esi,%eax
and    $0xfffffffd,%eax
mov    $0x1,%ebx
cmp    $0x1,%eax
je     11f1 <query_dec_mode+0xb1>
xor    %ebx,%ebx
cmp    $0x2,%esi
je     11f1 <query_dec_mode+0xb1>
cmp    $0x4,%esi
je     11f1 <query_dec_mode+0xb1>
lea    0xe26(%rip),%rdi
call   1040 <printf@plt>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [8 x i8] c"\1B[?%d$p\00"
@.str.1 = [10 x i8] c"?%d;%d$%c\00"
@.str.2 = [34 x i8] c"Unrecognised reply to DECRQM: %d\0A\00"
define i32 @query_dec_mode(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8
store i32 %0, i32* %3
%8 = load i32, i32* %3
%9 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %8)
store i8* null, i8** %4
br label %10
10: ; preds = %51, %1
%11 = load i8*, i8** %4
%12 = icmp ne i8* %11, null
br i1 %12, label %13, label %16
13: ; preds = %10
%14 = load i8*, i8** %4
%15 = call i32 @free(i8* %14)
br label %16
16: ; preds = %13, %10
%17 = call i8* (...) @read_csi()
store i8* %17, i8** %4
%18 = load i8*, i8** %4
%19 = call i32 @sscanf(i8* %18, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32* %5, i32* %6, i8* %7)
%20 = icmp slt i32 %19, 3
br i1 %20, label %21, label %22
21: ; preds = %16
br label %51
22: ; preds = %16
%23 = load i8, i8* %7
%24 = sext i8 %23 to i32
%25 = icmp ne i32 %24, 121
br i1 %25, label %26, label %27
26: ; preds = %22
br label %51
27: ; preds = %22
%28 = load i32, i32* %5
%29 = load i32, i32* %3
%30 = icmp ne i32 %28, %29
br i1 %30, label %31, label %32
31: ; preds = %27
br label %51
32: ; preds = %27
%33 = load i8*, i8** %4
%34 = call i32 @free(i8* %33)
%35 = load i32, i32* %6
%36 = icmp eq i32 %35, 1
br i1 %36, label %40, label %37
37: ; preds = %32
%38 = load i32, i32* %6
%39 = icmp eq i32 %38, 3
br i1 %39, label %40, label %41
40: ; preds = %37, %32
store i32 1, i32* %2
br label %52
41: ; preds = %37
%42 = load i32, i32* %6
%43 = icmp eq i32 %42, 2
br i1 %43, label %47, label %44
44: ; preds = %41
%45 = load i32, i32* %6
%46 = icmp eq i32 %45, 4
br i1 %46, label %47, label %48
47: ; preds = %44, %41
store i32 0, i32* %2
br label %52
48: ; preds = %44
%49 = load i32, i32* %6
%50 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i32 %49)
store i32 0, i32* %2
br label %52
51: ; preds = %31, %26, %21
br i1 true, label %10, label %52
52: ; preds = %40, %47, %48, %51
%53 = load i32, i32* %2
ret i32 %53
}

LLVM IR (-O2):
@.str = [8 x i8] c"\1B[?%d$p\00"
@.str.1 = [10 x i8] c"?%d;%d$%c\00"
@.str.2 = [34 x i8] c"Unrecognised reply to DECRQM: %d\0A\00"
define i32 @query_dec_mode(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8
%5 = tail call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %0)
%6 = bitcast i32* %2 to i8*
%7 = bitcast i32* %3 to i8*
br label %8
8: ; preds = %31, %1
%9 = phi i8* [ null, %1 ], [ %14, %31 ]
%10 = icmp eq i8* %9, null
br i1 %10, label %13, label %11
11: ; preds = %8
%12 = call i32 @free(i8* %9)
br label %13
13: ; preds = %11, %8
%14 = call i8* (...) @read_csi()
%15 = call i32 @sscanf(i8* %14, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32* %2, i32* %3, i8* %4)
%16 = icmp sgt i32 %15, 2
%17 = load i8, i8* %4
%18 = icmp eq i8 %17, 121
%19 = select i1 %16, i1 %18, i1 false
%20 = load i32, i32* %2
%21 = icmp eq i32 %20, %0
%22 = select i1 %19, i1 %21, i1 false
br i1 %22, label %23, label %31
23: ; preds = %13
%24 = call i32 @free(i8* %14)
%25 = load i32, i32* %3
%26 = and i32 %25, -3
%27 = icmp eq i32 %26, 1
br i1 %27, label %32, label %28
28: ; preds = %23
switch i32 %25, label %29 [
i32 4, label %32
i32 2, label %32
]
29: ; preds = %28
%30 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i32 %25)
br label %32
31: ; preds = %13
br label %8
32: ; preds = %29, %23, %28, %28
%33 = phi i32 [ 0, %28 ], [ 0, %28 ], [ 1, %23 ], [ 0, %29 ]
ret i32 %33
}