#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 /*<<< orphan*/ editorReadKey();
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 (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1 || ws.ws_col == 0) {
    if (write(STDOUT_FILENO, "\x1b[999C\x1b[999B", 12) != 12)
      return -1;
    editorReadKey();
    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    0x2e99(%rip),%rax
mov    (%rax),%edi
mov    0x2e70(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0xffffffff,%eax
je     117e <getWindowSize+0x3e>
cmpl   $0x0,-0x20(%rbp)
jne    11c9 <getWindowSize+0x89>
mov    0x2e6b(%rip),%rax
mov    (%rax),%edi
lea    0xe72(%rip),%rsi
mov    $0xc,%edx
call   1030 <write@plt>
cmp    $0xc,%eax
je     11ad <getWindowSize+0x6d>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e2 <getWindowSize+0xa2>
mov    $0x0,%al
call   1050 <editorReadKey@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <getCursorPosition@plt>
mov    %eax,-0x4(%rbp)
jmp    11e2 <getWindowSize+0xa2>
mov    -0x20(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x1c(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getWindowSize>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
mov    0x2e9a(%rip),%rbx
mov    (%rbx),%edi
mov    0x2e71(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
call   1040 <ioctl@plt>
cmp    $0xffffffff,%eax
je     1186 <getWindowSize+0x46>
mov    0x8(%rsp),%eax
test   %eax,%eax
je     1186 <getWindowSize+0x46>
mov    %eax,(%r14)
mov    0xc(%rsp),%eax
mov    %eax,(%r15)
xor    %eax,%eax
jmp    11b7 <getWindowSize+0x77>
mov    (%rbx),%edi
lea    0xe71(%rip),%rsi
mov    $0xc,%edx
call   1030 <write@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    $0xc,%ecx
jne    11b7 <getWindowSize+0x77>
xor    %eax,%eax
call   1050 <editorReadKey@plt>
mov    %r15,%rdi
mov    %r14,%rsi
call   1060 <getCursorPosition@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.winsize = type { i32, i32 }
@STDOUT_FILENO = global i32 0
@TIOCGWINSZ = global i32 0
@.str = [13 x i8] c"\1B[999C\1B[999B\00"
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 = load i32, i32* @TIOCGWINSZ
%9 = call i32 @ioctl(i32 %7, i32 %8, %struct.winsize* %6)
%10 = icmp eq i32 %9, -1
br i1 %10, label %15, label %11
11: ; preds = %2
%12 = getelementptr inbounds %struct.winsize, %struct.winsize* %6, i32 0, i32 0
%13 = load i32, i32* %12
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %25
15: ; preds = %11, %2
%16 = load i32, i32* @STDOUT_FILENO
%17 = call i32 @write(i32 %16, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 12)
%18 = icmp ne i32 %17, 12
br i1 %18, label %19, label %20
19: ; preds = %15
store i32 -1, i32* %3
br label %32
20: ; preds = %15
%21 = call i32 (...) @editorReadKey()
%22 = load i32*, i32** %4
%23 = load i32*, i32** %5
%24 = call i32 @getCursorPosition(i32* %22, i32* %23)
store i32 %24, i32* %3
br label %32
25: ; preds = %11
%26 = getelementptr inbounds %struct.winsize, %struct.winsize* %6, i32 0, i32 0
%27 = load i32, i32* %26
%28 = load i32*, i32** %5
store i32 %27, i32* %28
%29 = getelementptr inbounds %struct.winsize, %struct.winsize* %6, i32 0, i32 1
%30 = load i32, i32* %29
%31 = load i32*, i32** %4
store i32 %30, i32* %31
store i32 0, i32* %3
br label %32
32: ; preds = %25, %20, %19
%33 = load i32, i32* %3
ret i32 %33
}

LLVM IR (-O2):
%struct.winsize = type { i32, i32 }
@STDOUT_FILENO = global i32 0
@TIOCGWINSZ = global i32 0
@.str = [13 x i8] c"\1B[999C\1B[999B\00"
define i32 @getWindowSize(i32* %0, i32* %1) {
%3 = alloca %struct.winsize
%4 = bitcast %struct.winsize* %3 to i8*
%5 = load i32, i32* @STDOUT_FILENO
%6 = load i32, i32* @TIOCGWINSZ
%7 = call i32 @ioctl(i32 %5, i32 %6, %struct.winsize* %3)
%8 = icmp eq i32 %7, -1
%9 = getelementptr inbounds %struct.winsize, %struct.winsize* %3, i64 0, i32 0
%10 = load i32, i32* %9
%11 = icmp eq i32 %10, 0
%12 = select i1 %8, i1 true, i1 %11
br i1 %12, label %13, label %20
13: ; preds = %2
%14 = load i32, i32* @STDOUT_FILENO
%15 = call i32 @write(i32 %14, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 12)
%16 = icmp eq i32 %15, 12
br i1 %16, label %17, label %23
17: ; preds = %13
%18 = call i32 (...) @editorReadKey()
%19 = call i32 @getCursorPosition(i32* %0, i32* %1)
br label %23
20: ; preds = %2
store i32 %10, i32* %1
%21 = getelementptr inbounds %struct.winsize, %struct.winsize* %3, i64 0, i32 1
%22 = load i32, i32* %21
store i32 %22, i32* %0
br label %23
23: ; preds = %13, %20, %17
%24 = phi i32 [ %19, %17 ], [ 0, %20 ], [ -1, %13 ]
ret i32 %24
}