#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*/ free(char *);
scalar_t__ malloc(int);
scalar_t__ pedigree_module_get_depending(char *, char *, int);
int /*<<< orphan*/ pedigree_module_is_loaded(char *);
int /*<<< orphan*/ pedigree_module_unload(char *);
int /*<<< orphan*/ printf(char *, char *);
int unload_module(char *name) {
  if (!pedigree_module_is_loaded(name)) {
    printf("Module %s isn't loaded!\n", name);
    return -1;
  }
  printf("Module %s being unloaded...\n", name);
  char *dep = (char *)malloc(256);
  while (pedigree_module_get_depending(name, dep, 256)) {
    if (unload_module(dep) == -1)
      return -1;
  }
  free(dep);
  pedigree_module_unload(name);
  if (!pedigree_module_is_loaded(name))
    return 0;
  printf("Module %s couldn't been unloaded\n", name);
  return -1;
}


x86 assembly (clang, -O0):
<unload_module>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <pedigree_module_is_loaded@plt>
cmp    $0x0,%eax
jne    11aa <unload_module+0x3a>
mov    -0x10(%rbp),%rsi
lea    0xe67(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    124e <unload_module+0xde>
mov    -0x10(%rbp),%rsi
lea    0xe64(%rip),%rdi
call   1040 <printf@plt>
mov    $0x100,%edi
call   1070 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    $0x100,%edx
call   1080 <pedigree_module_get_depending@plt>
cmp    $0x0,%rax
je     1207 <unload_module+0x97>
mov    -0x18(%rbp),%rdi
call   1050 <unload_module@plt>
cmp    $0xffffffff,%eax
jne    1202 <unload_module+0x92>
movl   $0xffffffff,-0x4(%rbp)
jmp    124e <unload_module+0xde>
jmp    11c8 <unload_module+0x58>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rdi
call   1090 <pedigree_module_unload@plt>
mov    -0x10(%rbp),%rdi
call   1060 <pedigree_module_is_loaded@plt>
cmp    $0x0,%eax
jne    1237 <unload_module+0xc7>
movl   $0x0,-0x4(%rbp)
jmp    124e <unload_module+0xde>
mov    -0x10(%rbp),%rsi
lea    0xdf4(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<unload_module>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
call   1060 <pedigree_module_is_loaded@plt>
test   %eax,%eax
je     11c4 <unload_module+0x54>
lea    0xe92(%rip),%rdi
mov    %r14,%rsi
call   1040 <printf@plt>
mov    $0x100,%edi
call   1070 <malloc@plt>
mov    %rax,%rbx
nopl   0x0(%rax)
mov    %r14,%rdi
mov    %rbx,%rsi
mov    $0x100,%edx
call   1080 <pedigree_module_get_depending@plt>
test   %rax,%rax
je     11cd <unload_module+0x5d>
mov    %rbx,%rdi
call   1050 <unload_module@plt>
cmp    $0xffffffff,%eax
jne    11a0 <unload_module+0x30>
jmp    11f8 <unload_module+0x88>
lea    0xe35(%rip),%rdi
jmp    11f0 <unload_module+0x80>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r14,%rdi
call   1090 <pedigree_module_unload@plt>
mov    %r14,%rdi
call   1060 <pedigree_module_is_loaded@plt>
test   %eax,%eax
je     1205 <unload_module+0x95>
lea    0xe46(%rip),%rdi
mov    %r14,%rsi
call   1040 <printf@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
xor    %eax,%eax
jmp    11fd <unload_module+0x8d>

LLVM IR (-O0):
@.str = [25 x i8] c"Module %s isn't loaded!\0A\00"
@.str.1 = [29 x i8] c"Module %s being unloaded...\0A\00"
@.str.2 = [34 x i8] c"Module %s couldn't been unloaded\0A\00"
define i32 @unload_module(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
store i8* %0, i8** %3
%5 = load i8*, i8** %3
%6 = call i32 @pedigree_module_is_loaded(i8* %5)
%7 = icmp ne i32 %6, 0
br i1 %7, label %11, label %8
8: ; preds = %1
%9 = load i8*, i8** %3
%10 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %9)
store i32 -1, i32* %2
br label %39
11: ; preds = %1
%12 = load i8*, i8** %3
%13 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8* %12)
%14 = call i64 @malloc(i32 256)
%15 = inttoptr i64 %14 to i8*
store i8* %15, i8** %4
br label %16
16: ; preds = %26, %11
%17 = load i8*, i8** %3
%18 = load i8*, i8** %4
%19 = call i64 @pedigree_module_get_depending(i8* %17, i8* %18, i32 256)
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %27
21: ; preds = %16
%22 = load i8*, i8** %4
%23 = call i32 @unload_module(i8* %22)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %26
25: ; preds = %21
store i32 -1, i32* %2
br label %39
26: ; preds = %21
br label %16
27: ; preds = %16
%28 = load i8*, i8** %4
%29 = call i32 @free(i8* %28)
%30 = load i8*, i8** %3
%31 = call i32 @pedigree_module_unload(i8* %30)
%32 = load i8*, i8** %3
%33 = call i32 @pedigree_module_is_loaded(i8* %32)
%34 = icmp ne i32 %33, 0
br i1 %34, label %36, label %35
35: ; preds = %27
store i32 0, i32* %2
br label %39
36: ; preds = %27
%37 = load i8*, i8** %3
%38 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i8* %37)
store i32 -1, i32* %2
br label %39
39: ; preds = %36, %35, %25, %8
%40 = load i32, i32* %2
ret i32 %40
}

LLVM IR (-O2):
@.str = [25 x i8] c"Module %s isn't loaded!\0A\00"
@.str.1 = [29 x i8] c"Module %s being unloaded...\0A\00"
@.str.2 = [34 x i8] c"Module %s couldn't been unloaded\0A\00"
define i32 @unload_module(i8* %0) {
%2 = tail call i32 @pedigree_module_is_loaded(i8* %0)
%3 = icmp eq i32 %2, 0
br i1 %3, label %19, label %4
4: ; preds = %1
%5 = tail call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%6 = tail call i64 @malloc(i32 256)
%7 = inttoptr i64 %6 to i8*
br label %8
8: ; preds = %11, %4
%9 = tail call i64 @pedigree_module_get_depending(i8* %0, i8* %7, i32 256)
%10 = icmp eq i64 %9, 0
br i1 %10, label %14, label %11
11: ; preds = %8
%12 = tail call i32 @unload_module(i8* %7)
%13 = icmp eq i32 %12, -1
br i1 %13, label %22, label %8
14: ; preds = %8
%15 = tail call i32 @free(i8* %7)
%16 = tail call i32 @pedigree_module_unload(i8* %0)
%17 = tail call i32 @pedigree_module_is_loaded(i8* %0)
%18 = icmp eq i32 %17, 0
br i1 %18, label %22, label %19
19: ; preds = %14, %1
%20 = phi i8* [ getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), %1 ], [ getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), %14 ]
%21 = tail call i32 @printf(i8* %20, i8* %0)
br label %22
22: ; preds = %11, %19, %14
%23 = phi i32 [ 0, %14 ], [ -1, %19 ], [ -1, %11 ]
ret i32 %23
}