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

/* Variables and functions */
int fwrite(char *, int, int, int /*<<< orphan*/);
int getCursorPosition();
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int /*<<< orphan*/ stdout;
int strlen(char *);
int getColumns() {
  int start, cols;
  start = getCursorPosition();
  if (start == -1)
    goto failed;
  if (fwrite("\x1b[999C", 1, 6, stdout) != 6)
    goto failed;
  cols = getCursorPosition();
  if (cols == -1)
    goto failed;
  if (cols > start) {
    char seq[32];
    snprintf(seq, 32, "\x1b[%dD", cols - start);
    if (fwrite(seq, 1, strlen(seq), stdout) == -1) {
    }
  }
  return cols;
failed:
  return 80;
}


x86 assembly (clang, -O0):
<getColumns>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    $0x0,%al
call   1050 <getCursorPosition@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    1161 <getColumns+0x21>
jmp    1216 <getColumns+0xd6>
mov    0x2e78(%rip),%rax
mov    (%rax),%ecx
lea    0xe8f(%rip),%rdi
mov    $0x1,%esi
mov    $0x6,%edx
call   1060 <fwrite@plt>
cmp    $0x6,%eax
je     118e <getColumns+0x4e>
jmp    1216 <getColumns+0xd6>
mov    $0x0,%al
call   1050 <getCursorPosition@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    11a7 <getColumns+0x67>
jmp    1216 <getColumns+0xd6>
mov    -0xc(%rbp),%eax
cmp    -0x8(%rbp),%eax
jle    120b <getColumns+0xcb>
lea    -0x30(%rbp),%rdi
mov    -0xc(%rbp),%ecx
sub    -0x8(%rbp),%ecx
mov    $0x20,%esi
lea    0xe3e(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
lea    -0x30(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x38(%rbp),%rdi
mov    %eax,%edx
mov    0x2df4(%rip),%rax
mov    (%rax),%ecx
mov    $0x1,%esi
call   1060 <fwrite@plt>
cmp    $0xffffffff,%eax
jne    1206 <getColumns+0xc6>
jmp    1206 <getColumns+0xc6>
jmp    120b <getColumns+0xcb>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    121d <getColumns+0xdd>
movl   $0x50,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getColumns>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
xor    %eax,%eax
call   1050 <getCursorPosition@plt>
mov    $0x50,%ebx
cmp    $0xffffffff,%eax
je     11cb <getColumns+0x8b>
mov    %eax,%ebp
mov    0x2e7c(%rip),%r15
mov    (%r15),%ecx
lea    0xe92(%rip),%rdi
mov    $0x1,%esi
mov    $0x6,%edx
call   1060 <fwrite@plt>
cmp    $0x6,%eax
jne    11cb <getColumns+0x8b>
xor    %eax,%eax
call   1050 <getCursorPosition@plt>
cmp    $0xffffffff,%eax
je     11cb <getColumns+0x8b>
mov    %eax,%r14d
mov    %eax,%ecx
sub    %ebp,%ecx
jle    11c8 <getColumns+0x88>
lea    0xe69(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x20,%esi
call   1040 <snprintf@plt>
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    (%r15),%ecx
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %eax,%edx
call   1060 <fwrite@plt>
mov    %r14d,%ebx
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [7 x i8] c"\1B[999C\00"
@stdout = global i32 0
@.str.1 = [6 x i8] c"\1B[%dD\00"
define i32 @getColumns() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca [32 x i8]
%5 = call i32 (...) @getCursorPosition()
store i32 %5, i32* %2
%6 = load i32, i32* %2
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %9
8: ; preds = %0
br label %39
9: ; preds = %0
%10 = load i32, i32* @stdout
%11 = call i32 @fwrite(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 1, i32 6, i32 %10)
%12 = icmp ne i32 %11, 6
br i1 %12, label %13, label %14
13: ; preds = %9
br label %39
14: ; preds = %9
%15 = call i32 (...) @getCursorPosition()
store i32 %15, i32* %3
%16 = load i32, i32* %3
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %19
18: ; preds = %14
br label %39
19: ; preds = %14
%20 = load i32, i32* %3
%21 = load i32, i32* %2
%22 = icmp sgt i32 %20, %21
br i1 %22, label %23, label %37
23: ; preds = %19
%24 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 0
%25 = load i32, i32* %3
%26 = load i32, i32* %2
%27 = sub nsw i32 %25, %26
%28 = call i32 @snprintf(i8* %24, i32 32, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 %27)
%29 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 0
%30 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 0
%31 = call i32 @strlen(i8* %30)
%32 = load i32, i32* @stdout
%33 = call i32 @fwrite(i8* %29, i32 1, i32 %31, i32 %32)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %36
35: ; preds = %23
br label %36
36: ; preds = %35, %23
br label %37
37: ; preds = %36, %19
%38 = load i32, i32* %3
store i32 %38, i32* %1
br label %40
39: ; preds = %18, %13, %8
store i32 80, i32* %1
br label %40
40: ; preds = %39, %37
%41 = load i32, i32* %1
ret i32 %41
}

LLVM IR (-O2):
@.str = [7 x i8] c"\1B[999C\00"
@stdout = global i32 0
@.str.1 = [6 x i8] c"\1B[%dD\00"
define i32 @getColumns() {
%1 = alloca [32 x i8]
%2 = tail call i32 (...) @getCursorPosition()
%3 = icmp eq i32 %2, -1
br i1 %3, label %20, label %4
4: ; preds = %0
%5 = load i32, i32* @stdout
%6 = tail call i32 @fwrite(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 1, i32 6, i32 %5)
%7 = icmp eq i32 %6, 6
br i1 %7, label %8, label %20
8: ; preds = %4
%9 = tail call i32 (...) @getCursorPosition()
%10 = icmp eq i32 %9, -1
br i1 %10, label %20, label %11
11: ; preds = %8
%12 = icmp sgt i32 %9, %2
br i1 %12, label %13, label %20
13: ; preds = %11
%14 = getelementptr inbounds [32 x i8], [32 x i8]* %1, i64 0, i64 0
%15 = sub nsw i32 %9, %2
%16 = call i32 @snprintf(i8* %14, i32 32, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 %15)
%17 = call i32 @strlen(i8* %14)
%18 = load i32, i32* @stdout
%19 = call i32 @fwrite(i8* %14, i32 1, i32 %17, i32 %18)
br label %20
20: ; preds = %0, %4, %8, %11, %13
%21 = phi i32 [ %9, %13 ], [ %9, %11 ], [ 80, %8 ], [ 80, %4 ], [ 80, %0 ]
ret i32 %21
}