#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*/ control_write(char const *);
int dlclose(void *);
int /*<<< orphan*/ lock_data();
int /*<<< orphan*/ unlock_data();
int close_handle(void *handle) {
  unlock_data();
  int res = dlclose(handle);
  if (res != 0) {
    const char *error;
    error = "failed";
    lock_data();
    control_write("dlclose(): ");
    control_write(error);
    control_write("\n");
    return res;
  }
  lock_data();
  return 0;
}


x86 assembly (clang, -O0):
<close_handle>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    $0x0,%al
call   1060 <unlock_data@plt>
mov    -0x10(%rbp),%rdi
call   1050 <dlclose@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     11a7 <close_handle+0x67>
lea    0xe90(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   1040 <lock_data@plt>
lea    0xe85(%rip),%rdi
call   1030 <control_write@plt>
mov    -0x20(%rbp),%rdi
call   1030 <control_write@plt>
lea    0xe7c(%rip),%rdi
call   1030 <control_write@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11b5 <close_handle+0x75>
mov    $0x0,%al
call   1040 <lock_data@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<close_handle>:
push   %rbx
mov    %rdi,%rbx
xor    %eax,%eax
call   1060 <unlock_data@plt>
mov    %rbx,%rdi
call   1050 <dlclose@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1040 <lock_data@plt>
test   %ebx,%ebx
je     1184 <close_handle+0x44>
lea    0xea0(%rip),%rdi
call   1030 <control_write@plt>
lea    0xe8d(%rip),%rdi
call   1030 <control_write@plt>
lea    0xe94(%rip),%rdi
call   1030 <control_write@plt>
mov    %ebx,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [7 x i8] c"failed\00"
@.str.1 = [12 x i8] c"dlclose(): \00"
@.str.2 = [2 x i8] c"\0A\00"
define i32 @close_handle(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
store i8* %0, i8** %3
%6 = call i32 (...) @unlock_data()
%7 = load i8*, i8** %3
%8 = call i32 @dlclose(i8* %7)
store i32 %8, i32* %4
%9 = load i32, i32* %4
%10 = icmp ne i32 %9, 0
br i1 %10, label %11, label %18
11: ; preds = %1
store i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8** %5
%12 = call i32 (...) @lock_data()
%13 = call i32 @control_write(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%14 = load i8*, i8** %5
%15 = call i32 @control_write(i8* %14)
%16 = call i32 @control_write(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%17 = load i32, i32* %4
store i32 %17, i32* %2
br label %20
18: ; preds = %1
%19 = call i32 (...) @lock_data()
store i32 0, i32* %2
br label %20
20: ; preds = %18, %11
%21 = load i32, i32* %2
ret i32 %21
}

LLVM IR (-O2):
@.str = [7 x i8] c"failed\00"
@.str.1 = [12 x i8] c"dlclose(): \00"
@.str.2 = [2 x i8] c"\0A\00"
define i32 @close_handle(i8* %0) {
%2 = tail call i32 (...) @unlock_data()
%3 = tail call i32 @dlclose(i8* %0)
%4 = icmp eq i32 %3, 0
%5 = tail call i32 (...) @lock_data()
br i1 %4, label %10, label %6
6: ; preds = %1
%7 = tail call i32 @control_write(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%8 = tail call i32 @control_write(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%9 = tail call i32 @control_write(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %10
10: ; preds = %1, %6
ret i32 %3
}