#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 */
typedef int /*<<< orphan*/ dev_t;

/* Variables and functions */
scalar_t__ asprintf(char **, char *, char const *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ makedev(unsigned int, unsigned int);
char *oneline(char *);
int sscanf(char *, char *, unsigned int *, unsigned int *);
dev_t devattr(const char *tty) {
  unsigned int maj, min;
  dev_t dev = 0;
  char *path, *value;
  if (!tty || *tty == '\0')
    goto err;
  if (asprintf(&path, "/sys/class/tty/%s/dev", tty) < 0)
    goto err;
  if ((value = oneline(path)) == (char *)0)
    goto out;
  if (sscanf(value, "%u:%u", &maj, &min) == 2)
    dev = makedev(maj, min);
  free(value);
out:
  free(path);
err:
  return dev;
}


x86 assembly (clang, -O0):
<devattr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0x14(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     117e <devattr+0x2e>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    1183 <devattr+0x33>
jmp    1203 <devattr+0xb3>
mov    -0x8(%rbp),%rdx
lea    -0x20(%rbp),%rdi
lea    0xe6e(%rip),%rsi
call   1060 <asprintf@plt>
cmp    $0x0,%rax
jge    11a6 <devattr+0x56>
jmp    1203 <devattr+0xb3>
mov    -0x20(%rbp),%rdi
call   1050 <oneline@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    11c2 <devattr+0x72>
jmp    11fa <devattr+0xaa>
mov    -0x28(%rbp),%rdi
lea    0xe49(%rip),%rsi
lea    -0xc(%rbp),%rdx
lea    -0x10(%rbp),%rcx
call   1040 <sscanf@plt>
cmp    $0x2,%eax
jne    11f1 <devattr+0xa1>
mov    -0xc(%rbp),%edi
mov    -0x10(%rbp),%esi
call   1070 <makedev@plt>
mov    %eax,-0x14(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x14(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<devattr>:
push   %r14
push   %rbx
sub    $0x18,%rsp
xor    %ebx,%ebx
test   %rdi,%rdi
je     11d3 <devattr+0x83>
mov    %rdi,%rdx
cmpb   $0x0,(%rdi)
je     11d3 <devattr+0x83>
lea    0xe93(%rip),%rsi
lea    0x10(%rsp),%rdi
call   1060 <asprintf@plt>
test   %rax,%rax
js     11d3 <devattr+0x83>
mov    0x10(%rsp),%rdi
call   1050 <oneline@plt>
test   %rax,%rax
je     11c7 <devattr+0x77>
mov    %rax,%r14
lea    0xe81(%rip),%rsi
lea    0xc(%rsp),%rdx
lea    0x8(%rsp),%rcx
mov    %rax,%rdi
call   1040 <sscanf@plt>
xor    %ebx,%ebx
cmp    $0x2,%eax
jne    11bd <devattr+0x6d>
mov    0xc(%rsp),%edi
mov    0x8(%rsp),%esi
call   1070 <makedev@plt>
mov    %eax,%ebx
mov    %r14,%rdi
call   1030 <free@plt>
jmp    11c9 <devattr+0x79>
xor    %ebx,%ebx
mov    0x10(%rsp),%rdi
call   1030 <free@plt>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [22 x i8] c"/sys/class/tty/%s/dev\00"
@.str.1 = [6 x i8] c"%u:%u\00"
define i32 @devattr(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
store i8* %0, i8** %2
store i32 0, i32* %5
%8 = load i8*, i8** %2
%9 = icmp ne i8* %8, null
br i1 %9, label %10, label %15
10: ; preds = %1
%11 = load i8*, i8** %2
%12 = load i8, i8* %11
%13 = sext i8 %12 to i32
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %10, %1
br label %40
16: ; preds = %10
%17 = load i8*, i8** %2
%18 = call i64 @asprintf(i8** %6, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %17)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %16
br label %40
21: ; preds = %16
%22 = load i8*, i8** %6
%23 = call i8* @oneline(i8* %22)
store i8* %23, i8** %7
%24 = icmp eq i8* %23, null
br i1 %24, label %25, label %26
25: ; preds = %21
br label %37
26: ; preds = %21
%27 = load i8*, i8** %7
%28 = call i32 @sscanf(i8* %27, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32* %3, i32* %4)
%29 = icmp eq i32 %28, 2
br i1 %29, label %30, label %34
30: ; preds = %26
%31 = load i32, i32* %3
%32 = load i32, i32* %4
%33 = call i32 @makedev(i32 %31, i32 %32)
store i32 %33, i32* %5
br label %34
34: ; preds = %30, %26
%35 = load i8*, i8** %7
%36 = call i32 @free(i8* %35)
br label %37
37: ; preds = %34, %25
%38 = load i8*, i8** %6
%39 = call i32 @free(i8* %38)
br label %40
40: ; preds = %37, %20, %15
%41 = load i32, i32* %5
ret i32 %41
}

LLVM IR (-O2):
@.str = [22 x i8] c"/sys/class/tty/%s/dev\00"
@.str.1 = [6 x i8] c"%u:%u\00"
define i32 @devattr(i8* %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
%5 = bitcast i32* %2 to i8*
%6 = bitcast i32* %3 to i8*
%7 = bitcast i8** %4 to i8*
%8 = icmp eq i8* %0, null
br i1 %8, label %33, label %9
9: ; preds = %1
%10 = load i8, i8* %0
%11 = icmp eq i8 %10, 0
br i1 %11, label %33, label %12
12: ; preds = %9
%13 = call i64 @asprintf(i8** %4, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %0)
%14 = icmp slt i64 %13, 0
br i1 %14, label %33, label %15
15: ; preds = %12
%16 = load i8*, i8** %4
%17 = call i8* @oneline(i8* %16)
%18 = icmp eq i8* %17, null
br i1 %18, label %29, label %19
19: ; preds = %15
%20 = call i32 @sscanf(i8* %17, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32* %2, i32* %3)
%21 = icmp eq i32 %20, 2
br i1 %21, label %22, label %26
22: ; preds = %19
%23 = load i32, i32* %2
%24 = load i32, i32* %3
%25 = call i32 @makedev(i32 %23, i32 %24)
br label %26
26: ; preds = %22, %19
%27 = phi i32 [ %25, %22 ], [ 0, %19 ]
%28 = call i32 @free(i8* %17)
br label %29
29: ; preds = %15, %26
%30 = phi i32 [ 0, %15 ], [ %27, %26 ]
%31 = load i8*, i8** %4
%32 = call i32 @free(i8* %31)
br label %33
33: ; preds = %12, %1, %9, %29
%34 = phi i32 [ 0, %9 ], [ 0, %12 ], [ %30, %29 ], [ 0, %1 ]
ret i32 %34
}