#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*/ 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*/ 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;
  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 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)
xor    %edi,%edi
lea    -0x38(%rbp),%rsi
call   1080 <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     121b <main+0xbb>
cmpl   $0x88,-0x24(%rbp)
jl     11cd <main+0x6d>
cmpl   $0x8f,-0x24(%rbp)
jle    121b <main+0xbb>
mov    0x2e0c(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    -0x11(%rbp),%rdx
call   1060 <ioctl@plt>
mov    %eax,-0x30(%rbp)
cmp    $0x0,%eax
jge    1204 <main+0xa4>
lea    0xe0c(%rip),%rax
mov    %rax,-0x20(%rbp)
movl   $0x1,-0x2c(%rbp)
jmp    1216 <main+0xb6>
lea    0xdfc(%rip),%rax
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x2c(%rbp)
jmp    122d <main+0xcd>
lea    0xde8(%rip),%rax
mov    %rax,-0x20(%rbp)
movl   $0x2,-0x2c(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    1287 <main+0x127>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xdc8(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%eax
jne    1287 <main+0x127>
cmpl   $0x0,-0x30(%rbp)
jl     126f <main+0x10f>
mov    -0x30(%rbp),%eax
mov    -0x28(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
je     127b <main+0x11b>
movl   $0x1,-0x4(%rbp)
jmp    129d <main+0x13d>
movl   $0x0,-0x4(%rbp)
jmp    129d <main+0x13d>
mov    -0x20(%rbp),%rsi
lea    0xd7f(%rip),%rdi
call   1040 <printf@plt>
mov    -0x2c(%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,%r13d
movb   $0xc,0x7(%rsp)
lea    0x8(%rsp),%rsi
xor    %edi,%edi
call   1080 <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,%r15d
mov    $0xffffffff,%r12d
mov    $0x2,%ebp
lea    0xe59(%rip),%rbx
lea    -0x88(%r14),%eax
cmp    $0x8,%eax
jae    11fc <main+0x9c>
cmp    $0x2,%r13d
jl     11dc <main+0x7c>
mov    0x10(%rsp),%rax
mov    0x8(%rax),%rdi
lea    0xe3b(%rip),%rsi
call   1070 <strcmp@plt>
test   %eax,%eax
je     1236 <main+0xd6>
lea    0xe2e(%rip),%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
cmp    $0x3,%r14d
je     11bd <main+0x5d>
mov    0x2dd7(%rip),%rax
mov    (%rax),%esi
lea    0x7(%rsp),%rdx
xor    %edi,%edi
call   1060 <ioctl@plt>
mov    %eax,%r12d
test   %eax,%eax
lea    0xddd(%rip),%rax
lea    0xddd(%rip),%rbx
cmovs  %rax,%rbx
mov    %r12d,%ebp
shr    $0x1f,%ebp
jmp    11bd <main+0x5d>
test   %r12d,%r12d
sets   %al
add    $0xffffffff,%r15d
cmp    %r15d,%r12d
setne  %cl
or     %al,%cl
movzbl %cl,%ebp
jmp    11eb <main+0x8b>

LLVM IR (-O0):
%struct.stat = type { i32 }
@TIOCLINUX = global i32 0
@.str = [7 x i8] c"serial\00"
@.str.1 = [3 x i8] c"vt\00"
@.str.2 = [4 x i8] c"pty\00"
@.str.3 = [3 x i8] c"fg\00"
@.str.4 = [4 x i8] c"%s\0A\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 = call i32 @fstat(i32 0, %struct.stat* %12)
%14 = getelementptr inbounds %struct.stat, %struct.stat* %12, i32 0, i32 0
%15 = load i32, i32* %14
%16 = call i32 @major(i32 %15)
store i32 %16, i32* %8
%17 = getelementptr inbounds %struct.stat, %struct.stat* %12, i32 0, i32 0
%18 = load i32, i32* %17
%19 = call i32 @minor(i32 %18)
store i32 %19, i32* %9
%20 = load i32, i32* %8
%21 = icmp ne i32 %20, 3
br i1 %21, label %22, label %35
22: ; preds = %2
%23 = load i32, i32* %8
%24 = icmp slt i32 %23, 136
br i1 %24, label %28, label %25
25: ; preds = %22
%26 = load i32, i32* %8
%27 = icmp sgt i32 %26, 143
br i1 %27, label %28, label %35
28: ; preds = %25, %22
%29 = load i32, i32* @TIOCLINUX
%30 = call i32 @ioctl(i32 0, i32 %29, i8* %6)
store i32 %30, i32* %11
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %28
store i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8** %7
store i32 1, i32* %10
br label %34
33: ; preds = %28
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8** %7
store i32 0, i32* %10
br label %34
34: ; preds = %33, %32
br label %36
35: ; preds = %25, %2
store i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8** %7
store i32 2, i32* %10
br label %36
36: ; preds = %35, %34
%37 = load i32, i32* %4
%38 = icmp sgt i32 %37, 1
br i1 %38, label %39, label %55
39: ; preds = %36
%40 = load i8**, i8*** %5
%41 = getelementptr inbounds i8*, i8** %40, i64 1
%42 = load i8*, i8** %41
%43 = call i32 @strcmp(i8* %42, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%44 = icmp ne i32 %43, 0
br i1 %44, label %55, label %45
45: ; preds = %39
%46 = load i32, i32* %11
%47 = icmp slt i32 %46, 0
br i1 %47, label %53, label %48
48: ; preds = %45
%49 = load i32, i32* %11
%50 = load i32, i32* %9
%51 = sub nsw i32 %50, 1
%52 = icmp ne i32 %49, %51
br i1 %52, label %53, label %54
53: ; preds = %48, %45
store i32 1, i32* %3
br label %59
54: ; preds = %48
store i32 0, i32* %3
br label %59
55: ; preds = %39, %36
%56 = load i8*, i8** %7
%57 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* %56)
%58 = load i32, i32* %10
store i32 %58, i32* %3
br label %59
59: ; preds = %55, %54, %53
%60 = load i32, i32* %3
ret i32 %60
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@TIOCLINUX = global i32 0
@.str = [7 x i8] c"serial\00"
@.str.1 = [3 x i8] c"vt\00"
@.str.2 = [4 x i8] c"pty\00"
@.str.3 = [3 x i8] c"fg\00"
@.str.4 = [4 x i8] c"%s\0A\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 = call i32 @fstat(i32 0, %struct.stat* %4)
%7 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%8 = load i32, i32* %7
%9 = tail call i32 @major(i32 %8)
%10 = tail call i32 @minor(i32 %8)
switch i32 %9, label %11 [
i32 143, label %17
i32 142, label %17
i32 141, label %17
i32 140, label %17
i32 139, label %17
i32 138, label %17
i32 137, label %17
i32 136, label %17
i32 3, label %17
]
11: ; preds = %2
%12 = load i32, i32* @TIOCLINUX
%13 = call i32 @ioctl(i32 0, i32 %12, i8* %3)
%14 = icmp slt i32 %13, 0
%15 = select i1 %14, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0)
%16 = lshr i32 %13, 31
br label %17
17: ; preds = %2, %2, %2, %2, %2, %2, %2, %2, %2, %11
%18 = phi i8* [ %15, %11 ], [ getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), %2 ], [ getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), %2 ], [ getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), %2 ], [ getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), %2 ], [ getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), %2 ], [ getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), %2 ], [ getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), %2 ], [ getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), %2 ], [ getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), %2 ]
%19 = phi i32 [ %16, %11 ], [ 2, %2 ], [ 2, %2 ], [ 2, %2 ], [ 2, %2 ], [ 2, %2 ], [ 2, %2 ], [ 2, %2 ], [ 2, %2 ], [ 2, %2 ]
%20 = phi i32 [ %13, %11 ], [ -1, %2 ], [ -1, %2 ], [ -1, %2 ], [ -1, %2 ], [ -1, %2 ], [ -1, %2 ], [ -1, %2 ], [ -1, %2 ], [ -1, %2 ]
%21 = icmp sgt i32 %0, 1
br i1 %21, label %22, label %33
22: ; preds = %17
%23 = getelementptr inbounds i8*, i8** %1, i64 1
%24 = load i8*, i8** %23
%25 = call i32 @strcmp(i8* dereferenceable(1) %24, i8* dereferenceable(3) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %33
27: ; preds = %22
%28 = icmp slt i32 %20, 0
%29 = add nsw i32 %10, -1
%30 = icmp ne i32 %20, %29
%31 = select i1 %28, i1 true, i1 %30
%32 = zext i1 %31 to i32
br label %35
33: ; preds = %22, %17
%34 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* %18)
br label %35
35: ; preds = %27, %33
%36 = phi i32 [ %19, %33 ], [ %32, %27 ]
ret i32 %36
}