#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*/ STDOUT_FILENO;
int /*<<< orphan*/ TIOCGWINSZ;
int /*<<< orphan*/ exit(int);
scalar_t__ ioctl(int /*<<< orphan*/, int /*<<< orphan*/, struct winsize *);
scalar_t__ isatty(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, int);
int main(void) {
  struct winsize size;
  if (isatty(STDOUT_FILENO) == 0)
    exit(1);
  if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
    perror("ioctl TIOCGWINSZ error");
    exit(1);
  }
  printf("%d rows, %d columns\n", size.ws_row, size.ws_col);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e8a(%rip),%rax
mov    (%rax),%edi
call   1030 <isatty@plt>
cmp    $0x0,%rax
jne    1181 <main+0x31>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    0x2e68(%rip),%rax
mov    (%rax),%edi
mov    0x2e3f(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
jge    11bc <main+0x6c>
lea    0xe53(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x10(%rbp),%esi
mov    -0xc(%rbp),%edx
lea    0xe4e(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
mov    0x2e94(%rip),%rbx
mov    (%rbx),%edi
call   1030 <isatty@plt>
test   %rax,%rax
je     119e <main+0x4e>
mov    (%rbx),%edi
mov    0x2e5f(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
call   1050 <ioctl@plt>
test   %rax,%rax
js     11a8 <main+0x58>
mov    0x8(%rsp),%esi
mov    0xc(%rsp),%edx
lea    0xe86(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret
mov    $0x1,%edi
call   1070 <exit@plt>
lea    0xe51(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
%struct.winsize = type { i32, i32 }
@STDOUT_FILENO = global i32 0
@TIOCGWINSZ = global i32 0
@.str = [23 x i8] c"ioctl TIOCGWINSZ error\00"
@.str.1 = [21 x i8] c"%d rows, %d columns\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.winsize
store i32 0, i32* %1
%3 = load i32, i32* @STDOUT_FILENO
%4 = call i64 @isatty(i32 %3)
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = call i32 @exit(i32 1)
unreachable
8: ; preds = %0
%9 = load i32, i32* @STDOUT_FILENO
%10 = load i32, i32* @TIOCGWINSZ
%11 = call i64 @ioctl(i32 %9, i32 %10, %struct.winsize* %2)
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %8
%14 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %8
%17 = getelementptr inbounds %struct.winsize, %struct.winsize* %2, i32 0, i32 0
%18 = load i32, i32* %17
%19 = getelementptr inbounds %struct.winsize, %struct.winsize* %2, i32 0, i32 1
%20 = load i32, i32* %19
%21 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %18, i32 %20)
ret i32 0
}

LLVM IR (-O2):
%struct.winsize = type { i32, i32 }
@STDOUT_FILENO = global i32 0
@TIOCGWINSZ = global i32 0
@.str = [23 x i8] c"ioctl TIOCGWINSZ error\00"
@.str.1 = [21 x i8] c"%d rows, %d columns\0A\00"
define i32 @main() {
%1 = alloca %struct.winsize
%2 = bitcast %struct.winsize* %1 to i8*
%3 = load i32, i32* @STDOUT_FILENO
%4 = tail call i64 @isatty(i32 %3)
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %0
%9 = load i32, i32* @STDOUT_FILENO
%10 = load i32, i32* @TIOCGWINSZ
%11 = call i64 @ioctl(i32 %9, i32 %10, %struct.winsize* %1)
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %8
%14 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %8
%17 = getelementptr inbounds %struct.winsize, %struct.winsize* %1, i64 0, i32 0
%18 = load i32, i32* %17
%19 = getelementptr inbounds %struct.winsize, %struct.winsize* %1, i64 0, i32 1
%20 = load i32, i32* %19
%21 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %18, i32 %20)
ret i32 0
}