#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 /*<<< orphan*/ st_mode;
};
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int PATH_MAX;
int /*<<< orphan*/ S_ISCHR(int /*<<< orphan*/);
int /*<<< orphan*/ assert(int /*<<< orphan*/);
char *create_string(char const *);
int snprintf(char *, int, char *, char const *, char const *);
scalar_t__ stat(char const *, struct stat *);
int is_serial_dev(const char *dev, const char *cwd, char **path_ref) {
  char buf[PATH_MAX];
  int n;
  struct stat st;
  assert(dev != NULL);
  if ((dev[0] != '/') && (cwd != NULL)) {
    n = snprintf(buf, sizeof(buf), "%s/%s", cwd, dev);
    if ((n < 0) || (n >= sizeof(buf))) {
      return (0);
    }
    dev = buf;
  }
  if (stat(dev, &st) < 0) {
    return (0);
  }
  if (!S_ISCHR(st.st_mode)) {
    return (0);
  }
  if (path_ref) {
    *path_ref = create_string(dev);
  }
  return (1);
}


x86 assembly (clang, -O0):
<is_serial_dev>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    0x2e85(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x50(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x10(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
je     1218 <is_serial_dev+0xc8>
cmpq   $0x0,-0x18(%rbp)
je     1218 <is_serial_dev+0xc8>
mov    -0x48(%rbp),%rdi
mov    -0x50(%rbp),%rax
mov    %eax,%esi
mov    -0x18(%rbp),%rcx
mov    -0x10(%rbp),%r8
lea    0xe26(%rip),%rdx
call   1030 <snprintf@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jl     11fd <is_serial_dev+0xad>
mov    -0x50(%rbp),%rcx
movslq -0x34(%rbp),%rax
cmp    %rcx,%rax
jb     1210 <is_serial_dev+0xc0>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    1292 <is_serial_dev+0x142>
mov    -0x48(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x38(%rbp),%rsi
call   1060 <stat@plt>
cmp    $0x0,%rax
jge    1242 <is_serial_dev+0xf2>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    1292 <is_serial_dev+0x142>
mov    -0x38(%rbp),%edi
call   1070 <S_ISCHR@plt>
cmp    $0x0,%eax
jne    1266 <is_serial_dev+0x116>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    1292 <is_serial_dev+0x142>
cmpq   $0x0,-0x20(%rbp)
je     1284 <is_serial_dev+0x134>
mov    -0x10(%rbp),%rdi
call   1050 <create_string@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<is_serial_dev>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r13
mov    %rdi,%r15
mov    0x2e82(%rip),%rax
mov    (%rax),%r12d
mov    %rsp,%rbx
lea    0xf(%r12),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
xor    %edi,%edi
test   %r15,%r15
setne  %dil
call   1040 <assert@plt>
test   %r13,%r13
je     11c4 <is_serial_dev+0x74>
cmpb   $0x2f,(%r15)
je     11c4 <is_serial_dev+0x74>
lea    0xe5d(%rip),%rdx
mov    %rbx,%rdi
mov    %r12d,%esi
mov    %r13,%rcx
mov    %r15,%r8
call   1030 <snprintf@plt>
xor    %r15d,%r15d
test   %eax,%eax
js     11fd <is_serial_dev+0xad>
cltq
cmp    %r12,%rax
jb     11c7 <is_serial_dev+0x77>
jmp    11fd <is_serial_dev+0xad>
mov    %r15,%rbx
lea    -0x30(%rbp),%rsi
mov    %rbx,%rdi
call   1060 <stat@plt>
xor    %r15d,%r15d
test   %rax,%rax
js     11fd <is_serial_dev+0xad>
mov    -0x30(%rbp),%edi
call   1070 <S_ISCHR@plt>
test   %eax,%eax
je     11fd <is_serial_dev+0xad>
mov    $0x1,%r15d
test   %r14,%r14
je     11fd <is_serial_dev+0xad>
mov    %rbx,%rdi
call   1050 <create_string@plt>
mov    %rax,(%r14)
mov    %r15d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@PATH_MAX = global i32 0
@.str = [6 x i8] c"%s/%s\00"
define i32 @is_serial_dev(i8* %0, i8* %1, i8** %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8**
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i32
%11 = alloca %struct.stat
%12 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8** %2, i8*** %7
%13 = load i32, i32* @PATH_MAX
%14 = zext i32 %13 to i64
%15 = call i8* @llvm.stacksave()
store i8* %15, i8** %8
%16 = alloca i8, i64 %14
store i64 %14, i64* %9
%17 = load i8*, i8** %5
%18 = icmp ne i8* %17, null
%19 = zext i1 %18 to i32
%20 = call i32 @assert(i32 %19)
%21 = load i8*, i8** %5
%22 = getelementptr inbounds i8, i8* %21, i64 0
%23 = load i8, i8* %22
%24 = sext i8 %23 to i32
%25 = icmp ne i32 %24, 47
br i1 %25, label %26, label %42
26: ; preds = %3
%27 = load i8*, i8** %6
%28 = icmp ne i8* %27, null
br i1 %28, label %29, label %42
29: ; preds = %26
%30 = trunc i64 %14 to i32
%31 = load i8*, i8** %6
%32 = load i8*, i8** %5
%33 = call i32 @snprintf(i8* %16, i32 %30, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %31, i8* %32)
store i32 %33, i32* %10
%34 = load i32, i32* %10
%35 = icmp slt i32 %34, 0
br i1 %35, label %40, label %36
36: ; preds = %29
%37 = load i32, i32* %10
%38 = sext i32 %37 to i64
%39 = icmp uge i64 %38, %14
br i1 %39, label %40, label %41
40: ; preds = %36, %29
store i32 0, i32* %4
store i32 1, i32* %12
br label %61
41: ; preds = %36
store i8* %16, i8** %5
br label %42
42: ; preds = %41, %26, %3
%43 = load i8*, i8** %5
%44 = call i64 @stat(i8* %43, %struct.stat* %11)
%45 = icmp slt i64 %44, 0
br i1 %45, label %46, label %47
46: ; preds = %42
store i32 0, i32* %4
store i32 1, i32* %12
br label %61
47: ; preds = %42
%48 = getelementptr inbounds %struct.stat, %struct.stat* %11, i32 0, i32 0
%49 = load i32, i32* %48
%50 = call i32 @S_ISCHR(i32 %49)
%51 = icmp ne i32 %50, 0
br i1 %51, label %53, label %52
52: ; preds = %47
store i32 0, i32* %4
store i32 1, i32* %12
br label %61
53: ; preds = %47
%54 = load i8**, i8*** %7
%55 = icmp ne i8** %54, null
br i1 %55, label %56, label %60
56: ; preds = %53
%57 = load i8*, i8** %5
%58 = call i8* @create_string(i8* %57)
%59 = load i8**, i8*** %7
store i8* %58, i8** %59
br label %60
60: ; preds = %56, %53
store i32 1, i32* %4
store i32 1, i32* %12
br label %61
61: ; preds = %60, %52, %46, %40
%62 = load i8*, i8** %8
%63 = load i32, i32* %4
ret i32 %63
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@PATH_MAX = global i32 0
@.str = [6 x i8] c"%s/%s\00"
define i32 @is_serial_dev(i8* %0, i8* %1, i8** writeonly %2) {
%4 = alloca %struct.stat
%5 = load i32, i32* @PATH_MAX
%6 = zext i32 %5 to i64
%7 = alloca i8, i64 %6
%8 = bitcast %struct.stat* %4 to i8*
%9 = icmp ne i8* %0, null
%10 = zext i1 %9 to i32
%11 = tail call i32 @assert(i32 %10)
%12 = load i8, i8* %0
%13 = icmp ne i8 %12, 47
%14 = icmp ne i8* %1, null
%15 = and i1 %14, %13
br i1 %15, label %16, label %22
16: ; preds = %3
%17 = call i32 @snprintf(i8* %7, i32 %5, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %1, i8* %0)
%18 = icmp slt i32 %17, 0
%19 = sext i32 %17 to i64
%20 = icmp uge i64 %19, %6
%21 = select i1 %18, i1 true, i1 %20
br i1 %21, label %35, label %22
22: ; preds = %16, %3
%23 = phi i8* [ %0, %3 ], [ %7, %16 ]
%24 = call i64 @stat(i8* %23, %struct.stat* %4)
%25 = icmp slt i64 %24, 0
br i1 %25, label %35, label %26
26: ; preds = %22
%27 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%28 = load i32, i32* %27
%29 = call i32 @S_ISCHR(i32 %28)
%30 = icmp eq i32 %29, 0
br i1 %30, label %35, label %31
31: ; preds = %26
%32 = icmp eq i8** %2, null
br i1 %32, label %35, label %33
33: ; preds = %31
%34 = call i8* @create_string(i8* %23)
store i8* %34, i8** %2
br label %35
35: ; preds = %31, %33, %26, %22, %16
%36 = phi i32 [ 0, %16 ], [ 0, %22 ], [ 0, %26 ], [ 1, %33 ], [ 1, %31 ]
ret i32 %36
}