#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*/ _SC_TTY_NAME_MAX;
char *__alloca(size_t);
int __sysconf(int /*<<< orphan*/);
scalar_t__ __ttyname_r(int, char *, size_t);
int /*<<< orphan*/ endttyent();
struct ttyent *getttyent();
char *rindex(char *, char);
int /*<<< orphan*/ setttyent();
int /*<<< orphan*/ strcmp(int /*<<< orphan*/, char *);
int ttyslot(void) {
  struct ttyent *ttyp;
  int slot;
  char *p;
  int cnt;
  size_t buflen = __sysconf(_SC_TTY_NAME_MAX) + 1;
  char *name;
  if (buflen == 0)
    buflen = 32;
  name = __alloca(buflen);
  setttyent();
  for (cnt = 0; cnt < 3; ++cnt)
    if (__ttyname_r(cnt, name, buflen) == 0) {
      if ((p = rindex(name, '/')))
        ++p;
      else
        p = name;
      for (slot = 1; (ttyp = getttyent()); ++slot)
        if (!strcmp(ttyp->ty_name, p)) {
          endttyent();
          return (slot);
        }
      break;
    }
  endttyent();
  return (0);
}


x86 assembly (clang, -O0):
<ttyslot>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    0x2e59(%rip),%rax
mov    (%rax),%edi
call   1050 <__sysconf@plt>
add    $0x1,%eax
cltq
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11b2 <ttyslot+0x32>
movq   $0x20,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
call   10a0 <__alloca@plt>
mov    %rax,-0x38(%rbp)
mov    $0x0,%al
call   1040 <setttyent@plt>
movl   $0x0,-0x24(%rbp)
cmpl   $0x3,-0x24(%rbp)
jge    1297 <ttyslot+0x117>
mov    -0x24(%rbp),%edi
mov    -0x38(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1080 <__ttyname_r@plt>
cmp    $0x0,%rax
jne    1284 <ttyslot+0x104>
mov    -0x38(%rbp),%rdi
mov    $0x2f,%esi
call   1090 <rindex@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     121e <ttyslot+0x9e>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    1226 <ttyslot+0xa6>
mov    -0x38(%rbp),%rax
mov    %rax,-0x20(%rbp)
movl   $0x1,-0x14(%rbp)
mov    $0x0,%al
call   1060 <getttyent@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     127f <ttyslot+0xff>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%eax
jne    126c <ttyslot+0xec>
mov    $0x0,%al
call   1030 <endttyent@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12a5 <ttyslot+0x125>
jmp    1271 <ttyslot+0xf1>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    122d <ttyslot+0xad>
jmp    1297 <ttyslot+0x117>
jmp    1289 <ttyslot+0x109>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11cd <ttyslot+0x4d>
mov    $0x0,%al
call   1030 <endttyent@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ttyslot>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    0x2e5a(%rip),%rax
mov    (%rax),%edi
call   1050 <__sysconf@plt>
inc    %eax
mov    $0x20,%ecx
cmovne %eax,%ecx
movslq %ecx,%rbx
mov    %rbx,%rdi
call   10a0 <__alloca@plt>
mov    %rax,%r14
xor    %eax,%eax
call   1040 <setttyent@plt>
xor    %edi,%edi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1080 <__ttyname_r@plt>
test   %rax,%rax
je     11f2 <ttyslot+0x72>
mov    $0x1,%edi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1080 <__ttyname_r@plt>
test   %rax,%rax
je     11f2 <ttyslot+0x72>
mov    $0x2,%edi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1080 <__ttyname_r@plt>
xor    %ebx,%ebx
test   %rax,%rax
jne    1251 <ttyslot+0xd1>
mov    %r14,%rdi
mov    $0x2f,%esi
call   1090 <rindex@plt>
lea    0x1(%rax),%r15
test   %rax,%rax
cmove  %r14,%r15
xor    %eax,%eax
call   1060 <getttyent@plt>
mov    $0x0,%ebx
test   %rax,%rax
je     1251 <ttyslot+0xd1>
mov    $0x1,%ebp
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    (%rax),%edi
mov    %r15,%rsi
call   1070 <strcmp@plt>
test   %eax,%eax
je     124f <ttyslot+0xcf>
add    $0x1,%ebp
xor    %eax,%eax
call   1060 <getttyent@plt>
test   %rax,%rax
jne    1230 <ttyslot+0xb0>
jmp    1251 <ttyslot+0xd1>
mov    %ebp,%ebx
xor    %eax,%eax
call   1030 <endttyent@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.ttyent = type { i32 }
@_SC_TTY_NAME_MAX = global i32 0
define i32 @ttyslot() {
%1 = alloca i32
%2 = alloca %struct.ttyent*
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i64
%7 = alloca i8*
%8 = load i32, i32* @_SC_TTY_NAME_MAX
%9 = call i32 @__sysconf(i32 %8)
%10 = add nsw i32 %9, 1
%11 = sext i32 %10 to i64
store i64 %11, i64* %6
%12 = load i64, i64* %6
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %0
store i64 32, i64* %6
br label %15
15: ; preds = %14, %0
%16 = load i64, i64* %6
%17 = call i8* @__alloca(i64 %16)
store i8* %17, i8** %7
%18 = call i32 (...) @setttyent()
store i32 0, i32* %5
br label %19
19: ; preds = %57, %15
%20 = load i32, i32* %5
%21 = icmp slt i32 %20, 3
br i1 %21, label %22, label %60
22: ; preds = %19
%23 = load i32, i32* %5
%24 = load i8*, i8** %7
%25 = load i64, i64* %6
%26 = call i64 @__ttyname_r(i32 %23, i8* %24, i64 %25)
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %56
28: ; preds = %22
%29 = load i8*, i8** %7
%30 = call i8* @rindex(i8* %29, i8 signext 47)
store i8* %30, i8** %4
%31 = icmp ne i8* %30, null
br i1 %31, label %32, label %35
32: ; preds = %28
%33 = load i8*, i8** %4
%34 = getelementptr inbounds i8, i8* %33, i32 1
store i8* %34, i8** %4
br label %37
35: ; preds = %28
%36 = load i8*, i8** %7
store i8* %36, i8** %4
br label %37
37: ; preds = %35, %32
store i32 1, i32* %3
br label %38
38: ; preds = %52, %37
%39 = call %struct.ttyent* (...) @getttyent()
store %struct.ttyent* %39, %struct.ttyent** %2
%40 = icmp ne %struct.ttyent* %39, null
br i1 %40, label %41, label %55
41: ; preds = %38
%42 = load %struct.ttyent*, %struct.ttyent** %2
%43 = getelementptr inbounds %struct.ttyent, %struct.ttyent* %42, i32 0, i32 0
%44 = load i32, i32* %43
%45 = load i8*, i8** %4
%46 = call i32 @strcmp(i32 %44, i8* %45)
%47 = icmp ne i32 %46, 0
br i1 %47, label %51, label %48
48: ; preds = %41
%49 = call i32 (...) @endttyent()
%50 = load i32, i32* %3
store i32 %50, i32* %1
br label %62
51: ; preds = %41
br label %52
52: ; preds = %51
%53 = load i32, i32* %3
%54 = add nsw i32 %53, 1
store i32 %54, i32* %3
br label %38
55: ; preds = %38
br label %60
56: ; preds = %22
br label %57
57: ; preds = %56
%58 = load i32, i32* %5
%59 = add nsw i32 %58, 1
store i32 %59, i32* %5
br label %19
60: ; preds = %55, %19
%61 = call i32 (...) @endttyent()
store i32 0, i32* %1
br label %62
62: ; preds = %60, %48
%63 = load i32, i32* %1
ret i32 %63
}

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