#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 stat {
  int st_rdev;
};

/* Variables and functions */
int /*<<< orphan*/ snprintf(char *, int, char *, char const *const);
scalar_t__ stat(char const *const, struct stat *);
int tty_to_dev(const char *restrict const name) {
  struct stat sbuf;
  static char buf[32];
  if (name[0] == '/' && stat(name, &sbuf) >= 0)
    return sbuf.st_rdev;
  snprintf(buf, 32, "/dev/%s", name);
  if (stat(buf, &sbuf) >= 0)
    return sbuf.st_rdev;
  snprintf(buf, 32, "/dev/tty%s", name);
  if (stat(buf, &sbuf) >= 0)
    return sbuf.st_rdev;
  snprintf(buf, 32, "/dev/pts/%s", name);
  if (stat(buf, &sbuf) >= 0)
    return sbuf.st_rdev;
  return -1;
}


x86 assembly (clang, -O0):
<tty_to_dev>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    115e <tty_to_dev+0x3e>
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1040 <stat@plt>
cmp    $0x0,%rax
jl     115e <tty_to_dev+0x3e>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1228 <tty_to_dev+0x108>
mov    -0x10(%rbp),%rcx
lea    0x2ed7(%rip),%rdi
mov    $0x20,%esi
lea    0xe8b(%rip),%rdx
call   1030 <snprintf@plt>
lea    0x2ebf(%rip),%rdi
lea    -0x18(%rbp),%rsi
call   1040 <stat@plt>
cmp    $0x0,%rax
jl     119f <tty_to_dev+0x7f>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1228 <tty_to_dev+0x108>
mov    -0x10(%rbp),%rcx
lea    0x2e96(%rip),%rdi
mov    $0x20,%esi
lea    0xe52(%rip),%rdx
call   1030 <snprintf@plt>
lea    0x2e7e(%rip),%rdi
lea    -0x18(%rbp),%rsi
call   1040 <stat@plt>
cmp    $0x0,%rax
jl     11e0 <tty_to_dev+0xc0>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1228 <tty_to_dev+0x108>
mov    -0x10(%rbp),%rcx
lea    0x2e55(%rip),%rdi
mov    $0x20,%esi
lea    0xe1c(%rip),%rdx
call   1030 <snprintf@plt>
lea    0x2e3d(%rip),%rdi
lea    -0x18(%rbp),%rsi
call   1040 <stat@plt>
cmp    $0x0,%rax
jl     1221 <tty_to_dev+0x101>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1228 <tty_to_dev+0x108>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tty_to_dev>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
cmpb   $0x2f,(%rdi)
jne    1140 <tty_to_dev+0x20>
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1040 <stat@plt>
test   %rax,%rax
jns    11ca <tty_to_dev+0xaa>
lea    0x2ef9(%rip),%r14
lea    0xeb2(%rip),%rdx
mov    %r14,%rdi
mov    $0x20,%esi
mov    %rbx,%rcx
call   1030 <snprintf@plt>
mov    %rsp,%rsi
mov    %r14,%rdi
call   1040 <stat@plt>
test   %rax,%rax
jns    11ca <tty_to_dev+0xaa>
lea    0x2ecb(%rip),%r14
lea    0xe8c(%rip),%rdx
mov    %r14,%rdi
mov    $0x20,%esi
mov    %rbx,%rcx
call   1030 <snprintf@plt>
mov    %rsp,%rsi
mov    %r14,%rdi
call   1040 <stat@plt>
test   %rax,%rax
jns    11ca <tty_to_dev+0xaa>
lea    0x2e9d(%rip),%r14
lea    0xe69(%rip),%rdx
mov    %r14,%rdi
mov    $0x20,%esi
mov    %rbx,%rcx
call   1030 <snprintf@plt>
mov    %rsp,%rsi
mov    %r14,%rdi
call   1040 <stat@plt>
test   %rax,%rax
js     11d5 <tty_to_dev+0xb5>
mov    (%rsp),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    $0xffffffff,%eax
jmp    11cd <tty_to_dev+0xad>

