#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 {
  scalar_t__ ws_col;
};

/* Variables and functions */
int /*<<< orphan*/ TIOCGWINSZ;
int ioctl(int, int /*<<< orphan*/, struct winsize *);
int /*<<< orphan*/ isatty(int);
int ast_get_termcols(int fd) {
  struct winsize win;
  int cols = 0;
  if (!isatty(fd))
    return -1;
  if (ioctl(fd, TIOCGWINSZ, &win) != -1) {
    if (!cols && win.ws_col > 0)
      cols = (int)win.ws_col;
  } else {
    cols = 80;
  }
  return cols;
}


x86 assembly (clang, -O0):
<ast_get_termcols>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x8(%rbp),%edi
call   1030 <isatty@plt>
cmp    $0x0,%eax
jne    114f <ast_get_termcols+0x2f>
movl   $0xffffffff,-0x4(%rbp)
jmp    119b <ast_get_termcols+0x7b>
mov    -0x8(%rbp),%edi
mov    0x2e7f(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0xffffffff,%eax
je     118e <ast_get_termcols+0x6e>
cmpl   $0x0,-0x14(%rbp)
jne    1189 <ast_get_termcols+0x69>
cmpq   $0x0,-0x10(%rbp)
jle    1189 <ast_get_termcols+0x69>
mov    -0x10(%rbp),%rax
mov    %eax,-0x14(%rbp)
jmp    1195 <ast_get_termcols+0x75>
movl   $0x50,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ast_get_termcols>:
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebx
call   1030 <isatty@plt>
test   %eax,%eax
je     1159 <ast_get_termcols+0x39>
mov    0x2ea1(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1040 <ioctl@plt>
cmp    $0xffffffff,%eax
je     1160 <ast_get_termcols+0x40>
mov    0x8(%rsp),%rax
xor    %ecx,%ecx
test   %rax,%rax
cmovle %ecx,%eax
jmp    1165 <ast_get_termcols+0x45>
mov    $0xffffffff,%eax
jmp    1165 <ast_get_termcols+0x45>
mov    $0x50,%eax
add    $0x10,%rsp
pop    %rbx
ret

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

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