#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 (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)
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    12d4 <main+0x174>
movl   $0x0,-0x4(%rbp)
jmp    12d4 <main+0x174>
mov    -0x20(%rbp),%rsi
lea    0xd7f(%rip),%rdi
call   1040 <printf@plt>
cmpl   $0x1,-0x8(%rbp)
jle    12c8 <main+0x168>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xd65(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%eax
jne    12c8 <main+0x168>
xor    %eax,%eax
mov    %eax,-0x3c(%rbp)
jmp    12ce <main+0x16e>
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,%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,%ebp
mov    $0xffffffff,%r15d
mov    $0x2,%r12d
lea    0xe59(%rip),%rbx
lea    -0x88(%r14),%eax
cmp    $0x8,%eax
jae    1218 <main+0xb8>
cmp    $0x2,%r13d
jl     1207 <main+0xa7>
mov    %ebp,%r14d
mov    0x10(%rsp),%rbp
mov    0x8(%rbp),%rdi
lea    0xe38(%rip),%rsi
call   1070 <strcmp@plt>
test   %eax,%eax
je     1256 <main+0xf6>
lea    0xe2b(%rip),%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
mov    0x8(%rbp),%rdi
lea    0xe1c(%rip),%rsi
call   1070 <strcmp@plt>
test   %eax,%eax
jne    126c <main+0x10c>
xor    %r12d,%r12d
jmp    126c <main+0x10c>
lea    0xe03(%rip),%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
jmp    126c <main+0x10c>
cmp    $0x3,%r14d
je     11bd <main+0x5d>
mov    0x2dbb(%rip),%rax
mov    (%rax),%esi
lea    0x7(%rsp),%rdx
xor    %edi,%edi
call   1060 <ioctl@plt>
mov    %eax,%r15d
test   %eax,%eax
lea    0xdc1(%rip),%rax
lea    0xdc1(%rip),%rbx
cmovs  %rax,%rbx
mov    %r15d,%r12d
shr    $0x1f,%r12d
jmp    11bd <main+0x5d>
test   %r15d,%r15d
sets   %al
add    $0xffffffff,%r14d
cmp    %r14d,%r15d
setne  %cl
or     %al,%cl
movzbl %cl,%r12d
mov    %r12d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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"
@.str.5 = [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 = 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 %71
54: ; preds = %48
store i32 0, i32* %3
br label %71
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* %4
%59 = icmp sgt i32 %58, 1
br i1 %59, label %60, label %67
60: ; preds = %55
%61 = load i8**, i8*** %5
%62 = getelementptr inbounds i8*, i8** %61, i64 1
%63 = load i8*, i8** %62
%64 = call i32 @strcmp(i8* %63, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
%65 = icmp ne i32 %64, 0
br i1 %65, label %67, label %66
66: ; preds = %60
br label %69
67: ; preds = %60, %55
%68 = load i32, i32* %10
br label %69
69: ; preds = %67, %66
%70 = phi i32 [ 0, %66 ], [ %68, %67 ]
store i32 %70, i32* %3
br label %71
71: ; preds = %69, %54, %53
%72 = load i32, i32* %3
ret i32 %72
}

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"
@.str.5 = [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 = 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 %38
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 %41
33: ; preds = %22
%34 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* %18)
%35 = load i8*, i8** %23
%36 = call i32 @strcmp(i8* dereferenceable(1) %35, i8* dereferenceable(7) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
%37 = icmp eq i32 %36, 0
br i1 %37, label %41, label %40
38: ; preds = %17
%39 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* %18)
br label %40
40: ; preds = %38, %33
br label %41
41: ; preds = %27, %40, %33
%42 = phi i32 [ %19, %40 ], [ 0, %33 ], [ %32, %27 ]
ret i32 %42
}