#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 {
  scalar_t__ shm_nattch;
};

/* Variables and functions */
int EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ IPC_RMID;
int /*<<< orphan*/ SHM_INFO;
int /*<<< orphan*/ SHM_STAT;
int /*<<< orphan*/ printf(char *, ...);
int shmctl(int, int /*<<< orphan*/, struct shmid_ds *);
int main() {
  int ret;
  struct shmid_ds shm_info_buf;
  ret = EXIT_SUCCESS;
  int max_shm_kid = shmctl(0, SHM_INFO, &shm_info_buf);
  int shm_kid;
  for (shm_kid = max_shm_kid; shm_kid >= 0; shm_kid--) {
    struct shmid_ds buf;
    int shm_id = shmctl(shm_kid, SHM_STAT, &buf);
    if (shm_id == -1)
      continue;
    if (buf.shm_nattch == 0) {
      printf("Clearing unused shared memory segment %d.\n", shm_id);
      if (shmctl(shm_id, IPC_RMID, NULL) != 0) {
        printf("failed to remove %d.\n");
        ret = EXIT_FAILURE;
      }
    }
  }
  return ret;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e8a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8(%rbp)
mov    0x2ea6(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    -0x10(%rbp),%rdx
call   1040 <shmctl@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jl     11f5 <main+0xd5>
mov    -0x18(%rbp),%edi
mov    0x2e6c(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1040 <shmctl@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    1189 <main+0x69>
jmp    11e7 <main+0xc7>
cmpq   $0x0,-0x20(%rbp)
jne    11e2 <main+0xc2>
mov    -0x24(%rbp),%esi
lea    0xe62(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x24(%rbp),%edi
mov    0x2e19(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
mov    %eax,%edx
call   1040 <shmctl@plt>
cmp    $0x0,%eax
je     11dd <main+0xbd>
lea    0xe61(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2df8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8(%rbp)
jmp    11e2 <main+0xc2>
jmp    11e7 <main+0xc7>
mov    -0x18(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x18(%rbp)
jmp    1158 <main+0x38>
mov    -0x8(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    0x2e8b(%rip),%rax
mov    (%rax),%r14d
mov    0x2ea9(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%rdx
xor    %edi,%edi
call   1040 <shmctl@plt>
test   %eax,%eax
js     11d0 <main+0xb0>
mov    %eax,%ebx
mov    0x2e7e(%rip),%r12
lea    0x8(%rsp),%r13
mov    0x2e62(%rip),%r15
jmp    1179 <main+0x59>
nopl   0x0(%rax,%rax,1)
lea    -0x1(%rbx),%eax
test   %ebx,%ebx
mov    %eax,%ebx
jle    11d0 <main+0xb0>
mov    (%r12),%esi
mov    %ebx,%edi
mov    %r13,%rdx
call   1040 <shmctl@plt>
cmp    $0xffffffff,%eax
je     1170 <main+0x50>
cmpq   $0x0,0x8(%rsp)
jne    1170 <main+0x50>
mov    %eax,%ebp
lea    0xe63(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1030 <printf@plt>
mov    (%r15),%esi
mov    %ebp,%edi
xor    %edx,%edx
call   1040 <shmctl@plt>
test   %eax,%eax
je     1170 <main+0x50>
lea    0xe6e(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
mov    0x2e05(%rip),%rax
mov    (%rax),%r14d
jmp    1170 <main+0x50>
mov    %r14d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.shmid_ds = type { i64 }
@EXIT_SUCCESS = global i32 0
@SHM_INFO = global i32 0
@SHM_STAT = global i32 0
@.str = [43 x i8] c"Clearing unused shared memory segment %d.\0A\00"
@IPC_RMID = global i32 0
@.str.1 = [22 x i8] c"failed to remove %d.\0A\00"
@EXIT_FAILURE = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.shmid_ds
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.shmid_ds
%7 = alloca i32
store i32 0, i32* %1
%8 = load i32, i32* @EXIT_SUCCESS
store i32 %8, i32* %2
%9 = load i32, i32* @SHM_INFO
%10 = call i32 @shmctl(i32 0, i32 %9, %struct.shmid_ds* %3)
store i32 %10, i32* %4
%11 = load i32, i32* %4
store i32 %11, i32* %5
br label %12
12: ; preds = %38, %0
%13 = load i32, i32* %5
%14 = icmp sge i32 %13, 0
br i1 %14, label %15, label %41
15: ; preds = %12
%16 = load i32, i32* %5
%17 = load i32, i32* @SHM_STAT
%18 = call i32 @shmctl(i32 %16, i32 %17, %struct.shmid_ds* %6)
store i32 %18, i32* %7
%19 = load i32, i32* %7
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %22
21: ; preds = %15
br label %38
22: ; preds = %15
%23 = getelementptr inbounds %struct.shmid_ds, %struct.shmid_ds* %6, i32 0, i32 0
%24 = load i64, i64* %23
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %37
26: ; preds = %22
%27 = load i32, i32* %7
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0), i32 %27)
%29 = load i32, i32* %7
%30 = load i32, i32* @IPC_RMID
%31 = call i32 @shmctl(i32 %29, i32 %30, %struct.shmid_ds* null)
%32 = icmp ne i32 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %26
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
%35 = load i32, i32* @EXIT_FAILURE
store i32 %35, i32* %2
br label %36
36: ; preds = %33, %26
br label %37
37: ; preds = %36, %22
br label %38
38: ; preds = %37, %21
%39 = load i32, i32* %5
%40 = add nsw i32 %39, -1
store i32 %40, i32* %5
br label %12
41: ; preds = %12
%42 = load i32, i32* %2
ret i32 %42
}

LLVM IR (-O2):
%struct.shmid_ds = type { i64 }
@EXIT_SUCCESS = global i32 0
@SHM_INFO = global i32 0
@SHM_STAT = global i32 0
@.str = [43 x i8] c"Clearing unused shared memory segment %d.\0A\00"
@IPC_RMID = global i32 0
@.str.1 = [22 x i8] c"failed to remove %d.\0A\00"
@EXIT_FAILURE = global i32 0
define i32 @main() {
%1 = alloca %struct.shmid_ds
%2 = alloca %struct.shmid_ds
%3 = bitcast %struct.shmid_ds* %1 to i8*
%4 = load i32, i32* @EXIT_SUCCESS
%5 = load i32, i32* @SHM_INFO
%6 = call i32 @shmctl(i32 0, i32 %5, %struct.shmid_ds* %1)
%7 = icmp sgt i32 %6, -1
br i1 %7, label %8, label %32
8: ; preds = %0
%9 = bitcast %struct.shmid_ds* %2 to i8*
%10 = getelementptr inbounds %struct.shmid_ds, %struct.shmid_ds* %2, i64 0, i32 0
br label %11
11: ; preds = %8, %28
%12 = phi i32 [ %6, %8 ], [ %30, %28 ]
%13 = phi i32 [ %4, %8 ], [ %29, %28 ]
%14 = load i32, i32* @SHM_STAT
%15 = call i32 @shmctl(i32 %12, i32 %14, %struct.shmid_ds* %2)
%16 = icmp ne i32 %15, -1
%17 = load i64, i64* %10
%18 = icmp eq i64 %17, 0
%19 = select i1 %16, i1 %18, i1 false
br i1 %19, label %20, label %28
20: ; preds = %11
%21 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0), i32 %15)
%22 = load i32, i32* @IPC_RMID
%23 = call i32 @shmctl(i32 %15, i32 %22, %struct.shmid_ds* null)
%24 = icmp eq i32 %23, 0
br i1 %24, label %28, label %25
25: ; preds = %20
%26 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
%27 = load i32, i32* @EXIT_FAILURE
br label %28
28: ; preds = %25, %20, %11
%29 = phi i32 [ %13, %11 ], [ %27, %25 ], [ %13, %20 ]
%30 = add nsw i32 %12, -1
%31 = icmp sgt i32 %12, 0
br i1 %31, label %11, label %32
32: ; preds = %28, %0
%33 = phi i32 [ %4, %0 ], [ %29, %28 ]
ret i32 %33
}