#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 *);
scalar_t__ stat(char *, struct stat *);
int tty_to_dev(char *name) {
  struct stat sbuf;
  static char buf[32];
  if (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),%rdi
lea    -0x18(%rbp),%rsi
call   1040 <stat@plt>
cmp    $0x0,%rax
jl     114e <tty_to_dev+0x2e>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1218 <tty_to_dev+0xf8>
mov    -0x10(%rbp),%rcx
lea    0x2ee7(%rip),%rdi
mov    $0x20,%esi
lea    0xe9b(%rip),%rdx
call   1030 <snprintf@plt>
lea    0x2ecf(%rip),%rdi
lea    -0x18(%rbp),%rsi
call   1040 <stat@plt>
cmp    $0x0,%rax
jl     118f <tty_to_dev+0x6f>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1218 <tty_to_dev+0xf8>
mov    -0x10(%rbp),%rcx
lea    0x2ea6(%rip),%rdi
mov    $0x20,%esi
lea    0xe62(%rip),%rdx
call   1030 <snprintf@plt>
lea    0x2e8e(%rip),%rdi
lea    -0x18(%rbp),%rsi
call   1040 <stat@plt>
cmp    $0x0,%rax
jl     11d0 <tty_to_dev+0xb0>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1218 <tty_to_dev+0xf8>
mov    -0x10(%rbp),%rcx
lea    0x2e65(%rip),%rdi
mov    $0x20,%esi
lea    0xe2c(%rip),%rdx
call   1030 <snprintf@plt>
lea    0x2e4d(%rip),%rdi
lea    -0x18(%rbp),%rsi
call   1040 <stat@plt>
cmp    $0x0,%rax
jl     1211 <tty_to_dev+0xf1>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1218 <tty_to_dev+0xf8>
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
mov    %rsp,%rsi
call   1040 <stat@plt>
test   %rax,%rax
jns    11c2 <tty_to_dev+0xa2>
lea    0x2f01(%rip),%r14
lea    0xeba(%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    11c2 <tty_to_dev+0xa2>
lea    0x2ed3(%rip),%r14
lea    0xe94(%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    11c2 <tty_to_dev+0xa2>
lea    0x2ea5(%rip),%r14
lea    0xe71(%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     11cd <tty_to_dev+0xad>
mov    (%rsp),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    $0xffffffff,%eax
jmp    11c5 <tty_to_dev+0xa5>

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* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.stat
store i8* %0, i8** %3
%5 = load i8*, i8** %3
%6 = call i64 @stat(i8* %5, %struct.stat* %4)
%7 = icmp sge i64 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %1
%9 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%10 = load i32, i32* %9
store i32 %10, i32* %2
br label %36
11: ; preds = %1
%12 = load i8*, i8** %3
%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* %12)
%14 = call i64 @stat(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @tty_to_dev.buf, i64 0, i64 0), %struct.stat* %4)
%15 = icmp sge i64 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %11
%17 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%18 = load i32, i32* %17
store i32 %18, i32* %2
br label %36
19: ; preds = %11
%20 = load i8*, i8** %3
%21 = 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* %20)
%22 = call i64 @stat(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @tty_to_dev.buf, i64 0, i64 0), %struct.stat* %4)
%23 = icmp sge i64 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %19
%25 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%26 = load i32, i32* %25
store i32 %26, i32* %2
br label %36
27: ; preds = %19
%28 = load i8*, i8** %3
%29 = 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* %28)
%30 = call i64 @stat(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @tty_to_dev.buf, i64 0, i64 0), %struct.stat* %4)
%31 = icmp sge i64 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %27
%33 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%34 = load i32, i32* %33
store i32 %34, i32* %2
br label %36
35: ; preds = %27
store i32 -1, i32* %2
br label %36
36: ; preds = %35, %32, %24, %16, %8
%37 = load i32, i32* %2
ret i32 %37
}

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* %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = call i64 @stat(i8* %0, %struct.stat* %2)
%5 = icmp sgt i64 %4, -1
br i1 %5, label %6, label %9
6: ; preds = %1
%7 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%8 = load i32, i32* %7
br label %30
9: ; preds = %1
%10 = 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)
%11 = call i64 @stat(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @tty_to_dev.buf, i64 0, i64 0), %struct.stat* %2)
%12 = icmp sgt i64 %11, -1
br i1 %12, label %13, label %16
13: ; preds = %9
%14 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%15 = load i32, i32* %14
br label %30
16: ; preds = %9
%17 = 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)
%18 = call i64 @stat(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @tty_to_dev.buf, i64 0, i64 0), %struct.stat* %2)
%19 = icmp sgt i64 %18, -1
br i1 %19, label %20, label %23
20: ; preds = %16
%21 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%22 = load i32, i32* %21
br label %30
23: ; preds = %16
%24 = 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)
%25 = call i64 @stat(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @tty_to_dev.buf, i64 0, i64 0), %struct.stat* %2)
%26 = icmp sgt i64 %25, -1
br i1 %26, label %27, label %30
27: ; preds = %23
%28 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%29 = load i32, i32* %28
br label %30
30: ; preds = %23, %27, %20, %13, %6
%31 = phi i32 [ %8, %6 ], [ %15, %13 ], [ %22, %20 ], [ %29, %27 ], [ -1, %23 ]
ret i32 %31
}