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

/* Variables and functions */
int /*<<< orphan*/ endttyent();
struct ttyent *getttyent();
int /*<<< orphan*/ setttyent();
scalar_t__ strcmp(int /*<<< orphan*/, char *);
char *strrchr(char *, char);
char *ttyname(int);
int ttyslot() {
  register struct ttyent *ty;
  register char *tp, *p;
  register int s;
  if (!(tp = ttyname(0)) && !(tp = ttyname(1)) && !(tp = ttyname(2)))
    return 0;
  p = strrchr(tp, '/');
  if (!p)
    p = tp;
  else
    p++;
  setttyent();
  s = 0;
  while ((ty = getttyent())) {
    s++;
    if (strcmp(ty->ty_name, p) == 0) {
      endttyent();
      return s;
    }
  }
  endttyent();
  return 0;
}


x86 assembly (clang, -O0):
<ttyslot>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
xor    %edi,%edi
call   1080 <ttyname@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11b9 <ttyslot+0x59>
mov    $0x1,%edi
call   1080 <ttyname@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11b9 <ttyslot+0x59>
mov    $0x2,%edi
call   1080 <ttyname@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11b9 <ttyslot+0x59>
movl   $0x0,-0x4(%rbp)
jmp    1259 <ttyslot+0xf9>
mov    -0x18(%rbp),%rdi
mov    $0x2f,%esi
call   1060 <strrchr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11e3 <ttyslot+0x83>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    11ef <ttyslot+0x8f>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   1040 <setttyent@plt>
movl   $0x0,-0x24(%rbp)
mov    $0x0,%al
call   1050 <getttyent@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     124b <ttyslot+0xeb>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    1246 <ttyslot+0xe6>
mov    $0x0,%al
call   1030 <endttyent@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1259 <ttyslot+0xf9>
jmp    11fd <ttyslot+0x9d>
mov    $0x0,%al
call   1030 <endttyent@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ttyslot>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
xor    %ebx,%ebx
xor    %edi,%edi
call   1080 <ttyname@plt>
mov    %rax,%r14
test   %rax,%rax
jne    119c <ttyslot+0x3c>
mov    $0x1,%edi
call   1080 <ttyname@plt>
mov    %rax,%r14
test   %rax,%rax
jne    119c <ttyslot+0x3c>
mov    $0x2,%edi
call   1080 <ttyname@plt>
mov    %rax,%r14
test   %rax,%rax
je     11f6 <ttyslot+0x96>
mov    %r14,%rdi
mov    $0x2f,%esi
call   1060 <strrchr@plt>
lea    0x1(%rax),%r15
test   %rax,%rax
cmove  %r14,%r15
xor    %eax,%eax
call   1040 <setttyent@plt>
nopl   0x0(%rax,%rax,1)
xor    %ebp,%ebp
xor    %eax,%eax
call   1050 <getttyent@plt>
test   %rax,%rax
je     11e2 <ttyslot+0x82>
add    $0x1,%ebx
mov    (%rax),%edi
mov    %r15,%rsi
call   1070 <strcmp@plt>
mov    %ebx,%ebp
test   %rax,%rax
jne    11c0 <ttyslot+0x60>
xor    %eax,%eax
call   1030 <endttyent@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %ebp,%ebp
jmp    11e9 <ttyslot+0x89>

