#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;
int atoi(char *);
char *getenv(char *);
int ioctl(int /*<<< orphan*/, int /*<<< orphan*/, struct winsize *);
int gettermwidth(void) {
  int width = 0;
  char *columns = getenv("COLUMNS");
  if (columns)
    width = atoi(columns);
  if (!width) {
    struct winsize w;
    w.ws_col = 0;
    if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) != -1)
      width = w.ws_col;
  }
  return width ? width : 80;
}


x86 assembly (clang, -O0):
<gettermwidth>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xeba(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1166 <gettermwidth+0x36>
mov    -0x10(%rbp),%rdi
call   1050 <atoi@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jne    11a6 <gettermwidth+0x76>
movl   $0x0,-0x18(%rbp)
mov    0x2e72(%rip),%rax
mov    (%rax),%edi
mov    0x2e49(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0xffffffff,%eax
je     11a1 <gettermwidth+0x71>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11a6 <gettermwidth+0x76>
cmpl   $0x0,-0x4(%rbp)
je     11bb <gettermwidth+0x8b>
mov    -0x4(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    11c8 <gettermwidth+0x98>
mov    $0x50,%eax
mov    %eax,-0x1c(%rbp)
jmp    11c8 <gettermwidth+0x98>
mov    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gettermwidth>:
push   %rax
lea    0xec8(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     114e <gettermwidth+0x1e>
mov    %rax,%rdi
call   1050 <atoi@plt>
test   %eax,%eax
jne    117b <gettermwidth+0x4b>
movl   $0x0,(%rsp)
mov    0x2e94(%rip),%rax
mov    (%rax),%edi
mov    0x2e6b(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
call   1040 <ioctl@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0xffffffff,%ecx
je     117b <gettermwidth+0x4b>
mov    (%rsp),%eax
test   %eax,%eax
mov    $0x50,%ecx
cmovne %eax,%ecx
mov    %ecx,%eax
pop    %rcx
ret

LLVM IR (-O0):
%struct.winsize = type { i32 }
@.str = [8 x i8] c"COLUMNS\00"
@STDOUT_FILENO = global i32 0
@TIOCGWINSZ = global i32 0
define i32 @gettermwidth() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca %struct.winsize
store i32 0, i32* %1
%4 = call i8* @getenv(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
store i8* %4, i8** %2
%5 = load i8*, i8** %2
%6 = icmp ne i8* %5, null
br i1 %6, label %7, label %10
7: ; preds = %0
%8 = load i8*, i8** %2
%9 = call i32 @atoi(i8* %8)
store i32 %9, i32* %1
br label %10
10: ; preds = %7, %0
%11 = load i32, i32* %1
%12 = icmp ne i32 %11, 0
br i1 %12, label %23, label %13
13: ; preds = %10
%14 = getelementptr inbounds %struct.winsize, %struct.winsize* %3, i32 0, i32 0
store i32 0, i32* %14
%15 = load i32, i32* @STDOUT_FILENO
%16 = load i32, i32* @TIOCGWINSZ
%17 = call i32 @ioctl(i32 %15, i32 %16, %struct.winsize* %3)
%18 = icmp ne i32 %17, -1
br i1 %18, label %19, label %22
19: ; preds = %13
%20 = getelementptr inbounds %struct.winsize, %struct.winsize* %3, i32 0, i32 0
%21 = load i32, i32* %20
store i32 %21, i32* %1
br label %22
22: ; preds = %19, %13
br label %23
23: ; preds = %22, %10
%24 = load i32, i32* %1
%25 = icmp ne i32 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %23
%27 = load i32, i32* %1
br label %29
28: ; preds = %23
br label %29
29: ; preds = %28, %26
%30 = phi i32 [ %27, %26 ], [ 80, %28 ]
ret i32 %30
}

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