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

/* Variables and functions */
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ TIOCGWINSZ;
scalar_t__ ioctl(int /*<<< orphan*/, int /*<<< orphan*/, struct winsize *);
int update_terminal_size(size_t *restrict terminal_width,
                         size_t *restrict terminal_height) {
  struct winsize terminal_size;
  if (ioctl(STDIN_FILENO, TIOCGWINSZ, &terminal_size) < 0)
    return -1;
  *terminal_width = (size_t)(terminal_size.ws_col);
  *terminal_height = (size_t)(terminal_size.ws_row);
  return 0;
}


x86 assembly (clang, -O0):
<update_terminal_size>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2eb9(%rip),%rax
mov    (%rax),%edi
mov    0x2ea0(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1030 <ioctl@plt>
cmp    $0x0,%rax
jge    1151 <update_terminal_size+0x41>
movl   $0xffffffff,-0x4(%rbp)
jmp    116e <update_terminal_size+0x5e>
mov    -0x20(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<update_terminal_size>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2ebc(%rip),%rax
mov    (%rax),%edi
mov    0x2ea3(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
call   1030 <ioctl@plt>
test   %rax,%rax
js     1152 <update_terminal_size+0x42>
mov    0x10(%rsp),%rax
mov    %rax,(%rbx)
mov    0x8(%rsp),%rax
mov    %rax,(%r14)
xor    %eax,%eax
jmp    1157 <update_terminal_size+0x47>
mov    $0xffffffff,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.winsize = type { i64, i64 }
@STDIN_FILENO = global i32 0
@TIOCGWINSZ = global i32 0
define i32 @update_terminal_size(i64* noalias %0, i64* noalias %1) {
%3 = alloca i32
%4 = alloca i64*
%5 = alloca i64*
%6 = alloca %struct.winsize
store i64* %0, i64** %4
store i64* %1, i64** %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 slt i64 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %2
store i32 -1, i32* %3
br label %19
12: ; preds = %2
%13 = getelementptr inbounds %struct.winsize, %struct.winsize* %6, i32 0, i32 1
%14 = load i64, i64* %13
%15 = load i64*, i64** %4
store i64 %14, i64* %15
%16 = getelementptr inbounds %struct.winsize, %struct.winsize* %6, i32 0, i32 0
%17 = load i64, i64* %16
%18 = load i64*, i64** %5
store i64 %17, i64* %18
store i32 0, i32* %3
br label %19
19: ; preds = %12, %11
%20 = load i32, i32* %3
ret i32 %20
}

LLVM IR (-O2):
%struct.winsize = type { i64, i64 }
@STDIN_FILENO = global i32 0
@TIOCGWINSZ = global i32 0
define i32 @update_terminal_size(i64* noalias nocapture writeonly %0, i64* noalias 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 slt i64 %7, 0
br i1 %8, label %14, label %9
9: ; preds = %2
%10 = getelementptr inbounds %struct.winsize, %struct.winsize* %3, i64 0, i32 1
%11 = load i64, i64* %10
store i64 %11, i64* %0
%12 = getelementptr inbounds %struct.winsize, %struct.winsize* %3, i64 0, i32 0
%13 = load i64, i64* %12
store i64 %13, i64* %1
br label %14
14: ; preds = %2, %9
%15 = phi i32 [ 0, %9 ], [ -1, %2 ]
ret i32 %15
}