#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;
};

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
scalar_t__ S_ISCHR(int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int stat(char *, struct stat *);
int system(char *);
int open_duet_dev(void) {
  int ret;
  struct stat st;
  int duet_fd = -1;
  ret = stat("/dev/duet", &st);
  if (ret < 0) {
    ret = system("modprobe duet");
    if (ret == -1)
      return -1;
    ret = stat("/dev/duet", &st);
    if (ret < 0)
      return -1;
  }
  if (S_ISCHR(st.st_mode))
    duet_fd = open("/dev/duet", O_RDWR);
  return duet_fd;
}


x86 assembly (clang, -O0):
<open_duet_dev>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0xffffffff,-0x14(%rbp)
lea    0xeaa(%rip),%rdi
lea    -0x10(%rbp),%rsi
call   1040 <stat@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11bf <open_duet_dev+0x7f>
lea    0xe97(%rip),%rdi
call   1030 <system@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    1191 <open_duet_dev+0x51>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ef <open_duet_dev+0xaf>
lea    0xe68(%rip),%rdi
lea    -0x10(%rbp),%rsi
call   1040 <stat@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11ba <open_duet_dev+0x7a>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ef <open_duet_dev+0xaf>
jmp    11bf <open_duet_dev+0x7f>
mov    -0x10(%rbp),%edi
call   1050 <S_ISCHR@plt>
cmp    $0x0,%rax
je     11e9 <open_duet_dev+0xa9>
mov    0x2e10(%rip),%rax
mov    (%rax),%esi
lea    0xe1f(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_duet_dev>:
push   %rax
lea    0xeb8(%rip),%rdi
mov    %rsp,%rsi
call   1040 <stat@plt>
test   %eax,%eax
js     1177 <open_duet_dev+0x37>
mov    (%rsp),%edi
call   1050 <S_ISCHR@plt>
test   %rax,%rax
je     119b <open_duet_dev+0x5b>
mov    0x2e80(%rip),%rax
mov    (%rax),%esi
lea    0xe8f(%rip),%rdi
pop    %rax
jmp    1060 <open@plt>
lea    0xe8c(%rip),%rdi
call   1030 <system@plt>
cmp    $0xffffffff,%eax
je     119b <open_duet_dev+0x5b>
lea    0xe71(%rip),%rdi
mov    %rsp,%rsi
call   1040 <stat@plt>
test   %eax,%eax
jns    1154 <open_duet_dev+0x14>
mov    $0xffffffff,%eax
pop    %rcx
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [10 x i8] c"/dev/duet\00"
@.str.1 = [14 x i8] c"modprobe duet\00"
@O_RDWR = global i32 0
define i32 @open_duet_dev() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.stat
%4 = alloca i32
store i32 -1, i32* %4
%5 = call i32 @stat(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), %struct.stat* %3)
store i32 %5, i32* %2
%6 = load i32, i32* %2
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %19
8: ; preds = %0
%9 = call i32 @system(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
store i32 %9, i32* %2
%10 = load i32, i32* %2
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %13
12: ; preds = %8
store i32 -1, i32* %1
br label %29
13: ; preds = %8
%14 = call i32 @stat(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), %struct.stat* %3)
store i32 %14, i32* %2
%15 = load i32, i32* %2
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %13
store i32 -1, i32* %1
br label %29
18: ; preds = %13
br label %19
19: ; preds = %18, %0
%20 = getelementptr inbounds %struct.stat, %struct.stat* %3, i32 0, i32 0
%21 = load i32, i32* %20
%22 = call i64 @S_ISCHR(i32 %21)
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %19
%25 = load i32, i32* @O_RDWR
%26 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %25)
store i32 %26, i32* %4
br label %27
27: ; preds = %24, %19
%28 = load i32, i32* %4
store i32 %28, i32* %1
br label %29
29: ; preds = %27, %17, %12
%30 = load i32, i32* %1
ret i32 %30
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [10 x i8] c"/dev/duet\00"
@.str.1 = [14 x i8] c"modprobe duet\00"
@O_RDWR = global i32 0
define i32 @open_duet_dev() {
%1 = alloca %struct.stat
%2 = bitcast %struct.stat* %1 to i8*
%3 = call i32 @stat(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), %struct.stat* %1)
%4 = icmp slt i32 %3, 0
br i1 %4, label %5, label %11
5: ; preds = %0
%6 = tail call i32 @system(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%7 = icmp eq i32 %6, -1
br i1 %7, label %19, label %8
8: ; preds = %5
%9 = call i32 @stat(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), %struct.stat* %1)
%10 = icmp slt i32 %9, 0
br i1 %10, label %19, label %11
11: ; preds = %8, %0
%12 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 0
%13 = load i32, i32* %12
%14 = tail call i64 @S_ISCHR(i32 %13)
%15 = icmp eq i64 %14, 0
br i1 %15, label %19, label %16
16: ; preds = %11
%17 = load i32, i32* @O_RDWR
%18 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %17)
br label %19
19: ; preds = %11, %16, %8, %5
%20 = phi i32 [ -1, %5 ], [ -1, %8 ], [ %18, %16 ], [ -1, %11 ]
ret i32 %20
}