#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 {
  scalar_t__ ws_col;
  int /*<<< orphan*/ member_3;
  int /*<<< orphan*/ member_2;
  int /*<<< orphan*/ member_1;
  int /*<<< orphan*/ member_0;
};

/* Variables and functions */
int /*<<< orphan*/ TIOCGWINSZ;
int /*<<< orphan*/ fileno(int /*<<< orphan*/);
int /*<<< orphan*/ ioctl(int /*<<< orphan*/, int /*<<< orphan*/,
                         struct winsize *);
int /*<<< orphan*/ stdout;
int get_line_size(void) {
  struct winsize data = {0, 0, 0, 0};
  ioctl(fileno(stdout), TIOCGWINSZ, &data);
  if (data.ws_col > 0) {
    return data.ws_col;
  } else {
    return 80;
  }
}


x86 assembly (clang, -O0):
<get_line_size>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1030 <memset@plt>
mov    0x2e91(%rip),%rax
mov    (%rax),%edi
call   1050 <fileno@plt>
mov    %eax,%edi
mov    0x2e71(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1040 <ioctl@plt>
cmpq   $0x0,-0x20(%rbp)
jle    1181 <get_line_size+0x51>
mov    -0x20(%rbp),%rax
mov    %eax,-0x4(%rbp)
jmp    1188 <get_line_size+0x58>
movl   $0x50,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_line_size>:
sub    $0x18,%rsp
xorps  %xmm0,%xmm0
movaps %xmm0,(%rsp)
movq   $0x0,0x10(%rsp)
mov    0x2ea5(%rip),%rax
mov    (%rax),%edi
call   1040 <fileno@plt>
mov    0x2e87(%rip),%rcx
mov    (%rcx),%esi
mov    %rsp,%rdx
mov    %eax,%edi
call   1030 <ioctl@plt>
mov    (%rsp),%rcx
test   %rcx,%rcx
mov    $0x50,%eax
cmovg  %ecx,%eax
add    $0x18,%rsp
ret

LLVM IR (-O0):
%struct.winsize = type { i64, i32, i32, i32, i32 }
@stdout = global i32 0
@TIOCGWINSZ = global i32 0
define i32 @get_line_size() {
%1 = alloca i32
%2 = alloca %struct.winsize
%3 = bitcast %struct.winsize* %2 to i8*
%4 = load i32, i32* @stdout
%5 = call i32 @fileno(i32 %4)
%6 = load i32, i32* @TIOCGWINSZ
%7 = call i32 @ioctl(i32 %5, i32 %6, %struct.winsize* %2)
%8 = getelementptr inbounds %struct.winsize, %struct.winsize* %2, i32 0, i32 0
%9 = load i64, i64* %8
%10 = icmp sgt i64 %9, 0
br i1 %10, label %11, label %15
11: ; preds = %0
%12 = getelementptr inbounds %struct.winsize, %struct.winsize* %2, i32 0, i32 0
%13 = load i64, i64* %12
%14 = trunc i64 %13 to i32
store i32 %14, i32* %1
br label %16
15: ; preds = %0
store i32 80, i32* %1
br label %16
16: ; preds = %15, %11
%17 = load i32, i32* %1
ret i32 %17
}

LLVM IR (-O2):
%struct.winsize = type { i64, i32, i32, i32, i32 }
@stdout = global i32 0
@TIOCGWINSZ = global i32 0
define i32 @get_line_size() {
%1 = alloca %struct.winsize
%2 = bitcast %struct.winsize* %1 to i8*
%3 = load i32, i32* @stdout
%4 = tail call i32 @fileno(i32 %3)
%5 = load i32, i32* @TIOCGWINSZ
%6 = call i32 @ioctl(i32 %4, i32 %5, %struct.winsize* %1)
%7 = getelementptr inbounds %struct.winsize, %struct.winsize* %1, i64 0, i32 0
%8 = load i64, i64* %7
%9 = icmp sgt i64 %8, 0
%10 = trunc i64 %8 to i32
%11 = select i1 %9, i32 %10, i32 80
ret i32 %11
}