#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 */

/* Variables and functions */
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int read(int, char *, int);
char *rindex(char *, char);
int /*<<< orphan*/ strcmp(char *, char *);
char *ttyname(int);
ttyslot() {
  register char *tp, *p;
  int fd;
  int retval = 1;
  char buf[32];
  if (!(tp = ttyname(0)) && !(tp = ttyname(1)) && !(tp = ttyname(2)))
    return 0;
  if (!(p = rindex(tp, '/')))
    p = tp;
  else
    p++;
  if ((fd = open("/etc/ttys", 0)) < 0)
    return 0;
  for (;;) {
    tp = buf;
    for (;; tp++) {
      if (read(fd, tp, 1) != 1) {
        close(fd);
        return 0;
      }
      if (*tp == '\n' || tp >= &buf[31]) {
        *tp = 0;
        if (tp < buf + 2)
          buf[2] = '\0';
        tp = buf + 2;
        break;
      }
    }
    if (!strcmp(p, tp)) {
      close(fd);
      return retval;
    }
    retval++;
  }
}


x86 assembly (clang, -O0):
<ttyslot>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x1,-0x20(%rbp)
xor    %edi,%edi
call   1060 <ttyname@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    11c0 <ttyslot+0x60>
mov    $0x1,%edi
call   1060 <ttyname@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    11c0 <ttyslot+0x60>
mov    $0x2,%edi
call   1060 <ttyname@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    11c0 <ttyslot+0x60>
movl   $0x0,-0x4(%rbp)
jmp    12f9 <ttyslot+0x199>
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
call   1080 <rindex@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11e9 <ttyslot+0x89>
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
jmp    11f5 <ttyslot+0x95>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
lea    0xe04(%rip),%rdi
xor    %esi,%esi
call   1070 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    121b <ttyslot+0xbb>
movl   $0x0,-0x4(%rbp)
jmp    12f9 <ttyslot+0x199>
jmp    1220 <ttyslot+0xc0>
lea    -0x40(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
cmp    $0x1,%eax
je     1256 <ttyslot+0xf6>
mov    -0x1c(%rbp),%edi
call   1030 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    12f9 <ttyslot+0x199>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0xa,%eax
je     127b <ttyslot+0x11b>
mov    -0x10(%rbp),%rax
lea    -0x40(%rbp),%rcx
add    $0x1f,%rcx
cmp    %rcx,%rax
jb     12ac <ttyslot+0x14c>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rax
lea    -0x40(%rbp),%rcx
add    $0x2,%rcx
cmp    %rcx,%rax
jae    129b <ttyslot+0x13b>
movb   $0x0,-0x3e(%rbp)
lea    -0x40(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x10(%rbp)
jmp    12c2 <ttyslot+0x162>
jmp    12b1 <ttyslot+0x151>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    1228 <ttyslot+0xc8>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%eax
jne    12eb <ttyslot+0x18b>
mov    -0x1c(%rbp),%edi
call   1030 <close@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12f9 <ttyslot+0x199>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1220 <ttyslot+0xc0>
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ttyslot>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
xor    %r12d,%r12d
xor    %edi,%edi
call   1060 <ttyname@plt>
mov    %rax,%rbp
test   %rax,%rax
jne    11a8 <ttyslot+0x48>
mov    $0x1,%edi
call   1060 <ttyname@plt>
mov    %rax,%rbp
test   %rax,%rax
jne    11a8 <ttyslot+0x48>
mov    $0x2,%edi
call   1060 <ttyname@plt>
mov    %rax,%rbp
test   %rax,%rax
je     125a <ttyslot+0xfa>
mov    %rbp,%rdi
mov    $0x2f,%esi
call   1080 <rindex@plt>
lea    0x1(%rax),%r13
test   %rax,%rax
cmove  %rbp,%r13
lea    0xe39(%rip),%rdi
xor    %esi,%esi
call   1070 <open@plt>
test   %eax,%eax
js     125a <ttyslot+0xfa>
mov    %eax,%ebp
mov    %rsp,%rbx
mov    $0x1,%r14d
mov    %eax,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1040 <read@plt>
xor    %r12d,%r12d
cmp    $0x1,%eax
jne    1253 <ttyslot+0xf3>
lea    0x1f(%rsp),%r15
nopl   (%rax)
cmpb   $0xa,(%rbx)
je     1224 <ttyslot+0xc4>
cmp    %r15,%rbx
jae    1224 <ttyslot+0xc4>
add    $0x1,%rbx
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1040 <read@plt>
cmp    $0x1,%eax
je     1200 <ttyslot+0xa0>
jmp    1253 <ttyslot+0xf3>
movb   $0x0,(%rbx)
lea    0x2(%rsp),%rax
cmp    %rax,%rbx
jae    1236 <ttyslot+0xd6>
movb   $0x0,0x2(%rsp)
mov    %r13,%rdi
lea    0x2(%rsp),%rsi
call   1050 <strcmp@plt>
test   %eax,%eax
mov    %rsp,%rbx
je     1250 <ttyslot+0xf0>
add    $0x1,%r14d
jmp    120e <ttyslot+0xae>
mov    %r14d,%r12d
mov    %ebp,%edi
call   1030 <close@plt>
mov    %r12d,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [10 x i8] c"/etc/ttys\00"
define i32 @ttyslot() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca [32 x i8]
store i32 1, i32* %5
%7 = call i8* @ttyname(i32 0)
store i8* %7, i8** %2
%8 = icmp ne i8* %7, null
br i1 %8, label %16, label %9
9: ; preds = %0
%10 = call i8* @ttyname(i32 1)
store i8* %10, i8** %2
%11 = icmp ne i8* %10, null
br i1 %11, label %16, label %12
12: ; preds = %9
%13 = call i8* @ttyname(i32 2)
store i8* %13, i8** %2
%14 = icmp ne i8* %13, null
br i1 %14, label %16, label %15
15: ; preds = %12
store i32 0, i32* %1
br label %76
16: ; preds = %12, %9, %0
%17 = load i8*, i8** %2
%18 = call i8* @rindex(i8* %17, i8 signext 47)
store i8* %18, i8** %3
%19 = icmp ne i8* %18, null
br i1 %19, label %22, label %20
20: ; preds = %16
%21 = load i8*, i8** %2
store i8* %21, i8** %3
br label %25
22: ; preds = %16
%23 = load i8*, i8** %3
%24 = getelementptr inbounds i8, i8* %23, i32 1
store i8* %24, i8** %3
br label %25
25: ; preds = %22, %20
%26 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 0)
store i32 %26, i32* %4
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %25
store i32 0, i32* %1
br label %76
29: ; preds = %25
br label %30
30: ; preds = %73, %29
%31 = getelementptr inbounds [32 x i8], [32 x i8]* %6, i64 0, i64 0
store i8* %31, i8** %2
br label %32
32: ; preds = %61, %30
%33 = load i32, i32* %4
%34 = load i8*, i8** %2
%35 = call i32 @read(i32 %33, i8* %34, i32 1)
%36 = icmp ne i32 %35, 1
br i1 %36, label %37, label %40
37: ; preds = %32
%38 = load i32, i32* %4
%39 = call i32 @close(i32 %38)
store i32 0, i32* %1
br label %76
40: ; preds = %32
%41 = load i8*, i8** %2
%42 = load i8, i8* %41
%43 = sext i8 %42 to i32
%44 = icmp eq i32 %43, 10
br i1 %44, label %49, label %45
45: ; preds = %40
%46 = load i8*, i8** %2
%47 = getelementptr inbounds [32 x i8], [32 x i8]* %6, i64 0, i64 31
%48 = icmp uge i8* %46, %47
br i1 %48, label %49, label %60
49: ; preds = %45, %40
%50 = load i8*, i8** %2
store i8 0, i8* %50
%51 = load i8*, i8** %2
%52 = getelementptr inbounds [32 x i8], [32 x i8]* %6, i64 0, i64 0
%53 = getelementptr inbounds i8, i8* %52, i64 2
%54 = icmp ult i8* %51, %53
br i1 %54, label %55, label %57
55: ; preds = %49
%56 = getelementptr inbounds [32 x i8], [32 x i8]* %6, i64 0, i64 2
store i8 0, i8* %56
br label %57
57: ; preds = %55, %49
%58 = getelementptr inbounds [32 x i8], [32 x i8]* %6, i64 0, i64 0
%59 = getelementptr inbounds i8, i8* %58, i64 2
store i8* %59, i8** %2
br label %64
60: ; preds = %45
br label %61
61: ; preds = %60
%62 = load i8*, i8** %2
%63 = getelementptr inbounds i8, i8* %62, i32 1
store i8* %63, i8** %2
br label %32
64: ; preds = %57
%65 = load i8*, i8** %3
%66 = load i8*, i8** %2
%67 = call i32 @strcmp(i8* %65, i8* %66)
%68 = icmp ne i32 %67, 0
br i1 %68, label %73, label %69
69: ; preds = %64
%70 = load i32, i32* %4
%71 = call i32 @close(i32 %70)
%72 = load i32, i32* %5
store i32 %72, i32* %1
br label %76
73: ; preds = %64
%74 = load i32, i32* %5
%75 = add nsw i32 %74, 1
store i32 %75, i32* %5
br label %30
76: ; preds = %69, %37, %28, %15
%77 = load i32, i32* %1
ret i32 %77
}

