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

/* Variables and functions */
int /*<<< orphan*/ TIOCGWINSZ;
int /*<<< orphan*/ fileno(int /*<<< orphan*/ *);
char *getenv(char *);
scalar_t__ ioctl(int /*<<< orphan*/, int /*<<< orphan*/, struct winsize *);
int strtoimax(char *, char **, int);
int get_term_columns(FILE *stream) {
  struct winsize ws;
  char *env = getenv("COLUMNS");
  char *p;
  int i;
  if (env) {
    i = strtoimax(env, &p, 10);
    if (!*p)
      return i;
  }
  if (ioctl(fileno(stream), TIOCGWINSZ, &ws) == 0)
    return ws.ws_col;
  return 80;
}


x86 assembly (clang, -O0):
<get_term_columns>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
lea    0xead(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1199 <get_term_columns+0x59>
mov    -0x20(%rbp),%rdi
lea    -0x28(%rbp),%rsi
mov    $0xa,%edx
call   1040 <strtoimax@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%rax
cmpb   $0x0,(%rax)
jne    1194 <get_term_columns+0x54>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11d2 <get_term_columns+0x92>
jmp    1199 <get_term_columns+0x59>
mov    -0x10(%rbp),%rdi
call   1060 <fileno@plt>
mov    %eax,%edi
mov    0x2e2d(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
jne    11cb <get_term_columns+0x8b>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11d2 <get_term_columns+0x92>
movl   $0x50,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_term_columns>:
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
lea    0xeb1(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     1175 <get_term_columns+0x35>
lea    0x8(%rsp),%rsi
mov    %rax,%rdi
mov    $0xa,%edx
call   1040 <strtoimax@plt>
mov    0x8(%rsp),%rcx
cmpb   $0x0,(%rcx)
je     11a0 <get_term_columns+0x60>
mov    %rbx,%rdi
call   1060 <fileno@plt>
mov    0x2e54(%rip),%rcx
mov    (%rcx),%esi
mov    %rsp,%rdx
mov    %eax,%edi
call   1050 <ioctl@plt>
mov    %rax,%rcx
mov    $0x50,%eax
test   %rcx,%rcx
jne    11a0 <get_term_columns+0x60>
mov    (%rsp),%eax
add    $0x10,%rsp
pop    %rbx
ret

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

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