#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 */
int TTY_ON;
int /*<<< orphan*/ free(char *);
char *malloc(size_t);
char *strchr(char *, char);
scalar_t__ strcmp(char *, char const *);
scalar_t__ strlen(char *);
char *strsep(char **, char *);
int sysctlbyname(char *, char *, size_t *, int /*<<< orphan*/ *,
                 int /*<<< orphan*/);
int auto_tty_status(const char *ty_name) {
  size_t len;
  char *buf, *cons, *nextcons;
  buf = NULL;
  if (sysctlbyname("kern.console", NULL, &len, NULL, 0) == -1)
    return (0);
  buf = malloc(len);
  if (sysctlbyname("kern.console", buf, &len, NULL, 0) == -1)
    goto done;
  if ((cons = strchr(buf, '/')) == NULL)
    goto done;
  *cons = '\0';
  nextcons = buf;
  while ((cons = strsep(&nextcons, ",")) != NULL && strlen(cons) != 0) {
    if (strcmp(cons, ty_name) == 0) {
      free(buf);
      return (TTY_ON);
    }
  }
done:
  free(buf);
  return (0);
}


x86 assembly (clang, -O0):
<auto_tty_status>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x20(%rbp)
lea    0xe75(%rip),%rdi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x18(%rbp),%rdx
xor    %r8d,%r8d
mov    %rcx,%rsi
call   1090 <sysctlbyname@plt>
cmp    $0xffffffff,%eax
jne    11b3 <auto_tty_status+0x43>
movl   $0x0,-0x4(%rbp)
jmp    12ad <auto_tty_status+0x13d>
mov    -0x18(%rbp),%rdi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rsi
lea    0xe35(%rip),%rdi
lea    -0x18(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%ecx
xor    %r8d,%r8d
call   1090 <sysctlbyname@plt>
cmp    $0xffffffff,%eax
jne    11e9 <auto_tty_status+0x79>
jmp    129d <auto_tty_status+0x12d>
mov    -0x20(%rbp),%rdi
mov    $0x2f,%esi
call   1050 <strchr@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    120a <auto_tty_status+0x9a>
jmp    129d <auto_tty_status+0x12d>
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
lea    -0x30(%rbp),%rdi
lea    0xde9(%rip),%rsi
call   1080 <strsep@plt>
mov    %rax,%rcx
mov    %rcx,-0x28(%rbp)
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x31(%rbp)
je     1252 <auto_tty_status+0xe2>
mov    -0x28(%rbp),%rdi
call   1040 <strlen@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,-0x31(%rbp)
mov    -0x31(%rbp),%al
test   $0x1,%al
jne    1262 <auto_tty_status+0xf2>
jmp    1298 <auto_tty_status+0x128>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    1293 <auto_tty_status+0x123>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    0x2d5f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12ad <auto_tty_status+0x13d>
jmp    1219 <auto_tty_status+0xa9>
jmp    129d <auto_tty_status+0x12d>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<auto_tty_status>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
lea    0xe89(%rip),%rdi
xor    %r14d,%r14d
mov    %rsp,%rdx
xor    %esi,%esi
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1080 <sysctlbyname@plt>
cmp    $0xffffffff,%eax
je     1231 <auto_tty_status+0xd1>
mov    (%rsp),%rdi
call   1060 <malloc@plt>
mov    %rax,%r15
lea    0xe5b(%rip),%rdi
xor    %r14d,%r14d
mov    %rsp,%rdx
mov    %rax,%rsi
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1080 <sysctlbyname@plt>
cmp    $0xffffffff,%eax
je     1229 <auto_tty_status+0xc9>
mov    %r15,%rdi
mov    $0x2f,%esi
call   1040 <strchr@plt>
test   %rax,%rax
je     1229 <auto_tty_status+0xc9>
movb   $0x0,(%rax)
mov    %r15,0x8(%rsp)
lea    0xe2f(%rip),%r12
lea    0x8(%rsp),%r13
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r13,%rdi
mov    %r12,%rsi
call   1070 <strsep@plt>
test   %rax,%rax
je     1229 <auto_tty_status+0xc9>
cmpb   $0x0,(%rax)
je     1229 <auto_tty_status+0xc9>
mov    %rax,%rdi
mov    %rbx,%rsi
call   1050 <strcmp@plt>
test   %rax,%rax
jne    11f0 <auto_tty_status+0x90>
mov    %r15,%rdi
call   1030 <free@plt>
mov    0x2dc4(%rip),%rax
mov    (%rax),%r14d
jmp    1231 <auto_tty_status+0xd1>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [13 x i8] c"kern.console\00"
@.str.1 = [2 x i8] c",\00"
@TTY_ON = global i32 0
define i32 @auto_tty_status(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
store i8* %0, i8** %3
store i8* null, i8** %5
%8 = call i32 @sysctlbyname(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* null, i64* %4, i32* null, i32 0)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %11
10: ; preds = %1
store i32 0, i32* %2
br label %49
11: ; preds = %1
%12 = load i64, i64* %4
%13 = call i8* @malloc(i64 %12)
store i8* %13, i8** %5
%14 = load i8*, i8** %5
%15 = call i32 @sysctlbyname(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %14, i64* %4, i32* null, i32 0)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %18
17: ; preds = %11
br label %46
18: ; preds = %11
%19 = load i8*, i8** %5
%20 = call i8* @strchr(i8* %19, i8 signext 47)
store i8* %20, i8** %6
%21 = icmp eq i8* %20, null
br i1 %21, label %22, label %23
22: ; preds = %18
br label %46
23: ; preds = %18
%24 = load i8*, i8** %6
store i8 0, i8* %24
%25 = load i8*, i8** %5
store i8* %25, i8** %7
br label %26
26: ; preds = %44, %23
%27 = call i8* @strsep(i8** %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i8* %27, i8** %6
%28 = icmp ne i8* %27, null
br i1 %28, label %29, label %33
29: ; preds = %26
%30 = load i8*, i8** %6
%31 = call i64 @strlen(i8* %30)
%32 = icmp ne i64 %31, 0
br label %33
33: ; preds = %29, %26
%34 = phi i1 [ false, %26 ], [ %32, %29 ]
br i1 %34, label %35, label %45
35: ; preds = %33
%36 = load i8*, i8** %6
%37 = load i8*, i8** %3
%38 = call i64 @strcmp(i8* %36, i8* %37)
%39 = icmp eq i64 %38, 0
br i1 %39, label %40, label %44
40: ; preds = %35
%41 = load i8*, i8** %5
%42 = call i32 @free(i8* %41)
%43 = load i32, i32* @TTY_ON
store i32 %43, i32* %2
br label %49
44: ; preds = %35
br label %26
45: ; preds = %33
br label %46
46: ; preds = %45, %22, %17
%47 = load i8*, i8** %5
%48 = call i32 @free(i8* %47)
store i32 0, i32* %2
br label %49
49: ; preds = %46, %40, %10
%50 = load i32, i32* %2
ret i32 %50
}

LLVM IR (-O2):
@.str = [13 x i8] c"kern.console\00"
@.str.1 = [2 x i8] c",\00"
@TTY_ON = global i32 0
define i32 @auto_tty_status(i8* %0) {
%2 = alloca i64
%3 = alloca i8*
%4 = bitcast i64* %2 to i8*
%5 = bitcast i8** %3 to i8*
%6 = call i32 @sysctlbyname(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* null, i64* %2, i32* null, i32 0)
%7 = icmp eq i32 %6, -1
br i1 %7, label %31, label %8
8: ; preds = %1
%9 = load i64, i64* %2
%10 = call i8* @malloc(i64 %9)
%11 = call i32 @sysctlbyname(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %10, i64* %2, i32* null, i32 0)
%12 = icmp eq i32 %11, -1
br i1 %12, label %29, label %13
13: ; preds = %8
%14 = call i8* @strchr(i8* dereferenceable(1) %10, i8 signext 47)
%15 = icmp eq i8* %14, null
br i1 %15, label %29, label %16
16: ; preds = %13
store i8 0, i8* %14
store i8* %10, i8** %3
br label %17
17: ; preds = %23, %16
%18 = call i8* @strsep(i8** %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%19 = icmp eq i8* %18, null
br i1 %19, label %29, label %20
20: ; preds = %17
%21 = load i8, i8* %18
%22 = icmp eq i8 %21, 0
br i1 %22, label %29, label %23
23: ; preds = %20
%24 = call i64 @strcmp(i8* %18, i8* %0)
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %17
26: ; preds = %23
%27 = call i32 @free(i8* %10)
%28 = load i32, i32* @TTY_ON
br label %31
29: ; preds = %20, %17, %13, %8
%30 = call i32 @free(i8* %10)
br label %31
31: ; preds = %1, %29, %26
%32 = phi i32 [ 0, %29 ], [ %28, %26 ], [ 0, %1 ]
ret i32 %32
}