#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*/ move_cursor_right;

/* Variables and functions */
int /*<<< orphan*/ assert(int);
int fileno(int /*<<< orphan*/);
int /*<<< orphan*/ flushWrite();
int getCursorPosition();
int snprintf(char *, int, char const *, int);
int /*<<< orphan*/ stdout;
int write(int const, char const *, size_t const);
int getColumns(void) {
  int start = 0;
  int cols = 0;
  int written = 0;
  char seq[32] = {0};
  const int fd = fileno(stdout);
  const char move_cursor_right[] = "\x1b[999C";
  const size_t cmd_len = sizeof(move_cursor_right);
  const char set_cursor_pos[] = "\x1b[%dD";
  start = getCursorPosition();
  if (start == -1) {
    goto failed;
  }
  if (write(fd, move_cursor_right, cmd_len) != cmd_len) {
    goto failed;
  }
  flushWrite();
  cols = getCursorPosition();
  if (cols == -1) {
    goto failed;
  }
  if (cols > start) {
    written = snprintf(seq, 32, set_cursor_pos, cols - start);
    assert(written < 32);
    if (write(fd, seq, written) == -1) {
    }
    flushWrite();
  }
  return cols;
failed:
  return 80;
}


x86 assembly (clang, -O0):
<getColumns>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1050 <memset@plt>
mov    0x2e3c(%rip),%rax
mov    (%rax),%edi
call   1080 <fileno@plt>
mov    %eax,-0x34(%rbp)
mov    0xe4c(%rip),%eax
mov    %eax,-0x3b(%rbp)
mov    0xe46(%rip),%ax
mov    %ax,-0x37(%rbp)
mov    0xe3e(%rip),%al
mov    %al,-0x35(%rbp)
movq   $0x7,-0x48(%rbp)
mov    0xe2e(%rip),%eax
mov    %eax,-0x4e(%rbp)
mov    0xe28(%rip),%ax
mov    %ax,-0x4a(%rbp)
mov    $0x0,%al
call   1060 <getCursorPosition@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    1200 <getColumns+0x90>
jmp    12aa <getColumns+0x13a>
mov    -0x34(%rbp),%edi
lea    -0x3b(%rbp),%rsi
mov    $0x7,%edx
call   1030 <write@plt>
cltq
cmp    $0x7,%rax
je     1222 <getColumns+0xb2>
jmp    12aa <getColumns+0x13a>
mov    $0x0,%al
call   1090 <flushWrite@plt>
mov    $0x0,%al
call   1060 <getCursorPosition@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    1242 <getColumns+0xd2>
jmp    12aa <getColumns+0x13a>
mov    -0xc(%rbp),%eax
cmp    -0x8(%rbp),%eax
jle    129f <getColumns+0x12f>
lea    -0x30(%rbp),%rdi
lea    -0x4e(%rbp),%rdx
mov    -0xc(%rbp),%ecx
sub    -0x8(%rbp),%ecx
mov    $0x20,%esi
call   1040 <snprintf@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x20,-0x10(%rbp)
setl   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0x34(%rbp),%edi
lea    -0x30(%rbp),%rsi
movslq -0x10(%rbp),%rdx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    1298 <getColumns+0x128>
jmp    1298 <getColumns+0x128>
mov    $0x0,%al
call   1090 <flushWrite@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12b1 <getColumns+0x141>
movl   $0x50,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getColumns>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x38,%rsp
xorps  %xmm0,%xmm0
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
mov    0x2e62(%rip),%rax
mov    (%rax),%edi
call   1070 <fileno@plt>
mov    %eax,%r15d
movl   $0x433939,0xb(%rsp)
movl   $0x39395b1b,0x8(%rsp)
39
movw   $0x44,0x4(%rsp)
movl   $0x64255b1b,(%rsp)
xor    %eax,%eax
call   1050 <getCursorPosition@plt>
mov    $0x50,%ebp
cmp    $0xffffffff,%eax
je     1229 <getColumns+0xc9>
mov    %eax,%ebx
lea    0x8(%rsp),%rsi
mov    $0x7,%edx
mov    %r15d,%edi
call   1030 <write@plt>
cmp    $0x7,%eax
jne    1229 <getColumns+0xc9>
xor    %eax,%eax
call   1080 <flushWrite@plt>
xor    %eax,%eax
call   1050 <getCursorPosition@plt>
cmp    $0xffffffff,%eax
je     1229 <getColumns+0xc9>
mov    %eax,%r14d
mov    %eax,%ecx
sub    %ebx,%ecx
jle    1226 <getColumns+0xc6>
lea    0x10(%rsp),%rbx
mov    %rsp,%rdx
mov    %rbx,%rdi
mov    $0x20,%esi
call   1040 <snprintf@plt>
mov    %eax,%ebp
xor    %edi,%edi
cmp    $0x20,%eax
setl   %dil
call   1060 <assert@plt>
movslq %ebp,%rdx
mov    %r15d,%edi
mov    %rbx,%rsi
call   1030 <write@plt>
xor    %eax,%eax
call   1080 <flushWrite@plt>
mov    %r14d,%ebp
mov    %ebp,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stdout = global i32 0
@__const.getColumns.move_cursor_right = [7 x i8] c"\1B[999C\00"
@__const.getColumns.set_cursor_pos = [6 x i8] c"\1B[%dD\00"
define i32 @getColumns() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca [32 x i8]
%6 = alloca i32
%7 = alloca [7 x i8]
%8 = alloca i64
%9 = alloca [6 x i8]
store i32 0, i32* %2
store i32 0, i32* %3
store i32 0, i32* %4
%10 = bitcast [32 x i8]* %5 to i8*
%11 = load i32, i32* @stdout
%12 = call i32 @fileno(i32 %11)
store i32 %12, i32* %6
%13 = bitcast [7 x i8]* %7 to i8*
store i64 7, i64* %8
%14 = bitcast [6 x i8]* %9 to i8*
%15 = call i32 (...) @getCursorPosition()
store i32 %15, i32* %2
%16 = load i32, i32* %2
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %19
18: ; preds = %0
br label %58
19: ; preds = %0
%20 = load i32, i32* %6
%21 = getelementptr inbounds [7 x i8], [7 x i8]* %7, i64 0, i64 0
%22 = call i32 @write(i32 %20, i8* %21, i64 7)
%23 = sext i32 %22 to i64
%24 = icmp ne i64 %23, 7
br i1 %24, label %25, label %26
25: ; preds = %19
br label %58
26: ; preds = %19
%27 = call i32 (...) @flushWrite()
%28 = call i32 (...) @getCursorPosition()
store i32 %28, i32* %3
%29 = load i32, i32* %3
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %32
31: ; preds = %26
br label %58
32: ; preds = %26
%33 = load i32, i32* %3
%34 = load i32, i32* %2
%35 = icmp sgt i32 %33, %34
br i1 %35, label %36, label %56
36: ; preds = %32
%37 = getelementptr inbounds [32 x i8], [32 x i8]* %5, i64 0, i64 0
%38 = getelementptr inbounds [6 x i8], [6 x i8]* %9, i64 0, i64 0
%39 = load i32, i32* %3
%40 = load i32, i32* %2
%41 = sub nsw i32 %39, %40
%42 = call i32 @snprintf(i8* %37, i32 32, i8* %38, i32 %41)
store i32 %42, i32* %4
%43 = load i32, i32* %4
%44 = icmp slt i32 %43, 32
%45 = zext i1 %44 to i32
%46 = call i32 @assert(i32 %45)
%47 = load i32, i32* %6
%48 = getelementptr inbounds [32 x i8], [32 x i8]* %5, i64 0, i64 0
%49 = load i32, i32* %4
%50 = sext i32 %49 to i64
%51 = call i32 @write(i32 %47, i8* %48, i64 %50)
%52 = icmp eq i32 %51, -1
br i1 %52, label %53, label %54
53: ; preds = %36
br label %54
54: ; preds = %53, %36
%55 = call i32 (...) @flushWrite()
br label %56
56: ; preds = %54, %32
%57 = load i32, i32* %3
store i32 %57, i32* %1
br label %59
58: ; preds = %31, %25, %18
store i32 80, i32* %1
br label %59
59: ; preds = %58, %56
%60 = load i32, i32* %1
ret i32 %60
}

