#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*/ STDERR_FILENO;
int /*<<< orphan*/ TIOCGWINSZ;
int ioctl(int /*<<< orphan*/, int /*<<< orphan*/, struct winsize *);
int /*<<< orphan*/ isatty(int);
int guess_winsize() {
  int width;
  struct winsize ws;
  if (!isatty(2)) {
    width = -1;
    return width;
  }
  if (ioctl(STDERR_FILENO, TIOCGWINSZ, &ws) == -1 || ws.ws_col == 0)
    width = 79;
  else
    width = ws.ws_col - 1;
  return width;
}


x86 assembly (clang, -O0):
<guess_winsize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x2,%edi
call   1030 <isatty@plt>
cmp    $0x0,%eax
jne    114d <guess_winsize+0x2d>
movl   $0xffffffff,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1196 <guess_winsize+0x76>
mov    0x2e8c(%rip),%rax
mov    (%rax),%edi
mov    0x2e73(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0xffffffff,%eax
je     117b <guess_winsize+0x5b>
cmpl   $0x0,-0x10(%rbp)
jne    1187 <guess_winsize+0x67>
movl   $0x4f,-0x8(%rbp)
jmp    1190 <guess_winsize+0x70>
mov    -0x10(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<guess_winsize>:
push   %rax
mov    $0x2,%edi
call   1030 <isatty@plt>
test   %eax,%eax
je     115f <guess_winsize+0x3f>
mov    0x2eaa(%rip),%rax
mov    (%rax),%edi
mov    0x2e91(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
call   1040 <ioctl@plt>
mov    (%rsp),%ecx
sub    $0x1,%ecx
mov    $0x4f,%edx
cmovb  %edx,%ecx
cmp    $0xffffffff,%eax
cmove  %edx,%ecx
jmp    1164 <guess_winsize+0x44>
mov    $0xffffffff,%ecx
mov    %ecx,%eax
pop    %rcx
ret

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

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