#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();
char *rindex(char *, char);
int s;
int /*<<< orphan*/ setttyent();
scalar_t__ strcmp(int /*<<< orphan*/, char *);
char *ttyname(int);
ttyslot() {
  register struct ttyent *ty;
  register char *tp, *p;
  register s;
  if ((tp = ttyname(0)) == 0 && (tp = ttyname(1)) == 0 &&
      (tp = ttyname(2)) == 0)
    return (0);
  if ((p = rindex(tp, '/')) == 0)
    p = tp;
  else
    p++;
  setttyent();
  s = 0;
  while ((ty = getttyent()) != 0) {
    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   1070 <ttyname@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11b9 <ttyslot+0x59>
mov    $0x1,%edi
call   1070 <ttyname@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11b9 <ttyslot+0x59>
mov    $0x2,%edi
call   1070 <ttyname@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11b9 <ttyslot+0x59>
movl   $0x0,-0x4(%rbp)
jmp    1258 <ttyslot+0xf8>
mov    -0x18(%rbp),%rdi
mov    $0x2f,%esi
call   1080 <rindex@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11e2 <ttyslot+0x82>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    11ee <ttyslot+0x8e>
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     124a <ttyslot+0xea>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    1245 <ttyslot+0xe5>
mov    $0x0,%al
call   1030 <endttyent@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1258 <ttyslot+0xf8>
jmp    11fc <ttyslot+0x9c>
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   1070 <ttyname@plt>
mov    %rax,%r14
test   %rax,%rax
jne    119c <ttyslot+0x3c>
mov    $0x1,%edi
call   1070 <ttyname@plt>
mov    %rax,%r14
test   %rax,%rax
jne    119c <ttyslot+0x3c>
mov    $0x2,%edi
call   1070 <ttyname@plt>
mov    %rax,%r14
test   %rax,%rax
je     11f6 <ttyslot+0x96>
mov    %r14,%rdi
mov    $0x2f,%esi
call   1080 <rindex@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   1060 <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 }
@s = global i32 0
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 eq i8* %6, null
br i1 %7, label %8, label %15
8: ; preds = %0
%9 = call i8* @ttyname(i32 1)
store i8* %9, i8** %3
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %15
11: ; preds = %8
%12 = call i8* @ttyname(i32 2)
store i8* %12, i8** %3
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %15
14: ; preds = %11
store i32 0, i32* %1
br label %44
15: ; preds = %11, %8, %0
%16 = load i8*, i8** %3
%17 = call i8* @rindex(i8* %16, i8 signext 47)
store i8* %17, i8** %4
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %21
19: ; preds = %15
%20 = load i8*, i8** %3
store i8* %20, i8** %4
br label %24
21: ; preds = %15
%22 = load i8*, i8** %4
%23 = getelementptr inbounds i8, i8* %22, i32 1
store i8* %23, i8** %4
br label %24
24: ; preds = %21, %19
%25 = call i32 (...) @setttyent()
store i32 0, i32* %5
br label %26
26: ; preds = %41, %24
%27 = call %struct.ttyent* (...) @getttyent()
store %struct.ttyent* %27, %struct.ttyent** %2
%28 = icmp ne %struct.ttyent* %27, null
br i1 %28, label %29, label %42
29: ; preds = %26
%30 = load i32, i32* %5
%31 = add nsw i32 %30, 1
store i32 %31, i32* %5
%32 = load %struct.ttyent*, %struct.ttyent** %2
%33 = getelementptr inbounds %struct.ttyent, %struct.ttyent* %32, i32 0, i32 0
%34 = load i32, i32* %33
%35 = load i8*, i8** %4
%36 = call i64 @strcmp(i32 %34, i8* %35)
%37 = icmp eq i64 %36, 0
br i1 %37, label %38, label %41
38: ; preds = %29
%39 = call i32 (...) @endttyent()
%40 = load i32, i32* %5
store i32 %40, i32* %1
br label %44
41: ; preds = %29
br label %26
42: ; preds = %26
%43 = call i32 (...) @endttyent()
store i32 0, i32* %1
br label %44
44: ; preds = %42, %38, %14
%45 = load i32, i32* %1
ret i32 %45
}

LLVM IR (-O2):
%struct.ttyent = type { i32 }
@s = global i32 0
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* [ %7, %6 ], [ %4, %3 ], [ %1, %0 ]
%11 = tail call i8* @rindex(i8* %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
}