#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 MAP_FILE;
int MAP_FIXED;
int MAP_SHARED;
int /*<<< orphan*/ O_RDWR;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ close(int);
unsigned long ioctl(int, int, unsigned long);
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, int size) {
  void *adr = 0;
  int hook, rtai3 = 0;
  struct {
    unsigned long name, size;
  } arg;
  if (size <= 0) {
    return 0;
  }
  if ((hook = open("/dev/rtai_shm", O_RDWR)) < 0 && (rtai3 = 186) &&
      (hook = open("/dev/RTAI_SHM", O_RDWR)) < 0)
    goto out;
  arg.name = name;
  arg.size = size;
  if (!(size = ioctl(hook, 1 + rtai3, (unsigned long)(&arg))))
    goto out;
  adr = mmap(start_address, size, PROT_WRITE | PROT_READ,
             MAP_FIXED | MAP_SHARED | MAP_FILE, hook, 0);
out:
  close(hook);
  return adr;
}


x86 assembly (clang, -O0):
<rtai_malloc_adr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movq   $0x0,-0x28(%rbp)
movl   $0x0,-0x30(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jg     1179 <rtai_malloc_adr+0x39>
movq   $0x0,-0x8(%rbp)
jmp    125e <rtai_malloc_adr+0x11e>
mov    0x2e58(%rip),%rax
mov    (%rax),%esi
lea    0xe77(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
jge    11d6 <rtai_malloc_adr+0x96>
movl   $0xba,-0x30(%rbp)
mov    $0x1,%al
test   $0x1,%al
jne    11b0 <rtai_malloc_adr+0x70>
jmp    11d6 <rtai_malloc_adr+0x96>
mov    0x2e21(%rip),%rax
mov    (%rax),%esi
lea    0xe4e(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
jge    11d6 <rtai_malloc_adr+0x96>
jmp    124e <rtai_malloc_adr+0x10e>
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
movslq -0x1c(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x2c(%rbp),%edi
mov    -0x30(%rbp),%esi
add    $0x1,%esi
lea    -0x40(%rbp),%rdx
call   1040 <ioctl@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jne    1209 <rtai_malloc_adr+0xc9>
jmp    124e <rtai_malloc_adr+0x10e>
mov    -0x10(%rbp),%rdi
movslq -0x1c(%rbp),%rsi
mov    0x2da8(%rip),%rax
mov    (%rax),%edx
mov    0x2dd7(%rip),%rax
or     (%rax),%edx
mov    0x2d8e(%rip),%rax
mov    (%rax),%ecx
mov    0x2db5(%rip),%rax
or     (%rax),%ecx
mov    0x2d94(%rip),%rax
or     (%rax),%ecx
mov    -0x2c(%rbp),%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x28(%rbp)
mov    -0x2c(%rbp),%edi
call   1050 <close@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rtai_malloc_adr>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
test   %edx,%edx
jle    117c <rtai_malloc_adr+0x3c>
mov    %edx,%ebp
mov    %rsi,%r15
mov    %rdi,%r14
mov    0x2e79(%rip),%r12
mov    (%r12),%esi
lea    0xe96(%rip),%rdi
call   1060 <open@plt>
test   %eax,%eax
js     1183 <rtai_malloc_adr+0x43>
mov    %eax,%ebx
mov    $0x1,%esi
jmp    119e <rtai_malloc_adr+0x5e>
xor    %ebp,%ebp
jmp    1203 <rtai_malloc_adr+0xc3>
mov    (%r12),%esi
lea    0xe80(%rip),%rdi
call   1060 <open@plt>
mov    %eax,%ebx
mov    $0xbb,%esi
test   %eax,%eax
js     11fa <rtai_malloc_adr+0xba>
mov    %r15,(%rsp)
mov    %ebp,%eax
mov    %rax,0x8(%rsp)
mov    %rsp,%rdx
mov    %ebx,%edi
call   1040 <ioctl@plt>
test   %eax,%eax
je     11fa <rtai_malloc_adr+0xba>
mov    0x2e02(%rip),%rcx
mov    0x2e33(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
mov    0x2de8(%rip),%rsi
mov    0x2e11(%rip),%rcx
mov    (%rcx),%ecx
or     (%rsi),%ecx
movslq %eax,%rsi
mov    0x2deb(%rip),%rax
or     (%rax),%ecx
mov    %r14,%rdi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbp
jmp    11fc <rtai_malloc_adr+0xbc>
xor    %ebp,%ebp
mov    %ebx,%edi
call   1050 <close@plt>
mov    %rbp,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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