#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;
scalar_t__ ERR;
int /*<<< orphan*/ LINES;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ TIOCGWINSZ;
int /*<<< orphan*/ c_die(char *);
int /*<<< orphan*/ clear();
int ioctl(int, int /*<<< orphan*/, struct winsize *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ refresh();
int /*<<< orphan*/ resizeterm(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stdscr;
char *ttyname(int /*<<< orphan*/);
int /*<<< orphan*/ var_init();
scalar_t__ wresize(int /*<<< orphan*/, int /*<<< orphan*/, 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;
  resizeterm(LINES, COLS);
  if (wresize(stdscr, LINES, COLS) == ERR)
    c_die("Cannot resize window!");
  var_init();
  clear();
  refresh();
}


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   1060 <ttyname@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11cc <handle_sigwinch+0x3c>
jmp    1297 <handle_sigwinch+0x107>
mov    -0x10(%rbp),%rdi
mov    0x2e11(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11f0 <handle_sigwinch+0x60>
jmp    1297 <handle_sigwinch+0x107>
mov    -0x14(%rbp),%edi
mov    0x2db6(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1040 <ioctl@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    1217 <handle_sigwinch+0x87>
jmp    1297 <handle_sigwinch+0x107>
mov    -0x1c(%rbp),%ecx
mov    0x2da7(%rip),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%ecx
mov    0x2dab(%rip),%rax
mov    %ecx,(%rax)
mov    0x2da2(%rip),%rax
mov    (%rax),%edi
mov    0x2d89(%rip),%rax
mov    (%rax),%esi
call   10b0 <resizeterm@plt>
mov    0x2d83(%rip),%rax
mov    (%rax),%edi
mov    0x2d82(%rip),%rax
mov    (%rax),%esi
mov    0x2d69(%rip),%rax
mov    (%rax),%edx
call   1050 <wresize@plt>
mov    0x2d73(%rip),%rcx
cmp    (%rcx),%rax
jne    1282 <handle_sigwinch+0xf2>
lea    0xd83(%rip),%rdi
call   1030 <c_die@plt>
mov    $0x0,%al
call   10a0 <var_init@plt>
mov    $0x0,%al
call   1090 <clear@plt>
mov    $0x0,%al
call   1070 <refresh@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<handle_sigwinch>:
push   %r14
push   %rbx
push   %rax
xor    %edi,%edi
call   1060 <ttyname@plt>
test   %rax,%rax
je     1231 <handle_sigwinch+0xa1>
mov    0x2e3d(%rip),%rcx
mov    (%rcx),%esi
mov    %rax,%rdi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     1231 <handle_sigwinch+0xa1>
mov    0x2def(%rip),%rcx
mov    (%rcx),%esi
mov    %rsp,%rdx
mov    %eax,%edi
call   1040 <ioctl@plt>
cmp    $0xffffffff,%eax
je     1231 <handle_sigwinch+0xa1>
mov    (%rsp),%edi
mov    0x4(%rsp),%esi
mov    0x2de8(%rip),%r14
mov    %esi,(%r14)
mov    0x2dee(%rip),%rbx
mov    %edi,(%rbx)
call   10b0 <resizeterm@plt>
mov    0x2dd8(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%esi
mov    (%r14),%edx
call   1050 <wresize@plt>
mov    0x2dd5(%rip),%rcx
cmp    (%rcx),%rax
jne    121c <handle_sigwinch+0x8c>
lea    0xde9(%rip),%rdi
call   1030 <c_die@plt>
xor    %eax,%eax
call   10a0 <var_init@plt>
xor    %eax,%eax
call   1090 <clear@plt>
xor    %eax,%eax
call   1070 <refresh@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
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
@ERR = global i64 0
@.str = [22 x i8] c"Cannot resize window!\00"
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 %45
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 %45
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 %45
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 = load i32, i32* @LINES
%31 = load i32, i32* @COLS
%32 = call i32 @resizeterm(i32 %30, i32 %31)
%33 = load i32, i32* @stdscr
%34 = load i32, i32* @LINES
%35 = load i32, i32* @COLS
%36 = call i64 @wresize(i32 %33, i32 %34, i32 %35)
%37 = load i64, i64* @ERR
%38 = icmp eq i64 %36, %37
br i1 %38, label %39, label %41
39: ; preds = %25
%40 = call i32 @c_die(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %41
41: ; preds = %39, %25
%42 = call i32 (...) @var_init()
%43 = call i32 (...) @clear()
%44 = call i32 (...) @refresh()
br label %45
45: ; preds = %41, %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
@ERR = global i64 0
@.str = [22 x i8] c"Cannot resize window!\00"
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 %32, 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 %32, 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 %32, 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 @resizeterm(i32 %18, i32 %16)
%20 = load i32, i32* @stdscr
%21 = load i32, i32* @LINES
%22 = load i32, i32* @COLS
%23 = call i64 @wresize(i32 %20, i32 %21, i32 %22)
%24 = load i64, i64* @ERR
%25 = icmp eq i64 %23, %24
br i1 %25, label %26, label %28
26: ; preds = %14
%27 = call i32 @c_die(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %28
28: ; preds = %26, %14
%29 = call i32 (...) @var_init()
%30 = call i32 (...) @clear()
%31 = call i32 (...) @refresh()
br label %32
32: ; preds = %10, %6, %1, %28
ret void
}