#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_row;
  int ws_col;
};

/* Variables and functions */
int /*<<< orphan*/ TIOCGWINSZ;
int atoi(char *);
char *getenv(char *);
scalar_t__ ioctl(int /*<<< orphan*/, int /*<<< orphan*/, struct winsize *);
int /*<<< orphan*/ signal2(int, void (*)(int));
int /*<<< orphan*/ video_init(int, int);
void sig_winch(int sig) {
  struct winsize ws;
  int rows, cols;
  char *p;
  rows = 0;
  cols = 0;
  if (ioctl(0, TIOCGWINSZ, &ws) == 0) {
    rows = ws.ws_row;
    cols = ws.ws_col;
  } else {
    p = getenv("LINES");
    if (p != NULL)
      rows = atoi(p);
    p = getenv("COLUMNS");
    if (p != NULL)
      cols = atoi(p);
  }
  if (rows == 0)
    rows = 24;
  if (cols == 0)
    cols = 80;
  video_init(rows, cols);
  signal2(sig, sig_winch);
}


x86 assembly (clang, -O0):
<sig_winch>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    0x2e60(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    -0x10(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jne    1198 <sig_winch+0x48>
mov    -0x10(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    11eb <sig_winch+0x9b>
lea    0xe61(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11bf <sig_winch+0x6f>
mov    -0x20(%rbp),%rdi
call   1070 <atoi@plt>
mov    %eax,-0x14(%rbp)
lea    0xe40(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11e6 <sig_winch+0x96>
mov    -0x20(%rbp),%rdi
call   1070 <atoi@plt>
mov    %eax,-0x18(%rbp)
jmp    11eb <sig_winch+0x9b>
cmpl   $0x0,-0x14(%rbp)
jne    11fc <sig_winch+0xac>
movl   $0x18,-0x14(%rbp)
cmpl   $0x0,-0x18(%rbp)
jne    120d <sig_winch+0xbd>
movl   $0x50,-0x18(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x18(%rbp),%esi
call   1060 <video_init@plt>
mov    -0x4(%rbp),%edi
mov    0x2dd6(%rip),%rsi
call   1050 <signal2@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sig_winch>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebx
mov    0x2e74(%rip),%rax
mov    (%rax),%esi
xor    %ebp,%ebp
mov    %rsp,%rdx
xor    %edi,%edi
call   1040 <ioctl@plt>
test   %rax,%rax
je     11a7 <sig_winch+0x57>
lea    0xe8a(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     118a <sig_winch+0x3a>
mov    %rax,%rdi
call   1070 <atoi@plt>
mov    %eax,%ebp
lea    0xe75(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     11be <sig_winch+0x6e>
mov    %rax,%rdi
call   1070 <atoi@plt>
mov    %eax,%esi
jmp    11ae <sig_winch+0x5e>
mov    (%rsp),%ebp
mov    0x4(%rsp),%esi
test   %ebp,%ebp
mov    $0x18,%edi
cmovne %ebp,%edi
test   %esi,%esi
jne    11cd <sig_winch+0x7d>
jmp    11c8 <sig_winch+0x78>
test   %ebp,%ebp
mov    $0x18,%edi
cmovne %ebp,%edi
mov    $0x50,%esi
call   1060 <video_init@plt>
mov    0x2e1f(%rip),%rsi
mov    %ebx,%edi
call   1050 <signal2@plt>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.winsize = type { i32, i32 }
@TIOCGWINSZ = global i32 0
@.str = [6 x i8] c"LINES\00"
@.str.1 = [8 x i8] c"COLUMNS\00"
define void @sig_winch(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.winsize
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
store i32 %0, i32* %2
store i32 0, i32* %4
store i32 0, i32* %5
%7 = load i32, i32* @TIOCGWINSZ
%8 = call i64 @ioctl(i32 0, i32 %7, %struct.winsize* %3)
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %15
10: ; preds = %1
%11 = getelementptr inbounds %struct.winsize, %struct.winsize* %3, i32 0, i32 0
%12 = load i32, i32* %11
store i32 %12, i32* %4
%13 = getelementptr inbounds %struct.winsize, %struct.winsize* %3, i32 0, i32 1
%14 = load i32, i32* %13
store i32 %14, i32* %5
br label %30
15: ; preds = %1
%16 = call i8* @getenv(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i8* %16, i8** %6
%17 = load i8*, i8** %6
%18 = icmp ne i8* %17, null
br i1 %18, label %19, label %22
19: ; preds = %15
%20 = load i8*, i8** %6
%21 = call i32 @atoi(i8* %20)
store i32 %21, i32* %4
br label %22
22: ; preds = %19, %15
%23 = call i8* @getenv(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
store i8* %23, i8** %6
%24 = load i8*, i8** %6
%25 = icmp ne i8* %24, null
br i1 %25, label %26, label %29
26: ; preds = %22
%27 = load i8*, i8** %6
%28 = call i32 @atoi(i8* %27)
store i32 %28, i32* %5
br label %29
29: ; preds = %26, %22
br label %30
30: ; preds = %29, %10
%31 = load i32, i32* %4
%32 = icmp eq i32 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %30
store i32 24, i32* %4
br label %34
34: ; preds = %33, %30
%35 = load i32, i32* %5
%36 = icmp eq i32 %35, 0
br i1 %36, label %37, label %38
37: ; preds = %34
store i32 80, i32* %5
br label %38
38: ; preds = %37, %34
%39 = load i32, i32* %4
%40 = load i32, i32* %5
%41 = call i32 @video_init(i32 %39, i32 %40)
%42 = load i32, i32* %2
%43 = call i32 @signal2(i32 %42, void (i32)* @sig_winch)
ret void
}

LLVM IR (-O2):
%struct.winsize = type { i32, i32 }
@TIOCGWINSZ = global i32 0
@.str = [6 x i8] c"LINES\00"
@.str.1 = [8 x i8] c"COLUMNS\00"
define void @sig_winch(i32 %0) {
%2 = alloca %struct.winsize
%3 = bitcast %struct.winsize* %2 to i8*
%4 = load i32, i32* @TIOCGWINSZ
%5 = call i64 @ioctl(i32 0, i32 %4, %struct.winsize* %2)
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %12
7: ; preds = %1
%8 = getelementptr inbounds %struct.winsize, %struct.winsize* %2, i64 0, i32 0
%9 = load i32, i32* %8
%10 = getelementptr inbounds %struct.winsize, %struct.winsize* %2, i64 0, i32 1
%11 = load i32, i32* %10
br label %26
12: ; preds = %1
%13 = call i8* @getenv(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%14 = icmp eq i8* %13, null
br i1 %14, label %17, label %15
15: ; preds = %12
%16 = call i32 @atoi(i8* %13)
br label %17
17: ; preds = %15, %12
%18 = phi i32 [ %16, %15 ], [ 0, %12 ]
%19 = call i8* @getenv(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%20 = icmp eq i8* %19, null
br i1 %20, label %21, label %24
21: ; preds = %17
%22 = icmp eq i32 %18, 0
%23 = select i1 %22, i32 24, i32 %18
br label %32
24: ; preds = %17
%25 = call i32 @atoi(i8* %19)
br label %26
26: ; preds = %24, %7
%27 = phi i32 [ %9, %7 ], [ %18, %24 ]
%28 = phi i32 [ %11, %7 ], [ %25, %24 ]
%29 = icmp eq i32 %27, 0
%30 = select i1 %29, i32 24, i32 %27
%31 = icmp eq i32 %28, 0
br i1 %31, label %32, label %34
32: ; preds = %21, %26
%33 = phi i32 [ %23, %21 ], [ %30, %26 ]
br label %34
34: ; preds = %26, %32
%35 = phi i32 [ %33, %32 ], [ %30, %26 ]
%36 = phi i32 [ 80, %32 ], [ %28, %26 ]
%37 = call i32 @video_init(i32 %35, i32 %36)
%38 = call i32 @signal2(i32 %0, void (i32)* @sig_winch)
ret void
}