#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();
int /*<<< orphan*/ strcmp(char const *, int /*<<< orphan*/);
scalar_t__ strncmp(char const *, char *, int);
struct ttyent *getttynam(const char *tty) {
  struct ttyent *t;
  if (strncmp(tty, "/dev/", 5) == 0)
    tty += 5;
  setttyent();
  while ((t = getttyent()))
    if (!strcmp(tty, t->ty_name))
      break;
  endttyent();
  return (t);
}


x86 assembly (clang, -O0):
<getttynam>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe99(%rip),%rsi
mov    $0x5,%edx
call   1040 <strncmp@plt>
cmp    $0x0,%rax
jne    1187 <getttynam+0x37>
mov    -0x8(%rbp),%rax
add    $0x5,%rax
mov    %rax,-0x8(%rbp)
mov    $0x0,%al
call   1050 <setttyent@plt>
mov    $0x0,%al
call   1060 <getttyent@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     11c5 <getttynam+0x75>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%esi
call   1070 <strcmp@plt>
cmp    $0x0,%eax
jne    11c0 <getttynam+0x70>
jmp    11c5 <getttynam+0x75>
jmp    118e <getttynam+0x3e>
mov    $0x0,%al
call   1030 <endttyent@plt>
mov    -0x10(%rbp),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getttynam>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
lea    0xea2(%rip),%rsi
mov    $0x5,%edx
call   1040 <strncmp@plt>
xor    %ecx,%ecx
test   %rax,%rax
sete   %cl
lea    (%rcx,%rcx,4),%r14
add    %rbx,%r14
xor    %eax,%eax
call   1050 <setttyent@plt>
xchg   %ax,%ax
xor    %eax,%eax
call   1060 <getttyent@plt>
mov    %rax,%rbx
test   %rax,%rax
je     119d <getttynam+0x4d>
mov    (%rbx),%esi
mov    %r14,%rdi
call   1070 <strcmp@plt>
test   %eax,%eax
jne    1180 <getttynam+0x30>
xor    %eax,%eax
call   1030 <endttyent@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.ttyent = type { i32 }
@.str = [6 x i8] c"/dev/\00"
define %struct.ttyent* @getttynam(i8* %0) {
%2 = alloca i8*
%3 = alloca %struct.ttyent*
store i8* %0, i8** %2
%4 = load i8*, i8** %2
%5 = call i64 @strncmp(i8* %4, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 5)
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %1
%8 = load i8*, i8** %2
%9 = getelementptr inbounds i8, i8* %8, i64 5
store i8* %9, i8** %2
br label %10
10: ; preds = %7, %1
%11 = call i32 (...) @setttyent()
br label %12
12: ; preds = %23, %10
%13 = call %struct.ttyent* (...) @getttyent()
store %struct.ttyent* %13, %struct.ttyent** %3
%14 = icmp ne %struct.ttyent* %13, null
br i1 %14, label %15, label %24
15: ; preds = %12
%16 = load i8*, i8** %2
%17 = load %struct.ttyent*, %struct.ttyent** %3
%18 = getelementptr inbounds %struct.ttyent, %struct.ttyent* %17, i32 0, i32 0
%19 = load i32, i32* %18
%20 = call i32 @strcmp(i8* %16, i32 %19)
%21 = icmp ne i32 %20, 0
br i1 %21, label %23, label %22
22: ; preds = %15
br label %24
23: ; preds = %15
br label %12
24: ; preds = %22, %12
%25 = call i32 (...) @endttyent()
%26 = load %struct.ttyent*, %struct.ttyent** %3
ret %struct.ttyent* %26
}

LLVM IR (-O2):
%struct.ttyent = type { i32 }
@.str = [6 x i8] c"/dev/\00"
define %struct.ttyent* @getttynam(i8* %0) {
%2 = tail call i64 @strncmp(i8* %0, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 5)
%3 = icmp eq i64 %2, 0
%4 = select i1 %3, i64 5, i64 0
%5 = getelementptr i8, i8* %0, i64 %4
%6 = tail call i32 (...) @setttyent()
br label %7
7: ; preds = %10, %1
%8 = tail call %struct.ttyent* (...) @getttyent()
%9 = icmp eq %struct.ttyent* %8, null
br i1 %9, label %15, label %10
10: ; preds = %7
%11 = getelementptr inbounds %struct.ttyent, %struct.ttyent* %8, i64 0, i32 0
%12 = load i32, i32* %11
%13 = tail call i32 @strcmp(i8* %5, i32 %12)
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %7
15: ; preds = %10, %7
%16 = tail call i32 (...) @endttyent()
ret %struct.ttyent* %8
}