#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*/ ttybuf;
struct stat {
  int dummy;
};

/* Variables and functions */
int MAXPATHLEN;
char *_PATH_DEV;
int /*<<< orphan*/ snprintf(char *, int, char *, char *, int, char *);
scalar_t__ stat(char *, struct stat *);
int ttystat(char *line, int sz) {
  static struct stat sb;
  char ttybuf[MAXPATHLEN];
  (void)snprintf(ttybuf, sizeof(ttybuf), "%s%.*s", _PATH_DEV, sz, line);
  if (stat(ttybuf, &sb) == 0) {
    return 1;
  } else {
    return 0;
  }
}


x86 assembly (clang, -O0):
<ttystat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    0x2eaa(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x38(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x28(%rbp)
mov    %eax,%esi
mov    0x2e84(%rip),%rax
mov    (%rax),%rcx
mov    -0x14(%rbp),%r8d
mov    -0x10(%rbp),%r9
lea    0xe8a(%rip),%rdx
call   1030 <snprintf@plt>
mov    -0x38(%rbp),%rdi
lea    0x2eb2(%rip),%rsi
call   1040 <stat@plt>
cmp    $0x0,%rax
jne    11a8 <ttystat+0x88>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
jmp    11b6 <ttystat+0x96>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ttystat>:
push   %rbp
mov    %rsp,%rbp
push   %rbx
push   %rax
mov    %esi,%r8d
mov    %rdi,%r9
mov    0x2ead(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rbx
mov    %rsi,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    0x2e98(%rip),%rax
mov    (%rax),%rcx
lea    0xea6(%rip),%rdx
mov    %rbx,%rdi
call   1030 <snprintf@plt>
lea    0x2ecf(%rip),%rsi
mov    %rbx,%rdi
call   1040 <stat@plt>
xor    %ecx,%ecx
test   %rax,%rax
sete   %cl
mov    %ecx,%eax
lea    -0x8(%rbp),%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@ttystat.sb = internal global %struct.stat zeroinitializer
@MAXPATHLEN = global i32 0
@.str = [7 x i8] c"%s%.*s\00"
@_PATH_DEV = global i8* null
define i32 @ttystat(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%9 = load i32, i32* @MAXPATHLEN
%10 = zext i32 %9 to i64
%11 = call i8* @llvm.stacksave()
store i8* %11, i8** %6
%12 = alloca i8, i64 %10
store i64 %10, i64* %7
%13 = trunc i64 %10 to i32
%14 = load i8*, i8** @_PATH_DEV
%15 = load i32, i32* %5
%16 = load i8*, i8** %4
%17 = call i32 @snprintf(i8* %12, i32 %13, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* %14, i32 %15, i8* %16)
%18 = call i64 @stat(i8* %12, %struct.stat* @ttystat.sb)
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %2
store i32 1, i32* %3
store i32 1, i32* %8
br label %22
21: ; preds = %2
store i32 0, i32* %3
store i32 1, i32* %8
br label %22
22: ; preds = %21, %20
%23 = load i8*, i8** %6
%24 = load i32, i32* %3
ret i32 %24
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@ttystat.sb = internal global %struct.stat zeroinitializer
@MAXPATHLEN = global i32 0
@.str = [7 x i8] c"%s%.*s\00"
@_PATH_DEV = global i8* null
define i32 @ttystat(i8* %0, i32 %1) {
%3 = load i32, i32* @MAXPATHLEN
%4 = zext i32 %3 to i64
%5 = alloca i8, i64 %4
%6 = load i8*, i8** @_PATH_DEV
%7 = call i32 @snprintf(i8* %5, i32 %3, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* %6, i32 %1, i8* %0)
%8 = call i64 @stat(i8* %5, %struct.stat* @ttystat.sb)
%9 = icmp eq i64 %8, 0
%10 = zext i1 %9 to i32
ret i32 %10
}