#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 */
unsigned int SifAllocIopHeap(int);
int /*<<< orphan*/ SifFreeIopHeap(unsigned int);
int /*<<< orphan*/ SifInitIopHeap();
scalar_t__ SifLoadModuleBuffer(unsigned int, int, char *);
scalar_t__ iop_heap_dma_upload(char *, unsigned int, int);
int loadmodulemem(char *buf, int len, int lenarg, char *arg) {
  unsigned iopmem = 0;
  int ret = 0;
  SifInitIopHeap();
  iopmem = SifAllocIopHeap(len + 15);
  if (iopmem == 0) {
    return -1;
  }
  if (iop_heap_dma_upload(buf, iopmem, len + 15) < 0) {
    SifFreeIopHeap(iopmem);
    return -1;
  }
  if (SifLoadModuleBuffer(iopmem, lenarg, arg) < 0) {
    SifFreeIopHeap(iopmem);
    return -1;
  }
  return ret;
}


x86 assembly (clang, -O0):
<loadmodulemem>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
mov    $0x0,%al
call   1050 <SifInitIopHeap@plt>
mov    -0x14(%rbp),%edi
add    $0xf,%edi
call   1040 <SifAllocIopHeap@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jne    119f <loadmodulemem+0x4f>
movl   $0xffffffff,-0x4(%rbp)
jmp    1202 <loadmodulemem+0xb2>
mov    -0x10(%rbp),%rdi
mov    -0x24(%rbp),%esi
mov    -0x14(%rbp),%edx
add    $0xf,%edx
call   1070 <iop_heap_dma_upload@plt>
cmp    $0x0,%rax
jge    11cf <loadmodulemem+0x7f>
mov    -0x24(%rbp),%edi
call   1030 <SifFreeIopHeap@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1202 <loadmodulemem+0xb2>
mov    -0x24(%rbp),%edi
mov    -0x18(%rbp),%esi
mov    -0x20(%rbp),%rdx
call   1060 <SifLoadModuleBuffer@plt>
cmp    $0x0,%rax
jge    11fc <loadmodulemem+0xac>
mov    -0x24(%rbp),%edi
call   1030 <SifFreeIopHeap@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1202 <loadmodulemem+0xb2>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<loadmodulemem>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rcx,%r14
mov    %edx,%r15d
mov    %esi,%ebp
mov    %rdi,%r12
xor    %eax,%eax
call   1050 <SifInitIopHeap@plt>
add    $0xf,%ebp
mov    %ebp,%edi
call   1040 <SifAllocIopHeap@plt>
mov    %eax,%ebx
mov    $0xffffffff,%eax
test   %ebx,%ebx
je     11b3 <loadmodulemem+0x63>
mov    %r12,%rdi
mov    %ebx,%esi
mov    %ebp,%edx
call   1070 <iop_heap_dma_upload@plt>
test   %rax,%rax
js     11a7 <loadmodulemem+0x57>
mov    %ebx,%edi
mov    %r15d,%esi
mov    %r14,%rdx
call   1060 <SifLoadModuleBuffer@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
jns    11b3 <loadmodulemem+0x63>
mov    %ebx,%edi
call   1030 <SifFreeIopHeap@plt>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @loadmodulemem(i8* %0, i32 %1, i32 %2, i8* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i8* %3, i8** %9
store i32 0, i32* %10
store i32 0, i32* %11
%12 = call i32 (...) @SifInitIopHeap()
%13 = load i32, i32* %7
%14 = add nsw i32 %13, 15
%15 = call i32 @SifAllocIopHeap(i32 %14)
store i32 %15, i32* %10
%16 = load i32, i32* %10
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %4
store i32 -1, i32* %5
br label %40
19: ; preds = %4
%20 = load i8*, i8** %6
%21 = load i32, i32* %10
%22 = load i32, i32* %7
%23 = add nsw i32 %22, 15
%24 = call i64 @iop_heap_dma_upload(i8* %20, i32 %21, i32 %23)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %19
%27 = load i32, i32* %10
%28 = call i32 @SifFreeIopHeap(i32 %27)
store i32 -1, i32* %5
br label %40
29: ; preds = %19
%30 = load i32, i32* %10
%31 = load i32, i32* %8
%32 = load i8*, i8** %9
%33 = call i64 @SifLoadModuleBuffer(i32 %30, i32 %31, i8* %32)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %29
%36 = load i32, i32* %10
%37 = call i32 @SifFreeIopHeap(i32 %36)
store i32 -1, i32* %5
br label %40
38: ; preds = %29
%39 = load i32, i32* %11
store i32 %39, i32* %5
br label %40
40: ; preds = %38, %35, %26, %18
%41 = load i32, i32* %5
ret i32 %41
}

LLVM IR (-O2):
define i32 @loadmodulemem(i8* %0, i32 %1, i32 %2, i8* %3) {
%5 = tail call i32 (...) @SifInitIopHeap()
%6 = add nsw i32 %1, 15
%7 = tail call i32 @SifAllocIopHeap(i32 %6)
%8 = icmp eq i32 %7, 0
br i1 %8, label %17, label %9
9: ; preds = %4
%10 = tail call i64 @iop_heap_dma_upload(i8* %0, i32 %7, i32 %6)
%11 = icmp slt i64 %10, 0
br i1 %11, label %15, label %12
12: ; preds = %9
%13 = tail call i64 @SifLoadModuleBuffer(i32 %7, i32 %2, i8* %3)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %12, %9
%16 = tail call i32 @SifFreeIopHeap(i32 %7)
br label %17
17: ; preds = %15, %12, %4
%18 = phi i32 [ -1, %4 ], [ 0, %12 ], [ -1, %15 ]
ret i32 %18
}