#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*/ TIOCGWINSZ;
int getCursorPosition(int, int, int *, int *);
int ioctl(int, int /*<<< orphan*/, struct winsize *);
int /*<<< orphan*/ snprintf(char *, int, char *, int, int);
int strlen(char *);
int write(int, char *, int);
int getWindowSize(int ifd, int ofd, int *rows, int *cols) {
  struct winsize ws;
  if (ioctl(1, TIOCGWINSZ, &ws) == -1 || ws.ws_col == 0) {
    int orig_row, orig_col, retval;
    retval = getCursorPosition(ifd, ofd, &orig_row, &orig_col);
    if (retval == -1)
      goto failed;
    if (write(ofd, "\x1b[999C\x1b[999B", 12) != 12)
      goto failed;
    retval = getCursorPosition(ifd, ofd, rows, cols);
    if (retval == -1)
      goto failed;
    char seq[32];
    snprintf(seq, 32, "\x1b[%d;%dH", orig_row, orig_col);
    if (write(ofd, seq, strlen(seq)) == -1) {
    }
    return 0;
  } else {
    *cols = ws.ws_col;
    *rows = ws.ws_row;
    return 0;
  }
failed:
  return -1;
}


x86 assembly (clang, -O0):
<getWindowSize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    0x2e6b(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edi
lea    -0x28(%rbp),%rdx
call   1060 <ioctl@plt>
cmp    $0xffffffff,%eax
je     1190 <getWindowSize+0x40>
cmpl   $0x0,-0x28(%rbp)
jne    1257 <getWindowSize+0x107>
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
lea    -0x2c(%rbp),%rdx
lea    -0x30(%rbp),%rcx
call   1070 <getCursorPosition@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    11b5 <getWindowSize+0x65>
jmp    1275 <getWindowSize+0x125>
mov    -0xc(%rbp),%edi
lea    0xe41(%rip),%rsi
mov    $0xc,%edx
call   1030 <write@plt>
cmp    $0xc,%eax
je     11d7 <getWindowSize+0x87>
jmp    1275 <getWindowSize+0x125>
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
call   1070 <getCursorPosition@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    11fc <getWindowSize+0xac>
jmp    1275 <getWindowSize+0x125>
lea    -0x60(%rbp),%rdi
mov    -0x2c(%rbp),%ecx
mov    -0x30(%rbp),%r8d
mov    $0x20,%esi
lea    0xdfa(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x6c(%rbp)
lea    -0x60(%rbp),%rax
mov    %rax,-0x68(%rbp)
lea    -0x60(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x6c(%rbp),%edi
mov    -0x68(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    124b <getWindowSize+0xfb>
jmp    124b <getWindowSize+0xfb>
movl   $0x0,-0x4(%rbp)
jmp    127c <getWindowSize+0x12c>
mov    -0x28(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x24(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    127c <getWindowSize+0x12c>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getWindowSize>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x38,%rsp
mov    %rcx,%rbp
mov    %rdx,%r15
mov    %esi,%r14d
mov    %edi,%ebx
mov    0x2e6c(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    $0x1,%edi
call   1060 <ioctl@plt>
cmp    $0xffffffff,%eax
je     119b <getWindowSize+0x4b>
mov    0x8(%rsp),%eax
test   %eax,%eax
je     119b <getWindowSize+0x4b>
mov    %eax,0x0(%rbp)
mov    0xc(%rsp),%eax
mov    %eax,(%r15)
xor    %eax,%eax
jmp    1220 <getWindowSize+0xd0>
lea    0x4(%rsp),%rdx
mov    %rsp,%rcx
mov    %ebx,%edi
mov    %r14d,%esi
call   1070 <getCursorPosition@plt>
cmp    $0xffffffff,%eax
je     121b <getWindowSize+0xcb>
lea    0xe47(%rip),%rsi
mov    %r14d,%edi
mov    $0xc,%edx
call   1030 <write@plt>
cmp    $0xc,%eax
jne    121b <getWindowSize+0xcb>
mov    %ebx,%edi
mov    %r14d,%esi
mov    %r15,%rdx
mov    %rbp,%rcx
call   1070 <getCursorPosition@plt>
cmp    $0xffffffff,%eax
je     121b <getWindowSize+0xcb>
mov    0x4(%rsp),%ecx
mov    (%rsp),%r8d
lea    0xe1e(%rip),%rdx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x20,%esi
call   1050 <snprintf@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
jmp    1194 <getWindowSize+0x44>
mov    $0xffffffff,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.winsize = type { i32, i32 }
@TIOCGWINSZ = global i32 0
@.str = [13 x i8] c"\1B[999C\1B[999B\00"
@.str.1 = [9 x i8] c"\1B[%d;%dH\00"
define i32 @getWindowSize(i32 %0, i32 %1, i32* %2, i32* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca %struct.winsize
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca [32 x i8]
store i32 %0, i32* %6
store i32 %1, i32* %7
store i32* %2, i32** %8
store i32* %3, i32** %9
%15 = load i32, i32* @TIOCGWINSZ
%16 = call i32 @ioctl(i32 1, i32 %15, %struct.winsize* %10)
%17 = icmp eq i32 %16, -1
br i1 %17, label %22, label %18
18: ; preds = %4
%19 = getelementptr inbounds %struct.winsize, %struct.winsize* %10, i32 0, i32 0
%20 = load i32, i32* %19
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %56
22: ; preds = %18, %4
%23 = load i32, i32* %6
%24 = load i32, i32* %7
%25 = call i32 @getCursorPosition(i32 %23, i32 %24, i32* %11, i32* %12)
store i32 %25, i32* %13
%26 = load i32, i32* %13
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %29
28: ; preds = %22
br label %63
29: ; preds = %22
%30 = load i32, i32* %7
%31 = call i32 @write(i32 %30, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 12)
%32 = icmp ne i32 %31, 12
br i1 %32, label %33, label %34
33: ; preds = %29
br label %63
34: ; preds = %29
%35 = load i32, i32* %6
%36 = load i32, i32* %7
%37 = load i32*, i32** %8
%38 = load i32*, i32** %9
%39 = call i32 @getCursorPosition(i32 %35, i32 %36, i32* %37, i32* %38)
store i32 %39, i32* %13
%40 = load i32, i32* %13
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %43
42: ; preds = %34
br label %63
43: ; preds = %34
%44 = getelementptr inbounds [32 x i8], [32 x i8]* %14, i64 0, i64 0
%45 = load i32, i32* %11
%46 = load i32, i32* %12
%47 = call i32 @snprintf(i8* %44, i32 32, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %45, i32 %46)
%48 = load i32, i32* %7
%49 = getelementptr inbounds [32 x i8], [32 x i8]* %14, i64 0, i64 0
%50 = getelementptr inbounds [32 x i8], [32 x i8]* %14, i64 0, i64 0
%51 = call i32 @strlen(i8* %50)
%52 = call i32 @write(i32 %48, i8* %49, i32 %51)
%53 = icmp eq i32 %52, -1
br i1 %53, label %54, label %55
54: ; preds = %43
br label %55
55: ; preds = %54, %43
store i32 0, i32* %5
br label %64
56: ; preds = %18
%57 = getelementptr inbounds %struct.winsize, %struct.winsize* %10, i32 0, i32 0
%58 = load i32, i32* %57
%59 = load i32*, i32** %9
store i32 %58, i32* %59
%60 = getelementptr inbounds %struct.winsize, %struct.winsize* %10, i32 0, i32 1
%61 = load i32, i32* %60
%62 = load i32*, i32** %8
store i32 %61, i32* %62
store i32 0, i32* %5
br label %64
63: ; preds = %42, %33, %28
store i32 -1, i32* %5
br label %64
64: ; preds = %63, %56, %55
%65 = load i32, i32* %5
ret i32 %65
}

LLVM IR (-O2):
%struct.winsize = type { i32, i32 }
@TIOCGWINSZ = global i32 0
@.str = [13 x i8] c"\1B[999C\1B[999B\00"
@.str.1 = [9 x i8] c"\1B[%d;%dH\00"
define i32 @getWindowSize(i32 %0, i32 %1, i32* %2, i32* %3) {
%5 = alloca %struct.winsize
%6 = alloca i32
%7 = alloca i32
%8 = alloca [32 x i8]
%9 = bitcast %struct.winsize* %5 to i8*
%10 = load i32, i32* @TIOCGWINSZ
%11 = call i32 @ioctl(i32 1, i32 %10, %struct.winsize* %5)
%12 = icmp eq i32 %11, -1
%13 = getelementptr inbounds %struct.winsize, %struct.winsize* %5, i64 0, i32 0
%14 = load i32, i32* %13
%15 = icmp eq i32 %14, 0
%16 = select i1 %12, i1 true, i1 %15
br i1 %16, label %17, label %36
17: ; preds = %4
%18 = bitcast i32* %6 to i8*
%19 = bitcast i32* %7 to i8*
%20 = call i32 @getCursorPosition(i32 %0, i32 %1, i32* %6, i32* %7)
%21 = icmp eq i32 %20, -1
br i1 %21, label %35, label %22
22: ; preds = %17
%23 = call i32 @write(i32 %1, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 12)
%24 = icmp eq i32 %23, 12
br i1 %24, label %25, label %35
25: ; preds = %22
%26 = call i32 @getCursorPosition(i32 %0, i32 %1, i32* %2, i32* %3)
%27 = icmp eq i32 %26, -1
br i1 %27, label %35, label %28
28: ; preds = %25
%29 = getelementptr inbounds [32 x i8], [32 x i8]* %8, i64 0, i64 0
%30 = load i32, i32* %6
%31 = load i32, i32* %7
%32 = call i32 @snprintf(i8* %29, i32 32, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %30, i32 %31)
%33 = call i32 @strlen(i8* %29)
%34 = call i32 @write(i32 %1, i8* %29, i32 %33)
br label %39
35: ; preds = %17, %22, %25
br label %39
36: ; preds = %4
store i32 %14, i32* %3
%37 = getelementptr inbounds %struct.winsize, %struct.winsize* %5, i64 0, i32 1
%38 = load i32, i32* %37
store i32 %38, i32* %2
br label %39
39: ; preds = %35, %28, %36
%40 = phi i32 [ 0, %36 ], [ -1, %35 ], [ 0, %28 ]
ret i32 %40
}