#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*/ buf;

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
int /*<<< orphan*/ stderr;
int dev_open(char *dev_name) {
  int fd;
  char buf[50];
  snprintf(buf, sizeof(buf), "/dev/%s", dev_name);
  fd = open(buf, O_RDWR);
  if (fd <= 0)
    fprintf(stderr, "Could not open device %s\n", buf);
  return fd;
}


x86 assembly (clang, -O0):
<dev_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
lea    -0x40(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    $0x32,%esi
lea    0xeb0(%rip),%rdx
call   1030 <snprintf@plt>
lea    -0x40(%rbp),%rdi
mov    0x2e88(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jg     118d <dev_open+0x5d>
mov    0x2e5d(%rip),%rax
mov    (%rax),%edi
lea    -0x40(%rbp),%rdx
lea    0xe80(%rip),%rsi
call   1040 <fprintf@plt>
mov    -0xc(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dev_open>:
push   %rbx
sub    $0x40,%rsp
mov    %rdi,%rcx
lea    0xec1(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x32,%esi
call   1030 <snprintf@plt>
mov    0x2e92(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1050 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
jg     117e <dev_open+0x4e>
mov    0x2e6b(%rip),%rax
mov    (%rax),%edi
lea    0xe92(%rip),%rsi
mov    %rsp,%rdx
call   1040 <fprintf@plt>
mov    %ebx,%eax
add    $0x40,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [8 x i8] c"/dev/%s\00"
@O_RDWR = global i32 0
@stderr = global i32 0
@.str.1 = [26 x i8] c"Could not open device %s\0A\00"
define i32 @dev_open(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca [50 x i8]
store i8* %0, i8** %2
%5 = getelementptr inbounds [50 x i8], [50 x i8]* %4, i64 0, i64 0
%6 = load i8*, i8** %2
%7 = call i32 @snprintf(i8* %5, i32 50, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %6)
%8 = getelementptr inbounds [50 x i8], [50 x i8]* %4, i64 0, i64 0
%9 = load i32, i32* @O_RDWR
%10 = call i32 @open(i8* %8, i32 %9)
store i32 %10, i32* %3
%11 = load i32, i32* %3
%12 = icmp sle i32 %11, 0
br i1 %12, label %13, label %17
13: ; preds = %1
%14 = load i32, i32* @stderr
%15 = getelementptr inbounds [50 x i8], [50 x i8]* %4, i64 0, i64 0
%16 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %15)
br label %17
17: ; preds = %13, %1
%18 = load i32, i32* %3
ret i32 %18
}

LLVM IR (-O2):
@.str = [8 x i8] c"/dev/%s\00"
@O_RDWR = global i32 0
@stderr = global i32 0
@.str.1 = [26 x i8] c"Could not open device %s\0A\00"
define i32 @dev_open(i8* %0) {
%2 = alloca [50 x i8]
%3 = getelementptr inbounds [50 x i8], [50 x i8]* %2, i64 0, i64 0
%4 = call i32 @snprintf(i8* %3, i32 50, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %0)
%5 = load i32, i32* @O_RDWR
%6 = call i32 @open(i8* %3, i32 %5)
%7 = icmp slt i32 %6, 1
br i1 %7, label %8, label %11
8: ; preds = %1
%9 = load i32, i32* @stderr
%10 = call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %3)
br label %11
11: ; preds = %8, %1
ret i32 %6
}