#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*/ errno;
int /*<<< orphan*/ logmsg(int /*<<< orphan*/, char *, int, int /*<<< orphan*/,
                          ...);
scalar_t__ shmctl(int, int /*<<< orphan*/, int /*<<< orphan*/ *);
scalar_t__ shmdt(void *);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
void release_shared_memory(int shmid, void *shmseg) {
  if (shmseg != NULL) {
    if (shmdt(shmseg) < 0)
      logmsg(0, "shmdt() failed: %s errno=%d\n", strerror(errno), errno);
  }
  if (shmctl(shmid, IPC_RMID, NULL) < 0)
    logmsg(0, "failed to delete shmid (%x): %s (errno=%d)\n", shmid,
           strerror(errno), errno);
}


x86 assembly (clang, -O0):
<release_shared_memory>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     119b <release_shared_memory+0x5b>
mov    -0x10(%rbp),%rdi
call   1030 <shmdt@plt>
cmp    $0x0,%rax
jge    1196 <release_shared_memory+0x56>
mov    0x2e6c(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    %eax,%edx
mov    0x2e5c(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
lea    0xe71(%rip),%rsi
mov    $0x0,%al
call   1040 <logmsg@plt>
jmp    119b <release_shared_memory+0x5b>
mov    -0x4(%rbp),%edi
mov    0x2e33(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
mov    %eax,%edx
call   1050 <shmctl@plt>
cmp    $0x0,%rax
jge    11ed <release_shared_memory+0xad>
mov    -0x4(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    0x2e19(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x14(%rbp),%edx
mov    %eax,%ecx
mov    0x2e06(%rip),%rax
mov    (%rax),%r8d
xor    %edi,%edi
lea    0xe37(%rip),%rsi
mov    $0x0,%al
call   1040 <logmsg@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<release_shared_memory>:
push   %r14
push   %rbx
push   %rax
mov    %edi,%ebx
test   %rsi,%rsi
je     117c <release_shared_memory+0x3c>
mov    %rsi,%rdi
call   1030 <shmdt@plt>
test   %rax,%rax
jns    117c <release_shared_memory+0x3c>
mov    0x2e81(%rip),%r14
mov    (%r14),%edi
call   1060 <strerror@plt>
mov    (%r14),%ecx
lea    0xe8f(%rip),%rsi
xor    %edi,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <logmsg@plt>
mov    0x2e55(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1050 <shmctl@plt>
test   %rax,%rax
js     119b <release_shared_memory+0x5b>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2e3e(%rip),%r14
mov    (%r14),%edi
call   1060 <strerror@plt>
mov    (%r14),%r8d
lea    0xe69(%rip),%rsi
xor    %edi,%edi
mov    %ebx,%edx
mov    %eax,%ecx
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1040 <logmsg@plt>

LLVM IR (-O0):
@.str = [29 x i8] c"shmdt() failed: %s errno=%d\0A\00"
@errno = global i32 0
@IPC_RMID = global i32 0
@.str.1 = [44 x i8] c"failed to delete shmid (%x): %s (errno=%d)\0A\00"
define void @release_shared_memory(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
store i32 %0, i32* %3
store i8* %1, i8** %4
%5 = load i8*, i8** %4
%6 = icmp ne i8* %5, null
br i1 %6, label %7, label %17
7: ; preds = %2
%8 = load i8*, i8** %4
%9 = call i64 @shmdt(i8* %8)
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %16
11: ; preds = %7
%12 = load i32, i32* @errno
%13 = call i32 @strerror(i32 %12)
%14 = load i32, i32* @errno
%15 = call i32 (i32, i8*, i32, i32, ...) @logmsg(i32 0, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %13, i32 %14)
br label %16
16: ; preds = %11, %7
br label %17
17: ; preds = %16, %2
%18 = load i32, i32* %3
%19 = load i32, i32* @IPC_RMID
%20 = call i64 @shmctl(i32 %18, i32 %19, i32* null)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %28
22: ; preds = %17
%23 = load i32, i32* %3
%24 = load i32, i32* @errno
%25 = call i32 @strerror(i32 %24)
%26 = load i32, i32* @errno
%27 = call i32 (i32, i8*, i32, i32, ...) @logmsg(i32 0, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0), i32 %23, i32 %25, i32 %26)
br label %28
28: ; preds = %22, %17
ret void
}

LLVM IR (-O2):
@.str = [29 x i8] c"shmdt() failed: %s errno=%d\0A\00"
@errno = global i32 0
@IPC_RMID = global i32 0
@.str.1 = [44 x i8] c"failed to delete shmid (%x): %s (errno=%d)\0A\00"
define void @release_shared_memory(i32 %0, i8* %1) {
%3 = icmp eq i8* %1, null
br i1 %3, label %12, label %4
4: ; preds = %2
%5 = tail call i64 @shmdt(i8* %1)
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %12
7: ; preds = %4
%8 = load i32, i32* @errno
%9 = tail call i32 @strerror(i32 %8)
%10 = load i32, i32* @errno
%11 = tail call i32 (i32, i8*, i32, i32, ...) @logmsg(i32 0, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %9, i32 %10)
br label %12
12: ; preds = %4, %7, %2
%13 = load i32, i32* @IPC_RMID
%14 = tail call i64 @shmctl(i32 %0, i32 %13, i32* null)
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %21
16: ; preds = %12
%17 = load i32, i32* @errno
%18 = tail call i32 @strerror(i32 %17)
%19 = load i32, i32* @errno
%20 = tail call i32 (i32, i8*, i32, i32, ...) @logmsg(i32 0, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0), i32 %0, i32 %18, i32 %19)
br label %21
21: ; preds = %16, %12
ret void
}