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


x86 assembly (clang, -O0):
<rtai_malloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movq   $0x0,-0x20(%rbp)
cmpl   $0x0,-0x14(%rbp)
jg     116e <rtai_malloc+0x2e>
movq   $0x0,-0x8(%rbp)
jmp    1238 <rtai_malloc+0xf8>
mov    0x2e63(%rip),%rax
mov    (%rax),%esi
lea    0xe82(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    11bd <rtai_malloc+0x7d>
mov    0x2e42(%rip),%rax
mov    (%rax),%esi
lea    0xe6f(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    11bd <rtai_malloc+0x7d>
movq   $0x0,-0x8(%rbp)
jmp    1238 <rtai_malloc+0xf8>
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
movslq -0x14(%rbp),%rax
mov    %rax,-0x30(%rbp)
lea    -0x38(%rbp),%rsi
mov    $0x1,%edi
call   1050 <rtai_shmrq@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jne    11ec <rtai_malloc+0xac>
jmp    1228 <rtai_malloc+0xe8>
movslq -0x14(%rbp),%rsi
mov    0x2dc9(%rip),%rax
mov    (%rax),%edx
mov    0x2df8(%rip),%rax
or     (%rax),%edx
mov    0x2ddf(%rip),%rax
mov    (%rax),%ecx
mov    0x2dbe(%rip),%rax
or     (%rax),%ecx
mov    -0x24(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rtai_malloc>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
test   %esi,%esi
jle    11e0 <rtai_malloc+0xa0>
mov    %esi,%ebp
mov    %rdi,%r14
mov    0x2e7a(%rip),%r15
mov    (%r15),%esi
lea    0xe98(%rip),%rdi
call   1060 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    1188 <rtai_malloc+0x48>
mov    (%r15),%esi
lea    0xe91(%rip),%rdi
call   1060 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     11e0 <rtai_malloc+0xa0>
mov    %r14,0x8(%rsp)
mov    %ebp,%eax
mov    %rax,0x10(%rsp)
lea    0x8(%rsp),%rsi
mov    $0x1,%edi
call   1050 <rtai_shmrq@plt>
test   %eax,%eax
je     11e4 <rtai_malloc+0xa4>
movslq %eax,%rsi
mov    0x2e0f(%rip),%rax
mov    0x2e40(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e25(%rip),%rax
mov    0x2e06(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
xor    %edi,%edi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbp
jmp    11e6 <rtai_malloc+0xa6>
xor    %ebp,%ebp
jmp    11ed <rtai_malloc+0xad>
xor    %ebp,%ebp
mov    %ebx,%edi
call   1040 <close@plt>
mov    %rbp,%rax
add    $0x18,%rsp
pop    %rbx
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_SHARED = global i32 0
@MAP_FILE = global i32 0
define i8* @rtai_malloc(i64 %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca %struct.anon
store i64 %0, i64* %4
store i32 %1, i32* %5
store i8* null, i8** %6
%9 = load i32, i32* %5
%10 = icmp sle i32 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %2
store i8* null, i8** %3
br label %47
12: ; preds = %2
%13 = load i32, i32* @O_RDWR
%14 = call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %13)
store i32 %14, i32* %7
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %21
16: ; preds = %12
%17 = load i32, i32* @O_RDWR
%18 = call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %17)
store i32 %18, i32* %7
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %16
store i8* null, i8** %3
br label %47
21: ; preds = %16, %12
%22 = load i64, i64* %4
%23 = getelementptr inbounds %struct.anon, %struct.anon* %8, i32 0, i32 0
store i64 %22, i64* %23
%24 = load i32, i32* %5
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds %struct.anon, %struct.anon* %8, i32 0, i32 1
store i64 %25, i64* %26
%27 = ptrtoint %struct.anon* %8 to i64
%28 = call i64 @rtai_shmrq(i32 1, i64 %27)
%29 = trunc i64 %28 to i32
store i32 %29, i32* %5
%30 = icmp ne i32 %29, 0
br i1 %30, label %32, label %31
31: ; preds = %21
br label %43
32: ; preds = %21
%33 = load i32, i32* %5
%34 = sext i32 %33 to i64
%35 = load i32, i32* @PROT_WRITE
%36 = load i32, i32* @PROT_READ
%37 = or i32 %35, %36
%38 = load i32, i32* @MAP_SHARED
%39 = load i32, i32* @MAP_FILE
%40 = or i32 %38, %39
%41 = load i32, i32* %7
%42 = call i8* @mmap(i32* null, i64 %34, i32 %37, i32 %40, i32 %41, i32 0)
store i8* %42, i8** %6
br label %43
43: ; preds = %32, %31
%44 = load i32, i32* %7
%45 = call i32 @close(i32 %44)
%46 = load i8*, i8** %6
store i8* %46, i8** %3
br label %47
47: ; preds = %43, %20, %11
%48 = load i8*, i8** %3
ret i8* %48
}

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_SHARED = global i32 0
@MAP_FILE = global i32 0
define i8* @rtai_malloc(i64 %0, i32 %1) {
%3 = alloca %struct.anon
%4 = bitcast %struct.anon* %3 to i8*
%5 = icmp slt i32 %1, 1
br i1 %5, label %36, label %6
6: ; preds = %2
%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 %10, label %14
10: ; preds = %6
%11 = load i32, i32* @O_RDWR
%12 = tail call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %11)
%13 = icmp slt i32 %12, 0
br i1 %13, label %36, label %14
14: ; preds = %10, %6
%15 = phi i32 [ %12, %10 ], [ %8, %6 ]
%16 = getelementptr inbounds %struct.anon, %struct.anon* %3, i64 0, i32 0
store i64 %0, i64* %16
%17 = zext i32 %1 to i64
%18 = getelementptr inbounds %struct.anon, %struct.anon* %3, i64 0, i32 1
store i64 %17, i64* %18
%19 = ptrtoint %struct.anon* %3 to i64
%20 = call i64 @rtai_shmrq(i32 1, i64 %19)
%21 = trunc i64 %20 to i32
%22 = icmp eq i32 %21, 0
br i1 %22, label %33, label %23
23: ; preds = %14
%24 = shl i64 %20, 32
%25 = ashr exact i64 %24, 32
%26 = load i32, i32* @PROT_WRITE
%27 = load i32, i32* @PROT_READ
%28 = or i32 %27, %26
%29 = load i32, i32* @MAP_SHARED
%30 = load i32, i32* @MAP_FILE
%31 = or i32 %30, %29
%32 = call i8* @mmap(i32* null, i64 %25, i32 %28, i32 %31, i32 %15, i32 0)
br label %33
33: ; preds = %14, %23
%34 = phi i8* [ %32, %23 ], [ null, %14 ]
%35 = call i32 @close(i32 %15)
br label %36
36: ; preds = %10, %2, %33
%37 = phi i8* [ %34, %33 ], [ null, %2 ], [ null, %10 ]
ret i8* %37
}