#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*/ _PATH_DEV;
int /*<<< orphan*/ endttyent();
struct ttyent *getttyent();
int /*<<< orphan*/ setttyent();
int /*<<< orphan*/ strcmp(int /*<<< orphan*/, char *);
scalar_t__ strncmp(char *, int /*<<< orphan*/, int);
char *ttyname(int);
int ttyslot() {
  struct ttyent *ttyp;
  int slot;
  int cnt;
  char *name;
  setttyent();
  for (cnt = 0; cnt < 3; ++cnt)
    if ((name = ttyname(cnt))) {
      if (strncmp(name, _PATH_DEV, sizeof _PATH_DEV - 1) != 0)
        break;
      name += sizeof _PATH_DEV - 1;
      for (slot = 1; (ttyp = getttyent()); ++slot)
        if (!strcmp(ttyp->ty_name, name)) {
          endttyent();
          return (slot);
        }
      break;
    }
  endttyent();
  return (0);
}


x86 assembly (clang, -O0):
<ttyslot>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    $0x0,%al
call   1050 <setttyent@plt>
movl   $0x0,-0x18(%rbp)
cmpl   $0x3,-0x18(%rbp)
jge    1239 <ttyslot+0xd9>
mov    -0x18(%rbp),%edi
call   1080 <ttyname@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     1226 <ttyslot+0xc6>
mov    -0x20(%rbp),%rdi
mov    0x2e47(%rip),%rax
mov    (%rax),%esi
mov    $0x3,%edx
call   1040 <strncmp@plt>
cmp    $0x0,%rax
je     11bc <ttyslot+0x5c>
jmp    1239 <ttyslot+0xd9>
mov    -0x20(%rbp),%rax
add    $0x3,%rax
mov    %rax,-0x20(%rbp)
movl   $0x1,-0x14(%rbp)
mov    $0x0,%al
call   1060 <getttyent@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     1221 <ttyslot+0xc1>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%eax
jne    120e <ttyslot+0xae>
mov    $0x0,%al
call   1030 <endttyent@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1247 <ttyslot+0xe7>
jmp    1213 <ttyslot+0xb3>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11cf <ttyslot+0x6f>
jmp    1239 <ttyslot+0xd9>
jmp    122b <ttyslot+0xcb>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1176 <ttyslot+0x16>
mov    $0x0,%al
call   1030 <endttyent@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ttyslot>:
push   %rbp
push   %r14
push   %rbx
xor    %ebx,%ebx
xor    %eax,%eax
call   1050 <setttyent@plt>
xor    %edi,%edi
call   1080 <ttyname@plt>
mov    %rax,%r14
test   %rax,%rax
jne    11a0 <ttyslot+0x40>
mov    $0x1,%edi
call   1080 <ttyname@plt>
mov    %rax,%r14
test   %rax,%rax
jne    11a0 <ttyslot+0x40>
mov    $0x2,%edi
call   1080 <ttyname@plt>
mov    %rax,%r14
test   %rax,%rax
je     11bb <ttyslot+0x5b>
mov    0x2e41(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
mov    $0x3,%edx
call   1040 <strncmp@plt>
test   %rax,%rax
je     11c9 <ttyslot+0x69>
xor    %eax,%eax
call   1030 <endttyent@plt>
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
xor    %ebx,%ebx
xor    %eax,%eax
call   1060 <getttyent@plt>
test   %rax,%rax
je     11bb <ttyslot+0x5b>
add    $0x3,%r14
mov    $0x1,%ebp
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    (%rax),%edi
mov    %r14,%rsi
call   1070 <strcmp@plt>
test   %eax,%eax
je     120f <ttyslot+0xaf>
add    $0x1,%ebp
xor    %eax,%eax
call   1060 <getttyent@plt>
test   %rax,%rax
jne    11f0 <ttyslot+0x90>
jmp    11bb <ttyslot+0x5b>
mov    %ebp,%ebx
jmp    11bb <ttyslot+0x5b>

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

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