#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 */
struct shmid_ds {
  int dummy;
};
struct shmid {
  int dummy;
};
typedef char *key_t;

/* Variables and functions */
int /*<<< orphan*/ IPC_RMID;
int /*<<< orphan*/ getpid();
int /*<<< orphan*/ noprintf(char *, int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ perror(char *);
int shmctl(char *, int /*<<< orphan*/, struct shmid_ds *);
int shmdt(void *);
int rm_shared_mem(key_t shm_id, char *shm_addr, int cmd) {
  struct shmid *shmbuf = NULL;
  noprintf("pid[%d]: rm_shared_mem(): shm_id = %d shm_addr = %#x cmd = %d\n",
           getpid(), shm_id, shm_addr, cmd);
  if (shmctl(shm_id, IPC_RMID, (struct shmid_ds *)shmbuf) == -1) {
    noprintf("pid[%d]: rm_shared_mem(): shmctl unable to remove shm_id[%d]\n",
             getpid(), shm_id);
    perror("rm_shared_mem(): shmctl()");
    return -1;
  }
  if (cmd) {
    if (shmdt((void *)shm_addr) == -1) {
      noprintf("pid[%d]:rm_shared_mem(): shmdt unable to detach addr = %#x\n",
               getpid(), shm_addr);
      perror("rm_shared_mem(): shmdt()");
      return -1;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<rm_shared_mem>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movq   $0x0,-0x28(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    -0x1c(%rbp),%r8d
lea    0xe79(%rip),%rdi
mov    $0x0,%al
call   1050 <noprintf@plt>
mov    -0x10(%rbp),%rdi
mov    0x2e47(%rip),%rax
mov    (%rax),%esi
mov    -0x28(%rbp),%rdx
call   1060 <shmctl@plt>
cmp    $0xffffffff,%eax
jne    11e0 <rm_shared_mem+0x90>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
mov    -0x10(%rbp),%rdx
lea    0xe7e(%rip),%rdi
mov    $0x0,%al
call   1050 <noprintf@plt>
lea    0xeae(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    123b <rm_shared_mem+0xeb>
cmpl   $0x0,-0x1c(%rbp)
je     1234 <rm_shared_mem+0xe4>
mov    -0x18(%rbp),%rdi
call   1030 <shmdt@plt>
cmp    $0xffffffff,%eax
jne    122f <rm_shared_mem+0xdf>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
mov    -0x18(%rbp),%rdx
lea    0xe87(%rip),%rdi
mov    $0x0,%al
call   1050 <noprintf@plt>
lea    0xeb5(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    123b <rm_shared_mem+0xeb>
jmp    1234 <rm_shared_mem+0xe4>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rm_shared_mem>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %rsi,%r15
mov    %rdi,%rbx
xor    %r14d,%r14d
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe90(%rip),%rdi
mov    %eax,%esi
mov    %rbx,%rdx
mov    %r15,%rcx
mov    %ebp,%r8d
xor    %eax,%eax
call   1050 <noprintf@plt>
mov    0x2e57(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
xor    %edx,%edx
call   1060 <shmctl@plt>
cmp    $0xffffffff,%eax
je     11b9 <rm_shared_mem+0x69>
test   %ebp,%ebp
je     11ab <rm_shared_mem+0x5b>
mov    %r15,%rdi
call   1030 <shmdt@plt>
cmp    $0xffffffff,%eax
je     11dc <rm_shared_mem+0x8c>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe78(%rip),%rdi
mov    %eax,%esi
mov    %rbx,%rdx
xor    %eax,%eax
call   1050 <noprintf@plt>
lea    0xea3(%rip),%rdi
jmp    11fd <rm_shared_mem+0xad>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xead(%rip),%rdi
mov    %eax,%esi
mov    %r15,%rdx
xor    %eax,%eax
call   1050 <noprintf@plt>
lea    0xed6(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%r14d
jmp    11ab <rm_shared_mem+0x5b>

LLVM IR (-O0):
%struct.shmid = type { i32 }
%struct.shmid_ds = type { i32 }
@.str = [63 x i8] c"pid[%d]: rm_shared_mem(): shm_id = %d shm_addr = %#x cmd = %d\0A\00"
@IPC_RMID = global i32 0
@.str.1 = [62 x i8] c"pid[%d]: rm_shared_mem(): shmctl unable to remove shm_id[%d]\0A\00"
@.str.2 = [26 x i8] c"rm_shared_mem(): shmctl()\00"
@.str.3 = [60 x i8] c"pid[%d]:rm_shared_mem(): shmdt unable to detach addr = %#x\0A\00"
@.str.4 = [25 x i8] c"rm_shared_mem(): shmdt()\00"
define i32 @rm_shared_mem(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca %struct.shmid*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store %struct.shmid* null, %struct.shmid** %8
%9 = call i32 (...) @getpid()
%10 = load i8*, i8** %5
%11 = load i8*, i8** %6
%12 = load i32, i32* %7
%13 = call i32 (i8*, i32, i8*, ...) @noprintf(i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str, i64 0, i64 0), i32 %9, i8* %10, i8* %11, i32 %12)
%14 = load i8*, i8** %5
%15 = load i32, i32* @IPC_RMID
%16 = load %struct.shmid*, %struct.shmid** %8
%17 = bitcast %struct.shmid* %16 to %struct.shmid_ds*
%18 = call i32 @shmctl(i8* %14, i32 %15, %struct.shmid_ds* %17)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %25
20: ; preds = %3
%21 = call i32 (...) @getpid()
%22 = load i8*, i8** %5
%23 = call i32 (i8*, i32, i8*, ...) @noprintf(i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.1, i64 0, i64 0), i32 %21, i8* %22)
%24 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %4
br label %39
25: ; preds = %3
%26 = load i32, i32* %7
%27 = icmp ne i32 %26, 0
br i1 %27, label %28, label %38
28: ; preds = %25
%29 = load i8*, i8** %6
%30 = call i32 @shmdt(i8* %29)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %37
32: ; preds = %28
%33 = call i32 (...) @getpid()
%34 = load i8*, i8** %6
%35 = call i32 (i8*, i32, i8*, ...) @noprintf(i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str.3, i64 0, i64 0), i32 %33, i8* %34)
%36 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %4
br label %39
37: ; preds = %28
br label %38
38: ; preds = %37, %25
store i32 0, i32* %4
br label %39
39: ; preds = %38, %32, %20
%40 = load i32, i32* %4
ret i32 %40
}

LLVM IR (-O2):
%struct.shmid = type { i32 }
%struct.shmid_ds = type { i32 }
@.str = [63 x i8] c"pid[%d]: rm_shared_mem(): shm_id = %d shm_addr = %#x cmd = %d\0A\00"
@IPC_RMID = global i32 0
@.str.1 = [62 x i8] c"pid[%d]: rm_shared_mem(): shmctl unable to remove shm_id[%d]\0A\00"
@.str.2 = [26 x i8] c"rm_shared_mem(): shmctl()\00"
@.str.3 = [60 x i8] c"pid[%d]:rm_shared_mem(): shmdt unable to detach addr = %#x\0A\00"
@.str.4 = [25 x i8] c"rm_shared_mem(): shmdt()\00"
define i32 @rm_shared_mem(i8* %0, i8* %1, i32 %2) {
%4 = tail call i32 (...) @getpid()
%5 = tail call i32 (i8*, i32, i8*, ...) @noprintf(i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str, i64 0, i64 0), i32 %4, i8* %0, i8* %1, i32 %2)
%6 = load i32, i32* @IPC_RMID
%7 = tail call i32 @shmctl(i8* %0, i32 %6, %struct.shmid_ds* null)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %12
9: ; preds = %3
%10 = tail call i32 (...) @getpid()
%11 = tail call i32 (i8*, i32, i8*, ...) @noprintf(i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.1, i64 0, i64 0), i32 %10, i8* %0)
br label %20
12: ; preds = %3
%13 = icmp eq i32 %2, 0
br i1 %13, label %23, label %14
14: ; preds = %12
%15 = tail call i32 @shmdt(i8* %1)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %23
17: ; preds = %14
%18 = tail call i32 (...) @getpid()
%19 = tail call i32 (i8*, i32, i8*, ...) @noprintf(i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str.3, i64 0, i64 0), i32 %18, i8* %1)
br label %20
20: ; preds = %9, %17
%21 = phi i8* [ getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), %17 ], [ getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), %9 ]
%22 = tail call i32 @perror(i8* %21)
br label %23
23: ; preds = %20, %12, %14
%24 = phi i32 [ 0, %14 ], [ 0, %12 ], [ -1, %20 ]
ret i32 %24
}