#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 main() {
  struct winsize size;
  if (isatty(STDOUT_FILENO) == 0) {
    printf("not a tty\n");
    exit(1);
  }
  if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
    perror("ioctl winsize error.");
    exit(2);
  }
  printf("row:%d, column:%d\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    118f <main+0x3f>
lea    0xe82(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    0x2e5a(%rip),%rax
mov    (%rax),%edi
mov    0x2e31(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
jge    11ca <main+0x7a>
lea    0xe50(%rip),%rdi
call   1060 <perror@plt>
mov    $0x2,%edi
call   1070 <exit@plt>
mov    -0x10(%rbp),%esi
mov    -0xc(%rbp),%edx
lea    0xe49(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
mov    0x2e84(%rip),%rbx
mov    (%rbx),%edi
call   1040 <isatty@plt>
test   %rax,%rax
je     11b0 <main+0x50>
mov    (%rbx),%edi
mov    0x2e4f(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
call   1060 <ioctl@plt>
test   %rax,%rax
js     11c6 <main+0x66>
mov    0x8(%rsp),%esi
mov    0xc(%rsp),%edx
lea    0xe74(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe71(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    0xe33(%rip),%rdi
call   1070 <perror@plt>
mov    $0x2,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.winsize = type { i32, i32 }
@STDOUT_FILENO = global i32 0
@.str = [11 x i8] c"not a tty\0A\00"
@TIOCGWINSZ = global i32 0
@.str.1 = [21 x i8] c"ioctl winsize error.\00"
@.str.2 = [19 x i8] c"row:%d, column:%d\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 %9
6: ; preds = %0
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%8 = call i32 @exit(i32 1)
unreachable
9: ; preds = %0
%10 = load i32, i32* @STDOUT_FILENO
%11 = load i32, i32* @TIOCGWINSZ
%12 = call i64 @ioctl(i32 %10, i32 %11, %struct.winsize* %2)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %9
%15 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%16 = call i32 @exit(i32 2)
unreachable
17: ; preds = %9
%18 = getelementptr inbounds %struct.winsize, %struct.winsize* %2, i32 0, i32 0
%19 = load i32, i32* %18
%20 = getelementptr inbounds %struct.winsize, %struct.winsize* %2, i32 0, i32 1
%21 = load i32, i32* %20
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i32 %19, i32 %21)
ret i32 0
}

LLVM IR (-O2):
%struct.winsize = type { i32, i32 }
@STDOUT_FILENO = global i32 0
@TIOCGWINSZ = global i32 0
@.str.1 = [21 x i8] c"ioctl winsize error.\00"
@.str.2 = [19 x i8] c"row:%d, column:%d\0A\00"
@str = [10 x i8] c"not a tty\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 %9
6: ; preds = %0
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str, i64 0, i64 0))
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %0
%10 = load i32, i32* @STDOUT_FILENO
%11 = load i32, i32* @TIOCGWINSZ
%12 = call i64 @ioctl(i32 %10, i32 %11, %struct.winsize* %1)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %9
%15 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%16 = call i32 @exit(i32 2)
unreachable
17: ; preds = %9
%18 = getelementptr inbounds %struct.winsize, %struct.winsize* %1, i64 0, i32 0
%19 = load i32, i32* %18
%20 = getelementptr inbounds %struct.winsize, %struct.winsize* %1, i64 0, i32 1
%21 = load i32, i32* %20
%22 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i32 %19, i32 %21)
ret i32 0
}