#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*/ IPC_RMID;
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ shmctl(int, int /*<<< orphan*/, int /*<<< orphan*/ *);
scalar_t__ shmdt(void const *);
int free_shm_hugepage(int key, void *addr) {
  if (shmdt((const void *)addr) != 0) {
    perror("Detach failure");
    shmctl(key, IPC_RMID, NULL);
    return -1;
  }
  shmctl(key, IPC_RMID, NULL);
  return 0;
}


x86 assembly (clang, -O0):
<free_shm_hugepage>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <shmdt@plt>
cmp    $0x0,%rax
je     117f <free_shm_hugepage+0x4f>
lea    0xea7(%rip),%rdi
call   1050 <perror@plt>
mov    -0x8(%rbp),%edi
mov    0x2e78(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
mov    %eax,%edx
call   1040 <shmctl@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    119b <free_shm_hugepage+0x6b>
mov    -0x8(%rbp),%edi
mov    0x2e57(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
mov    %eax,%edx
call   1040 <shmctl@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<free_shm_hugepage>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebx
mov    %rsi,%rdi
call   1030 <shmdt@plt>
xor    %ebp,%ebp
test   %rax,%rax
jne    115f <free_shm_hugepage+0x2f>
mov    0x2e95(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1040 <shmctl@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe9a(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%ebp
jmp    1144 <free_shm_hugepage+0x14>

LLVM IR (-O0):
@.str = [15 x i8] c"Detach failure\00"
@IPC_RMID = global i32 0
define i32 @free_shm_hugepage(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
store i32 %0, i32* %4
store i8* %1, i8** %5
%6 = load i8*, i8** %5
%7 = call i64 @shmdt(i8* %6)
%8 = icmp ne i64 %7, 0
br i1 %8, label %9, label %14
9: ; preds = %2
%10 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%11 = load i32, i32* %4
%12 = load i32, i32* @IPC_RMID
%13 = call i32 @shmctl(i32 %11, i32 %12, i32* null)
store i32 -1, i32* %3
br label %18
14: ; preds = %2
%15 = load i32, i32* %4
%16 = load i32, i32* @IPC_RMID
%17 = call i32 @shmctl(i32 %15, i32 %16, i32* null)
store i32 0, i32* %3
br label %18
18: ; preds = %14, %9
%19 = load i32, i32* %3
ret i32 %19
}

LLVM IR (-O2):
@.str = [15 x i8] c"Detach failure\00"
@IPC_RMID = global i32 0
define i32 @free_shm_hugepage(i32 %0, i8* %1) {
%3 = tail call i64 @shmdt(i8* %1)
%4 = icmp eq i64 %3, 0
br i1 %4, label %7, label %5
5: ; preds = %2
%6 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %7
7: ; preds = %2, %5
%8 = phi i32 [ -1, %5 ], [ 0, %2 ]
%9 = load i32, i32* @IPC_RMID
%10 = tail call i32 @shmctl(i32 %0, i32 %9, i32* null)
ret i32 %8
}