#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 */
struct termios {
  int c_lflag;
};
typedef int /*<<< orphan*/ cpr;

/* Variables and functions */
int ECHO;
int ICANON;
int TCSAFLUSH;
int TCSANOW;
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int pv_getnum_i(char *);
int read(int, char *, int);
int /*<<< orphan*/ tcgetattr(int, struct termios *);
int /*<<< orphan*/ tcsetattr(int, int, struct termios *);
int /*<<< orphan*/ write(int, char *, int);
int pv_crs_get_ypos(int terminalfd) {
  struct termios tty;
  struct termios old_tty;
  char cpr[32];
  int ypos;
  int r;
  tcgetattr(terminalfd, &tty);
  tcgetattr(terminalfd, &old_tty);
  tty.c_lflag &= ~(ICANON | ECHO);
  tcsetattr(terminalfd, TCSANOW | TCSAFLUSH, &tty);
  write(terminalfd, "\033[6n", 4);
  memset(cpr, 0, sizeof(cpr));
  r = read(terminalfd, cpr, sizeof(cpr));
  if (r <= 0) {
    do {
    } while (0);
  } else {
    do {
    } while (0);
  }
  ypos = pv_getnum_i(cpr + 2);
  tcsetattr(terminalfd, TCSANOW | TCSAFLUSH, &old_tty);
  do {
  } while (0);
  return ypos;
}


