#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 stat {
  int /*<<< orphan*/ st_rdev;
};

/* Variables and functions */
int /*<<< orphan*/ TIOCLINUX;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ fstat(int /*<<< orphan*/, struct stat *);
int ioctl(int /*<<< orphan*/, int /*<<< orphan*/, unsigned char *);
int major(int /*<<< orphan*/);
int minor(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcmp(char *, char *);
int main(int argc, char **argv) {
  unsigned char twelve = 12;
  char *type;
  int maj, min, ret = 0, fg = -1;
  struct stat sb;
  fprintf(stderr, "warning: consoletype is now deprecated, and will be removed "
                  "in the near future!\n"
                  "warning: use tty (1) instead! More info: 'man 1 tty'\n");
  fstat(0, &sb);
  maj = major(sb.st_rdev);
  min = minor(sb.st_rdev);
  if (maj != 3 && (maj < 136 || maj > 143)) {
    if ((fg = ioctl(0, TIOCLINUX, &twelve)) < 0) {
      type = "serial";
      ret = 1;
    } else {
      type = "vt";
      ret = 0;
    }
  } else {
    type = "pty";
    ret = 2;
  }
  if (argc > 1 && !strcmp(argv[1], "fg")) {
    if (fg < 0 || fg != (min - 1))
      return 1;
    return 0;
  } else {
    printf("%s\n", type);
    return (argc > 1 && !strcmp(argv[1], "stdout")) ? 0 : ret;
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movb   $0xc,-0x11(%rbp)
movl   $0x0,-0x2c(%rbp)
movl   $0xffffffff,-0x30(%rbp)
mov    0x2e41(%rip),%rax
mov    (%rax),%edi
lea    0xe58(%rip),%rsi
call   1080 <fprintf@plt>
xor    %edi,%edi
lea    -0x38(%rbp),%rsi
call   1090 <fstat@plt>
mov    -0x38(%rbp),%edi
call   1050 <major@plt>
mov    %eax,-0x24(%rbp)
mov    -0x38(%rbp),%edi
call   1030 <minor@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x3,-0x24(%rbp)
je     1240 <main+0xd0>
cmpl   $0x88,-0x24(%rbp)
jl     11f2 <main+0x82>
cmpl   $0x8f,-0x24(%rbp)
jle    1240 <main+0xd0>
mov    0x2ddf(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    -0x11(%rbp),%rdx
call   1060 <ioctl@plt>
mov    %eax,-0x30(%rbp)
cmp    $0x0,%eax
jge    1229 <main+0xb9>
lea    0xe6d(%rip),%rax
mov    %rax,-0x20(%rbp)
movl   $0x1,-0x2c(%rbp)
jmp    123b <main+0xcb>
lea    0xe5d(%rip),%rax
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x2c(%rbp)
jmp    1252 <main+0xe2>
lea    0xe49(%rip),%rax
mov    %rax,-0x20(%rbp)
movl   $0x2,-0x2c(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    12ac <main+0x13c>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe29(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%eax
jne    12ac <main+0x13c>
cmpl   $0x0,-0x30(%rbp)
jl     1294 <main+0x124>
mov    -0x30(%rbp),%eax
mov    -0x28(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
je     12a0 <main+0x130>
movl   $0x1,-0x4(%rbp)
jmp    12f9 <main+0x189>
movl   $0x0,-0x4(%rbp)
jmp    12f9 <main+0x189>
mov    -0x20(%rbp),%rsi
lea    0xde0(%rip),%rdi
call   1040 <printf@plt>
cmpl   $0x1,-0x8(%rbp)
jle    12ed <main+0x17d>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xdc6(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%eax
jne    12ed <main+0x17d>
xor    %eax,%eax
mov    %eax,-0x3c(%rbp)
jmp    12f3 <main+0x183>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%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
mov    %rsi,0x10(%rsp)
mov    %edi,%r12d
movb   $0xc,0x7(%rsp)
mov    0x2e4e(%rip),%rax
mov    (%rax),%edi
lea    0xe65(%rip),%rsi
call   1080 <fprintf@plt>
lea    0x8(%rsp),%rsi
xor    %edi,%edi
call   1090 <fstat@plt>
mov    0x8(%rsp),%ebx
mov    %ebx,%edi
call   1050 <major@plt>
mov    %eax,%r14d
mov    %ebx,%edi
call   1030 <minor@plt>
mov    %eax,%ebp
mov    $0xffffffff,%r15d
mov    $0x2,%r13d
lea    0xeba(%rip),%rbx
lea    -0x88(%r14),%eax
cmp    $0x8,%eax
jae    123d <main+0xcd>
cmp    $0x2,%r12d
jl     122c <main+0xbc>
mov    %ebp,%r14d
mov    0x10(%rsp),%rbp
mov    0x8(%rbp),%rdi
lea    0xe99(%rip),%rsi
call   1070 <strcmp@plt>
test   %eax,%eax
je     127b <main+0x10b>
lea    0xe8c(%rip),%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
mov    0x8(%rbp),%rdi
lea    0xe7d(%rip),%rsi
call   1070 <strcmp@plt>
test   %eax,%eax
jne    1291 <main+0x121>
xor    %r13d,%r13d
jmp    1291 <main+0x121>
lea    0xe64(%rip),%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
jmp    1291 <main+0x121>
cmp    $0x3,%r14d
je     11e2 <main+0x72>
mov    0x2d8e(%rip),%rax
mov    (%rax),%esi
lea    0x7(%rsp),%rdx
xor    %edi,%edi
call   1060 <ioctl@plt>
mov    %eax,%r15d
test   %eax,%eax
lea    0xe22(%rip),%rax
lea    0xe22(%rip),%rbx
cmovs  %rax,%rbx
mov    %r15d,%r13d
shr    $0x1f,%r13d
jmp    11e2 <main+0x72>
test   %r15d,%r15d
sets   %al
add    $0xffffffff,%r14d
cmp    %r14d,%r15d
setne  %cl
or     %al,%cl
movzbl %cl,%r13d
mov    %r13d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [134 x i8] c"warning: consoletype is now deprecated, and will be removed in the near future!\0Awarning: use tty (1) instead! More info: 'man 1 tty'\0A\00"
@TIOCLINUX = global i32 0
@.str.1 = [7 x i8] c"serial\00"
@.str.2 = [3 x i8] c"vt\00"
@.str.3 = [4 x i8] c"pty\00"
@.str.4 = [3 x i8] c"fg\00"
@.str.5 = [4 x i8] c"%s\0A\00"
@.str.6 = [7 x i8] c"stdout\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca %struct.stat
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8 12, i8* %6
store i32 0, i32* %10
store i32 -1, i32* %11
%13 = load i32, i32* @stderr
%14 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([134 x i8], [134 x i8]* @.str, i64 0, i64 0))
%15 = call i32 @fstat(i32 0, %struct.stat* %12)
%16 = getelementptr inbounds %struct.stat, %struct.stat* %12, i32 0, i32 0
%17 = load i32, i32* %16
%18 = call i32 @major(i32 %17)
store i32 %18, i32* %8
%19 = getelementptr inbounds %struct.stat, %struct.stat* %12, i32 0, i32 0
%20 = load i32, i32* %19
%21 = call i32 @minor(i32 %20)
store i32 %21, i32* %9
%22 = load i32, i32* %8
%23 = icmp ne i32 %22, 3
br i1 %23, label %24, label %37
24: ; preds = %2
%25 = load i32, i32* %8
%26 = icmp slt i32 %25, 136
br i1 %26, label %30, label %27
27: ; preds = %24
%28 = load i32, i32* %8
%29 = icmp sgt i32 %28, 143
br i1 %29, label %30, label %37
30: ; preds = %27, %24
%31 = load i32, i32* @TIOCLINUX
%32 = call i32 @ioctl(i32 0, i32 %31, i8* %6)
store i32 %32, i32* %11
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %30
store i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8** %7
store i32 1, i32* %10
br label %36
35: ; preds = %30
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8** %7
store i32 0, i32* %10
br label %36
36: ; preds = %35, %34
br label %38
37: ; preds = %27, %2
store i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8** %7
store i32 2, i32* %10
br label %38
38: ; preds = %37, %36
%39 = load i32, i32* %4
%40 = icmp sgt i32 %39, 1
br i1 %40, label %41, label %57
41: ; preds = %38
%42 = load i8**, i8*** %5
%43 = getelementptr inbounds i8*, i8** %42, i64 1
%44 = load i8*, i8** %43
%45 = call i32 @strcmp(i8* %44, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%46 = icmp ne i32 %45, 0
br i1 %46, label %57, label %47
47: ; preds = %41
%48 = load i32, i32* %11
%49 = icmp slt i32 %48, 0
br i1 %49, label %55, label %50
50: ; preds = %47
%51 = load i32, i32* %11
%52 = load i32, i32* %9
%53 = sub nsw i32 %52, 1
%54 = icmp ne i32 %51, %53
br i1 %54, label %55, label %56
55: ; preds = %50, %47
store i32 1, i32* %3
br label %73
56: ; preds = %50
store i32 0, i32* %3
br label %73
57: ; preds = %41, %38
%58 = load i8*, i8** %7
%59 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i8* %58)
%60 = load i32, i32* %4
%61 = icmp sgt i32 %60, 1
br i1 %61, label %62, label %69
62: ; preds = %57
%63 = load i8**, i8*** %5
%64 = getelementptr inbounds i8*, i8** %63, i64 1
%65 = load i8*, i8** %64
%66 = call i32 @strcmp(i8* %65, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0))
%67 = icmp ne i32 %66, 0
br i1 %67, label %69, label %68
68: ; preds = %62
br label %71
69: ; preds = %62, %57
%70 = load i32, i32* %10
br label %71
71: ; preds = %69, %68
%72 = phi i32 [ 0, %68 ], [ %70, %69 ]
store i32 %72, i32* %3
br label %73
73: ; preds = %71, %56, %55
%74 = load i32, i32* %3
ret i32 %74
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [134 x i8] c"warning: consoletype is now deprecated, and will be removed in the near future!\0Awarning: use tty (1) instead! More info: 'man 1 tty'\0A\00"
@TIOCLINUX = global i32 0
@.str.1 = [7 x i8] c"serial\00"
@.str.2 = [3 x i8] c"vt\00"
@.str.3 = [4 x i8] c"pty\00"
@.str.4 = [3 x i8] c"fg\00"
@.str.5 = [4 x i8] c"%s\0A\00"
@.str.6 = [7 x i8] c"stdout\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i8
%4 = alloca %struct.stat
store i8 12, i8* %3
%5 = bitcast %struct.stat* %4 to i8*
%6 = load i32, i32* @stderr
%7 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([134 x i8], [134 x i8]* @.str, i64 0, i64 0))
%8 = call i32 @fstat(i32 0, %struct.stat* %4)
%9 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%10 = load i32, i32* %9
%11 = tail call i32 @major(i32 %10)
%12 = tail call i32 @minor(i32 %10)
switch i32 %11, label %13 [
i32 143, label %19
i32 142, label %19
i32 141, label %19
i32 140, label %19
i32 139, label %19
i32 138, label %19
i32 137, label %19
i32 136, label %19
i32 3, label %19
]
13: ; preds = %2
%14 = load i32, i32* @TIOCLINUX
%15 = call i32 @ioctl(i32 0, i32 %14, i8* %3)
%16 = icmp slt i32 %15, 0
%17 = select i1 %16, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0)
%18 = lshr i32 %15, 31
br label %19
19: ; preds = %2, %2, %2, %2, %2, %2, %2, %2, %2, %13
%20 = phi i8* [ %17, %13 ], [ getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), %2 ], [ getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), %2 ], [ getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), %2 ], [ getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), %2 ], [ getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), %2 ], [ getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), %2 ], [ getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), %2 ], [ getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), %2 ], [ getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), %2 ]
%21 = phi i32 [ %18, %13 ], [ 2, %2 ], [ 2, %2 ], [ 2, %2 ], [ 2, %2 ], [ 2, %2 ], [ 2, %2 ], [ 2, %2 ], [ 2, %2 ], [ 2, %2 ]
%22 = phi i32 [ %15, %13 ], [ -1, %2 ], [ -1, %2 ], [ -1, %2 ], [ -1, %2 ], [ -1, %2 ], [ -1, %2 ], [ -1, %2 ], [ -1, %2 ], [ -1, %2 ]
%23 = icmp sgt i32 %0, 1
br i1 %23, label %24, label %40
24: ; preds = %19
%25 = getelementptr inbounds i8*, i8** %1, i64 1
%26 = load i8*, i8** %25
%27 = call i32 @strcmp(i8* dereferenceable(1) %26, i8* dereferenceable(3) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %35
29: ; preds = %24
%30 = icmp slt i32 %22, 0
%31 = add nsw i32 %12, -1
%32 = icmp ne i32 %22, %31
%33 = select i1 %30, i1 true, i1 %32
%34 = zext i1 %33 to i32
br label %43
35: ; preds = %24
%36 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i8* %20)
%37 = load i8*, i8** %25
%38 = call i32 @strcmp(i8* dereferenceable(1) %37, i8* dereferenceable(7) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0))
%39 = icmp eq i32 %38, 0
br i1 %39, label %43, label %42
40: ; preds = %19
%41 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i8* %20)
br label %42
42: ; preds = %40, %35
br label %43
43: ; preds = %29, %42, %35
%44 = phi i32 [ %21, %42 ], [ 0, %35 ], [ %34, %29 ]
ret i32 %44
}