#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*/ cmd;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ TCSANOW;
int /*<<< orphan*/ cfmakeraw(struct termios *);
int /*<<< orphan*/ fileno(int /*<<< orphan*/);
scalar_t__ isatty(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char, char);
int /*<<< orphan*/ read(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ tcgetattr(int /*<<< orphan*/, struct termios *);
int /*<<< orphan*/ tcsetattr(int /*<<< orphan*/, int /*<<< orphan*/,
                             struct termios *);
int /*<<< orphan*/ write(int, char *, int);
int main() {
  char buf[8];
  int curline;
  char cmd[] = "echo -e '\033[6n'";
  struct termios save, raw;
  tcgetattr(0, &save);
  cfmakeraw(&raw);
  tcsetattr(0, TCSANOW, &raw);
  if (isatty(fileno(stdin))) {
    write(1, cmd, sizeof(cmd));
    read(0, buf, sizeof(buf));
    printf("\n\rCurrent Line: %c%c\n\r", buf[2], buf[3]);
  }
  tcsetattr(0, TCSANOW, &save);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xe6a(%rip),%rax
mov    %rax,-0x1f(%rbp)
mov    0xe68(%rip),%eax
mov    %eax,-0x17(%rbp)
mov    0xe62(%rip),%ax
mov    %ax,-0x13(%rbp)
mov    0xe5a(%rip),%al
mov    %al,-0x11(%rbp)
xor    %edi,%edi
lea    -0x28(%rbp),%rsi
call   1090 <tcgetattr@plt>
lea    -0x30(%rbp),%rdi
call   1080 <cfmakeraw@plt>
mov    0x2e0e(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    -0x30(%rbp),%rdx
call   10a0 <tcsetattr@plt>
mov    0x2e02(%rip),%rax
mov    (%rax),%edi
call   1070 <fileno@plt>
mov    %eax,%edi
call   1030 <isatty@plt>
cmp    $0x0,%rax
je     1237 <main+0xb7>
lea    -0x1f(%rbp),%rsi
mov    $0x1,%edi
mov    $0xf,%edx
call   1040 <write@plt>
lea    -0xc(%rbp),%rsi
xor    %edi,%edi
mov    $0x8,%edx
call   1060 <read@plt>
mov    -0xa(%rbp),%al
lea    0xde4(%rip),%rdi
movsbl %al,%esi
movsbl -0x9(%rbp),%edx
call   1050 <printf@plt>
mov    0x2da2(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    -0x28(%rbp),%rdx
call   10a0 <tcsetattr@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x28,%rsp
movabs $0x276e365b1b2720,%rax
mov    %rax,0x1f(%rsp)
movabs $0x20652d206f686365,%rax
2d 65 20
mov    %rax,0x18(%rsp)
mov    %rsp,%rsi
xor    %edi,%edi
call   1090 <tcgetattr@plt>
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
call   1080 <cfmakeraw@plt>
mov    0x2e1d(%rip),%r14
mov    (%r14),%esi
xor    %edi,%edi
mov    %rbx,%rdx
call   10a0 <tcsetattr@plt>
mov    0x2e11(%rip),%rax
mov    (%rax),%edi
call   1070 <fileno@plt>
mov    %eax,%edi
call   1030 <isatty@plt>
test   %rax,%rax
je     1225 <main+0xa5>
lea    0x18(%rsp),%rsi
mov    $0x1,%edi
mov    $0xf,%edx
call   1040 <write@plt>
lea    0x8(%rsp),%rsi
xor    %edi,%edi
mov    $0x8,%edx
call   1060 <read@plt>
movsbl 0xa(%rsp),%esi
movsbl 0xb(%rsp),%edx
lea    0xdef(%rip),%rdi
call   1050 <printf@plt>
mov    (%r14),%esi
mov    %rsp,%rdx
xor    %edi,%edi
call   10a0 <tcsetattr@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.termios = type { i32 }
@__const.main.cmd = [15 x i8] c"echo -e '\1B[6n'\00"
@TCSANOW = global i32 0
@stdin = global i32 0
@.str = [23 x i8] c"\0A\0DCurrent Line: %c%c\0A\0D\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [8 x i8]
%3 = alloca i32
%4 = alloca [15 x i8]
%5 = alloca %struct.termios
%6 = alloca %struct.termios
store i32 0, i32* %1
%7 = bitcast [15 x i8]* %4 to i8*
%8 = call i32 @tcgetattr(i32 0, %struct.termios* %5)
%9 = call i32 @cfmakeraw(%struct.termios* %6)
%10 = load i32, i32* @TCSANOW
%11 = call i32 @tcsetattr(i32 0, i32 %10, %struct.termios* %6)
%12 = load i32, i32* @stdin
%13 = call i32 @fileno(i32 %12)
%14 = call i64 @isatty(i32 %13)
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %26
16: ; preds = %0
%17 = getelementptr inbounds [15 x i8], [15 x i8]* %4, i64 0, i64 0
%18 = call i32 @write(i32 1, i8* %17, i32 15)
%19 = getelementptr inbounds [8 x i8], [8 x i8]* %2, i64 0, i64 0
%20 = call i32 @read(i32 0, i8* %19, i32 8)
%21 = getelementptr inbounds [8 x i8], [8 x i8]* %2, i64 0, i64 2
%22 = load i8, i8* %21
%23 = getelementptr inbounds [8 x i8], [8 x i8]* %2, i64 0, i64 3
%24 = load i8, i8* %23
%25 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8 signext %22, i8 signext %24)
br label %26
26: ; preds = %16, %0
%27 = load i32, i32* @TCSANOW
%28 = call i32 @tcsetattr(i32 0, i32 %27, %struct.termios* %5)
ret i32 0
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@__const.main.cmd = [15 x i8] c"echo -e '\1B[6n'\00"
@TCSANOW = global i32 0
@stdin = global i32 0
@.str = [23 x i8] c"\0A\0DCurrent Line: %c%c\0A\0D\00"
define i32 @main() {
%1 = alloca [8 x i8]
%2 = alloca [15 x i8]
%3 = alloca %struct.termios
%4 = alloca %struct.termios
%5 = getelementptr inbounds [8 x i8], [8 x i8]* %1, i64 0, i64 0
%6 = getelementptr inbounds [15 x i8], [15 x i8]* %2, i64 0, i64 0
%7 = bitcast %struct.termios* %3 to i8*
%8 = bitcast %struct.termios* %4 to i8*
%9 = call i32 @tcgetattr(i32 0, %struct.termios* %3)
%10 = call i32 @cfmakeraw(%struct.termios* %4)
%11 = load i32, i32* @TCSANOW
%12 = call i32 @tcsetattr(i32 0, i32 %11, %struct.termios* %4)
%13 = load i32, i32* @stdin
%14 = call i32 @fileno(i32 %13)
%15 = call i64 @isatty(i32 %14)
%16 = icmp eq i64 %15, 0
br i1 %16, label %25, label %17
17: ; preds = %0
%18 = call i32 @write(i32 1, i8* %6, i32 15)
%19 = call i32 @read(i32 0, i8* %5, i32 8)
%20 = getelementptr inbounds [8 x i8], [8 x i8]* %1, i64 0, i64 2
%21 = load i8, i8* %20
%22 = getelementptr inbounds [8 x i8], [8 x i8]* %1, i64 0, i64 3
%23 = load i8, i8* %22
%24 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8 signext %21, i8 signext %23)
br label %25
25: ; preds = %17, %0
%26 = load i32, i32* @TCSANOW
%27 = call i32 @tcsetattr(i32 0, i32 %26, %struct.termios* %3)
ret i32 0
}