#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 */
typedef int /*<<< orphan*/ uint16_t;
struct winsize {
  int /*<<< orphan*/ ws_row;
  int /*<<< orphan*/ ws_col;
};

/* Variables and functions */
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ TIOCGWINSZ;
scalar_t__ ioctl(int /*<<< orphan*/, int /*<<< orphan*/, struct winsize *);
int /*<<< orphan*/ perror(char *);
int get_terminal_size(uint16_t *width, uint16_t *height) {
  struct winsize ws;
  if (ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) != 0) {
    perror("TIOCGWINSZ");
    return -1;
  }
  *width = ws.ws_col;
  *height = ws.ws_row;
  return 0;
}


x86 assembly (clang, -O0):
<get_terminal_size>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2ea9(%rip),%rax
mov    (%rax),%edi
mov    0x2e90(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1030 <ioctl@plt>
cmp    $0x0,%rax
je     116d <get_terminal_size+0x4d>
lea    0xea4(%rip),%rdi
call   1040 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1186 <get_terminal_size+0x66>
mov    -0x1c(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_terminal_size>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2eaf(%rip),%rax
mov    (%rax),%edi
mov    0x2e96(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
call   1030 <ioctl@plt>
test   %rax,%rax
jne    115f <get_terminal_size+0x3f>
mov    0x4(%rsp),%eax
mov    %eax,(%rbx)
mov    (%rsp),%eax
mov    %eax,(%r14)
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe9a(%rip),%rdi
call   1040 <perror@plt>
mov    $0xffffffff,%eax
jmp    1157 <get_terminal_size+0x37>

LLVM IR (-O0):
%struct.winsize = type { i32, i32 }
@STDIN_FILENO = global i32 0
@TIOCGWINSZ = global i32 0
@.str = [11 x i8] c"TIOCGWINSZ\00"
define i32 @get_terminal_size(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* @STDIN_FILENO
%8 = load i32, i32* @TIOCGWINSZ
%9 = call i64 @ioctl(i32 %7, i32 %8, %struct.winsize* %6)
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %20
13: ; preds = %2
%14 = getelementptr inbounds %struct.winsize, %struct.winsize* %6, i32 0, i32 1
%15 = load i32, i32* %14
%16 = load i32*, i32** %4
store i32 %15, i32* %16
%17 = getelementptr inbounds %struct.winsize, %struct.winsize* %6, i32 0, i32 0
%18 = load i32, i32* %17
%19 = load i32*, i32** %5
store i32 %18, i32* %19
store i32 0, i32* %3
br label %20
20: ; preds = %13, %11
%21 = load i32, i32* %3
ret i32 %21
}

LLVM IR (-O2):
%struct.winsize = type { i32, i32 }
@STDIN_FILENO = global i32 0
@TIOCGWINSZ = global i32 0
@.str = [11 x i8] c"TIOCGWINSZ\00"
define i32 @get_terminal_size(i32* nocapture writeonly %0, i32* nocapture writeonly %1) {
%3 = alloca %struct.winsize
%4 = bitcast %struct.winsize* %3 to i8*
%5 = load i32, i32* @STDIN_FILENO
%6 = load i32, i32* @TIOCGWINSZ
%7 = call i64 @ioctl(i32 %5, i32 %6, %struct.winsize* %3)
%8 = icmp eq i64 %7, 0
br i1 %8, label %11, label %9
9: ; preds = %2
%10 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %16
11: ; preds = %2
%12 = getelementptr inbounds %struct.winsize, %struct.winsize* %3, i64 0, i32 1
%13 = load i32, i32* %12
store i32 %13, i32* %0
%14 = getelementptr inbounds %struct.winsize, %struct.winsize* %3, i64 0, i32 0
%15 = load i32, i32* %14
store i32 %15, i32* %1
br label %16
16: ; preds = %11, %9
%17 = phi i32 [ -1, %9 ], [ 0, %11 ]
ret i32 %17
}