#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 CursorPosition(int *rows, int *cols) {
  if (write(STDOUT_FILENO, "\x1b[6n", 4) != 4)
    return -1;
  char buf[32];
  unsigned int i;
  for (i = 0; i < sizeof(buf) - 1; ++i) {
    if (read(STDIN_FILENO, &buf[i], 1) != 1)
      break;
    if (buf[i] == 'R')
      break;
  }
  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):
<CursorPosition>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2ea9(%rip),%rax
mov    (%rax),%edi
lea    0xeb0(%rip),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
cmp    $0x4,%eax
je     116f <CursorPosition+0x3f>
movl   $0xffffffff,-0x4(%rbp)
jmp    123d <CursorPosition+0x10d>
movl   $0x0,-0x44(%rbp)
mov    -0x44(%rbp),%eax
cmp    $0x1f,%rax
jae    11d7 <CursorPosition+0xa7>
mov    0x2e4e(%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     11ae <CursorPosition+0x7e>
jmp    11d7 <CursorPosition+0xa7>
mov    -0x44(%rbp),%eax
movsbl -0x40(%rbp,%rax,1),%eax
cmp    $0x52,%eax
jne    11c4 <CursorPosition+0x94>
jmp    11d7 <CursorPosition+0xa7>
jmp    11c9 <CursorPosition+0x99>
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
jmp    1176 <CursorPosition+0x46>
mov    -0x44(%rbp),%eax
movb   $0x0,-0x40(%rbp,%rax,1)
movsbl -0x40(%rbp),%eax
cmp    $0x1b,%eax
jne    11f9 <CursorPosition+0xc9>
movsbl -0x3f(%rbp),%eax
cmp    $0x5b,%eax
je     1205 <CursorPosition+0xd5>
movl   $0xffffffff,-0x4(%rbp)
jmp    123d <CursorPosition+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 <CursorPosition+0x106>
movl   $0xffffffff,-0x4(%rbp)
jmp    123d <CursorPosition+0x10d>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<CursorPosition>:
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 <CursorPosition+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 <CursorPosition+0x7f>
cmpb   $0x52,(%rbx)
je     11af <CursorPosition+0x7f>
add    $0x1,%r12
cmp    $0x1f,%r12
jne    1180 <CursorPosition+0x50>
mov    $0x1f,%r12d
mov    %r12d,%eax
movb   $0x0,(%rsp,%rax,1)
cmpb   $0x1b,(%rsp)
mov    $0xffffffff,%eax
jne    11eb <CursorPosition+0xbb>
cmpb   $0x5b,0x1(%rsp)
jne    11eb <CursorPosition+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 @CursorPosition(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
%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 %59
12: ; preds = %2
store i32 0, i32* %7
br label %13
13: ; preds = %34, %12
%14 = load i32, i32* %7
%15 = zext i32 %14 to i64
%16 = icmp ult i64 %15, 31
br i1 %16, label %17, label %37
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 %37
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 %37
33: ; preds = %25
br label %34
34: ; preds = %33
%35 = load i32, i32* %7
%36 = add i32 %35, 1
store i32 %36, i32* %7
br label %13
37: ; preds = %32, %24, %13
%38 = load i32, i32* %7
%39 = zext i32 %38 to i64
%40 = getelementptr inbounds [32 x i8], [32 x i8]* %6, i64 0, i64 %39
store i8 0, i8* %40
%41 = getelementptr inbounds [32 x i8], [32 x i8]* %6, i64 0, i64 0
%42 = load i8, i8* %41
%43 = sext i8 %42 to i32
%44 = icmp ne i32 %43, 27
br i1 %44, label %50, label %45
45: ; preds = %37
%46 = getelementptr inbounds [32 x i8], [32 x i8]* %6, i64 0, i64 1
%47 = load i8, i8* %46
%48 = sext i8 %47 to i32
%49 = icmp ne i32 %48, 91
br i1 %49, label %50, label %51
50: ; preds = %45, %37
store i32 -1, i32* %3
br label %59
51: ; preds = %45
%52 = getelementptr inbounds [32 x i8], [32 x i8]* %6, i64 0, i64 2
%53 = load i32*, i32** %4
%54 = load i32*, i32** %5
%55 = call i32 @sscanf(i8* %52, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32* %53, i32* %54)
%56 = icmp ne i32 %55, 2
br i1 %56, label %57, label %58
57: ; preds = %51
store i32 -1, i32* %3
br label %59
58: ; preds = %51
store i32 0, i32* %3
br label %59
59: ; preds = %58, %57, %50, %11
%60 = load i32, i32* %3
ret i32 %60
}

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 @CursorPosition(i32* %0, i32* %1) {
%3 = alloca [32 x i8]
%4 = load i32, i32* @STDOUT_FILENO
%5 = tail call i32 @write(i32 %4, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 4)
%6 = icmp eq i32 %5, 4
br i1 %6, label %7, label %38
7: ; preds = %2
%8 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 0
br label %9
9: ; preds = %7, %18
%10 = phi i64 [ 0, %7 ], [ %19, %18 ]
%11 = load i32, i32* @STDIN_FILENO
%12 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 %10
%13 = call i32 @read(i32 %11, i8* %12, i32 1)
%14 = icmp eq i32 %13, 1
br i1 %14, label %15, label %21
15: ; preds = %9
%16 = load i8, i8* %12
%17 = icmp eq i8 %16, 82
br i1 %17, label %21, label %18
18: ; preds = %15
%19 = add nuw nsw i64 %10, 1
%20 = icmp eq i64 %19, 31
br i1 %20, label %21, label %9
21: ; preds = %15, %9, %18
%22 = phi i64 [ %10, %15 ], [ %10, %9 ], [ 31, %18 ]
%23 = and i64 %22, 4294967295
%24 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 %23
store i8 0, i8* %24
%25 = load i8, i8* %8
%26 = icmp ne i8 %25, 27
%27 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 1
%28 = load i8, i8* %27
%29 = icmp ne i8 %28, 91
%30 = select i1 %26, i1 true, i1 %29
br i1 %30, label %36, label %31
31: ; preds = %21
%32 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 2
%33 = call i32 @sscanf(i8* %32, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32* %0, i32* %1)
%34 = icmp ne i32 %33, 2
%35 = sext i1 %34 to i32
br label %36
36: ; preds = %31, %21
%37 = phi i32 [ -1, %21 ], [ %35, %31 ]
br label %38
38: ; preds = %2, %36
%39 = phi i32 [ %37, %36 ], [ -1, %2 ]
ret i32 %39
}