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

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int snprintf(char *, int, char *, char *, char *);
int dmabuf_heap_open(char *name) {
  int ret, fd;
  char buf[256];
  ret = snprintf(buf, 256, "%s/%s", "/dev/dma_heap", name);
  if (ret < 0) {
    printf("snprintf failed!\n");
    return ret;
  }
  fd = open(buf, O_RDWR);
  if (fd < 0)
    printf("open %s failed!\n", buf);
  return fd;
}


x86 assembly (clang, -O0):
<dmabuf_heap_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x120(%rbp),%rdi
mov    -0x10(%rbp),%r8
mov    $0x100,%esi
lea    0xeaa(%rip),%rdx
lea    0xea9(%rip),%rcx
call   1040 <snprintf@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1188 <dmabuf_heap_open+0x58>
lea    0xe9e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11c5 <dmabuf_heap_open+0x95>
lea    -0x120(%rbp),%rdi
mov    0x2e52(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11bf <dmabuf_heap_open+0x8f>
lea    -0x120(%rbp),%rsi
lea    0xe6e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dmabuf_heap_open>:
push   %rbx
sub    $0x100,%rsp
mov    %rdi,%r8
lea    0xeae(%rip),%rdx
lea    0xead(%rip),%rcx
mov    %rsp,%rdi
mov    $0x100,%esi
call   1050 <snprintf@plt>
test   %eax,%eax
js     1194 <dmabuf_heap_open+0x54>
mov    0x2e77(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdi
call   1060 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    11a2 <dmabuf_heap_open+0x62>
lea    0xe8c(%rip),%rdi
mov    %rsp,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    11a2 <dmabuf_heap_open+0x62>
mov    %eax,%ebx
lea    0xe88(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%eax
add    $0x100,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [6 x i8] c"%s/%s\00"
@.str.1 = [14 x i8] c"/dev/dma_heap\00"
@.str.2 = [18 x i8] c"snprintf failed!\0A\00"
@O_RDWR = global i32 0
@.str.3 = [17 x i8] c"open %s failed!\0A\00"
define i32 @dmabuf_heap_open(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca [256 x i8]
store i8* %0, i8** %3
%7 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%8 = load i8*, i8** %3
%9 = call i32 @snprintf(i8* %7, i32 256, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* %8)
store i32 %9, i32* %4
%10 = load i32, i32* %4
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %1
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
%14 = load i32, i32* %4
store i32 %14, i32* %2
br label %26
15: ; preds = %1
%16 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%17 = load i32, i32* @O_RDWR
%18 = call i32 @open(i8* %16, i32 %17)
store i32 %18, i32* %5
%19 = load i32, i32* %5
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %15
%22 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i8* %22)
br label %24
24: ; preds = %21, %15
%25 = load i32, i32* %5
store i32 %25, i32* %2
br label %26
26: ; preds = %24, %12
%27 = load i32, i32* %2
ret i32 %27
}

LLVM IR (-O2):
@.str = [6 x i8] c"%s/%s\00"
@.str.1 = [14 x i8] c"/dev/dma_heap\00"
@O_RDWR = global i32 0
@.str.3 = [17 x i8] c"open %s failed!\0A\00"
@str = [17 x i8] c"snprintf failed!\00"
define i32 @dmabuf_heap_open(i8* %0) {
%2 = alloca [256 x i8]
%3 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%4 = call i32 @snprintf(i8* %3, i32 256, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %1
%7 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0))
br label %14
8: ; preds = %1
%9 = load i32, i32* @O_RDWR
%10 = call i32 @open(i8* %3, i32 %9)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %8
%13 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i8* %3)
br label %14
14: ; preds = %8, %12, %6
%15 = phi i32 [ %4, %6 ], [ %10, %12 ], [ %10, %8 ]
ret i32 %15
}