#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 */
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int read(int /*<<< orphan*/, char *, int);
int sscanf(char *, char *, int *, int *);
int write(int /*<<< orphan*/, char *, int);
int getCursorPosition(int *rows, int *cols) {
  char buf[32];
  unsigned int i = 0;
  if (write(STDOUT_FILENO, "\x1b[6n", 4) != 4) {
    return -1;
  }
  while (i < sizeof(buf) - 1) {
    if (read(STDIN_FILENO, &buf[i], 1) != 1)
      break;
    if (buf[i] == 'R')
      break;
    i++;
  }
  buf[i] = '\0';
  if (buf[0] != '\x1b' || buf[1] != '[')
    return 1;
  if (sscanf(&buf[2], "%d;%d", rows, cols) != 2)
    return -1;
  return 0;
}


x86 assembly (clang, -O0):
<getCursorPosition>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x44(%rbp)
mov    0x2ea2(%rip),%rax
mov    (%rax),%edi
lea    0xea9(%rip),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
cmp    $0x4,%eax
je     1176 <getCursorPosition+0x46>
movl   $0xffffffff,-0x4(%rbp)
jmp    123d <getCursorPosition+0x10d>
jmp    117b <getCursorPosition+0x4b>
mov    -0x44(%rbp),%eax
cmp    $0x1f,%rax
jae    11d7 <getCursorPosition+0xa7>
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
mov    -0x44(%rbp),%eax
lea    -0x40(%rbp),%rsi
add    %rax,%rsi
mov    $0x1,%edx
call   1040 <read@plt>
cmp    $0x1,%eax
je     11b3 <getCursorPosition+0x83>
jmp    11d7 <getCursorPosition+0xa7>
mov    -0x44(%rbp),%eax
movsbl -0x40(%rbp,%rax,1),%eax
cmp    $0x52,%eax
jne    11c9 <getCursorPosition+0x99>
jmp    11d7 <getCursorPosition+0xa7>
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
jmp    117b <getCursorPosition+0x4b>
mov    -0x44(%rbp),%eax
movb   $0x0,-0x40(%rbp,%rax,1)
movsbl -0x40(%rbp),%eax
cmp    $0x1b,%eax
jne    11f9 <getCursorPosition+0xc9>
movsbl -0x3f(%rbp),%eax
cmp    $0x5b,%eax
je     1205 <getCursorPosition+0xd5>
movl   $0x1,-0x4(%rbp)
jmp    123d <getCursorPosition+0x10d>
lea    -0x40(%rbp),%rdi
add    $0x2,%rdi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
lea    0xde9(%rip),%rsi
call   1050 <sscanf@plt>
cmp    $0x2,%eax
je     1236 <getCursorPosition+0x106>
movl   $0xffffffff,-0x4(%rbp)
jmp    123d <getCursorPosition+0x10d>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getCursorPosition>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
mov    0x2ea6(%rip),%rax
mov    (%rax),%edi
lea    0xead(%rip),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    $0x4,%ecx
jne    11eb <getCursorPosition+0xbb>
xor    %r12d,%r12d
mov    0x2e61(%rip),%r13
nopw   0x0(%rax,%rax,1)
mov    0x0(%r13),%edi
lea    (%rsp,%r12,1),%rbx
mov    %rbx,%rsi
mov    $0x1,%edx
call   1040 <read@plt>
cmp    $0x1,%eax
jne    11af <getCursorPosition+0x7f>
cmpb   $0x52,(%rbx)
je     11af <getCursorPosition+0x7f>
add    $0x1,%r12
cmp    $0x1f,%r12
jne    1180 <getCursorPosition+0x50>
mov    $0x1f,%r12d
mov    %r12d,%eax
movb   $0x0,(%rsp,%rax,1)
cmpb   $0x1b,(%rsp)
mov    $0x1,%eax
jne    11eb <getCursorPosition+0xbb>
cmpb   $0x5b,0x1(%rsp)
jne    11eb <getCursorPosition+0xbb>
lea    0x2(%rsp),%rdi
lea    0xe31(%rip),%rsi
mov    %r15,%rdx
mov    %r14,%rcx
call   1050 <sscanf@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0x2,%ecx
setne  %al
neg    %eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@STDOUT_FILENO = global i32 0
@.str = [5 x i8] c"\1B[6n\00"
@STDIN_FILENO = global i32 0
@.str.1 = [6 x i8] c"%d;%d\00"
define i32 @getCursorPosition(i32* %0, i32* %1) {
%3 = alloca [32 x i8]
%4 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 0
%5 = load i32, i32* @STDOUT_FILENO
%6 = tail call i32 @write(i32 %5, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 4)
%7 = icmp eq i32 %6, 4
br i1 %7, label %8, label %35
8: ; preds = %2, %17
%9 = phi i64 [ %18, %17 ], [ 0, %2 ]
%10 = load i32, i32* @STDIN_FILENO
%11 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 %9
%12 = call i32 @read(i32 %10, i8* %11, i32 1)
%13 = icmp eq i32 %12, 1
br i1 %13, label %14, label %20
14: ; preds = %8
%15 = load i8, i8* %11
%16 = icmp eq i8 %15, 82
br i1 %16, label %20, label %17
17: ; preds = %14
%18 = add nuw nsw i64 %9, 1
%19 = icmp eq i64 %18, 31
br i1 %19, label %20, label %8
20: ; preds = %14, %8, %17
%21 = phi i64 [ %9, %14 ], [ %9, %8 ], [ 31, %17 ]
%22 = and i64 %21, 4294967295
%23 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 %22
store i8 0, i8* %23
%24 = load i8, i8* %4
%25 = icmp ne i8 %24, 27
%26 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 1
%27 = load i8, i8* %26
%28 = icmp ne i8 %27, 91
%29 = select i1 %25, i1 true, i1 %28
br i1 %29, label %35, label %30
30: ; preds = %20
%31 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 2
%32 = call i32 @sscanf(i8* %31, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32* %0, i32* %1)
%33 = icmp ne i32 %32, 2
%34 = sext i1 %33 to i32
br label %35
35: ; preds = %30, %20, %2
%36 = phi i32 [ -1, %2 ], [ 1, %20 ], [ %34, %30 ]
ret i32 %36
}