#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 */
void *MAP_FAILED;
int MAP_LOCKED;
int MAP_SHARED;
int /*<<< orphan*/ O_RDWR;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ close(int);
unsigned long ioctl(int, int, ...);
void *mmap(void *, unsigned long, int, int, int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
void *rtai_malloc_adr(void *start_address, unsigned long name,
                      unsigned long size) {
  void *adr = 0;
  int hook;
  struct {
    unsigned long name, size, suprt;
  } arg;
  if ((hook = open("/dev/RTAI_SHM", O_RDWR)) < 0)
    return 0;
  arg.name = name;
  arg.size = size;
  arg.suprt = 0;
  if ((size = ioctl(hook, 187, (unsigned long)&arg)))
    adr = mmap(start_address, size, PROT_WRITE | PROT_READ,
               MAP_SHARED | MAP_LOCKED, hook, 0);
  if (adr == MAP_FAILED) {
    ioctl(hook, 188, &name);
    adr = 0;
  }
  close(hook);
  return adr;
}


x86 assembly (clang, -O0):
<rtai_malloc_adr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    0x2e75(%rip),%rax
mov    (%rax),%esi
lea    0xe94(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
jge    118a <rtai_malloc_adr+0x4a>
movq   $0x0,-0x8(%rbp)
jmp    123e <rtai_malloc_adr+0xfe>
mov    -0x18(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x2c(%rbp),%edi
lea    -0x48(%rbp),%rdx
mov    $0xbb,%esi
mov    $0x0,%al
call   1040 <ioctl@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     11ff <rtai_malloc_adr+0xbf>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    0x2dee(%rip),%rax
mov    (%rax),%edx
mov    0x2e1d(%rip),%rax
or     (%rax),%edx
mov    0x2e04(%rip),%rax
mov    (%rax),%ecx
mov    0x2ddb(%rip),%rax
or     (%rax),%ecx
mov    -0x2c(%rbp),%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    0x2dae(%rip),%rcx
cmp    (%rcx),%rax
jne    122e <rtai_malloc_adr+0xee>
mov    -0x2c(%rbp),%edi
mov    $0xbc,%esi
lea    -0x18(%rbp),%rdx
mov    $0x0,%al
call   1040 <ioctl@plt>
movq   $0x0,-0x28(%rbp)
mov    -0x2c(%rbp),%edi
call   1050 <close@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rtai_malloc_adr>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %rdx,%r15
mov    %rsi,%rbx
mov    %rdi,%r14
mov    %rsi,0x8(%rsp)
mov    0x2e79(%rip),%rax
mov    (%rax),%esi
lea    0xe98(%rip),%rdi
call   1060 <open@plt>
test   %eax,%eax
js     1206 <rtai_malloc_adr+0xc6>
mov    %eax,%ebp
mov    %rbx,0x10(%rsp)
mov    %r15,0x18(%rsp)
movq   $0x0,0x20(%rsp)
xor    %ebx,%ebx
lea    0x10(%rsp),%rdx
mov    %eax,%edi
mov    $0xbb,%esi
xor    %eax,%eax
call   1040 <ioctl@plt>
test   %rax,%rax
je     11dc <rtai_malloc_adr+0x9c>
mov    0x2e15(%rip),%rcx
mov    0x2e46(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
mov    0x2e2b(%rip),%rsi
mov    0x2e04(%rip),%rcx
mov    (%rcx),%ecx
or     (%rsi),%ecx
mov    %r14,%rdi
mov    %rax,%rsi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbx
mov    0x2dd5(%rip),%rax
cmp    (%rax),%rbx
jne    11fd <rtai_malloc_adr+0xbd>
xor    %ebx,%ebx
lea    0x8(%rsp),%rdx
mov    %ebp,%edi
mov    $0xbc,%esi
xor    %eax,%eax
call   1040 <ioctl@plt>
mov    %ebp,%edi
call   1050 <close@plt>
jmp    1208 <rtai_malloc_adr+0xc8>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.anon = type { i64, i64, i64 }
@.str = [14 x i8] c"/dev/RTAI_SHM\00"
@O_RDWR = global i32 0
@PROT_WRITE = global i32 0
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MAP_LOCKED = global i32 0
@MAP_FAILED = global i8* null
define i8* @rtai_malloc_adr(i8* %0, i64 %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i32
%10 = alloca %struct.anon
store i8* %0, i8** %5
store i64 %1, i64* %6
store i64 %2, i64* %7
store i8* null, i8** %8
%11 = load i32, i32* @O_RDWR
%12 = call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %11)
store i32 %12, i32* %9
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %3
store i8* null, i8** %4
br label %47
15: ; preds = %3
%16 = load i64, i64* %6
%17 = getelementptr inbounds %struct.anon, %struct.anon* %10, i32 0, i32 0
store i64 %16, i64* %17
%18 = load i64, i64* %7
%19 = getelementptr inbounds %struct.anon, %struct.anon* %10, i32 0, i32 1
store i64 %18, i64* %19
%20 = getelementptr inbounds %struct.anon, %struct.anon* %10, i32 0, i32 2
store i64 0, i64* %20
%21 = load i32, i32* %9
%22 = ptrtoint %struct.anon* %10 to i64
%23 = call i64 (i32, i32, ...) @ioctl(i32 %21, i32 187, i64 %22)
store i64 %23, i64* %7
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %36
25: ; preds = %15
%26 = load i8*, i8** %5
%27 = load i64, i64* %7
%28 = load i32, i32* @PROT_WRITE
%29 = load i32, i32* @PROT_READ
%30 = or i32 %28, %29
%31 = load i32, i32* @MAP_SHARED
%32 = load i32, i32* @MAP_LOCKED
%33 = or i32 %31, %32
%34 = load i32, i32* %9
%35 = call i8* @mmap(i8* %26, i64 %27, i32 %30, i32 %33, i32 %34, i32 0)
store i8* %35, i8** %8
br label %36
36: ; preds = %25, %15
%37 = load i8*, i8** %8
%38 = load i8*, i8** @MAP_FAILED
%39 = icmp eq i8* %37, %38
br i1 %39, label %40, label %43
40: ; preds = %36
%41 = load i32, i32* %9
%42 = call i64 (i32, i32, ...) @ioctl(i32 %41, i32 188, i64* %6)
store i8* null, i8** %8
br label %43
43: ; preds = %40, %36
%44 = load i32, i32* %9
%45 = call i32 @close(i32 %44)
%46 = load i8*, i8** %8
store i8* %46, i8** %4
br label %47
47: ; preds = %43, %14
%48 = load i8*, i8** %4
ret i8* %48
}

LLVM IR (-O2):
%struct.anon = type { i64, i64, i64 }
@.str = [14 x i8] c"/dev/RTAI_SHM\00"
@O_RDWR = global i32 0
@PROT_WRITE = global i32 0
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MAP_LOCKED = global i32 0
@MAP_FAILED = global i8* null
define i8* @rtai_malloc_adr(i8* %0, i64 %1, i64 %2) {
%4 = alloca i64
%5 = alloca %struct.anon
store i64 %1, i64* %4
%6 = bitcast %struct.anon* %5 to i8*
%7 = load i32, i32* @O_RDWR
%8 = tail call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %34, label %10
10: ; preds = %3
%11 = getelementptr inbounds %struct.anon, %struct.anon* %5, i64 0, i32 0
store i64 %1, i64* %11
%12 = getelementptr inbounds %struct.anon, %struct.anon* %5, i64 0, i32 1
store i64 %2, i64* %12
%13 = getelementptr inbounds %struct.anon, %struct.anon* %5, i64 0, i32 2
store i64 0, i64* %13
%14 = ptrtoint %struct.anon* %5 to i64
%15 = call i64 (i32, i32, ...) @ioctl(i32 %8, i32 187, i64 %14)
%16 = icmp eq i64 %15, 0
br i1 %16, label %25, label %17
17: ; preds = %10
%18 = load i32, i32* @PROT_WRITE
%19 = load i32, i32* @PROT_READ
%20 = or i32 %19, %18
%21 = load i32, i32* @MAP_SHARED
%22 = load i32, i32* @MAP_LOCKED
%23 = or i32 %22, %21
%24 = call i8* @mmap(i8* %0, i64 %15, i32 %20, i32 %23, i32 %8, i32 0)
br label %25
25: ; preds = %17, %10
%26 = phi i8* [ %24, %17 ], [ null, %10 ]
%27 = load i8*, i8** @MAP_FAILED
%28 = icmp eq i8* %26, %27
br i1 %28, label %29, label %31
29: ; preds = %25
%30 = call i64 (i32, i32, ...) @ioctl(i32 %8, i32 188, i64* %4)
br label %31
31: ; preds = %29, %25
%32 = phi i8* [ null, %29 ], [ %26, %25 ]
%33 = call i32 @close(i32 %8)
br label %34
34: ; preds = %3, %31
%35 = phi i8* [ %32, %31 ], [ null, %3 ]
ret i8* %35
}