#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*/ fd_printf(int, char *);
char fd_read_char(int, int);
int queryCursor(int fd, int *cols) {
  fd_printf(fd, "\x1b[6n");
  if (fd_read_char(fd, 100) == 0x1b && fd_read_char(fd, 100) == '[') {
    int n = 0;
    while (1) {
      int ch = fd_read_char(fd, 100);
      if (ch == ';') {
        n = 0;
      } else if (ch == 'R') {
        if (n != 0 && n < 1000) {
          *cols = n;
        }
        break;
      } else if (ch >= 0 && ch <= '9') {
        n = n * 10 + ch - '0';
      } else {
        break;
      }
    }
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<queryCursor>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%edi
lea    0xec7(%rip),%rsi
call   1030 <fd_printf@plt>
mov    -0x8(%rbp),%edi
mov    $0x64,%esi
call   1040 <fd_read_char@plt>
movsbl %al,%eax
cmp    $0x1b,%eax
jne    1215 <queryCursor+0xf5>
mov    -0x8(%rbp),%edi
mov    $0x64,%esi
call   1040 <fd_read_char@plt>
movsbl %al,%eax
cmp    $0x5b,%eax
jne    1215 <queryCursor+0xf5>
movl   $0x0,-0x14(%rbp)
mov    -0x8(%rbp),%edi
mov    $0x64,%esi
call   1040 <fd_read_char@plt>
movsbl %al,%eax
mov    %eax,-0x18(%rbp)
cmpl   $0x3b,-0x18(%rbp)
jne    11a0 <queryCursor+0x80>
movl   $0x0,-0x14(%rbp)
jmp    1204 <queryCursor+0xe4>
cmpl   $0x52,-0x18(%rbp)
jne    11cf <queryCursor+0xaf>
cmpl   $0x0,-0x14(%rbp)
je     11ca <queryCursor+0xaa>
cmpl   $0x3e8,-0x14(%rbp)
jge    11ca <queryCursor+0xaa>
mov    -0x14(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
jmp    1209 <queryCursor+0xe9>
cmpl   $0x0,-0x18(%rbp)
jl     11f5 <queryCursor+0xd5>
cmpl   $0x39,-0x18(%rbp)
jg     11f5 <queryCursor+0xd5>
imul   $0xa,-0x14(%rbp),%eax
add    -0x18(%rbp),%eax
sub    $0x30,%eax
mov    %eax,-0x14(%rbp)
jmp    11fa <queryCursor+0xda>
jmp    1209 <queryCursor+0xe9>
jmp    11ff <queryCursor+0xdf>
jmp    1204 <queryCursor+0xe4>
jmp    1177 <queryCursor+0x57>
movl   $0x1,-0x4(%rbp)
jmp    121c <queryCursor+0xfc>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<queryCursor>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %edi,%ebp
lea    0xecd(%rip),%rsi
call   1030 <fd_printf@plt>
mov    %ebp,%edi
mov    $0x64,%esi
call   1040 <fd_read_char@plt>
xor    %r15d,%r15d
cmp    $0x1b,%al
jne    11aa <queryCursor+0x8a>
mov    %ebp,%edi
mov    $0x64,%esi
call   1040 <fd_read_char@plt>
cmp    $0x5b,%al
jne    11aa <queryCursor+0x8a>
xor    %ecx,%ecx
nopl   (%rax)
mov    %ecx,%ebx
mov    %ebp,%edi
mov    $0x64,%esi
call   1040 <fd_read_char@plt>
mov    $0x0,%ecx
cmp    $0x3b,%al
je     1160 <queryCursor+0x40>
cmp    $0x52,%al
je     118d <queryCursor+0x6d>
cmp    $0x39,%al
ja     11a4 <queryCursor+0x84>
movsbl %al,%eax
lea    (%rbx,%rbx,4),%ecx
lea    (%rax,%rcx,2),%ecx
add    $0xffffffd0,%ecx
jmp    1160 <queryCursor+0x40>
mov    $0x1,%r15d
test   %ebx,%ebx
je     11aa <queryCursor+0x8a>
cmp    $0x3e7,%ebx
jg     11aa <queryCursor+0x8a>
mov    %ebx,(%r14)
jmp    11aa <queryCursor+0x8a>
mov    $0x1,%r15d
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"\1B[6n\00"
define i32 @queryCursor(i32 %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %4
store i32* %1, i32** %5
%8 = load i32, i32* %4
%9 = call i32 @fd_printf(i32 %8, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%10 = load i32, i32* %4
%11 = call signext i8 @fd_read_char(i32 %10, i32 100)
%12 = sext i8 %11 to i32
%13 = icmp eq i32 %12, 27
br i1 %13, label %14, label %57
14: ; preds = %2
%15 = load i32, i32* %4
%16 = call signext i8 @fd_read_char(i32 %15, i32 100)
%17 = sext i8 %16 to i32
%18 = icmp eq i32 %17, 91
br i1 %18, label %19, label %57
19: ; preds = %14
store i32 0, i32* %6
br label %20
20: ; preds = %19, %55
%21 = load i32, i32* %4
%22 = call signext i8 @fd_read_char(i32 %21, i32 100)
%23 = sext i8 %22 to i32
store i32 %23, i32* %7
%24 = load i32, i32* %7
%25 = icmp eq i32 %24, 59
br i1 %25, label %26, label %27
26: ; preds = %20
store i32 0, i32* %6
br label %55
27: ; preds = %20
%28 = load i32, i32* %7
%29 = icmp eq i32 %28, 82
br i1 %29, label %30, label %40
30: ; preds = %27
%31 = load i32, i32* %6
%32 = icmp ne i32 %31, 0
br i1 %32, label %33, label %39
33: ; preds = %30
%34 = load i32, i32* %6
%35 = icmp slt i32 %34, 1000
br i1 %35, label %36, label %39
36: ; preds = %33
%37 = load i32, i32* %6
%38 = load i32*, i32** %5
store i32 %37, i32* %38
br label %39
39: ; preds = %36, %33, %30
br label %56
40: ; preds = %27
%41 = load i32, i32* %7
%42 = icmp sge i32 %41, 0
br i1 %42, label %43, label %52
43: ; preds = %40
%44 = load i32, i32* %7
%45 = icmp sle i32 %44, 57
br i1 %45, label %46, label %52
46: ; preds = %43
%47 = load i32, i32* %6
%48 = mul nsw i32 %47, 10
%49 = load i32, i32* %7
%50 = add nsw i32 %48, %49
%51 = sub nsw i32 %50, 48
store i32 %51, i32* %6
br label %53
52: ; preds = %43, %40
br label %56
53: ; preds = %46
br label %54
54: ; preds = %53
br label %55
55: ; preds = %54, %26
br label %20
56: ; preds = %52, %39
store i32 1, i32* %3
br label %58
57: ; preds = %14, %2
store i32 0, i32* %3
br label %58
58: ; preds = %57, %56
%59 = load i32, i32* %3
ret i32 %59
}

LLVM IR (-O2):
@.str = [5 x i8] c"\1B[6n\00"
define i32 @queryCursor(i32 %0, i32* nocapture writeonly %1) {
%3 = tail call i32 @fd_printf(i32 %0, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%4 = tail call signext i8 @fd_read_char(i32 %0, i32 100)
%5 = icmp eq i8 %4, 27
br i1 %5, label %6, label %26
6: ; preds = %2
%7 = tail call signext i8 @fd_read_char(i32 %0, i32 100)
%8 = icmp eq i8 %7, 91
br i1 %8, label %9, label %26
9: ; preds = %6, %24
%10 = phi i32 [ %25, %24 ], [ 0, %6 ]
%11 = tail call signext i8 @fd_read_char(i32 %0, i32 100)
%12 = sext i8 %11 to i32
switch i8 %11, label %18 [
i8 59, label %24
i8 82, label %13
]
13: ; preds = %9
%14 = icmp ne i32 %10, 0
%15 = icmp slt i32 %10, 1000
%16 = and i1 %14, %15
br i1 %16, label %17, label %26
17: ; preds = %13
store i32 %10, i32* %1
br label %26
18: ; preds = %9
%19 = icmp ult i8 %11, 58
br i1 %19, label %20, label %26
20: ; preds = %18
%21 = mul nsw i32 %10, 10
%22 = add i32 %21, -48
%23 = add i32 %22, %12
br label %24
24: ; preds = %20, %9
%25 = phi i32 [ %23, %20 ], [ 0, %9 ]
br label %9
26: ; preds = %18, %13, %17, %2, %6
%27 = phi i32 [ 0, %6 ], [ 0, %2 ], [ 1, %17 ], [ 1, %13 ], [ 1, %18 ]
ret i32 %27
}