#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 /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ IPC_RMID;
int /*<<< orphan*/ IPC_SET;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
scalar_t__ shmat(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int shmctl(int, int /*<<< orphan*/, struct shmid_ds *);
int shmdt(char *);
int /*<<< orphan*/ sscanf(char *, char *, int *);
int /*<<< orphan*/ system(char *);
int main(int argc, char *argv[]) {
  if (argc <= 1) {
    printf("Usage: ./shmdt shmid ...\n");
    return 0;
  }
  int i = 0;
  int cnt = argc - 1;
  int shmids[1024];
  for (i = 0; i < cnt; ++i) {
    sscanf(argv[i + 1], "%d", &shmids[i]);
  }
  printf("删除之前：\n");
  system("ipcs -m");
  for (i = 0; i < cnt; ++i) {
    char *p = (char *)shmat(shmids[i], NULL, 0);
    if (p == (char *)-1) {
      perror("shmat");
      exit(EXIT_FAILURE);
    }
    if (shmdt(p) == -1) {
      perror("shmdt");
      exit(EXIT_FAILURE);
    }
    struct shmid_ds val;
    val.shm_nattch = 0;
    if (shmctl(shmids[i], IPC_SET, &val) == -1) {
      perror("shmctl");
      exit(EXIT_FAILURE);
    }
    if (shmctl(shmids[i], IPC_RMID, NULL) == -1) {
      perror("shmctl");
      exit(EXIT_FAILURE);
    }
  }
  printf("删除之后：\n");
  system("ipcs -m");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x1,-0x8(%rbp)
jg     11bb <main+0x3b>
lea    0xe56(%rip),%rdi
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1380 <main+0x200>
movl   $0x0,-0x14(%rbp)
mov    -0x8(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x18(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    121b <main+0x9b>
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rdi
movslq -0x14(%rbp),%rax
lea    -0x1020(%rbp),%rdx
shl    $0x2,%rax
add    %rax,%rdx
lea    0xe12(%rip),%rsi
call   1060 <sscanf@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11d2 <main+0x52>
lea    0xdfb(%rip),%rdi
call   1050 <printf@plt>
lea    0xe00(%rip),%rdi
call   1040 <system@plt>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    1361 <main+0x1e1>
movslq -0x14(%rbp),%rax
mov    -0x1020(%rbp,%rax,4),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1090 <shmat@plt>
mov    %rax,-0x1028(%rbp)
mov    $0xffffffffffffffff,%rax
cmp    %rax,-0x1028(%rbp)
jne    1291 <main+0x111>
lea    0xdb8(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d4e(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x1028(%rbp),%rdi
call   1030 <shmdt@plt>
cmp    $0xffffffff,%eax
jne    12c0 <main+0x140>
lea    0xd8f(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d1f(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
movq   $0x0,-0x1030(%rbp)
movslq -0x14(%rbp),%rax
mov    -0x1020(%rbp,%rax,4),%edi
mov    0x2d0b(%rip),%rax
mov    (%rax),%esi
lea    -0x1030(%rbp),%rdx
call   1070 <shmctl@plt>
cmp    $0xffffffff,%eax
jne    130e <main+0x18e>
lea    0xd47(%rip),%rdi
call   1080 <perror@plt>
mov    0x2cd1(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
movslq -0x14(%rbp),%rax
mov    -0x1020(%rbp,%rax,4),%edi
mov    0x2cb0(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
mov    %eax,%edx
call   1070 <shmctl@plt>
cmp    $0xffffffff,%eax
jne    134e <main+0x1ce>
lea    0xd07(%rip),%rdi
call   1080 <perror@plt>
mov    0x2c91(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
jmp    1353 <main+0x1d3>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    123a <main+0xba>
lea    0xce1(%rip),%rdi
call   1050 <printf@plt>
lea    0xcba(%rip),%rdi
call   1040 <system@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1018,%rsp
cmp    $0x1,%edi
jg     11a7 <main+0x27>
lea    0xea1(%rip),%rdi
call   1030 <puts@plt>
jmp    12a3 <main+0x123>
mov    %rsi,%r13
mov    %edi,%r14d
lea    -0x1(%r14),%r12d
lea    0x0(,%r12,4),%rbx
xor    %ebp,%ebp
lea    0xe3e(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    0x8(%r13,%rbp,2),%rdi
lea    (%rsp,%rbp,1),%rdx
add    $0x10,%rdx
mov    %r15,%rsi
call   1060 <sscanf@plt>
add    $0x4,%rbp
cmp    %rbp,%rbx
jne    11d0 <main+0x50>
lea    0xe29(%rip),%rdi
call   1030 <puts@plt>
lea    0xe02(%rip),%rdi
call   1050 <system@plt>
cmp    $0x2,%r14d
jl     128b <main+0x10b>
mov    %r12d,%r15d
xor    %ebx,%ebx
mov    0x2dd0(%rip),%r12
lea    0x8(%rsp),%r14
mov    0x2dac(%rip),%r13
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    0x10(%rsp,%rbx,4),%edi
xor    %esi,%esi
xor    %edx,%edx
call   1090 <shmat@plt>
cmp    $0xffffffffffffffff,%rax
je     12c0 <main+0x140>
mov    %rax,%rdi
call   1040 <shmdt@plt>
cmp    $0xffffffff,%eax
je     12c9 <main+0x149>
movq   $0x0,0x8(%rsp)
mov    0x10(%rsp,%rbx,4),%edi
mov    (%r12),%esi
mov    %r14,%rdx
call   1070 <shmctl@plt>
cmp    $0xffffffff,%eax
je     12b7 <main+0x137>
mov    0x10(%rsp,%rbx,4),%edi
mov    0x0(%r13),%esi
xor    %edx,%edx
call   1070 <shmctl@plt>
cmp    $0xffffffff,%eax
je     12b7 <main+0x137>
add    $0x1,%rbx
cmp    %rbx,%r15
jne    1230 <main+0xb0>
lea    0xd9c(%rip),%rdi
call   1030 <puts@plt>
lea    0xd65(%rip),%rdi
call   1050 <system@plt>
xor    %eax,%eax
add    $0x1018,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd59(%rip),%rdi
jmp    12d0 <main+0x150>
lea    0xd44(%rip),%rdi
jmp    12d0 <main+0x150>
lea    0xd41(%rip),%rdi
call   1080 <perror@plt>
mov    0x2cfc(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.shmid_ds = type { i64 }
@.str = [26 x i8] c"Usage: ./shmdt shmid ...\0A\00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [17 x i8] c"\E5\88\A0\E9\99\A4\E4\B9\8B\E5\89\8D\EF\BC\9A\0A\00"
@.str.3 = [8 x i8] c"ipcs -m\00"
@.str.4 = [6 x i8] c"shmat\00"
@EXIT_FAILURE = global i32 0
@.str.5 = [6 x i8] c"shmdt\00"
@IPC_SET = global i32 0
@.str.6 = [7 x i8] c"shmctl\00"
@IPC_RMID = global i32 0
@.str.7 = [17 x i8] c"\E5\88\A0\E9\99\A4\E4\B9\8B\E5\90\8E\EF\BC\9A\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca [1024 x i32]
%9 = alloca i8*
%10 = alloca %struct.shmid_ds
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp sle i32 %11, 1
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %3
br label %96
15: ; preds = %2
store i32 0, i32* %6
%16 = load i32, i32* %4
%17 = sub nsw i32 %16, 1
store i32 %17, i32* %7
store i32 0, i32* %6
br label %18
18: ; preds = %33, %15
%19 = load i32, i32* %6
%20 = load i32, i32* %7
%21 = icmp slt i32 %19, %20
br i1 %21, label %22, label %36
22: ; preds = %18
%23 = load i8**, i8*** %5
%24 = load i32, i32* %6
%25 = add nsw i32 %24, 1
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8*, i8** %23, i64 %26
%28 = load i8*, i8** %27
%29 = load i32, i32* %6
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds [1024 x i32], [1024 x i32]* %8, i64 0, i64 %30
%32 = call i32 @sscanf(i8* %28, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %31)
br label %33
33: ; preds = %22
%34 = load i32, i32* %6
%35 = add nsw i32 %34, 1
store i32 %35, i32* %6
br label %18
36: ; preds = %18
%37 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
%38 = call i32 @system(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %6
br label %39
39: ; preds = %90, %36
%40 = load i32, i32* %6
%41 = load i32, i32* %7
%42 = icmp slt i32 %40, %41
br i1 %42, label %43, label %93
43: ; preds = %39
%44 = load i32, i32* %6
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds [1024 x i32], [1024 x i32]* %8, i64 0, i64 %45
%47 = load i32, i32* %46
%48 = call i64 @shmat(i32 %47, i32* null, i32 0)
%49 = inttoptr i64 %48 to i8*
store i8* %49, i8** %9
%50 = load i8*, i8** %9
%51 = icmp eq i8* %50, inttoptr (i64 -1 to i8*)
br i1 %51, label %52, label %56
52: ; preds = %43
%53 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%54 = load i32, i32* @EXIT_FAILURE
%55 = call i32 @exit(i32 %54)
unreachable
56: ; preds = %43
%57 = load i8*, i8** %9
%58 = call i32 @shmdt(i8* %57)
%59 = icmp eq i32 %58, -1
br i1 %59, label %60, label %64
60: ; preds = %56
%61 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
%62 = load i32, i32* @EXIT_FAILURE
%63 = call i32 @exit(i32 %62)
unreachable
64: ; preds = %56
%65 = getelementptr inbounds %struct.shmid_ds, %struct.shmid_ds* %10, i32 0, i32 0
store i64 0, i64* %65
%66 = load i32, i32* %6
%67 = sext i32 %66 to i64
%68 = getelementptr inbounds [1024 x i32], [1024 x i32]* %8, i64 0, i64 %67
%69 = load i32, i32* %68
%70 = load i32, i32* @IPC_SET
%71 = call i32 @shmctl(i32 %69, i32 %70, %struct.shmid_ds* %10)
%72 = icmp eq i32 %71, -1
br i1 %72, label %73, label %77
73: ; preds = %64
%74 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0))
%75 = load i32, i32* @EXIT_FAILURE
%76 = call i32 @exit(i32 %75)
unreachable
77: ; preds = %64
%78 = load i32, i32* %6
%79 = sext i32 %78 to i64
%80 = getelementptr inbounds [1024 x i32], [1024 x i32]* %8, i64 0, i64 %79
%81 = load i32, i32* %80
%82 = load i32, i32* @IPC_RMID
%83 = call i32 @shmctl(i32 %81, i32 %82, %struct.shmid_ds* null)
%84 = icmp eq i32 %83, -1
br i1 %84, label %85, label %89
85: ; preds = %77
%86 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0))
%87 = load i32, i32* @EXIT_FAILURE
%88 = call i32 @exit(i32 %87)
unreachable
89: ; preds = %77
br label %90
90: ; preds = %89
%91 = load i32, i32* %6
%92 = add nsw i32 %91, 1
store i32 %92, i32* %6
br label %39
93: ; preds = %39
%94 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.7, i64 0, i64 0))
%95 = call i32 @system(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %3
br label %96
96: ; preds = %93, %13
%97 = load i32, i32* %3
ret i32 %97
}

LLVM IR (-O2):
%struct.shmid_ds = type { i64 }
@.str.1 = [3 x i8] c"%d\00"
@.str.3 = [8 x i8] c"ipcs -m\00"
@.str.4 = [6 x i8] c"shmat\00"
@EXIT_FAILURE = global i32 0
@.str.5 = [6 x i8] c"shmdt\00"
@IPC_SET = global i32 0
@.str.6 = [7 x i8] c"shmctl\00"
@IPC_RMID = global i32 0
@str = [16 x i8] c"\E5\88\A0\E9\99\A4\E4\B9\8B\E5\89\8D\EF\BC\9A\00"
@str.8 = [16 x i8] c"\E5\88\A0\E9\99\A4\E4\B9\8B\E5\90\8E\EF\BC\9A\00"
@str.9 = [25 x i8] c"Usage: ./shmdt shmid ...\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [1024 x i32]
%4 = alloca %struct.shmid_ds
%5 = icmp slt i32 %0, 2
br i1 %5, label %6, label %8
6: ; preds = %2
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.9, i64 0, i64 0))
br label %70
8: ; preds = %2
%9 = add nsw i32 %0, -1
%10 = bitcast [1024 x i32]* %3 to i8*
%11 = zext i32 %9 to i64
br label %12
12: ; preds = %8, %12
%13 = phi i64 [ 0, %8 ], [ %14, %12 ]
%14 = add nuw nsw i64 %13, 1
%15 = getelementptr inbounds i8*, i8** %1, i64 %14
%16 = load i8*, i8** %15
%17 = getelementptr inbounds [1024 x i32], [1024 x i32]* %3, i64 0, i64 %13
%18 = call i32 @sscanf(i8* %16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %17)
%19 = icmp eq i64 %14, %11
br i1 %19, label %20, label %12
20: ; preds = %12
%21 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str, i64 0, i64 0))
%22 = call i32 @system(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%23 = icmp sgt i32 %0, 1
br i1 %23, label %24, label %67
24: ; preds = %20
%25 = bitcast %struct.shmid_ds* %4 to i8*
%26 = getelementptr inbounds %struct.shmid_ds, %struct.shmid_ds* %4, i64 0, i32 0
%27 = zext i32 %9 to i64
br label %28
28: ; preds = %24, %64
%29 = phi i64 [ 0, %24 ], [ %65, %64 ]
%30 = getelementptr inbounds [1024 x i32], [1024 x i32]* %3, i64 0, i64 %29
%31 = load i32, i32* %30
%32 = call i64 @shmat(i32 %31, i32* null, i32 0)
%33 = inttoptr i64 %32 to i8*
%34 = icmp eq i8* %33, inttoptr (i64 -1 to i8*)
br i1 %34, label %35, label %39
35: ; preds = %28
%36 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%37 = load i32, i32* @EXIT_FAILURE
%38 = call i32 @exit(i32 %37)
unreachable
39: ; preds = %28
%40 = call i32 @shmdt(i8* %33)
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %46
42: ; preds = %39
%43 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
%44 = load i32, i32* @EXIT_FAILURE
%45 = call i32 @exit(i32 %44)
unreachable
46: ; preds = %39
store i64 0, i64* %26
%47 = load i32, i32* %30
%48 = load i32, i32* @IPC_SET
%49 = call i32 @shmctl(i32 %47, i32 %48, %struct.shmid_ds* %4)
%50 = icmp eq i32 %49, -1
br i1 %50, label %51, label %55
51: ; preds = %46
%52 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0))
%53 = load i32, i32* @EXIT_FAILURE
%54 = call i32 @exit(i32 %53)
unreachable
55: ; preds = %46
%56 = load i32, i32* %30
%57 = load i32, i32* @IPC_RMID
%58 = call i32 @shmctl(i32 %56, i32 %57, %struct.shmid_ds* null)
%59 = icmp eq i32 %58, -1
br i1 %59, label %60, label %64
60: ; preds = %55
%61 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0))
%62 = load i32, i32* @EXIT_FAILURE
%63 = call i32 @exit(i32 %62)
unreachable
64: ; preds = %55
%65 = add nuw nsw i64 %29, 1
%66 = icmp eq i64 %65, %27
br i1 %66, label %67, label %28
67: ; preds = %64, %20
%68 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.8, i64 0, i64 0))
%69 = call i32 @system(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
br label %70
70: ; preds = %67, %6
ret i32 0
}