x86 assembly (clang, -O0):
<pv_crs_get_ypos>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x8(%rbp),%rsi
call   1060 <tcgetattr@plt>
mov    -0x4(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   1060 <tcgetattr@plt>
mov    0x2e3e(%rip),%rax
mov    (%rax),%eax
mov    0x2e45(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x8(%rbp),%eax
mov    %eax,-0x8(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e38(%rip),%rax
mov    (%rax),%esi
mov    0x2e37(%rip),%rax
or     (%rax),%esi
lea    -0x8(%rbp),%rdx
call   1070 <tcsetattr@plt>
mov    -0x4(%rbp),%edi
lea    0xe3a(%rip),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    -0x4(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x20,%edx
call   1050 <read@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0x0,-0x38(%rbp)
jg     120d <pv_crs_get_ypos+0xad>
jmp    1203 <pv_crs_get_ypos+0xa3>
jmp    1208 <pv_crs_get_ypos+0xa8>
jmp    121c <pv_crs_get_ypos+0xbc>
jmp    1212 <pv_crs_get_ypos+0xb2>
jmp    1217 <pv_crs_get_ypos+0xb7>
jmp    121c <pv_crs_get_ypos+0xbc>
lea    -0x30(%rbp),%rdi
add    $0x2,%rdi
call   1080 <pv_getnum_i@plt>
mov    %eax,-0x34(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2daa(%rip),%rax
mov    (%rax),%esi
mov    0x2da9(%rip),%rax
or     (%rax),%esi
lea    -0x10(%rbp),%rdx
call   1070 <tcsetattr@plt>
jmp    124f <pv_crs_get_ypos+0xef>
mov    -0x34(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pv_crs_get_ypos>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %edi,%ebx
mov    %rsp,%r15
mov    %r15,%rsi
call   1060 <tcgetattr@plt>
lea    0x8(%rsp),%r14
mov    %ebx,%edi
mov    %r14,%rsi
call   1060 <tcgetattr@plt>
mov    0x2e37(%rip),%rax
mov    0x2e40(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
not    %ecx
and    %ecx,(%rsp)
mov    0x2e38(%rip),%r12
mov    0x2e39(%rip),%r13
mov    0x0(%r13),%esi
or     (%r12),%esi
mov    %ebx,%edi
mov    %r15,%rdx
call   1070 <tcsetattr@plt>
lea    0xe38(%rip),%rsi
mov    %ebx,%edi
mov    $0x4,%edx
call   1030 <write@plt>
lea    0x10(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x20,%edx
call   1050 <read@plt>
lea    0x12(%rsp),%rdi
call   1080 <pv_getnum_i@plt>
mov    %eax,%ebp
mov    0x0(%r13),%esi
or     (%r12),%esi
mov    %ebx,%edi
mov    %r14,%rdx
call   1070 <tcsetattr@plt>
mov    %ebp,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i32 }
@ICANON = global i32 0
@ECHO = global i32 0
@TCSANOW = global i32 0
@TCSAFLUSH = global i32 0
@.str = [5 x i8] c"\1B[6n\00"
define i32 @pv_crs_get_ypos(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.termios
%4 = alloca %struct.termios
%5 = alloca [32 x i8]
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %2
%8 = load i32, i32* %2
%9 = call i32 @tcgetattr(i32 %8, %struct.termios* %3)
%10 = load i32, i32* %2
%11 = call i32 @tcgetattr(i32 %10, %struct.termios* %4)
%12 = load i32, i32* @ICANON
%13 = load i32, i32* @ECHO
%14 = or i32 %12, %13
%15 = xor i32 %14, -1
%16 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 0
%17 = load i32, i32* %16
%18 = and i32 %17, %15
store i32 %18, i32* %16
%19 = load i32, i32* %2
%20 = load i32, i32* @TCSANOW
%21 = load i32, i32* @TCSAFLUSH
%22 = or i32 %20, %21
%23 = call i32 @tcsetattr(i32 %19, i32 %22, %struct.termios* %3)
%24 = load i32, i32* %2
%25 = call i32 @write(i32 %24, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 4)
%26 = getelementptr inbounds [32 x i8], [32 x i8]* %5, i64 0, i64 0
%27 = call i32 @memset(i8* %26, i32 0, i32 32)
%28 = load i32, i32* %2
%29 = getelementptr inbounds [32 x i8], [32 x i8]* %5, i64 0, i64 0
%30 = call i32 @read(i32 %28, i8* %29, i32 32)
store i32 %30, i32* %7
%31 = load i32, i32* %7
%32 = icmp sle i32 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %1
br label %34
34: ; preds = %33
br label %35
35: ; preds = %34
br label %39
36: ; preds = %1
br label %37
37: ; preds = %36
br label %38
38: ; preds = %37
br label %39
39: ; preds = %38, %35
%40 = getelementptr inbounds [32 x i8], [32 x i8]* %5, i64 0, i64 0
%41 = getelementptr inbounds i8, i8* %40, i64 2
%42 = call i32 @pv_getnum_i(i8* %41)
store i32 %42, i32* %6
%43 = load i32, i32* %2
%44 = load i32, i32* @TCSANOW
%45 = load i32, i32* @TCSAFLUSH
%46 = or i32 %44, %45
%47 = call i32 @tcsetattr(i32 %43, i32 %46, %struct.termios* %4)
br label %48
48: ; preds = %39
br label %49
49: ; preds = %48
%50 = load i32, i32* %6
ret i32 %50
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@ICANON = global i32 0
@ECHO = global i32 0
@TCSANOW = global i32 0
@TCSAFLUSH = global i32 0
@.str = [5 x i8] c"\1B[6n\00"
define i32 @pv_crs_get_ypos(i32 %0) {
%2 = alloca %struct.termios
%3 = alloca %struct.termios
%4 = alloca [32 x i8]
%5 = bitcast %struct.termios* %2 to i8*
%6 = bitcast %struct.termios* %3 to i8*
%7 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 0
%8 = call i32 @tcgetattr(i32 %0, %struct.termios* %2)
%9 = call i32 @tcgetattr(i32 %0, %struct.termios* %3)
%10 = load i32, i32* @ICANON
%11 = load i32, i32* @ECHO
%12 = or i32 %11, %10
%13 = xor i32 %12, -1
%14 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 0
%15 = load i32, i32* %14
%16 = and i32 %15, %13
store i32 %16, i32* %14
%17 = load i32, i32* @TCSANOW
%18 = load i32, i32* @TCSAFLUSH
%19 = or i32 %18, %17
%20 = call i32 @tcsetattr(i32 %0, i32 %19, %struct.termios* %2)
%21 = call i32 @write(i32 %0, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 4)
%22 = call i32 @memset(i8* %7, i32 0, i32 32)
%23 = call i32 @read(i32 %0, i8* %7, i32 32)
%24 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 2
%25 = call i32 @pv_getnum_i(i8* %24)
%26 = load i32, i32* @TCSANOW
%27 = load i32, i32* @TCSAFLUSH
%28 = or i32 %27, %26
%29 = call i32 @tcsetattr(i32 %0, i32 %28, %struct.termios* %3)
ret i32 %25
}