#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 winsize {
  int ws_col;
  int ws_row;
};

/* Variables and functions */
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ TIOCGWINSZ;
int getCursorPosition(int *, int *);
int ioctl(int /*<<< orphan*/, int /*<<< orphan*/, struct winsize *);
int write(int /*<<< orphan*/, char *, int);
int getWindowSize(int *rows, int *cols) {
  struct winsize ws;
  if (1 || ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1 || ws.ws_col == 0) {
    if (write(STDOUT_FILENO, "\x1b[999C\x1b[999B", 12) != 12) {
      return -1;
    }
    return getCursorPosition(rows, cols);
  } else {
    *cols = ws.ws_col;
    *rows = ws.ws_row;
    return 0;
  }
}


x86 assembly (clang, -O0):
<getWindowSize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2eb9(%rip),%rax
mov    (%rax),%edi
lea    0xec0(%rip),%rsi
mov    $0xc,%edx
call   1030 <write@plt>
cmp    $0xc,%eax
je     115f <getWindowSize+0x3f>
movl   $0xffffffff,-0x4(%rbp)
jmp    116f <getWindowSize+0x4f>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <getCursorPosition@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getWindowSize>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2ebf(%rip),%rax
mov    (%rax),%edi
lea    0xec6(%rip),%rsi
mov    $0xc,%edx
call   1030 <write@plt>
cmp    $0xc,%eax
jne    115b <getWindowSize+0x3b>
mov    %rbx,%rdi
mov    %r14,%rsi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1040 <getCursorPosition@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.winsize = type { i32, i32 }
@STDOUT_FILENO = global i32 0
@.str = [13 x i8] c"\1B[999C\1B[999B\00"
@TIOCGWINSZ = global i32 0
define i32 @getWindowSize(i32* %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca %struct.winsize
store i32* %0, i32** %4
store i32* %1, i32** %5
%7 = load i32, i32* @STDOUT_FILENO
%8 = call i32 @write(i32 %7, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 12)
%9 = icmp ne i32 %8, 12
br i1 %9, label %10, label %11
10: ; preds = %2
store i32 -1, i32* %3
br label %15
11: ; preds = %2
%12 = load i32*, i32** %4
%13 = load i32*, i32** %5
%14 = call i32 @getCursorPosition(i32* %12, i32* %13)
store i32 %14, i32* %3
br label %15
15: ; preds = %11, %10
%16 = load i32, i32* %3
ret i32 %16
}

LLVM IR (-O2):
%struct.winsize = type { i32, i32 }
@STDOUT_FILENO = global i32 0
@.str = [13 x i8] c"\1B[999C\1B[999B\00"
@TIOCGWINSZ = global i32 0
define i32 @getWindowSize(i32* %0, i32* %1) {
%3 = load i32, i32* @STDOUT_FILENO
%4 = tail call i32 @write(i32 %3, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 12)
%5 = icmp eq i32 %4, 12
br i1 %5, label %6, label %8
6: ; preds = %2
%7 = tail call i32 @getCursorPosition(i32* %0, i32* %1)
br label %8
8: ; preds = %2, %6
%9 = phi i32 [ %7, %6 ], [ -1, %2 ]
ret i32 %9
}