#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 dummy;
};

/* Variables and functions */
int /*<<< orphan*/ TIOCGWINSZ;
int /*<<< orphan*/ fileno(int /*<<< orphan*/);
int /*<<< orphan*/ ioctl(int /*<<< orphan*/, int /*<<< orphan*/,
                         struct winsize *);
scalar_t__ malloc(int);
int /*<<< orphan*/ stdout;
struct winsize *get_winsize(char resized) {
  static struct winsize *ws = NULL;
  if (resized == 0 && ws != NULL)
    return ws;
  if (ws == NULL) {
    ws = (struct winsize *)malloc(sizeof(struct winsize));
  }
  ioctl(fileno(stdout), TIOCGWINSZ, ws);
  return ws;
}


x86 assembly (clang, -O0):
<get_winsize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %dil,%al
mov    %al,-0x9(%rbp)
movsbl -0x9(%rbp),%eax
cmp    $0x0,%eax
jne    1169 <get_winsize+0x39>
cmpq   $0x0,0x2eed(%rip)
je     1169 <get_winsize+0x39>
mov    0x2ee0(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    11b8 <get_winsize+0x88>
cmpq   $0x0,0x2ecf(%rip)
jne    1188 <get_winsize+0x58>
mov    $0x4,%edi
call   1050 <malloc@plt>
mov    %rax,0x2eb8(%rip)
mov    0x2e51(%rip),%rax
mov    (%rax),%edi
call   1040 <fileno@plt>
mov    %eax,%edi
mov    0x2e31(%rip),%rax
mov    (%rax),%esi
mov    0x2e98(%rip),%rdx
call   1030 <ioctl@plt>
mov    0x2e8c(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_winsize>:
push   %rax
mov    0x2f08(%rip),%rax
test   %dil,%dil
jne    1144 <get_winsize+0x14>
test   %rax,%rax
je     1144 <get_winsize+0x14>
pop    %rcx
ret
test   %rax,%rax
jne    115a <get_winsize+0x2a>
mov    $0x4,%edi
call   1050 <malloc@plt>
mov    %rax,0x2ee6(%rip)
mov    0x2e7f(%rip),%rax
mov    (%rax),%edi
call   1040 <fileno@plt>
mov    0x2e61(%rip),%rcx
mov    (%rcx),%esi
mov    0x2ec8(%rip),%rdx
mov    %eax,%edi
call   1030 <ioctl@plt>
mov    0x2eba(%rip),%rax
pop    %rcx
ret

LLVM IR (-O0):
%struct.winsize = type { i32 }
@get_winsize.ws = internal global %struct.winsize* null
@stdout = global i32 0
@TIOCGWINSZ = global i32 0
define %struct.winsize* @get_winsize(i8 signext %0) {
%2 = alloca %struct.winsize*
%3 = alloca i8
store i8 %0, i8* %3
%4 = load i8, i8* %3
%5 = sext i8 %4 to i32
%6 = icmp eq i32 %5, 0
br i1 %6, label %7, label %12
7: ; preds = %1
%8 = load %struct.winsize*, %struct.winsize** @get_winsize.ws
%9 = icmp ne %struct.winsize* %8, null
br i1 %9, label %10, label %12
10: ; preds = %7
%11 = load %struct.winsize*, %struct.winsize** @get_winsize.ws
store %struct.winsize* %11, %struct.winsize** %2
br label %25
12: ; preds = %7, %1
%13 = load %struct.winsize*, %struct.winsize** @get_winsize.ws
%14 = icmp eq %struct.winsize* %13, null
br i1 %14, label %15, label %18
15: ; preds = %12
%16 = call i64 @malloc(i32 4)
%17 = inttoptr i64 %16 to %struct.winsize*
store %struct.winsize* %17, %struct.winsize** @get_winsize.ws
br label %18
18: ; preds = %15, %12
%19 = load i32, i32* @stdout
%20 = call i32 @fileno(i32 %19)
%21 = load i32, i32* @TIOCGWINSZ
%22 = load %struct.winsize*, %struct.winsize** @get_winsize.ws
%23 = call i32 @ioctl(i32 %20, i32 %21, %struct.winsize* %22)
%24 = load %struct.winsize*, %struct.winsize** @get_winsize.ws
store %struct.winsize* %24, %struct.winsize** %2
br label %25
25: ; preds = %18, %10
%26 = load %struct.winsize*, %struct.winsize** %2
ret %struct.winsize* %26
}

LLVM IR (-O2):
%struct.winsize = type { i32 }
@get_winsize.ws = internal unnamed_addr global %struct.winsize* null
@stdout = global i32 0
@TIOCGWINSZ = global i32 0
define %struct.winsize* @get_winsize(i8 signext %0) {
%2 = icmp eq i8 %0, 0
%3 = load %struct.winsize*, %struct.winsize** @get_winsize.ws
%4 = icmp ne %struct.winsize* %3, null
%5 = select i1 %2, i1 %4, i1 false
br i1 %5, label %18, label %6
6: ; preds = %1
%7 = icmp eq %struct.winsize* %3, null
br i1 %7, label %8, label %11
8: ; preds = %6
%9 = tail call i64 @malloc(i32 4)
%10 = inttoptr i64 %9 to %struct.winsize*
store %struct.winsize* %10, %struct.winsize** @get_winsize.ws
br label %11
11: ; preds = %8, %6
%12 = load i32, i32* @stdout
%13 = tail call i32 @fileno(i32 %12)
%14 = load i32, i32* @TIOCGWINSZ
%15 = load %struct.winsize*, %struct.winsize** @get_winsize.ws
%16 = tail call i32 @ioctl(i32 %13, i32 %14, %struct.winsize* %15)
%17 = load %struct.winsize*, %struct.winsize** @get_winsize.ws
br label %18
18: ; preds = %1, %11
%19 = phi %struct.winsize* [ %17, %11 ], [ %3, %1 ]
ret %struct.winsize* %19
}