#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 /*<<< orphan*/ ws_row;
  int /*<<< orphan*/ ws_col;
};

/* Variables and functions */
int /*<<< orphan*/ COLS;
int /*<<< orphan*/ LINES;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ TIOCGWINSZ;
int ioctl(int, int /*<<< orphan*/, struct winsize *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ stdscr;
char *ttyname(int /*<<< orphan*/);
int /*<<< orphan*/ var_init();
int /*<<< orphan*/ wclear(int /*<<< orphan*/);
int /*<<< orphan*/ wrefresh(int /*<<< orphan*/);
void handle_sigwinch(int s) {
  char *tty = NULL;
  int fd = 0;
  int result = 0;
  struct winsize win;
  tty = ttyname(0);
  if (!tty)
    return;
  fd = open(tty, O_RDWR);
  if (fd == -1)
    return;
  result = ioctl(fd, TIOCGWINSZ, &win);
  if (result == -1)
    return;
  COLS = win.ws_col;
  LINES = win.ws_row;
  var_init();
  wclear(stdscr);
  wrefresh(stdscr);
}


x86 assembly (clang, -O0):
<handle_sigwinch>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
movq   $0x0,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
xor    %edi,%edi
call   1050 <ttyname@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    119c <handle_sigwinch+0x3c>
jmp    1222 <handle_sigwinch+0xc2>
mov    -0x10(%rbp),%rdi
mov    0x2e41(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11c0 <handle_sigwinch+0x60>
jmp    1222 <handle_sigwinch+0xc2>
mov    -0x14(%rbp),%edi
mov    0x2dee(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1040 <ioctl@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    11e7 <handle_sigwinch+0x87>
jmp    1222 <handle_sigwinch+0xc2>
mov    -0x1c(%rbp),%ecx
mov    0x2ddf(%rip),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%ecx
mov    0x2de3(%rip),%rax
mov    %ecx,(%rax)
mov    $0x0,%al
call   1080 <var_init@plt>
mov    0x2dcb(%rip),%rax
mov    (%rax),%edi
call   1060 <wclear@plt>
mov    0x2dbd(%rip),%rax
mov    (%rax),%edi
call   1030 <wrefresh@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<handle_sigwinch>:
push   %rbx
sub    $0x10,%rsp
xor    %edi,%edi
call   1050 <ttyname@plt>
test   %rax,%rax
je     11d7 <handle_sigwinch+0x77>
mov    0x2e70(%rip),%rcx
mov    (%rcx),%esi
mov    %rax,%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     11d7 <handle_sigwinch+0x77>
mov    0x2e2a(%rip),%rcx
mov    (%rcx),%esi
lea    0x8(%rsp),%rdx
mov    %eax,%edi
call   1040 <ioctl@plt>
cmp    $0xffffffff,%eax
je     11d7 <handle_sigwinch+0x77>
mov    0x8(%rsp),%eax
mov    0xc(%rsp),%ecx
mov    0x2e20(%rip),%rdx
mov    %ecx,(%rdx)
mov    0x2e27(%rip),%rcx
mov    %eax,(%rcx)
xor    %eax,%eax
call   1080 <var_init@plt>
mov    0x2e0f(%rip),%rbx
mov    (%rbx),%edi
call   1060 <wclear@plt>
mov    (%rbx),%edi
call   1030 <wrefresh@plt>
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.winsize = type { i32, i32 }
@O_RDWR = global i32 0
@TIOCGWINSZ = global i32 0
@COLS = global i32 0
@LINES = global i32 0
@stdscr = global i32 0
define void @handle_sigwinch(i32 %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.winsize
store i32 %0, i32* %2
store i8* null, i8** %3
store i32 0, i32* %4
store i32 0, i32* %5
%7 = call i8* @ttyname(i32 0)
store i8* %7, i8** %3
%8 = load i8*, i8** %3
%9 = icmp ne i8* %8, null
br i1 %9, label %11, label %10
10: ; preds = %1
br label %35
11: ; preds = %1
%12 = load i8*, i8** %3
%13 = load i32, i32* @O_RDWR
%14 = call i32 @open(i8* %12, i32 %13)
store i32 %14, i32* %4
%15 = load i32, i32* %4
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %18
17: ; preds = %11
br label %35
18: ; preds = %11
%19 = load i32, i32* %4
%20 = load i32, i32* @TIOCGWINSZ
%21 = call i32 @ioctl(i32 %19, i32 %20, %struct.winsize* %6)
store i32 %21, i32* %5
%22 = load i32, i32* %5
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %25
24: ; preds = %18
br label %35
25: ; preds = %18
%26 = getelementptr inbounds %struct.winsize, %struct.winsize* %6, i32 0, i32 1
%27 = load i32, i32* %26
store i32 %27, i32* @COLS
%28 = getelementptr inbounds %struct.winsize, %struct.winsize* %6, i32 0, i32 0
%29 = load i32, i32* %28
store i32 %29, i32* @LINES
%30 = call i32 (...) @var_init()
%31 = load i32, i32* @stdscr
%32 = call i32 @wclear(i32 %31)
%33 = load i32, i32* @stdscr
%34 = call i32 @wrefresh(i32 %33)
br label %35
35: ; preds = %25, %24, %17, %10
ret void
}

LLVM IR (-O2):
%struct.winsize = type { i32, i32 }
@O_RDWR = global i32 0
@TIOCGWINSZ = global i32 0
@COLS = global i32 0
@LINES = global i32 0
@stdscr = global i32 0
define void @handle_sigwinch(i32 %0) {
%2 = alloca %struct.winsize
%3 = bitcast %struct.winsize* %2 to i8*
%4 = tail call i8* @ttyname(i32 0)
%5 = icmp eq i8* %4, null
br i1 %5, label %24, label %6
6: ; preds = %1
%7 = load i32, i32* @O_RDWR
%8 = tail call i32 @open(i8* %4, i32 %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %24, label %10
10: ; preds = %6
%11 = load i32, i32* @TIOCGWINSZ
%12 = call i32 @ioctl(i32 %8, i32 %11, %struct.winsize* %2)
%13 = icmp eq i32 %12, -1
br i1 %13, label %24, label %14
14: ; preds = %10
%15 = getelementptr inbounds %struct.winsize, %struct.winsize* %2, i64 0, i32 1
%16 = load i32, i32* %15
store i32 %16, i32* @COLS
%17 = getelementptr inbounds %struct.winsize, %struct.winsize* %2, i64 0, i32 0
%18 = load i32, i32* %17
store i32 %18, i32* @LINES
%19 = call i32 (...) @var_init()
%20 = load i32, i32* @stdscr
%21 = call i32 @wclear(i32 %20)
%22 = load i32, i32* @stdscr
%23 = call i32 @wrefresh(i32 %22)
br label %24
24: ; preds = %10, %6, %1, %14
ret void
}