#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 */

/* Variables and functions */
scalar_t__ check_ttyname(char *);
scalar_t__ strncmp(char *, char *, int);
int ttyname_r(int, char *, int);
const char *_get_tty(char *tname, int size) {
  int rc, i, found = 0;
  for (i = 0; i < 3 && !found; i++) {
    rc = ttyname_r(i, tname, size);
    if (rc == 0 && tname[0] != '\0')
      found = 1;
  }
  if (!found)
    return NULL;
  if (check_ttyname(tname))
    return NULL;
  if (strncmp(tname, "/dev/", 5) == 0)
    return &tname[5];
  return tname;
}


x86 assembly (clang, -O0):
<_get_tty>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0x0,-0x20(%rbp)
movl   $0x0,-0x1c(%rbp)
xor    %eax,%eax
cmpl   $0x3,-0x1c(%rbp)
mov    %al,-0x21(%rbp)
jge    1168 <_get_tty+0x38>
cmpl   $0x0,-0x20(%rbp)
setne  %al
xor    $0xff,%al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    1178 <_get_tty+0x48>
jmp    11be <_get_tty+0x8e>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1050 <ttyname_r@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jne    11ab <_get_tty+0x7b>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11ab <_get_tty+0x7b>
movl   $0x1,-0x20(%rbp)
jmp    11b0 <_get_tty+0x80>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    114d <_get_tty+0x1d>
cmpl   $0x0,-0x20(%rbp)
jne    11d5 <_get_tty+0xa5>
movq   $0x0,-0x8(%rbp)
jmp    122d <_get_tty+0xfd>
mov    -0x10(%rbp),%rdi
call   1040 <check_ttyname@plt>
cmp    $0x0,%rax
je     11f5 <_get_tty+0xc5>
movq   $0x0,-0x8(%rbp)
jmp    122d <_get_tty+0xfd>
mov    -0x10(%rbp),%rdi
lea    0xe00(%rip),%rsi
mov    $0x5,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    1225 <_get_tty+0xf5>
mov    -0x10(%rbp),%rax
add    $0x5,%rax
mov    %rax,-0x8(%rbp)
jmp    122d <_get_tty+0xfd>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_get_tty>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%ebp
mov    %rdi,%r14
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
mov    %ebx,%edi
mov    %r14,%rsi
mov    %ebp,%edx
call   1050 <ttyname_r@plt>
mov    $0x0,%ecx
test   %eax,%eax
jne    115e <_get_tty+0x2e>
xor    %ecx,%ecx
cmpb   $0x0,(%r14)
setne  %cl
cmp    $0x1,%ebx
ja     116a <_get_tty+0x3a>
add    $0x1,%ebx
test   %ecx,%ecx
je     1140 <_get_tty+0x10>
test   %ecx,%ecx
je     117b <_get_tty+0x4b>
mov    %r14,%rdi
call   1040 <check_ttyname@plt>
test   %rax,%rax
je     117f <_get_tty+0x4f>
xor    %eax,%eax
jmp    11a2 <_get_tty+0x72>
lea    0xe7a(%rip),%rsi
mov    %r14,%rdi
mov    $0x5,%edx
call   1030 <strncmp@plt>
xor    %ecx,%ecx
test   %rax,%rax
sete   %cl
lea    (%rcx,%rcx,4),%rax
add    %r14,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [6 x i8] c"/dev/\00"
define i8* @_get_tty(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 0, i32* %8
store i32 0, i32* %7
br label %9
9: ; preds = %33, %2
%10 = load i32, i32* %7
%11 = icmp slt i32 %10, 3
br i1 %11, label %12, label %16
12: ; preds = %9
%13 = load i32, i32* %8
%14 = icmp ne i32 %13, 0
%15 = xor i1 %14, true
br label %16
16: ; preds = %12, %9
%17 = phi i1 [ false, %9 ], [ %15, %12 ]
br i1 %17, label %18, label %36
18: ; preds = %16
%19 = load i32, i32* %7
%20 = load i8*, i8** %4
%21 = load i32, i32* %5
%22 = call i32 @ttyname_r(i32 %19, i8* %20, i32 %21)
store i32 %22, i32* %6
%23 = load i32, i32* %6
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %32
25: ; preds = %18
%26 = load i8*, i8** %4
%27 = getelementptr inbounds i8, i8* %26, i64 0
%28 = load i8, i8* %27
%29 = sext i8 %28 to i32
%30 = icmp ne i32 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %25
store i32 1, i32* %8
br label %32
32: ; preds = %31, %25, %18
br label %33
33: ; preds = %32
%34 = load i32, i32* %7
%35 = add nsw i32 %34, 1
store i32 %35, i32* %7
br label %9
36: ; preds = %16
%37 = load i32, i32* %8
%38 = icmp ne i32 %37, 0
br i1 %38, label %40, label %39
39: ; preds = %36
store i8* null, i8** %3
br label %54
40: ; preds = %36
%41 = load i8*, i8** %4
%42 = call i64 @check_ttyname(i8* %41)
%43 = icmp ne i64 %42, 0
br i1 %43, label %44, label %45
44: ; preds = %40
store i8* null, i8** %3
br label %54
45: ; preds = %40
%46 = load i8*, i8** %4
%47 = call i64 @strncmp(i8* %46, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 5)
%48 = icmp eq i64 %47, 0
br i1 %48, label %49, label %52
49: ; preds = %45
%50 = load i8*, i8** %4
%51 = getelementptr inbounds i8, i8* %50, i64 5
store i8* %51, i8** %3
br label %54
52: ; preds = %45
%53 = load i8*, i8** %4
store i8* %53, i8** %3
br label %54
54: ; preds = %52, %49, %44, %39
%55 = load i8*, i8** %3
ret i8* %55
}

LLVM IR (-O2):
@.str = [6 x i8] c"/dev/\00"
define i8* @_get_tty(i8* %0, i32 %1) {
br label %3
3: ; preds = %2, %11
%4 = phi i32 [ 0, %2 ], [ %13, %11 ]
%5 = tail call i32 @ttyname_r(i32 %4, i8* %0, i32 %1)
%6 = icmp eq i32 %5, 0
br i1 %6, label %7, label %11
7: ; preds = %3
%8 = load i8, i8* %0
%9 = icmp ne i8 %8, 0
%10 = zext i1 %9 to i32
br label %11
11: ; preds = %7, %3
%12 = phi i32 [ 0, %3 ], [ %10, %7 ]
%13 = add nuw nsw i32 %4, 1
%14 = icmp ult i32 %4, 2
%15 = icmp eq i32 %12, 0
%16 = select i1 %14, i1 %15, i1 false
br i1 %16, label %3, label %17
17: ; preds = %11
br i1 %15, label %26, label %18
18: ; preds = %17
%19 = tail call i64 @check_ttyname(i8* %0)
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %26
21: ; preds = %18
%22 = tail call i64 @strncmp(i8* %0, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 5)
%23 = icmp eq i64 %22, 0
%24 = select i1 %23, i64 5, i64 0
%25 = getelementptr i8, i8* %0, i64 %24
br label %26
26: ; preds = %21, %18, %17
%27 = phi i8* [ null, %17 ], [ null, %18 ], [ %25, %21 ]
ret i8* %27
}