#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*/ sz;
struct winsize {
  int /*<<< orphan*/ ws_col;
  int /*<<< orphan*/ ws_row;
};
struct termios {
  int dummy;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ TCSAFLUSH;
int /*<<< orphan*/ TIOCSWINSZ;
int /*<<< orphan*/ cfmakeraw(struct termios *);
int /*<<< orphan*/ *cursor_address;
int fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fputs(char const *, int /*<<< orphan*/ *);
int fscanf(int /*<<< orphan*/ *, char *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct winsize *);
int /*<<< orphan*/ memset(struct winsize *, int /*<<< orphan*/, int);
char *restore_cursor;
char *save_cursor;
int /*<<< orphan*/ setbuf(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
scalar_t__ setupterm(int /*<<< orphan*/ *, int, int /*<<< orphan*/ *);
scalar_t__ tcgetattr(int, struct termios *);
scalar_t__ tcsetattr(int, int /*<<< orphan*/, struct termios *);
char *tiparm(int /*<<< orphan*/ *, int, int);
int main(void) {
  int err = 1;
  struct termios old, t;
  FILE *termfile;
  if ((termfile = fopen("/dev/tty", "r+")) == NULL)
    return err;
  int term_fd = fileno(termfile);
  if (setupterm(NULL, term_fd, NULL))
    return err;
  setbuf(termfile, NULL);
  if (cursor_address == NULL || save_cursor == NULL || restore_cursor == NULL)
    return err;
  if (tcgetattr(term_fd, &old))
    return err;
  t = old;
  cfmakeraw(&t);
  if (tcsetattr(term_fd, TCSAFLUSH, &t))
    return err;
  fputs(save_cursor, termfile);
  const char *buf = tiparm(cursor_address, 1000, 1000);
  if (!buf)
    goto term_restore;
  fputs(buf, termfile);
  fputs("\033[6n", termfile);
  struct winsize sz;
  memset(&sz, 0, sizeof(sz));
  if (fscanf(termfile, "\033[%hu;%huR", &sz.ws_row, &sz.ws_col) != 2)
    goto cursor_restore;
  if (ioctl(term_fd, TIOCSWINSZ, &sz) == 0)
    err = 0;
cursor_restore:
  fputs(restore_cursor, termfile);
term_restore:
  tcsetattr(term_fd, TCSAFLUSH, &old);
  return err;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x8(%rbp)
lea    0xe23(%rip),%rdi
lea    0xe25(%rip),%rsi
call   10d0 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1202 <main+0x42>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    13c4 <main+0x204>
mov    -0x20(%rbp),%rdi
call   1090 <fileno@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%esi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rdi
call   1030 <setupterm@plt>
cmp    $0x0,%rax
je     1232 <main+0x72>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    13c4 <main+0x204>
mov    -0x20(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <setbuf@plt>
mov    0x2d82(%rip),%rax
cmpq   $0x0,(%rax)
je     1272 <main+0xb2>
mov    0x2d79(%rip),%rax
cmpq   $0x0,(%rax)
je     1272 <main+0xb2>
mov    0x2d50(%rip),%rax
cmpq   $0x0,(%rax)
jne    127d <main+0xbd>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    13c4 <main+0x204>
mov    -0x24(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   10b0 <tcgetattr@plt>
cmp    $0x0,%rax
je     129e <main+0xde>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    13c4 <main+0x204>
mov    -0x10(%rbp),%eax
mov    %eax,-0x18(%rbp)
lea    -0x18(%rbp),%rdi
call   10a0 <cfmakeraw@plt>
mov    -0x24(%rbp),%edi
mov    0x2d31(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   10c0 <tcsetattr@plt>
cmp    $0x0,%rax
je     12d7 <main+0x117>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    13c4 <main+0x204>
mov    0x2cf2(%rip),%rax
mov    (%rax),%rdi
mov    -0x20(%rbp),%rsi
call   1050 <fputs@plt>
mov    0x2cd7(%rip),%rax
mov    (%rax),%rdi
mov    $0x3e8,%edx
mov    %edx,%esi
call   10e0 <tiparm@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1314 <main+0x154>
jmp    13a9 <main+0x1e9>
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1050 <fputs@plt>
mov    -0x20(%rbp),%rsi
lea    0xce0(%rip),%rdi
call   1050 <fputs@plt>
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
mov    -0x20(%rbp),%rdi
lea    -0x38(%rbp),%rdx
add    $0x4,%rdx
lea    -0x38(%rbp),%rcx
lea    0xcb9(%rip),%rsi
call   1070 <fscanf@plt>
cmp    $0x2,%eax
je     136b <main+0x1ab>
jmp    1396 <main+0x1d6>
mov    -0x24(%rbp),%edi
mov    0x2c6b(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdx
call   1080 <ioctl@plt>
cmp    $0x0,%rax
jne    1391 <main+0x1d1>
movl   $0x0,-0x8(%rbp)
jmp    1396 <main+0x1d6>
mov    0x2c1b(%rip),%rax
mov    (%rax),%rdi
mov    -0x20(%rbp),%rsi
call   1050 <fputs@plt>
mov    -0x24(%rbp),%edi
mov    0x2c35(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdx
call   10c0 <tcsetattr@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
lea    0xe1b(%rip),%rdi
lea    0xe1d(%rip),%rsi
call   10d0 <fopen@plt>
mov    $0x1,%ebp
test   %rax,%rax
je     1219 <main+0x49>
mov    %rax,%rbx
mov    %rax,%rdi
call   1090 <fileno@plt>
mov    %eax,%r14d
xor    %edi,%edi
mov    %eax,%esi
xor    %edx,%edx
call   1030 <setupterm@plt>
test   %rax,%rax
je     122a <main+0x5a>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rbx,%rdi
xor    %esi,%esi
call   1040 <setbuf@plt>
mov    0x2d8d(%rip),%r13
cmpq   $0x0,0x0(%r13)
je     1219 <main+0x49>
mov    0x2d87(%rip),%r12
cmpq   $0x0,(%r12)
je     1219 <main+0x49>
mov    0x2d61(%rip),%rax
cmpq   $0x0,(%rax)
je     1219 <main+0x49>
mov    %rsp,%rsi
mov    %r14d,%edi
call   10b0 <tcgetattr@plt>
test   %rax,%rax
jne    1219 <main+0x49>
mov    (%rsp),%eax
mov    %eax,0x8(%rsp)
lea    0x8(%rsp),%r15
mov    %r15,%rdi
call   10a0 <cfmakeraw@plt>
mov    0x2d60(%rip),%rax
mov    (%rax),%esi
mov    %r14d,%edi
mov    %r15,%rdx
call   10c0 <tcsetattr@plt>
test   %rax,%rax
jne    1219 <main+0x49>
mov    (%r12),%rdi
mov    %rbx,%rsi
call   1050 <fputs@plt>
mov    0x0(%r13),%rdi
mov    $0x3e8,%esi
mov    $0x3e8,%edx
call   10e0 <tiparm@plt>
test   %rax,%rax
mov    0x2cf1(%rip),%r15
je     1352 <main+0x182>
mov    %rax,%rdi
mov    %rbx,%rsi
call   1050 <fputs@plt>
lea    0xd2d(%rip),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   10f0 <fwrite@plt>
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
lea    0x14(%rsp),%rdx
lea    0xd00(%rip),%rsi
mov    %rbx,%rdi
mov    %rbp,%rcx
call   1070 <fscanf@plt>
mov    $0x1,%ebp
cmp    $0x2,%eax
jne    1345 <main+0x175>
mov    0x2cb3(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%rdx
mov    %r14d,%edi
call   1080 <ioctl@plt>
xor    %ebp,%ebp
test   %rax,%rax
setne  %bpl
mov    (%r15),%rdi
mov    %rbx,%rsi
call   1050 <fputs@plt>
jmp    1357 <main+0x187>
mov    $0x1,%ebp
mov    0x2c8a(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %r14d,%edi
call   10c0 <tcsetattr@plt>
jmp    1219 <main+0x49>

LLVM IR (-O0):
%struct.termios = type { i32 }
%struct.winsize = type { i32, i32 }
@.str = [9 x i8] c"/dev/tty\00"
@.str.1 = [3 x i8] c"r+\00"
@cursor_address = global i32* null
@save_cursor = global i8* null
@restore_cursor = global i8* null
@TCSAFLUSH = global i32 0
@.str.2 = [5 x i8] c"\1B[6n\00"
@.str.3 = [11 x i8] c"\1B[%hu;%huR\00"
@TIOCSWINSZ = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.termios
%4 = alloca %struct.termios
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca %struct.winsize
store i32 0, i32* %1
store i32 1, i32* %2
%9 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %9, i32** %5
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = load i32, i32* %2
store i32 %12, i32* %1
br label %88
13: ; preds = %0
%14 = load i32*, i32** %5
%15 = call i32 @fileno(i32* %14)
store i32 %15, i32* %6
%16 = load i32, i32* %6
%17 = call i64 @setupterm(i32* null, i32 %16, i32* null)
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %13
%20 = load i32, i32* %2
store i32 %20, i32* %1
br label %88
21: ; preds = %13
%22 = load i32*, i32** %5
%23 = call i32 @setbuf(i32* %22, i32* null)
%24 = load i32*, i32** @cursor_address
%25 = icmp eq i32* %24, null
br i1 %25, label %32, label %26
26: ; preds = %21
%27 = load i8*, i8** @save_cursor
%28 = icmp eq i8* %27, null
br i1 %28, label %32, label %29
29: ; preds = %26
%30 = load i8*, i8** @restore_cursor
%31 = icmp eq i8* %30, null
br i1 %31, label %32, label %34
32: ; preds = %29, %26, %21
%33 = load i32, i32* %2
store i32 %33, i32* %1
br label %88
34: ; preds = %29
%35 = load i32, i32* %6
%36 = call i64 @tcgetattr(i32 %35, %struct.termios* %3)
%37 = icmp ne i64 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %34
%39 = load i32, i32* %2
store i32 %39, i32* %1
br label %88
40: ; preds = %34
%41 = bitcast %struct.termios* %4 to i8*
%42 = bitcast %struct.termios* %3 to i8*
%43 = call i32 @cfmakeraw(%struct.termios* %4)
%44 = load i32, i32* %6
%45 = load i32, i32* @TCSAFLUSH
%46 = call i64 @tcsetattr(i32 %44, i32 %45, %struct.termios* %4)
%47 = icmp ne i64 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %40
%49 = load i32, i32* %2
store i32 %49, i32* %1
br label %88
50: ; preds = %40
%51 = load i8*, i8** @save_cursor
%52 = load i32*, i32** %5
%53 = call i32 @fputs(i8* %51, i32* %52)
%54 = load i32*, i32** @cursor_address
%55 = call i8* @tiparm(i32* %54, i32 1000, i32 1000)
store i8* %55, i8** %7
%56 = load i8*, i8** %7
%57 = icmp ne i8* %56, null
br i1 %57, label %59, label %58
58: ; preds = %50
br label %83
59: ; preds = %50
%60 = load i8*, i8** %7
%61 = load i32*, i32** %5
%62 = call i32 @fputs(i8* %60, i32* %61)
%63 = load i32*, i32** %5
%64 = call i32 @fputs(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32* %63)
%65 = call i32 @memset(%struct.winsize* %8, i32 0, i32 8)
%66 = load i32*, i32** %5
%67 = getelementptr inbounds %struct.winsize, %struct.winsize* %8, i32 0, i32 1
%68 = getelementptr inbounds %struct.winsize, %struct.winsize* %8, i32 0, i32 0
%69 = call i32 @fscanf(i32* %66, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i32* %67, i32* %68)
%70 = icmp ne i32 %69, 2
br i1 %70, label %71, label %72
71: ; preds = %59
br label %79
72: ; preds = %59
%73 = load i32, i32* %6
%74 = load i32, i32* @TIOCSWINSZ
%75 = call i64 @ioctl(i32 %73, i32 %74, %struct.winsize* %8)
%76 = icmp eq i64 %75, 0
br i1 %76, label %77, label %78
77: ; preds = %72
store i32 0, i32* %2
br label %78
78: ; preds = %77, %72
br label %79
79: ; preds = %78, %71
%80 = load i8*, i8** @restore_cursor
%81 = load i32*, i32** %5
%82 = call i32 @fputs(i8* %80, i32* %81)
br label %83
83: ; preds = %79, %58
%84 = load i32, i32* %6
%85 = load i32, i32* @TCSAFLUSH
%86 = call i64 @tcsetattr(i32 %84, i32 %85, %struct.termios* %3)
%87 = load i32, i32* %2
store i32 %87, i32* %1
br label %88
88: ; preds = %83, %48, %38, %32, %19, %11
%89 = load i32, i32* %1
ret i32 %89
}

LLVM IR (-O2):
%struct.termios = type { i32 }
%struct.winsize = type { i32, i32 }
@.str = [9 x i8] c"/dev/tty\00"
@.str.1 = [3 x i8] c"r+\00"
@cursor_address = global i32* null
@save_cursor = global i8* null
@restore_cursor = global i8* null
@TCSAFLUSH = global i32 0
@.str.2 = [5 x i8] c"\1B[6n\00"
@.str.3 = [11 x i8] c"\1B[%hu;%huR\00"
@TIOCSWINSZ = global i32 0
define i32 @main() {
%1 = alloca %struct.termios
%2 = alloca %struct.termios
%3 = alloca %struct.winsize
%4 = bitcast %struct.termios* %1 to i8*
%5 = bitcast %struct.termios* %2 to i8*
%6 = tail call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %60, label %8
8: ; preds = %0
%9 = tail call i32 @fileno(i32* %6)
%10 = tail call i64 @setupterm(i32* null, i32 %9, i32* null)
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %60
12: ; preds = %8
%13 = tail call i32 @setbuf(i32* %6, i32* null)
%14 = load i32*, i32** @cursor_address
%15 = icmp eq i32* %14, null
%16 = load i8*, i8** @save_cursor
%17 = icmp eq i8* %16, null
%18 = select i1 %15, i1 true, i1 %17
%19 = load i8*, i8** @restore_cursor
%20 = icmp eq i8* %19, null
%21 = select i1 %18, i1 true, i1 %20
br i1 %21, label %60, label %22
22: ; preds = %12
%23 = call i64 @tcgetattr(i32 %9, %struct.termios* %1)
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %60
25: ; preds = %22
%26 = getelementptr inbounds %struct.termios, %struct.termios* %1, i64 0, i32 0
%27 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 0
%28 = load i32, i32* %26
store i32 %28, i32* %27
%29 = call i32 @cfmakeraw(%struct.termios* %2)
%30 = load i32, i32* @TCSAFLUSH
%31 = call i64 @tcsetattr(i32 %9, i32 %30, %struct.termios* %2)
%32 = icmp eq i64 %31, 0
br i1 %32, label %33, label %60
33: ; preds = %25
%34 = load i8*, i8** @save_cursor
%35 = call i32 @fputs(i8* %34, i32* %6)
%36 = load i32*, i32** @cursor_address
%37 = call i8* @tiparm(i32* %36, i32 1000, i32 1000)
%38 = icmp eq i8* %37, null
br i1 %38, label %56, label %39
39: ; preds = %33
%40 = call i32 @fputs(i8* %37, i32* %6)
%41 = call i64 @fwrite(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i64 4, i64 1, i32* %6)
%42 = call i32 @memset(%struct.winsize* %3, i32 0, i32 8)
%43 = getelementptr inbounds %struct.winsize, %struct.winsize* %3, i64 0, i32 1
%44 = getelementptr inbounds %struct.winsize, %struct.winsize* %3, i64 0, i32 0
%45 = call i32 @fscanf(i32* %6, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i32* %43, i32* %44)
%46 = icmp eq i32 %45, 2
br i1 %46, label %47, label %52
47: ; preds = %39
%48 = load i32, i32* @TIOCSWINSZ
%49 = call i64 @ioctl(i32 %9, i32 %48, %struct.winsize* %3)
%50 = icmp ne i64 %49, 0
%51 = zext i1 %50 to i32
br label %52
52: ; preds = %47, %39
%53 = phi i32 [ 1, %39 ], [ %51, %47 ]
%54 = load i8*, i8** @restore_cursor
%55 = call i32 @fputs(i8* %54, i32* %6)
br label %56
56: ; preds = %33, %52
%57 = phi i32 [ %53, %52 ], [ 1, %33 ]
%58 = load i32, i32* @TCSAFLUSH
%59 = call i64 @tcsetattr(i32 %9, i32 %58, %struct.termios* %1)
br label %60
60: ; preds = %56, %8, %12, %22, %25, %0
%61 = phi i32 [ 1, %0 ], [ %57, %56 ], [ 1, %8 ], [ 1, %12 ], [ 1, %22 ], [ 1, %25 ]
ret i32 %61
}