LLVM IR (-O0):
%struct.ttyent = type { i32 }
define i32 @ttyslot() {
%1 = alloca i32
%2 = alloca %struct.ttyent*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = call i8* @ttyname(i32 0)
store i8* %6, i8** %3
%7 = icmp ne i8* %6, null
br i1 %7, label %15, label %8
8: ; preds = %0
%9 = call i8* @ttyname(i32 1)
store i8* %9, i8** %3
%10 = icmp ne i8* %9, null
br i1 %10, label %15, label %11
11: ; preds = %8
%12 = call i8* @ttyname(i32 2)
store i8* %12, i8** %3
%13 = icmp ne i8* %12, null
br i1 %13, label %15, label %14
14: ; preds = %11
store i32 0, i32* %1
br label %45
15: ; preds = %11, %8, %0
%16 = load i8*, i8** %3
%17 = call i8* @strrchr(i8* %16, i8 signext 47)
store i8* %17, i8** %4
%18 = load i8*, i8** %4
%19 = icmp ne i8* %18, null
br i1 %19, label %22, label %20
20: ; preds = %15
%21 = load i8*, i8** %3
store i8* %21, i8** %4
br label %25
22: ; preds = %15
%23 = load i8*, i8** %4
%24 = getelementptr inbounds i8, i8* %23, i32 1
store i8* %24, i8** %4
br label %25
25: ; preds = %22, %20
%26 = call i32 (...) @setttyent()
store i32 0, i32* %5
br label %27
27: ; preds = %42, %25
%28 = call %struct.ttyent* (...) @getttyent()
store %struct.ttyent* %28, %struct.ttyent** %2
%29 = icmp ne %struct.ttyent* %28, null
br i1 %29, label %30, label %43
30: ; preds = %27
%31 = load i32, i32* %5
%32 = add nsw i32 %31, 1
store i32 %32, i32* %5
%33 = load %struct.ttyent*, %struct.ttyent** %2
%34 = getelementptr inbounds %struct.ttyent, %struct.ttyent* %33, i32 0, i32 0
%35 = load i32, i32* %34
%36 = load i8*, i8** %4
%37 = call i64 @strcmp(i32 %35, i8* %36)
%38 = icmp eq i64 %37, 0
br i1 %38, label %39, label %42
39: ; preds = %30
%40 = call i32 (...) @endttyent()
%41 = load i32, i32* %5
store i32 %41, i32* %1
br label %45
42: ; preds = %30
br label %27
43: ; preds = %27
%44 = call i32 (...) @endttyent()
store i32 0, i32* %1
br label %45
45: ; preds = %43, %39, %14
%46 = load i32, i32* %1
ret i32 %46
}

LLVM IR (-O2):
%struct.ttyent = type { i32 }
define i32 @ttyslot() {
%1 = tail call i8* @ttyname(i32 0)
%2 = icmp eq i8* %1, null
br i1 %2, label %3, label %9
3: ; preds = %0
%4 = tail call i8* @ttyname(i32 1)
%5 = icmp eq i8* %4, null
br i1 %5, label %6, label %9
6: ; preds = %3
%7 = tail call i8* @ttyname(i32 2)
%8 = icmp eq i8* %7, null
br i1 %8, label %29, label %9
9: ; preds = %6, %3, %0
%10 = phi i8* [ %1, %0 ], [ %4, %3 ], [ %7, %6 ]
%11 = tail call i8* @strrchr(i8* dereferenceable(1) %10, i8 signext 47)
%12 = icmp eq i8* %11, null
%13 = getelementptr inbounds i8, i8* %11, i64 1
%14 = select i1 %12, i8* %10, i8* %13
%15 = tail call i32 (...) @setttyent()
br label %16
16: ; preds = %20, %9
%17 = phi i32 [ 0, %9 ], [ %21, %20 ]
%18 = tail call %struct.ttyent* (...) @getttyent()
%19 = icmp eq %struct.ttyent* %18, null
br i1 %19, label %26, label %20
20: ; preds = %16
%21 = add nuw nsw i32 %17, 1
%22 = getelementptr inbounds %struct.ttyent, %struct.ttyent* %18, i64 0, i32 0
%23 = load i32, i32* %22
%24 = tail call i64 @strcmp(i32 %23, i8* %14)
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %16
26: ; preds = %16, %20
%27 = phi i32 [ %21, %20 ], [ 0, %16 ]
%28 = tail call i32 (...) @endttyent()
br label %29
29: ; preds = %26, %6
%30 = phi i32 [ 0, %6 ], [ %27, %26 ]
ret i32 %30
}