#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 ioctl(int, int /*<<< orphan*/, struct winsize *);
int /*<<< orphan*/ printf(char *, char *);
int get_tty_size(int tty_fd, int *numrows, int *numcols) {
  struct winsize window_arg;
  if (-1 == ioctl(tty_fd, TIOCGWINSZ, &window_arg)) {
    printf("%s\n", "Failed ioctl");
    return 0;
  }
  *numrows = window_arg.ws_row;
  *numcols = window_arg.ws_col;
  return 1;
}


x86 assembly (clang, -O0):
<get_tty_size>:
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    0x2e9b(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1040 <ioctl@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    1176 <get_tty_size+0x56>
lea    0xea2(%rip),%rdi
lea    0xe9f(%rip),%rsi
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    118f <get_tty_size+0x6f>
mov    -0x20(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x1c(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_tty_size>:
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%rbx
mov    0x2ea7(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
call   1040 <ioctl@plt>
cmp    $0xffffffff,%eax
je     1153 <get_tty_size+0x33>
mov    (%rsp),%eax
mov    %eax,(%rbx)
mov    0x4(%rsp),%eax
mov    %eax,(%r14)
mov    $0x1,%eax
jmp    1168 <get_tty_size+0x48>
lea    0xea6(%rip),%rdi
lea    0xea3(%rip),%rsi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.winsize = type { i32, i32 }
@TIOCGWINSZ = global i32 0
@.str = [4 x i8] c"%s\0A\00"
@.str.1 = [13 x i8] c"Failed ioctl\00"
define i32 @get_tty_size(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 i32 @ioctl(i32 %9, i32 %10, %struct.winsize* %8)
%12 = icmp eq i32 -1, %11
br i1 %12, label %13, label %15
13: ; preds = %3
%14 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %4
br label %22
15: ; preds = %3
%16 = getelementptr inbounds %struct.winsize, %struct.winsize* %8, i32 0, i32 0
%17 = load i32, i32* %16
%18 = load i32*, i32** %6
store i32 %17, i32* %18
%19 = getelementptr inbounds %struct.winsize, %struct.winsize* %8, i32 0, i32 1
%20 = load i32, i32* %19
%21 = load i32*, i32** %7
store i32 %20, i32* %21
store i32 1, i32* %4
br label %22
22: ; preds = %15, %13
%23 = load i32, i32* %4
ret i32 %23
}

LLVM IR (-O2):
%struct.winsize = type { i32, i32 }
@TIOCGWINSZ = global i32 0
@.str = [4 x i8] c"%s\0A\00"
@.str.1 = [13 x i8] c"Failed ioctl\00"
define i32 @get_tty_size(i32 %0, i32* nocapture writeonly %1, i32* nocapture writeonly %2) {
%4 = alloca %struct.winsize
%5 = bitcast %struct.winsize* %4 to i8*
%6 = load i32, i32* @TIOCGWINSZ
%7 = call i32 @ioctl(i32 %0, i32 %6, %struct.winsize* %4)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %11
9: ; preds = %3
%10 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %16
11: ; preds = %3
%12 = getelementptr inbounds %struct.winsize, %struct.winsize* %4, i64 0, i32 0
%13 = load i32, i32* %12
store i32 %13, i32* %1
%14 = getelementptr inbounds %struct.winsize, %struct.winsize* %4, i64 0, i32 1
%15 = load i32, i32* %14
store i32 %15, i32* %2
br label %16
16: ; preds = %11, %9
%17 = phi i32 [ 0, %9 ], [ 1, %11 ]
ret i32 %17
}