LLVM IR (-O2):
@.str = [10 x i8] c"/etc/ttys\00"
define i32 @ttyslot() {
%1 = alloca [32 x i8]
%2 = getelementptr inbounds [32 x i8], [32 x i8]* %1, i64 0, i64 0
%3 = tail call i8* @ttyname(i32 0)
%4 = icmp eq i8* %3, null
br i1 %4, label %5, label %11
5: ; preds = %0
%6 = tail call i8* @ttyname(i32 1)
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %11
8: ; preds = %5
%9 = tail call i8* @ttyname(i32 2)
%10 = icmp eq i8* %9, null
br i1 %10, label %50, label %11
11: ; preds = %8, %5, %0
%12 = phi i8* [ %3, %0 ], [ %6, %5 ], [ %9, %8 ]
%13 = tail call i8* @rindex(i8* %12, i8 signext 47)
%14 = icmp eq i8* %13, null
%15 = getelementptr inbounds i8, i8* %13, i64 1
%16 = select i1 %14, i8* %12, i8* %15
%17 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 0)
%18 = icmp slt i32 %17, 0
br i1 %18, label %50, label %19
19: ; preds = %11
%20 = call i32 @read(i32 %17, i8* %2, i32 1)
%21 = icmp eq i32 %20, 1
br i1 %21, label %22, label %47
22: ; preds = %19
%23 = getelementptr inbounds [32 x i8], [32 x i8]* %1, i64 0, i64 31
%24 = getelementptr inbounds [32 x i8], [32 x i8]* %1, i64 0, i64 2
br label %25
25: ; preds = %22, %43
%26 = phi i32 [ 1, %22 ], [ %44, %43 ]
br label %27
27: ; preds = %25, %39
%28 = phi i8* [ %2, %25 ], [ %40, %39 ]
%29 = load i8, i8* %28
%30 = icmp eq i8 %29, 10
%31 = icmp uge i8* %28, %23
%32 = select i1 %30, i1 true, i1 %31
br i1 %32, label %33, label %39
33: ; preds = %27
store i8 0, i8* %28
%34 = icmp ult i8* %28, %24
br i1 %34, label %35, label %36
35: ; preds = %33
store i8 0, i8* %24
br label %36
36: ; preds = %35, %33
%37 = call i32 @strcmp(i8* dereferenceable(1) %16, i8* %24)
%38 = icmp eq i32 %37, 0
br i1 %38, label %47, label %43
39: ; preds = %27
%40 = getelementptr inbounds i8, i8* %28, i64 1
%41 = call i32 @read(i32 %17, i8* %40, i32 1)
%42 = icmp eq i32 %41, 1
br i1 %42, label %27, label %47
43: ; preds = %36
%44 = add nuw nsw i32 %26, 1
%45 = call i32 @read(i32 %17, i8* %2, i32 1)
%46 = icmp eq i32 %45, 1
br i1 %46, label %25, label %47
47: ; preds = %36, %43, %39, %19
%48 = phi i32 [ 0, %19 ], [ 0, %39 ], [ 0, %43 ], [ %26, %36 ]
%49 = call i32 @close(i32 %17)
br label %50
50: ; preds = %47, %11, %8
%51 = phi i32 [ 0, %8 ], [ 0, %11 ], [ %48, %47 ]
ret i32 %51
}