#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_col;
};

/* Variables and functions */
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ TIOCGWINSZ;
scalar_t__ ioctl(int /*<<< orphan*/, int /*<<< orphan*/, struct winsize *);
int /*<<< orphan*/ isatty(int /*<<< orphan*/);
int getttywidth(void) {
  int width = 0;
  struct winsize win;
  if (!isatty(STDOUT_FILENO))
    return 0;
  if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &win) == 0)
    width = win.ws_col;
  return width == 0 ? 80 : width;
}


x86 assembly (clang, -O0):
<getttywidth>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x8(%rbp)
mov    0x2eba(%rip),%rax
mov    (%rax),%edi
call   1030 <isatty@plt>
cmp    $0x0,%eax
jne    1152 <getttywidth+0x32>
movl   $0x0,-0x4(%rbp)
jmp    11a0 <getttywidth+0x80>
mov    0x2e97(%rip),%rax
mov    (%rax),%edi
mov    0x2e6e(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jne    117d <getttywidth+0x5d>
mov    -0x10(%rbp),%eax
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jne    1194 <getttywidth+0x74>
mov    $0x50,%eax
mov    %eax,-0x14(%rbp)
jmp    119a <getttywidth+0x7a>
mov    -0x8(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getttywidth>:
push   %rbx
sub    $0x10,%rsp
mov    0x2ec4(%rip),%rbx
mov    (%rbx),%edi
call   1030 <isatty@plt>
test   %eax,%eax
je     1163 <getttywidth+0x43>
mov    (%rbx),%edi
mov    0x2e90(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
call   1040 <ioctl@plt>
xor    %ecx,%ecx
test   %rax,%rax
jne    1157 <getttywidth+0x37>
mov    0x8(%rsp),%ecx
test   %ecx,%ecx
mov    $0x50,%eax
cmovne %ecx,%eax
jmp    1165 <getttywidth+0x45>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret

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

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