LLVM IR (-O0):
%struct.stat = type { i32 }
@tty_to_dev.buf = internal global [32 x i8] zeroinitializer
@.str = [8 x i8] c"/dev/%s\00"
@.str.1 = [11 x i8] c"/dev/tty%s\00"
@.str.2 = [12 x i8] c"/dev/pts/%s\00"
define i32 @tty_to_dev(i8* noalias %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.stat
store i8* %0, i8** %3
%5 = load i8*, i8** %3
%6 = getelementptr inbounds i8, i8* %5, i64 0
%7 = load i8, i8* %6
%8 = sext i8 %7 to i32
%9 = icmp eq i32 %8, 47
br i1 %9, label %10, label %17
10: ; preds = %1
%11 = load i8*, i8** %3
%12 = call i64 @stat(i8* %11, %struct.stat* %4)
%13 = icmp sge i64 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %10
%15 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%16 = load i32, i32* %15
store i32 %16, i32* %2
br label %42
17: ; preds = %10, %1
%18 = load i8*, i8** %3
%19 = call i32 @snprintf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @tty_to_dev.buf, i64 0, i64 0), i32 32, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %18)
%20 = call i64 @stat(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @tty_to_dev.buf, i64 0, i64 0), %struct.stat* %4)
%21 = icmp sge i64 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %17
%23 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%24 = load i32, i32* %23
store i32 %24, i32* %2
br label %42
25: ; preds = %17
%26 = load i8*, i8** %3
%27 = call i32 @snprintf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @tty_to_dev.buf, i64 0, i64 0), i32 32, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* %26)
%28 = call i64 @stat(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @tty_to_dev.buf, i64 0, i64 0), %struct.stat* %4)
%29 = icmp sge i64 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %25
%31 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%32 = load i32, i32* %31
store i32 %32, i32* %2
br label %42
33: ; preds = %25
%34 = load i8*, i8** %3
%35 = call i32 @snprintf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @tty_to_dev.buf, i64 0, i64 0), i32 32, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8* %34)
%36 = call i64 @stat(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @tty_to_dev.buf, i64 0, i64 0), %struct.stat* %4)
%37 = icmp sge i64 %36, 0
br i1 %37, label %38, label %41
38: ; preds = %33
%39 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%40 = load i32, i32* %39
store i32 %40, i32* %2
br label %42
41: ; preds = %33
store i32 -1, i32* %2
br label %42
42: ; preds = %41, %38, %30, %22, %14
%43 = load i32, i32* %2
ret i32 %43
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@tty_to_dev.buf = internal global [32 x i8] zeroinitializer
@.str = [8 x i8] c"/dev/%s\00"
@.str.1 = [11 x i8] c"/dev/tty%s\00"
@.str.2 = [12 x i8] c"/dev/pts/%s\00"
define i32 @tty_to_dev(i8* noalias %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = load i8, i8* %0
%5 = icmp eq i8 %4, 47
br i1 %5, label %6, label %12
6: ; preds = %1
%7 = call i64 @stat(i8* %0, %struct.stat* %2)
%8 = icmp sgt i64 %7, -1
br i1 %8, label %9, label %12
9: ; preds = %6
%10 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%11 = load i32, i32* %10
br label %33
12: ; preds = %6, %1
%13 = call i32 @snprintf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @tty_to_dev.buf, i64 0, i64 0), i32 32, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %0)
%14 = call i64 @stat(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @tty_to_dev.buf, i64 0, i64 0), %struct.stat* %2)
%15 = icmp sgt i64 %14, -1
br i1 %15, label %16, label %19
16: ; preds = %12
%17 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%18 = load i32, i32* %17
br label %33
19: ; preds = %12
%20 = call i32 @snprintf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @tty_to_dev.buf, i64 0, i64 0), i32 32, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%21 = call i64 @stat(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @tty_to_dev.buf, i64 0, i64 0), %struct.stat* %2)
%22 = icmp sgt i64 %21, -1
br i1 %22, label %23, label %26
23: ; preds = %19
%24 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%25 = load i32, i32* %24
br label %33
26: ; preds = %19
%27 = call i32 @snprintf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @tty_to_dev.buf, i64 0, i64 0), i32 32, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8* %0)
%28 = call i64 @stat(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @tty_to_dev.buf, i64 0, i64 0), %struct.stat* %2)
%29 = icmp sgt i64 %28, -1
br i1 %29, label %30, label %33
30: ; preds = %26
%31 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%32 = load i32, i32* %31
br label %33
33: ; preds = %26, %30, %23, %16, %9
%34 = phi i32 [ %11, %9 ], [ %18, %16 ], [ %25, %23 ], [ %32, %30 ], [ -1, %26 ]
ret i32 %34
}