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

/* Variables and functions */
int /*<<< orphan*/ EIO;
int /*<<< orphan*/ ENOTTY;
int /*<<< orphan*/ EX_SOFTWARE;
int /*<<< orphan*/ TCIOFLUSH;
int /*<<< orphan*/ TCSANOW;
int /*<<< orphan*/ cfmakeraw(struct termios *);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ *fdopen(int, char *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int fscanf(int /*<<< orphan*/ *, char *, unsigned int *, unsigned int *);
scalar_t__ isatty(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ tcflush(int, int /*<<< orphan*/);
int /*<<< orphan*/ tcgetattr(int, struct termios *);
int /*<<< orphan*/ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int is_in_UTF8_mode(int fd) {
  unsigned int y, x;
  struct termios termios_orig, termios;
  FILE *f;
  if (isatty(fd)) {
    f = fdopen(fd, "a+");
    if (!f) {
      perror("is_in_UTF8_mode");
      exit(EX_SOFTWARE);
    }
    tcgetattr(fd, &termios_orig);
    termios = termios_orig;
    tcflush(fd, TCIOFLUSH);
    cfmakeraw(&termios);
    tcsetattr(fd, TCSANOW, &termios);
    fprintf(f, "\030\032"
               "\r\357\200\240"
               "\033[6n\033D");
    if (2 != fscanf(f, "\033[%u;%u", &y, &x)) {
      errno = EIO;
      return -1;
    }
    tcsetattr(fd, TCSANOW, &termios_orig);
    fprintf(f,
            "\033[1F"
            "\033[%uX",
            (x - 1));
    fflush(f);
    switch (x) {
    case 2:
      x = 1;
      break;
    case 4:
      x = 0;
      break;
    default:
      x = -1;
    }
  } else {
    errno = ENOTTY;
    x = -1;
  }
  return (x);
}


x86 assembly (clang, -O0):
<is_in_UTF8_mode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
call   1030 <isatty@plt>
cmp    $0x0,%rax
je     131c <is_in_UTF8_mode+0x16c>
mov    -0x8(%rbp),%edi
lea    0xe29(%rip),%rsi
call   1080 <fdopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1205 <is_in_UTF8_mode+0x55>
lea    0xe11(%rip),%rdi
call   10c0 <perror@plt>
mov    0x2dba(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   10a0 <tcgetattr@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2d9f(%rip),%rax
mov    (%rax),%esi
call   1040 <tcflush@plt>
lea    -0x20(%rbp),%rdi
call   1090 <cfmakeraw@plt>
mov    -0x8(%rbp),%edi
mov    0x2da5(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   10b0 <tcsetattr@plt>
mov    -0x28(%rbp),%rdi
lea    0xdc2(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x28(%rbp),%rdi
lea    0xdbd(%rip),%rsi
lea    -0xc(%rbp),%rdx
lea    -0x10(%rbp),%rcx
call   1050 <fscanf@plt>
mov    %eax,%ecx
mov    $0x2,%eax
cmp    %ecx,%eax
je     129d <is_in_UTF8_mode+0xed>
mov    0x2d4a(%rip),%rax
mov    (%rax),%ecx
mov    0x2d39(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    133b <is_in_UTF8_mode+0x18b>
mov    -0x8(%rbp),%edi
mov    0x2d39(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   10b0 <tcsetattr@plt>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%edx
add    $0xffffffff,%edx
lea    0xd65(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    -0x28(%rbp),%rdi
call   1070 <fflush@plt>
mov    -0x10(%rbp),%eax
mov    %eax,-0x2c(%rbp)
sub    $0x2,%eax
je     12f8 <is_in_UTF8_mode+0x148>
jmp    12e7 <is_in_UTF8_mode+0x137>
mov    -0x2c(%rbp),%eax
sub    $0x4,%eax
je     1304 <is_in_UTF8_mode+0x154>
jmp    1310 <is_in_UTF8_mode+0x160>
movl   $0x1,-0x10(%rbp)
jmp    1317 <is_in_UTF8_mode+0x167>
movl   $0x0,-0x10(%rbp)
jmp    1317 <is_in_UTF8_mode+0x167>
movl   $0xffffffff,-0x10(%rbp)
jmp    1335 <is_in_UTF8_mode+0x185>
mov    0x2cc5(%rip),%rax
mov    (%rax),%ecx
mov    0x2c9c(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x10(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<is_in_UTF8_mode>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebp
call   1030 <isatty@plt>
test   %rax,%rax
je     12ba <is_in_UTF8_mode+0xfa>
lea    0xe1f(%rip),%rsi
mov    %ebp,%edi
call   1080 <fdopen@plt>
test   %rax,%rax
je     12e5 <is_in_UTF8_mode+0x125>
mov    %rax,%rbx
lea    0x8(%rsp),%rsi
mov    %ebp,%edi
call   10a0 <tcgetattr@plt>
mov    0x8(%rsp),%eax
mov    %eax,0x10(%rsp)
mov    0x2db1(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1040 <tcflush@plt>
lea    0x10(%rsp),%r14
mov    %r14,%rdi
call   1090 <cfmakeraw@plt>
mov    0x2db4(%rip),%r15
mov    (%r15),%esi
mov    %ebp,%edi
mov    %r14,%rdx
call   10b0 <tcsetattr@plt>
lea    0xdd3(%rip),%rdi
mov    $0xc,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   10e0 <fwrite@plt>
lea    0xdc7(%rip),%rsi
lea    0x14(%rsp),%rdx
lea    0xc(%rsp),%rcx
mov    %rbx,%rdi
call   1050 <fscanf@plt>
cmp    $0x2,%eax
jne    12c3 <is_in_UTF8_mode+0x103>
mov    (%r15),%esi
lea    0x8(%rsp),%rdx
mov    %ebp,%edi
call   10b0 <tcsetattr@plt>
mov    0xc(%rsp),%edx
add    $0xffffffff,%edx
lea    0xd9b(%rip),%rsi
mov    %rbx,%rdi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %rbx,%rdi
call   1070 <fflush@plt>
mov    0xc(%rsp),%eax
xor    %ecx,%ecx
cmp    $0x4,%eax
setne  %cl
neg    %ecx
cmp    $0x2,%eax
mov    $0x1,%eax
cmovne %ecx,%eax
jmp    12da <is_in_UTF8_mode+0x11a>
mov    0x2d27(%rip),%rax
jmp    12ca <is_in_UTF8_mode+0x10a>
mov    0x2d06(%rip),%rax
mov    (%rax),%eax
mov    0x2cf5(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd17(%rip),%rdi
call   10c0 <perror@plt>
mov    0x2cc0(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
%struct.termios = type { i32 }
@.str = [3 x i8] c"a+\00"
@.str.1 = [16 x i8] c"is_in_UTF8_mode\00"
@EX_SOFTWARE = global i32 0
@TCIOFLUSH = global i32 0
@TCSANOW = global i32 0
@.str.2 = [13 x i8] c"\18\1A\0D\EF\80\A0\1B[6n\1BD\00"
@.str.3 = [8 x i8] c"\1B[%u;%u\00"
@EIO = global i32 0
@errno = global i32 0
@.str.4 = [10 x i8] c"\1B[1F\1B[%uX\00"
@ENOTTY = global i32 0
define i32 @is_in_UTF8_mode(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.termios
%7 = alloca %struct.termios
%8 = alloca i32*
store i32 %0, i32* %3
%9 = load i32, i32* %3
%10 = call i64 @isatty(i32 %9)
%11 = icmp ne i64 %10, 0
br i1 %11, label %12, label %55
12: ; preds = %1
%13 = load i32, i32* %3
%14 = call i32* @fdopen(i32 %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %14, i32** %8
%15 = load i32*, i32** %8
%16 = icmp ne i32* %15, null
br i1 %16, label %21, label %17
17: ; preds = %12
%18 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%19 = load i32, i32* @EX_SOFTWARE
%20 = call i32 @exit(i32 %19)
unreachable
21: ; preds = %12
%22 = load i32, i32* %3
%23 = call i32 @tcgetattr(i32 %22, %struct.termios* %6)
%24 = bitcast %struct.termios* %7 to i8*
%25 = bitcast %struct.termios* %6 to i8*
%26 = load i32, i32* %3
%27 = load i32, i32* @TCIOFLUSH
%28 = call i32 @tcflush(i32 %26, i32 %27)
%29 = call i32 @cfmakeraw(%struct.termios* %7)
%30 = load i32, i32* %3
%31 = load i32, i32* @TCSANOW
%32 = call i32 @tcsetattr(i32 %30, i32 %31, %struct.termios* %7)
%33 = load i32*, i32** %8
%34 = call i32 (i32*, i8*, ...) @fprintf(i32* %33, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%35 = load i32*, i32** %8
%36 = call i32 @fscanf(i32* %35, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i32* %4, i32* %5)
%37 = icmp ne i32 2, %36
br i1 %37, label %38, label %40
38: ; preds = %21
%39 = load i32, i32* @EIO
store i32 %39, i32* @errno
store i32 -1, i32* %2
br label %59
40: ; preds = %21
%41 = load i32, i32* %3
%42 = load i32, i32* @TCSANOW
%43 = call i32 @tcsetattr(i32 %41, i32 %42, %struct.termios* %6)
%44 = load i32*, i32** %8
%45 = load i32, i32* %5
%46 = sub i32 %45, 1
%47 = call i32 (i32*, i8*, ...) @fprintf(i32* %44, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i32 %46)
%48 = load i32*, i32** %8
%49 = call i32 @fflush(i32* %48)
%50 = load i32, i32* %5
switch i32 %50, label %53 [
i32 2, label %51
i32 4, label %52
]
51: ; preds = %40
store i32 1, i32* %5
br label %54
52: ; preds = %40
store i32 0, i32* %5
br label %54
53: ; preds = %40
store i32 -1, i32* %5
br label %54
54: ; preds = %53, %52, %51
br label %57
55: ; preds = %1
%56 = load i32, i32* @ENOTTY
store i32 %56, i32* @errno
store i32 -1, i32* %5
br label %57
57: ; preds = %55, %54
%58 = load i32, i32* %5
store i32 %58, i32* %2
br label %59
59: ; preds = %57, %38
%60 = load i32, i32* %2
ret i32 %60
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@.str = [3 x i8] c"a+\00"
@.str.1 = [16 x i8] c"is_in_UTF8_mode\00"
@EX_SOFTWARE = global i32 0
@TCIOFLUSH = global i32 0
@TCSANOW = global i32 0
@.str.2 = [13 x i8] c"\18\1A\0D\EF\80\A0\1B[6n\1BD\00"
@.str.3 = [8 x i8] c"\1B[%u;%u\00"
@EIO = global i32 0
@errno = global i32 0
@.str.4 = [10 x i8] c"\1B[1F\1B[%uX\00"
@ENOTTY = global i32 0
define i32 @is_in_UTF8_mode(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.termios
%5 = alloca %struct.termios
%6 = bitcast i32* %2 to i8*
%7 = bitcast i32* %3 to i8*
%8 = bitcast %struct.termios* %4 to i8*
%9 = bitcast %struct.termios* %5 to i8*
%10 = tail call i64 @isatty(i32 %0)
%11 = icmp eq i64 %10, 0
br i1 %11, label %46, label %12
12: ; preds = %1
%13 = tail call i32* @fdopen(i32 %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %19
15: ; preds = %12
%16 = tail call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%17 = load i32, i32* @EX_SOFTWARE
%18 = tail call i32 @exit(i32 %17)
unreachable
19: ; preds = %12
%20 = call i32 @tcgetattr(i32 %0, %struct.termios* %4)
%21 = getelementptr inbounds %struct.termios, %struct.termios* %4, i64 0, i32 0
%22 = getelementptr inbounds %struct.termios, %struct.termios* %5, i64 0, i32 0
%23 = load i32, i32* %21
store i32 %23, i32* %22
%24 = load i32, i32* @TCIOFLUSH
%25 = call i32 @tcflush(i32 %0, i32 %24)
%26 = call i32 @cfmakeraw(%struct.termios* %5)
%27 = load i32, i32* @TCSANOW
%28 = call i32 @tcsetattr(i32 %0, i32 %27, %struct.termios* %5)
%29 = call i64 @fwrite(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i64 12, i64 1, i32* %13)
%30 = call i32 @fscanf(i32* %13, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i32* %2, i32* %3)
%31 = icmp eq i32 %30, 2
br i1 %31, label %34, label %32
32: ; preds = %19
%33 = load i32, i32* @EIO
store i32 %33, i32* @errno
br label %48
34: ; preds = %19
%35 = load i32, i32* @TCSANOW
%36 = call i32 @tcsetattr(i32 %0, i32 %35, %struct.termios* %4)
%37 = load i32, i32* %3
%38 = add i32 %37, -1
%39 = call i32 (i32*, i8*, ...) @fprintf(i32* %13, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i32 %38)
%40 = call i32 @fflush(i32* %13)
%41 = load i32, i32* %3
%42 = icmp ne i32 %41, 4
%43 = sext i1 %42 to i32
%44 = icmp eq i32 %41, 2
%45 = select i1 %44, i32 1, i32 %43
br label %48
46: ; preds = %1
%47 = load i32, i32* @ENOTTY
store i32 %47, i32* @errno
br label %48
48: ; preds = %34, %46, %32
%49 = phi i32 [ -1, %32 ], [ -1, %46 ], [ %45, %34 ]
ret i32 %49
}