#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 atoi(char *);
int read(int /*<<< orphan*/, char *, int);
char *strchr(char *, char);
int write(int /*<<< orphan*/, char *, int);
int get_cursor_position(int *rows, int *cols) {
  char buf[32];
  unsigned int i = 0;
  char *ptr;
  if (write(STDOUT_FILENO, "\x1b[6n", 4) != 4) {
    return -1;
  }
  while (1 < 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;
  ptr = &buf[2];
  *rows = atoi(ptr);
  ptr = strchr(ptr, ';');
  if (ptr == NULL)
    return -1;
  *cols = atoi(ptr + 1);
  return 0;
}


x86 assembly (clang, -O0):
<get_cursor_position>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x44(%rbp)
mov    0x2e92(%rip),%rax
mov    (%rax),%edi
lea    0xe99(%rip),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
cmp    $0x4,%eax
je     1186 <get_cursor_position+0x46>
movl   $0xffffffff,-0x4(%rbp)
jmp    126a <get_cursor_position+0x12a>
jmp    118b <get_cursor_position+0x4b>
mov    0x2e46(%rip),%rax
mov    (%rax),%edi
mov    -0x44(%rbp),%eax
lea    -0x40(%rbp),%rsi
add    %rax,%rsi
mov    $0x1,%edx
call   1050 <read@plt>
cmp    $0x1,%eax
je     11b6 <get_cursor_position+0x76>
jmp    11da <get_cursor_position+0x9a>
mov    -0x44(%rbp),%eax
movsbl -0x40(%rbp,%rax,1),%eax
cmp    $0x52,%eax
jne    11cc <get_cursor_position+0x8c>
jmp    11da <get_cursor_position+0x9a>
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
jmp    118b <get_cursor_position+0x4b>
mov    -0x44(%rbp),%eax
movb   $0x0,-0x40(%rbp,%rax,1)
movsbl -0x40(%rbp),%eax
cmp    $0x1b,%eax
jne    11fc <get_cursor_position+0xbc>
movsbl -0x3f(%rbp),%eax
cmp    $0x5b,%eax
je     1208 <get_cursor_position+0xc8>
movl   $0xffffffff,-0x4(%rbp)
jmp    126a <get_cursor_position+0x12a>
lea    -0x40(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x50(%rbp)
mov    -0x50(%rbp),%rdi
call   1060 <atoi@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x50(%rbp),%rdi
mov    $0x3b,%esi
call   1040 <strchr@plt>
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
jne    124e <get_cursor_position+0x10e>
movl   $0xffffffff,-0x4(%rbp)
jmp    126a <get_cursor_position+0x12a>
mov    -0x50(%rbp),%rdi
add    $0x1,%rdi
call   1060 <atoi@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_cursor_position>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
mov    0x2e95(%rip),%rax
mov    (%rax),%edi
lea    0xe9c(%rip),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
mov    $0xffffffff,%r12d
cmp    $0x4,%eax
jne    1218 <get_cursor_position+0xd8>
mov    %rsp,%rbx
mov    0x2e51(%rip),%r13
mov    0x0(%r13),%edi
mov    $0x1,%ebp
mov    %rbx,%rsi
mov    $0x1,%edx
call   1050 <read@plt>
cmp    $0x1,%eax
jne    11d4 <get_cursor_position+0x94>
mov    %rsp,%rbx
cs nopw 0x0(%rax,%rax,1)
nop
cmpb   $0x52,(%rbx)
je     11d4 <get_cursor_position+0x94>
mov    0x0(%r13),%edi
mov    %ebp,%eax
lea    (%rsp,%rax,1),%rbx
mov    %rbx,%rsi
mov    $0x1,%edx
call   1050 <read@plt>
add    $0x1,%ebp
cmp    $0x1,%eax
je     11b0 <get_cursor_position+0x70>
movb   $0x0,(%rbx)
cmpb   $0x1b,(%rsp)
jne    1218 <get_cursor_position+0xd8>
cmpb   $0x5b,0x1(%rsp)
jne    1218 <get_cursor_position+0xd8>
lea    0x2(%rsp),%rbx
mov    %rbx,%rdi
call   1060 <atoi@plt>
mov    %eax,(%r15)
mov    %rbx,%rdi
mov    $0x3b,%esi
call   1040 <strchr@plt>
test   %rax,%rax
je     1218 <get_cursor_position+0xd8>
add    $0x1,%rax
mov    %rax,%rdi
call   1060 <atoi@plt>
mov    %eax,(%r14)
xor    %r12d,%r12d
mov    %r12d,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@STDOUT_FILENO = global i32 0
@.str = [5 x i8] c"\1B[6n\00"
@STDIN_FILENO = global i32 0
define i32 @get_cursor_position(i32* nocapture writeonly %0, i32* nocapture writeonly %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 %40
8: ; preds = %2
%9 = load i32, i32* @STDIN_FILENO
%10 = call i32 @read(i32 %9, i8* %4, i32 1)
%11 = icmp eq i32 %10, 1
br i1 %11, label %18, label %24
12: ; preds = %18
%13 = load i32, i32* @STDIN_FILENO
%14 = zext i32 %23 to i64
%15 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 %14
%16 = call i32 @read(i32 %13, i8* %15, i32 1)
%17 = icmp eq i32 %16, 1
br i1 %17, label %18, label %24
18: ; preds = %8, %12
%19 = phi i8* [ %15, %12 ], [ %4, %8 ]
%20 = phi i32 [ %23, %12 ], [ 0, %8 ]
%21 = load i8, i8* %19
%22 = icmp eq i8 %21, 82
%23 = add i32 %20, 1
br i1 %22, label %24, label %12
24: ; preds = %12, %18, %8
%25 = phi i8* [ %4, %8 ], [ %19, %18 ], [ %15, %12 ]
store i8 0, i8* %25
%26 = load i8, i8* %4
%27 = icmp ne i8 %26, 27
%28 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 1
%29 = load i8, i8* %28
%30 = icmp ne i8 %29, 91
%31 = select i1 %27, i1 true, i1 %30
br i1 %31, label %40, label %32
32: ; preds = %24
%33 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 2
%34 = call i32 @atoi(i8* %33)
store i32 %34, i32* %0
%35 = call i8* @strchr(i8* %33, i8 signext 59)
%36 = icmp eq i8* %35, null
br i1 %36, label %40, label %37
37: ; preds = %32
%38 = getelementptr inbounds i8, i8* %35, i64 1
%39 = call i32 @atoi(i8* %38)
store i32 %39, i32* %1
br label %40
40: ; preds = %32, %24, %2, %37
%41 = phi i32 [ 0, %37 ], [ -1, %2 ], [ -1, %24 ], [ -1, %32 ]
ret i32 %41
}