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

/* Variables and functions */
scalar_t__ SCSI_GENERIC_MAJOR;
scalar_t__ S_ISCHR(int /*<<< orphan*/);
scalar_t__ major(int /*<<< orphan*/);
scalar_t__ stat(char const *, struct stat *);
int is_sg_device(const char *dev) {
  struct stat st;
  if (stat(dev, &st) < 0) {
    return -1;
  }
  return S_ISCHR(st.st_mode) && major(st.st_rdev) == SCSI_GENERIC_MAJOR ? 1 : 0;
}


x86 assembly (clang, -O0):
<is_sg_device>:
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
jge    115f <is_sg_device+0x2f>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a4 <is_sg_device+0x74>
mov    -0x14(%rbp),%edi
call   1050 <S_ISCHR@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x19(%rbp)
je     1191 <is_sg_device+0x61>
mov    -0x18(%rbp),%edi
call   1030 <major@plt>
mov    0x2e60(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%dl
xor    %eax,%eax
mov    $0x1,%ecx
test   $0x1,%dl
cmovne %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<is_sg_device>:
push   %rax
mov    %rsp,%rsi
call   1040 <stat@plt>
test   %rax,%rax
js     1168 <is_sg_device+0x38>
mov    0x4(%rsp),%edi
call   1050 <S_ISCHR@plt>
test   %rax,%rax
je     116f <is_sg_device+0x3f>
mov    (%rsp),%edi
call   1030 <major@plt>
mov    %rax,%rcx
mov    0x2e8a(%rip),%rdx
xor    %eax,%eax
cmp    (%rdx),%rcx
sete   %al
pop    %rcx
ret
mov    $0xffffffff,%eax
pop    %rcx
ret
xor    %eax,%eax
pop    %rcx
ret

LLVM IR (-O0):
%struct.stat = type { i32, i32 }
@SCSI_GENERIC_MAJOR = global i64 0
define i32 @is_sg_device(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 slt i64 %6, 0
br i1 %7, label %8, label %9
8: ; preds = %1
store i32 -1, i32* %2
br label %24
9: ; preds = %1
%10 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 1
%11 = load i32, i32* %10
%12 = call i64 @S_ISCHR(i32 %11)
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %20
14: ; preds = %9
%15 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%16 = load i32, i32* %15
%17 = call i64 @major(i32 %16)
%18 = load i64, i64* @SCSI_GENERIC_MAJOR
%19 = icmp eq i64 %17, %18
br label %20
20: ; preds = %14, %9
%21 = phi i1 [ false, %9 ], [ %19, %14 ]
%22 = zext i1 %21 to i64
%23 = select i1 %21, i32 1, i32 0
store i32 %23, i32* %2
br label %24
24: ; preds = %20, %8
%25 = load i32, i32* %2
ret i32 %25
}

LLVM IR (-O2):
%struct.stat = type { i32, i32 }
@SCSI_GENERIC_MAJOR = global i64 0
define i32 @is_sg_device(i8* %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = call i64 @stat(i8* %0, %struct.stat* %2)
%5 = icmp slt i64 %4, 0
br i1 %5, label %18, label %6
6: ; preds = %1
%7 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 1
%8 = load i32, i32* %7
%9 = call i64 @S_ISCHR(i32 %8)
%10 = icmp eq i64 %9, 0
br i1 %10, label %18, label %11
11: ; preds = %6
%12 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%13 = load i32, i32* %12
%14 = call i64 @major(i32 %13)
%15 = load i64, i64* @SCSI_GENERIC_MAJOR
%16 = icmp eq i64 %14, %15
%17 = zext i1 %16 to i32
br label %18
18: ; preds = %6, %11, %1
%19 = phi i32 [ -1, %1 ], [ 0, %6 ], [ %17, %11 ]
ret i32 %19
}