#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;
scalar_t__ ioctl(int, int /*<<< orphan*/, struct winsize *);
int getwinsize(int fd, int *row, int *col) {
  struct winsize ws;
  if (ioctl(fd, TIOCGWINSZ, &ws) == 0) {
    if (row)
      *row = ws.ws_row;
    if (col)
      *col = ws.ws_col;
    return 0;
  } else {
    return -1;
  }
}


x86 assembly (clang, -O0):
<getwinsize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2eab(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1030 <ioctl@plt>
cmp    $0x0,%rax
jne    1176 <getwinsize+0x66>
cmpq   $0x0,-0x10(%rbp)
je     1156 <getwinsize+0x46>
mov    -0x20(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
cmpq   $0x0,-0x18(%rbp)
je     116a <getwinsize+0x5a>
mov    -0x1c(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    117d <getwinsize+0x6d>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getwinsize>:
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%rbx
mov    0x2eb7(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
call   1030 <ioctl@plt>
mov    %rax,%rcx
mov    $0xffffffff,%eax
test   %rcx,%rcx
jne    1150 <getwinsize+0x40>
test   %rbx,%rbx
je     1142 <getwinsize+0x32>
mov    (%rsp),%eax
mov    %eax,(%rbx)
xor    %eax,%eax
test   %r14,%r14
je     1150 <getwinsize+0x40>
mov    0x4(%rsp),%ecx
mov    %ecx,(%r14)
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.winsize = type { i32, i32 }
@TIOCGWINSZ = global i32 0
define i32 @getwinsize(i32 %0, i32* %1, i32* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca %struct.winsize
store i32 %0, i32* %5
store i32* %1, i32** %6
store i32* %2, i32** %7
%9 = load i32, i32* %5
%10 = load i32, i32* @TIOCGWINSZ
%11 = call i64 @ioctl(i32 %9, i32 %10, %struct.winsize* %8)
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %28
13: ; preds = %3
%14 = load i32*, i32** %6
%15 = icmp ne i32* %14, null
br i1 %15, label %16, label %20
16: ; preds = %13
%17 = getelementptr inbounds %struct.winsize, %struct.winsize* %8, i32 0, i32 0
%18 = load i32, i32* %17
%19 = load i32*, i32** %6
store i32 %18, i32* %19
br label %20
20: ; preds = %16, %13
%21 = load i32*, i32** %7
%22 = icmp ne i32* %21, null
br i1 %22, label %23, label %27
23: ; preds = %20
%24 = getelementptr inbounds %struct.winsize, %struct.winsize* %8, i32 0, i32 1
%25 = load i32, i32* %24
%26 = load i32*, i32** %7
store i32 %25, i32* %26
br label %27
27: ; preds = %23, %20
store i32 0, i32* %4
br label %29
28: ; preds = %3
store i32 -1, i32* %4
br label %29
29: ; preds = %28, %27
%30 = load i32, i32* %4
ret i32 %30
}

LLVM IR (-O2):
%struct.winsize = type { i32, i32 }
@TIOCGWINSZ = global i32 0
define i32 @getwinsize(i32 %0, i32* writeonly %1, i32* writeonly %2) {
%4 = alloca %struct.winsize
%5 = bitcast %struct.winsize* %4 to i8*
%6 = load i32, i32* @TIOCGWINSZ
%7 = call i64 @ioctl(i32 %0, i32 %6, %struct.winsize* %4)
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %19
9: ; preds = %3
%10 = icmp eq i32* %1, null
br i1 %10, label %14, label %11
11: ; preds = %9
%12 = getelementptr inbounds %struct.winsize, %struct.winsize* %4, i64 0, i32 0
%13 = load i32, i32* %12
store i32 %13, i32* %1
br label %14
14: ; preds = %11, %9
%15 = icmp eq i32* %2, null
br i1 %15, label %19, label %16
16: ; preds = %14
%17 = getelementptr inbounds %struct.winsize, %struct.winsize* %4, i64 0, i32 1
%18 = load i32, i32* %17
store i32 %18, i32* %2
br label %19
19: ; preds = %3, %14, %16
%20 = phi i32 [ 0, %16 ], [ 0, %14 ], [ -1, %3 ]
ret i32 %20
}