#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 get_cursor_position(int *, int *);
int ioctl(int /*<<< orphan*/, int /*<<< orphan*/, struct winsize *);
int /*<<< orphan*/ write(int /*<<< orphan*/, char *, int);
int get_window_size(int *rows, int *cols) {
  int result;
  struct winsize ws;
  result = ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws);
  if ((result == -1) || (ws.ws_col == 0)) {
    write(STDOUT_FILENO, "\x1b[999C\x1b[999B", 12);
    return get_cursor_position(rows, cols);
  } else {
    *cols = ws.ws_col;
    *rows = ws.ws_row;
  }
  return 0;
}


x86 assembly (clang, -O0):
<get_window_size>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2ea9(%rip),%rax
mov    (%rax),%edi
mov    0x2e80(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1040 <ioctl@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
je     1172 <get_window_size+0x42>
cmpl   $0x0,-0x28(%rbp)
jne    11a1 <get_window_size+0x71>
mov    0x2e77(%rip),%rax
mov    (%rax),%edi
lea    0xe7e(%rip),%rsi
mov    $0xc,%edx
call   1030 <write@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <get_cursor_position@plt>
mov    %eax,-0x4(%rbp)
jmp    11ba <get_window_size+0x8a>
mov    -0x28(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x24(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_window_size>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2eaa(%rip),%r15
mov    (%r15),%edi
mov    0x2e80(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
call   1040 <ioctl@plt>
cmp    $0xffffffff,%eax
je     1176 <get_window_size+0x46>
mov    0x8(%rsp),%eax
test   %eax,%eax
je     1176 <get_window_size+0x46>
mov    %eax,(%r14)
mov    0xc(%rsp),%eax
mov    %eax,(%rbx)
xor    %eax,%eax
jmp    1195 <get_window_size+0x65>
mov    (%r15),%edi
lea    0xe80(%rip),%rsi
mov    $0xc,%edx
call   1030 <write@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1050 <get_cursor_position@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 @get_window_size(i32* %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca %struct.winsize
store i32* %0, i32** %4
store i32* %1, i32** %5
%8 = load i32, i32* @STDOUT_FILENO
%9 = load i32, i32* @TIOCGWINSZ
%10 = call i32 @ioctl(i32 %8, i32 %9, %struct.winsize* %7)
store i32 %10, i32* %6
%11 = load i32, i32* %6
%12 = icmp eq i32 %11, -1
br i1 %12, label %17, label %13
13: ; preds = %2
%14 = getelementptr inbounds %struct.winsize, %struct.winsize* %7, i32 0, i32 0
%15 = load i32, i32* %14
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %23
17: ; preds = %13, %2
%18 = load i32, i32* @STDOUT_FILENO
%19 = call i32 @write(i32 %18, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 12)
%20 = load i32*, i32** %4
%21 = load i32*, i32** %5
%22 = call i32 @get_cursor_position(i32* %20, i32* %21)
store i32 %22, i32* %3
br label %31
23: ; preds = %13
%24 = getelementptr inbounds %struct.winsize, %struct.winsize* %7, i32 0, i32 0
%25 = load i32, i32* %24
%26 = load i32*, i32** %5
store i32 %25, i32* %26
%27 = getelementptr inbounds %struct.winsize, %struct.winsize* %7, i32 0, i32 1
%28 = load i32, i32* %27
%29 = load i32*, i32** %4
store i32 %28, i32* %29
br label %30
30: ; preds = %23
store i32 0, i32* %3
br label %31
31: ; preds = %30, %17
%32 = load i32, i32* %3
ret i32 %32
}

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 @get_window_size(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 %17
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 = call i32 @get_cursor_position(i32* %0, i32* %1)
br label %20
17: ; preds = %2
store i32 %10, i32* %1
%18 = getelementptr inbounds %struct.winsize, %struct.winsize* %3, i64 0, i32 1
%19 = load i32, i32* %18
store i32 %19, i32* %0
br label %20
20: ; preds = %17, %13
%21 = phi i32 [ %16, %13 ], [ 0, %17 ]
ret i32 %21
}