#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 ssize_t;
typedef scalar_t__ off_t;
typedef int /*<<< orphan*/ buffer;

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int read(int, char *, int);
int /*<<< orphan*/ stderr;
bool read_active_tty(int active_tty_fd, bool is_changed) {
  char buffer[1024];
  ssize_t bytes;
  size_t count;
  if (lseek(active_tty_fd, SEEK_SET, 0) == (off_t)-1) {
    perror("lseek");
    return false;
  }
  count = 0;
  do {
    bytes = read(active_tty_fd, buffer + count, sizeof(buffer) - 1 - count);
    if (bytes == -1) {
      perror("read(active tty)");
      return false;
    }
    assert(bytes >= 0);
    count += (size_t)bytes;
  } while (bytes && count < sizeof(buffer));
  if (count >= sizeof(buffer)) {
    fprintf(stderr, "Error: active tty file too big");
    return false;
  }
  while (count > 0 && buffer[count - 1] == '\n') {
    count--;
  }
  buffer[count] = '\0';
  if (!is_changed) {
    printf("Active TTY is %s\n", buffer);
  } else {
    printf("TTY switched to %s\n", buffer);
  }
  return true;
}


x86 assembly (clang, -O0):
<read_active_tty>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e5d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <lseek@plt>
cmp    $0xffffffffffffffff,%rax
jne    11a6 <read_active_tty+0x46>
lea    0xe6b(%rip),%rdi
call   1080 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    1341 <read_active_tty+0x1e1>
movq   $0x0,-0x420(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x410(%rbp),%rsi
add    -0x420(%rbp),%rsi
mov    $0x3ff,%eax
sub    -0x420(%rbp),%rax
mov    %eax,%edx
call   1050 <read@plt>
mov    %eax,-0x414(%rbp)
cmpl   $0xffffffff,-0x414(%rbp)
jne    1200 <read_active_tty+0xa0>
lea    0xe17(%rip),%rdi
call   1080 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    1341 <read_active_tty+0x1e1>
cmpl   $0x0,-0x414(%rbp)
setge  %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
movslq -0x414(%rbp),%rax
add    -0x420(%rbp),%rax
mov    %rax,-0x420(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x414(%rbp)
mov    %al,-0x421(%rbp)
je     1252 <read_active_tty+0xf2>
cmpq   $0x400,-0x420(%rbp)
setb   %al
mov    %al,-0x421(%rbp)
mov    -0x421(%rbp),%al
test   $0x1,%al
jne    11b1 <read_active_tty+0x51>
cmpq   $0x400,-0x420(%rbp)
jb     1292 <read_active_tty+0x132>
mov    0x2d68(%rip),%rax
mov    (%rax),%edi
lea    0xd96(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1341 <read_active_tty+0x1e1>
jmp    1297 <read_active_tty+0x137>
xor    %eax,%eax
cmpq   $0x0,-0x420(%rbp)
mov    %al,-0x422(%rbp)
jbe    12cc <read_active_tty+0x16c>
mov    -0x420(%rbp),%rax
sub    $0x1,%rax
movsbl -0x410(%rbp,%rax,1),%eax
ff
cmp    $0xa,%eax
sete   %al
mov    %al,-0x422(%rbp)
mov    -0x422(%rbp),%al
test   $0x1,%al
jne    12df <read_active_tty+0x17f>
jmp    12f6 <read_active_tty+0x196>
mov    -0x420(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x420(%rbp)
jmp    1297 <read_active_tty+0x137>
mov    -0x420(%rbp),%rax
movb   $0x0,-0x410(%rbp,%rax,1)
cmpl   $0x0,-0xc(%rbp)
jne    1327 <read_active_tty+0x1c7>
lea    -0x410(%rbp),%rsi
lea    0xd19(%rip),%rdi
call   1030 <printf@plt>
jmp    133a <read_active_tty+0x1da>
lea    -0x410(%rbp),%rsi
lea    0xd13(%rip),%rdi
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_active_tty>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x408,%rsp
mov    %esi,%r14d
mov    %edi,%r15d
mov    0x2e5e(%rip),%rax
mov    (%rax),%esi
xor    %ebx,%ebx
xor    %edx,%edx
call   1040 <lseek@plt>
cmp    $0xffffffffffffffff,%rax
je     1246 <read_active_tty+0xe6>
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
lea    (%rsp,%rbp,1),%rsi
mov    $0x3ff,%edx
sub    %ebp,%edx
mov    %r15d,%edi
call   1050 <read@plt>
cmp    $0xffffffff,%eax
je     1254 <read_active_tty+0xf4>
mov    %eax,%ebx
mov    %eax,%edi
not    %edi
shr    $0x1f,%edi
call   1060 <assert@plt>
movslq %ebx,%rax
add    %rax,%rbp
test   %ebx,%ebx
je     11dd <read_active_tty+0x7d>
cmp    $0x400,%rbp
jb     11a0 <read_active_tty+0x40>
cmp    $0x3ff,%rbp
jbe    1200 <read_active_tty+0xa0>
mov    0x2df3(%rip),%rax
mov    (%rax),%edi
lea    0xe21(%rip),%rsi
call   1070 <fprintf@plt>
xor    %ebx,%ebx
jmp    1236 <read_active_tty+0xd6>
nop
mov    %rbp,%rax
sub    $0x1,%rbp
jb     1210 <read_active_tty+0xb0>
cmpb   $0xa,-0x1(%rsp,%rax,1)
je     1200 <read_active_tty+0xa0>
movb   $0x0,(%rsp,%rax,1)
test   %r14d,%r14d
je     1222 <read_active_tty+0xc2>
lea    0xe28(%rip),%rdi
jmp    1229 <read_active_tty+0xc9>
lea    0xe0d(%rip),%rdi
mov    %rsp,%rsi
call   1030 <printf@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdb3(%rip),%rdi
call   1080 <perror@plt>
jmp    1236 <read_active_tty+0xd6>
lea    0xdab(%rip),%rdi
call   1080 <perror@plt>
xor    %ebx,%ebx
jmp    1236 <read_active_tty+0xd6>

LLVM IR (-O0):
@SEEK_SET = global i32 0
@.str = [6 x i8] c"lseek\00"
@.str.1 = [17 x i8] c"read(active tty)\00"
@stderr = global i32 0
@.str.2 = [31 x i8] c"Error: active tty file too big\00"
@.str.3 = [18 x i8] c"Active TTY is %s\0A\00"
@.str.4 = [20 x i8] c"TTY switched to %s\0A\00"
define i32 @read_active_tty(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [1024 x i8]
%7 = alloca i32
%8 = alloca i64
store i32 %0, i32* %4
store i32 %1, i32* %5
%9 = load i32, i32* %4
%10 = load i32, i32* @SEEK_SET
%11 = call i64 @lseek(i32 %9, i32 %10, i32 0)
%12 = icmp eq i64 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %3
br label %80
15: ; preds = %2
store i64 0, i64* %8
br label %16
16: ; preds = %44, %15
%17 = load i32, i32* %4
%18 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%19 = load i64, i64* %8
%20 = getelementptr inbounds i8, i8* %18, i64 %19
%21 = load i64, i64* %8
%22 = sub i64 1023, %21
%23 = trunc i64 %22 to i32
%24 = call i32 @read(i32 %17, i8* %20, i32 %23)
store i32 %24, i32* %7
%25 = load i32, i32* %7
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %29
27: ; preds = %16
%28 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %3
br label %80
29: ; preds = %16
%30 = load i32, i32* %7
%31 = icmp sge i32 %30, 0
%32 = zext i1 %31 to i32
%33 = call i32 @assert(i32 %32)
%34 = load i32, i32* %7
%35 = sext i32 %34 to i64
%36 = load i64, i64* %8
%37 = add i64 %36, %35
store i64 %37, i64* %8
br label %38
38: ; preds = %29
%39 = load i32, i32* %7
%40 = icmp ne i32 %39, 0
br i1 %40, label %41, label %44
41: ; preds = %38
%42 = load i64, i64* %8
%43 = icmp ult i64 %42, 1024
br label %44
44: ; preds = %41, %38
%45 = phi i1 [ false, %38 ], [ %43, %41 ]
br i1 %45, label %16, label %46
46: ; preds = %44
%47 = load i64, i64* %8
%48 = icmp uge i64 %47, 1024
br i1 %48, label %49, label %52
49: ; preds = %46
%50 = load i32, i32* @stderr
%51 = call i32 @fprintf(i32 %50, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %3
br label %80
52: ; preds = %46
br label %53
53: ; preds = %65, %52
%54 = load i64, i64* %8
%55 = icmp ugt i64 %54, 0
br i1 %55, label %56, label %63
56: ; preds = %53
%57 = load i64, i64* %8
%58 = sub i64 %57, 1
%59 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 %58
%60 = load i8, i8* %59
%61 = sext i8 %60 to i32
%62 = icmp eq i32 %61, 10
br label %63
63: ; preds = %56, %53
%64 = phi i1 [ false, %53 ], [ %62, %56 ]
br i1 %64, label %65, label %68
65: ; preds = %63
%66 = load i64, i64* %8
%67 = add i64 %66, -1
store i64 %67, i64* %8
br label %53
68: ; preds = %63
%69 = load i64, i64* %8
%70 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 %69
store i8 0, i8* %70
%71 = load i32, i32* %5
%72 = icmp ne i32 %71, 0
br i1 %72, label %76, label %73
73: ; preds = %68
%74 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%75 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i8* %74)
br label %79
76: ; preds = %68
%77 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%78 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i8* %77)
br label %79
79: ; preds = %76, %73
store i32 1, i32* %3
br label %80
80: ; preds = %79, %49, %27, %13
%81 = load i32, i32* %3
ret i32 %81
}

LLVM IR (-O2):
@SEEK_SET = global i32 0
@.str = [6 x i8] c"lseek\00"
@.str.1 = [17 x i8] c"read(active tty)\00"
@stderr = global i32 0
@.str.2 = [31 x i8] c"Error: active tty file too big\00"
@.str.3 = [18 x i8] c"Active TTY is %s\0A\00"
@.str.4 = [20 x i8] c"TTY switched to %s\0A\00"
define i32 @read_active_tty(i32 %0, i32 %1) {
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%5 = load i32, i32* @SEEK_SET
%6 = tail call i64 @lseek(i32 %0, i32 %5, i32 0)
%7 = icmp eq i64 %6, -1
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %48
10: ; preds = %2, %19
%11 = phi i64 [ %24, %19 ], [ 0, %2 ]
%12 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 %11
%13 = trunc i64 %11 to i32
%14 = sub nuw nsw i32 1023, %13
%15 = call i32 @read(i32 %0, i8* %12, i32 %14)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %19
17: ; preds = %10
%18 = tail call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
br label %48
19: ; preds = %10
%20 = xor i32 %15, -1
%21 = lshr i32 %20, 31
%22 = tail call i32 @assert(i32 %21)
%23 = sext i32 %15 to i64
%24 = add nsw i64 %11, %23
%25 = icmp ne i32 %15, 0
%26 = icmp ult i64 %24, 1024
%27 = select i1 %25, i1 %26, i1 false
br i1 %27, label %10, label %28
28: ; preds = %19
%29 = icmp ugt i64 %24, 1023
br i1 %29, label %30, label %33
30: ; preds = %28
%31 = load i32, i32* @stderr
%32 = tail call i32 @fprintf(i32 %31, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
br label %48
33: ; preds = %28, %36
%34 = phi i64 [ %37, %36 ], [ %24, %28 ]
%35 = icmp eq i64 %34, 0
br i1 %35, label %41, label %36
36: ; preds = %33
%37 = add i64 %34, -1
%38 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 %37
%39 = load i8, i8* %38
%40 = icmp eq i8 %39, 10
br i1 %40, label %33, label %41
41: ; preds = %33, %36
%42 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 %34
store i8 0, i8* %42
%43 = icmp eq i32 %1, 0
br i1 %43, label %44, label %46
44: ; preds = %41
%45 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i8* %4)
br label %48
46: ; preds = %41
%47 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i8* %4)
br label %48
48: ; preds = %44, %46, %30, %17, %8
%49 = phi i32 [ 0, %8 ], [ 0, %17 ], [ 0, %30 ], [ 1, %46 ], [ 1, %44 ]
ret i32 %49
}