LLVM IR (-O2):
@stdout = global i32 0
@__const.getColumns.move_cursor_right = [7 x i8] c"\1B[999C\00"
@__const.getColumns.set_cursor_pos = [6 x i8] c"\1B[%dD\00"
define i32 @getColumns() {
%1 = alloca [32 x i8]
%2 = alloca [7 x i8]
%3 = alloca [6 x i8]
%4 = getelementptr inbounds [32 x i8], [32 x i8]* %1, i64 0, i64 0
%5 = load i32, i32* @stdout
%6 = tail call i32 @fileno(i32 %5)
%7 = getelementptr inbounds [7 x i8], [7 x i8]* %2, i64 0, i64 0
%8 = getelementptr inbounds [6 x i8], [6 x i8]* %3, i64 0, i64 0
%9 = tail call i32 (...) @getCursorPosition()
%10 = icmp eq i32 %9, -1
br i1 %10, label %29, label %11
11: ; preds = %0
%12 = call i32 @write(i32 %6, i8* %7, i64 7)
%13 = icmp eq i32 %12, 7
br i1 %13, label %14, label %29
14: ; preds = %11
%15 = tail call i32 (...) @flushWrite()
%16 = tail call i32 (...) @getCursorPosition()
%17 = icmp eq i32 %16, -1
br i1 %17, label %29, label %18
18: ; preds = %14
%19 = icmp sgt i32 %16, %9
br i1 %19, label %20, label %29
20: ; preds = %18
%21 = sub nsw i32 %16, %9
%22 = call i32 @snprintf(i8* %4, i32 32, i8* %8, i32 %21)
%23 = icmp slt i32 %22, 32
%24 = zext i1 %23 to i32
%25 = call i32 @assert(i32 %24)
%26 = sext i32 %22 to i64
%27 = call i32 @write(i32 %6, i8* %4, i64 %26)
%28 = call i32 (...) @flushWrite()
br label %29
29: ; preds = %0, %11, %14, %18, %20
%30 = phi i32 [ %16, %20 ], [ %16, %18 ], [ 80, %14 ], [ 80, %11 ], [ 80, %0 ]
ret i32 %30
}