#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;
};

/* Variables and functions */
int /*<<< orphan*/ TIOCGWINSZ;
int getCursorPosition(int, int);
int ioctl(int, int /*<<< orphan*/, struct winsize *);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int strlen(char *);
int write(int, char *, int);
int getColumns(int ifd, int ofd) {
  struct winsize ws;
  if (ioctl(1, TIOCGWINSZ, &ws) == -1 || ws.ws_col == 0) {
    int start, cols;
    start = getCursorPosition(ifd, ofd);
    if (start == -1)
      goto failed;
    if (write(ofd, "\x1b[999C", 6) != 6)
      goto failed;
    cols = getCursorPosition(ifd, ofd);
    if (cols == -1)
      goto failed;
    if (cols > start) {
      char seq[32];
      snprintf(seq, 32, "\x1b[%dD", cols - start);
      if (write(ofd, seq, strlen(seq)) == -1) {
      }
    }
    return cols;
  } else {
    return ws.ws_col;
  }
failed:
  return 80;
}


x86 assembly (clang, -O0):
<getColumns>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    0x2e73(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edi
lea    -0x18(%rbp),%rdx
call   1060 <ioctl@plt>
cmp    $0xffffffff,%eax
je     1189 <getColumns+0x39>
cmpq   $0x0,-0x18(%rbp)
jne    124f <getColumns+0xff>
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1070 <getCursorPosition@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11a6 <getColumns+0x56>
jmp    125b <getColumns+0x10b>
mov    -0xc(%rbp),%edi
lea    0xe50(%rip),%rsi
mov    $0x6,%edx
call   1030 <write@plt>
cmp    $0x6,%eax
je     11c8 <getColumns+0x78>
jmp    125b <getColumns+0x10b>
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1070 <getCursorPosition@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    11e5 <getColumns+0x95>
jmp    125b <getColumns+0x10b>
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jle    1244 <getColumns+0xf4>
lea    -0x40(%rbp),%rdi
mov    -0x20(%rbp),%ecx
sub    -0x1c(%rbp),%ecx
mov    $0x20,%esi
lea    0xe00(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4c(%rbp)
lea    -0x40(%rbp),%rax
mov    %rax,-0x48(%rbp)
lea    -0x40(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x4c(%rbp),%edi
mov    -0x48(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    123f <getColumns+0xef>
jmp    123f <getColumns+0xef>
jmp    1244 <getColumns+0xf4>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1262 <getColumns+0x112>
mov    -0x18(%rbp),%rax
mov    %eax,-0x4(%rbp)
jmp    1262 <getColumns+0x112>
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
mov    %esi,%r14d
mov    %edi,%ebp
mov    0x2e72(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    $0x1,%edi
call   1060 <ioctl@plt>
cmp    $0xffffffff,%eax
je     1186 <getColumns+0x36>
mov    0x8(%rsp),%rbx
test   %rbx,%rbx
jne    11fe <getColumns+0xae>
mov    %ebp,%edi
mov    %r14d,%esi
call   1070 <getCursorPosition@plt>
mov    $0x50,%ebx
cmp    $0xffffffff,%eax
je     11fe <getColumns+0xae>
mov    %eax,%r15d
lea    0xe5c(%rip),%rsi
mov    %r14d,%edi
mov    $0x6,%edx
call   1030 <write@plt>
cmp    $0x6,%eax
jne    11fe <getColumns+0xae>
mov    %ebp,%edi
mov    %r14d,%esi
call   1070 <getCursorPosition@plt>
cmp    $0xffffffff,%eax
je     11fe <getColumns+0xae>
mov    %eax,%ebp
mov    %eax,%ecx
sub    %r15d,%ecx
jle    11fc <getColumns+0xac>
lea    0xe32(%rip),%rdx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x20,%esi
call   1050 <snprintf@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %ebp,%ebx
mov    %ebx,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.winsize = type { i64 }
@TIOCGWINSZ = global i32 0
@.str = [7 x i8] c"\1B[999C\00"
@.str.1 = [6 x i8] c"\1B[%dD\00"
define i32 @getColumns(i32 %0, i32 %1) {
%3 = alloca %struct.winsize
%4 = alloca [32 x i8]
%5 = bitcast %struct.winsize* %3 to i8*
%6 = load i32, i32* @TIOCGWINSZ
%7 = call i32 @ioctl(i32 1, i32 %6, %struct.winsize* %3)
%8 = icmp eq i32 %7, -1
%9 = getelementptr inbounds %struct.winsize, %struct.winsize* %3, i64 0, i32 0
%10 = load i64, i64* %9
%11 = icmp eq i64 %10, 0
%12 = select i1 %8, i1 true, i1 %11
br i1 %12, label %13, label %30
13: ; preds = %2
%14 = call i32 @getCursorPosition(i32 %0, i32 %1)
%15 = icmp eq i32 %14, -1
br i1 %15, label %32, label %16
16: ; preds = %13
%17 = call i32 @write(i32 %1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 6)
%18 = icmp eq i32 %17, 6
br i1 %18, label %19, label %32
19: ; preds = %16
%20 = call i32 @getCursorPosition(i32 %0, i32 %1)
%21 = icmp eq i32 %20, -1
br i1 %21, label %32, label %22
22: ; preds = %19
%23 = icmp sgt i32 %20, %14
br i1 %23, label %24, label %32
24: ; preds = %22
%25 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 0
%26 = sub nsw i32 %20, %14
%27 = call i32 @snprintf(i8* %25, i32 32, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 %26)
%28 = call i32 @strlen(i8* %25)
%29 = call i32 @write(i32 %1, i8* %25, i32 %28)
br label %32
30: ; preds = %2
%31 = trunc i64 %10 to i32
br label %32
32: ; preds = %22, %24, %13, %16, %19, %30
%33 = phi i32 [ %31, %30 ], [ %20, %24 ], [ %20, %22 ], [ 80, %13 ], [ 80, %16 ], [ 80, %19 ]
ret i32 %33
}