#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 */
typedef int /*<<< orphan*/ columns;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int atoi(char *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fileno(int /*<<< orphan*/);
char *getenv(char *);
scalar_t__ isatty(int /*<<< orphan*/);
int /*<<< orphan*/ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char *, char *);
int /*<<< orphan*/ stdout;
scalar_t__ strncmp(char *, char *, int);
int get_screen_width() {
  int width = 80;
  char *p = getenv("NUMASTAT_WIDTH");
  if (p != NULL) {
    width = atoi(p);
    if ((width < 1) || (width > 10000000)) {
      width = 80;
    }
  } else if (isatty(fileno(stdout))) {
    FILE *fs = popen("resize 2>/dev/null", "r");
    if (fs != NULL) {
      char columns[72];
      fgets(columns, sizeof(columns), fs);
      pclose(fs);
      if (strncmp(columns, "COLUMNS=", 8) == 0) {
        width = atoi(&columns[8]);
        if ((width < 1) || (width > 10000000)) {
          width = 80;
        }
      }
    }
  } else {
    width = 10000000;
  }
  if (width < 32) {
    width = 32;
  }
  return width;
}


x86 assembly (clang, -O0):
<get_screen_width>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x50,-0x4(%rbp)
lea    0xe6a(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11d9 <get_screen_width+0x59>
mov    -0x10(%rbp),%rdi
call   10a0 <atoi@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x1,-0x4(%rbp)
jl     11cd <get_screen_width+0x4d>
cmpl   $0x989680,-0x4(%rbp)
jle    11d4 <get_screen_width+0x54>
movl   $0x50,-0x4(%rbp)
jmp    129d <get_screen_width+0x11d>
mov    0x2e00(%rip),%rax
mov    (%rax),%edi
call   1080 <fileno@plt>
mov    %eax,%edi
call   1050 <isatty@plt>
cmp    $0x0,%rax
je     1291 <get_screen_width+0x111>
lea    0xe10(%rip),%rdi
lea    0xe1c(%rip),%rsi
call   1090 <popen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     128c <get_screen_width+0x10c>
lea    -0x60(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x48,%esi
call   1070 <fgets@plt>
mov    -0x18(%rbp),%rdi
call   1060 <pclose@plt>
lea    -0x60(%rbp),%rdi
lea    0xde4(%rip),%rsi
mov    $0x8,%edx
call   1040 <strncmp@plt>
cmp    $0x0,%rax
jne    1287 <get_screen_width+0x107>
lea    -0x60(%rbp),%rdi
add    $0x8,%rdi
call   10a0 <atoi@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x1,-0x4(%rbp)
jl     127b <get_screen_width+0xfb>
cmpl   $0x989680,-0x4(%rbp)
jle    1282 <get_screen_width+0x102>
movl   $0x50,-0x4(%rbp)
jmp    1287 <get_screen_width+0x107>
jmp    128c <get_screen_width+0x10c>
jmp    1298 <get_screen_width+0x118>
movl   $0x989680,-0x4(%rbp)
jmp    129d <get_screen_width+0x11d>
cmpl   $0x20,-0x4(%rbp)
jge    12ae <get_screen_width+0x12e>
movl   $0x20,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_screen_width>:
push   %r14
push   %rbx
sub    $0x48,%rsp
lea    0xe72(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     11be <get_screen_width+0x3e>
mov    %rax,%rdi
call   10a0 <atoi@plt>
mov    %eax,%ecx
add    $0xff67697f,%ecx
mov    $0x50,%edx
cmp    $0xff676980,%ecx
jae    1252 <get_screen_width+0xd2>
jmp    126c <get_screen_width+0xec>
mov    0x2e1b(%rip),%rax
mov    (%rax),%edi
call   1080 <fileno@plt>
mov    %eax,%edi
call   1050 <isatty@plt>
test   %rax,%rax
je     1260 <get_screen_width+0xe0>
lea    0xe2c(%rip),%rdi
lea    0xe38(%rip),%rsi
call   1090 <popen@plt>
test   %rax,%rax
je     1267 <get_screen_width+0xe7>
mov    %rax,%rbx
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0x48,%esi
mov    %rax,%rdx
call   1070 <fgets@plt>
mov    %rbx,%rdi
call   1060 <pclose@plt>
lea    0xe0b(%rip),%rsi
mov    %r14,%rdi
mov    $0x8,%edx
call   1040 <strncmp@plt>
mov    %rax,%rcx
mov    $0x50,%eax
test   %rcx,%rcx
jne    1252 <get_screen_width+0xd2>
lea    0x8(%rsp),%rdi
call   10a0 <atoi@plt>
mov    %eax,%ecx
lea    -0x989681(%rcx),%eax
cmp    $0xff676980,%eax
mov    $0x50,%eax
cmovae %ecx,%eax
mov    $0x20,%ecx
mov    %eax,%edx
cmp    $0x21,%eax
jae    126c <get_screen_width+0xec>
jmp    126e <get_screen_width+0xee>
mov    $0x989680,%edx
jmp    126c <get_screen_width+0xec>
mov    $0x50,%edx
mov    %edx,%ecx
mov    %ecx,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [15 x i8] c"NUMASTAT_WIDTH\00"
@stdout = global i32 0
@.str.1 = [19 x i8] c"resize 2>/dev/null\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [9 x i8] c"COLUMNS=\00"
define i32 @get_screen_width() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca [72 x i8]
store i32 80, i32* %1
%5 = call i8* @getenv(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
store i8* %5, i8** %2
%6 = load i8*, i8** %2
%7 = icmp ne i8* %6, null
br i1 %7, label %8, label %18
8: ; preds = %0
%9 = load i8*, i8** %2
%10 = call i32 @atoi(i8* %9)
store i32 %10, i32* %1
%11 = load i32, i32* %1
%12 = icmp slt i32 %11, 1
br i1 %12, label %16, label %13
13: ; preds = %8
%14 = load i32, i32* %1
%15 = icmp sgt i32 %14, 10000000
br i1 %15, label %16, label %17
16: ; preds = %13, %8
store i32 80, i32* %1
br label %17
17: ; preds = %16, %13
br label %50
18: ; preds = %0
%19 = load i32, i32* @stdout
%20 = call i32 @fileno(i32 %19)
%21 = call i64 @isatty(i32 %20)
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %48
23: ; preds = %18
%24 = call i32* @popen(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %24, i32** %3
%25 = load i32*, i32** %3
%26 = icmp ne i32* %25, null
br i1 %26, label %27, label %47
27: ; preds = %23
%28 = getelementptr inbounds [72 x i8], [72 x i8]* %4, i64 0, i64 0
%29 = load i32*, i32** %3
%30 = call i32 @fgets(i8* %28, i32 72, i32* %29)
%31 = load i32*, i32** %3
%32 = call i32 @pclose(i32* %31)
%33 = getelementptr inbounds [72 x i8], [72 x i8]* %4, i64 0, i64 0
%34 = call i64 @strncmp(i8* %33, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 8)
%35 = icmp eq i64 %34, 0
br i1 %35, label %36, label %46
36: ; preds = %27
%37 = getelementptr inbounds [72 x i8], [72 x i8]* %4, i64 0, i64 8
%38 = call i32 @atoi(i8* %37)
store i32 %38, i32* %1
%39 = load i32, i32* %1
%40 = icmp slt i32 %39, 1
br i1 %40, label %44, label %41
41: ; preds = %36
%42 = load i32, i32* %1
%43 = icmp sgt i32 %42, 10000000
br i1 %43, label %44, label %45
44: ; preds = %41, %36
store i32 80, i32* %1
br label %45
45: ; preds = %44, %41
br label %46
46: ; preds = %45, %27
br label %47
47: ; preds = %46, %23
br label %49
48: ; preds = %18
store i32 10000000, i32* %1
br label %49
49: ; preds = %48, %47
br label %50
50: ; preds = %49, %17
%51 = load i32, i32* %1
%52 = icmp slt i32 %51, 32
br i1 %52, label %53, label %54
53: ; preds = %50
store i32 32, i32* %1
br label %54
54: ; preds = %53, %50
%55 = load i32, i32* %1
ret i32 %55
}

LLVM IR (-O2):
@.str = [15 x i8] c"NUMASTAT_WIDTH\00"
@stdout = global i32 0
@.str.1 = [19 x i8] c"resize 2>/dev/null\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [9 x i8] c"COLUMNS=\00"
define i32 @get_screen_width() {
%1 = alloca [72 x i8]
%2 = tail call i8* @getenv(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i8* %2, null
br i1 %3, label %8, label %4
4: ; preds = %0
%5 = tail call i32 @atoi(i8* %2)
%6 = add i32 %5, -10000001
%7 = icmp ult i32 %6, -10000000
br i1 %7, label %33, label %30
8: ; preds = %0
%9 = load i32, i32* @stdout
%10 = tail call i32 @fileno(i32 %9)
%11 = tail call i64 @isatty(i32 %10)
%12 = icmp eq i64 %11, 0
br i1 %12, label %33, label %13
13: ; preds = %8
%14 = tail call i32* @popen(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%15 = icmp eq i32* %14, null
br i1 %15, label %33, label %16
16: ; preds = %13
%17 = getelementptr inbounds [72 x i8], [72 x i8]* %1, i64 0, i64 0
%18 = call i32 @fgets(i8* %17, i32 72, i32* %14)
%19 = call i32 @pclose(i32* %14)
%20 = call i64 @strncmp(i8* %17, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 8)
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %28
22: ; preds = %16
%23 = getelementptr inbounds [72 x i8], [72 x i8]* %1, i64 0, i64 8
%24 = call i32 @atoi(i8* %23)
%25 = add i32 %24, -10000001
%26 = icmp ult i32 %25, -10000000
%27 = select i1 %26, i32 80, i32 %24
br label %28
28: ; preds = %22, %16
%29 = phi i32 [ %27, %22 ], [ 80, %16 ]
br label %30
30: ; preds = %4, %28
%31 = phi i32 [ %5, %4 ], [ %29, %28 ]
%32 = icmp ugt i32 %31, 32
br i1 %32, label %33, label %35
33: ; preds = %4, %8, %13, %30
%34 = phi i32 [ %31, %30 ], [ 10000000, %8 ], [ 80, %13 ], [ 80, %4 ]
br label %35
35: ; preds = %30, %33
%36 = phi i32 [ %34, %33 ], [ 32, %30 ]
ret i32 %36
}