#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 */
typedef scalar_t__ key_t;

/* Variables and functions */
int IPC_CREAT;
scalar_t__ errno;
scalar_t__ getpid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ rm_shm(int);
scalar_t__ shmat(int, void *, int /*<<< orphan*/);
int shmdt(char *);
int shmget(scalar_t__, int, int);
int /*<<< orphan*/ tst_exit();
int /*<<< orphan*/ tst_resm(int, char *, ...);
int main(void) {
  register int shmid;
  char *cp;
  key_t key;
  int r;
  errno = 0;
  key = (key_t)getpid();
  if ((shmid = shmget(key, 16 * 1024, IPC_CREAT | 0666)) < 0) {
    perror("shmget");
    tst_resm(1, "shmget Failed: shmid = %d, errno = %d\n", shmid, errno);
    tst_exit();
  }
  tst_resm(0, "shmget");
  cp = (char *)shmat(shmid, (void *)0x80000, 0);
  if (cp == (char *)-1) {
    perror("shmat");
    tst_resm(1, "shmat Failed: shmid = %d, errno = %d\n", shmid, errno);
    rm_shm(shmid);
    tst_exit();
  }
  *cp = '1';
  *(cp + 1) = '2';
  tst_resm(0, "shmat");
  r = shmdt(cp);
  if (r < 0) {
    perror("shmdt");
    tst_resm(1, "shmdt Failed: shmid = %d, errno = %d\n", shmid, errno);
    rm_shm(shmid);
    tst_exit();
  }
  tst_resm(0, "shmdt first time.");
  r = shmdt(cp);
  if (r == 0) {
    perror("shmdt");
    tst_resm(1, "shmdt Failed: shmid = %d, errno = %d\n", shmid, errno);
    rm_shm(shmid);
    tst_exit();
  }
  rm_shm(shmid);
  tst_resm(0, "shmdt second time.");
  tst_exit();
  return (0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e4a(%rip),%rax
movq   $0x0,(%rax)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2e25(%rip),%rax
mov    (%rax),%edx
or     $0x1b6,%edx
mov    $0x4000,%esi
call   1090 <shmget@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    1204 <main+0x84>
lea    0xe28(%rip),%rdi
call   1070 <perror@plt>
mov    -0x8(%rbp),%edx
mov    0x2df9(%rip),%rax
mov    (%rax),%rcx
mov    $0x1,%edi
lea    0xe11(%rip),%rsi
mov    $0x0,%al
call   10a0 <tst_resm@plt>
mov    $0x0,%al
call   1050 <tst_exit@plt>
xor    %edi,%edi
lea    0xdf3(%rip),%rsi
mov    $0x0,%al
call   10a0 <tst_resm@plt>
mov    -0x8(%rbp),%edi
mov    $0x80000,%esi
xor    %edx,%edx
call   1080 <shmat@plt>
mov    %rax,-0x10(%rbp)
mov    $0xffffffffffffffff,%rax
cmp    %rax,-0x10(%rbp)
jne    1273 <main+0xf3>
lea    0xdef(%rip),%rdi
call   1070 <perror@plt>
mov    -0x8(%rbp),%edx
mov    0x2d92(%rip),%rax
mov    (%rax),%rcx
mov    $0x1,%edi
lea    0xdd7(%rip),%rsi
mov    $0x0,%al
call   10a0 <tst_resm@plt>
mov    -0x8(%rbp),%edi
call   1060 <rm_shm@plt>
mov    $0x0,%al
call   1050 <tst_exit@plt>
mov    -0x10(%rbp),%rax
movb   $0x31,(%rax)
mov    -0x10(%rbp),%rax
movb   $0x32,0x1(%rax)
xor    %edi,%edi
lea    0xda3(%rip),%rsi
mov    $0x0,%al
call   10a0 <tst_resm@plt>
mov    -0x10(%rbp),%rdi
call   1030 <shmdt@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    12e3 <main+0x163>
lea    0xdab(%rip),%rdi
call   1070 <perror@plt>
mov    -0x8(%rbp),%edx
mov    0x2d22(%rip),%rax
mov    (%rax),%rcx
mov    $0x1,%edi
lea    0xd93(%rip),%rsi
mov    $0x0,%al
call   10a0 <tst_resm@plt>
mov    -0x8(%rbp),%edi
call   1060 <rm_shm@plt>
mov    $0x0,%al
call   1050 <tst_exit@plt>
xor    %edi,%edi
lea    0xd9a(%rip),%rsi
mov    $0x0,%al
call   10a0 <tst_resm@plt>
mov    -0x10(%rbp),%rdi
call   1030 <shmdt@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    1344 <main+0x1c4>
lea    0xd4a(%rip),%rdi
call   1070 <perror@plt>
mov    -0x8(%rbp),%edx
mov    0x2cc1(%rip),%rax
mov    (%rax),%rcx
mov    $0x1,%edi
lea    0xd32(%rip),%rsi
mov    $0x0,%al
call   10a0 <tst_resm@plt>
mov    -0x8(%rbp),%edi
call   1060 <rm_shm@plt>
mov    $0x0,%al
call   1050 <tst_exit@plt>
mov    -0x8(%rbp),%edi
call   1060 <rm_shm@plt>
xor    %edi,%edi
lea    0xd43(%rip),%rsi
mov    $0x0,%al
call   10a0 <tst_resm@plt>
mov    $0x0,%al
call   1050 <tst_exit@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
mov    0x2e55(%rip),%r14
movq   $0x0,(%r14)
xor    %eax,%eax
call   1040 <getpid@plt>
mov    0x2e38(%rip),%rcx
mov    $0x1b6,%edx
or     (%rcx),%edx
mov    %rax,%rdi
mov    $0x4000,%esi
call   1090 <shmget@plt>
mov    %eax,%ebp
test   %eax,%eax
js     1253 <main+0xd3>
lea    0xe3b(%rip),%rsi
xor    %edi,%edi
xor    %eax,%eax
call   10a0 <tst_resm@plt>
mov    $0x80000,%esi
mov    %ebp,%edi
xor    %edx,%edx
call   1080 <shmat@plt>
mov    %rax,%rbx
cmp    $0xffffffffffffffff,%rax
je     1283 <main+0x103>
movw   $0x3231,(%rbx)
lea    0xe39(%rip),%rsi
xor    %edi,%edi
xor    %eax,%eax
call   10a0 <tst_resm@plt>
mov    %rbx,%rdi
call   1030 <shmdt@plt>
test   %eax,%eax
js     12ba <main+0x13a>
lea    0xe71(%rip),%rsi
xor    %edi,%edi
xor    %eax,%eax
call   10a0 <tst_resm@plt>
mov    %rbx,%rdi
call   1030 <shmdt@plt>
test   %eax,%eax
je     12f1 <main+0x171>
mov    %ebp,%edi
call   1060 <rm_shm@plt>
lea    0xe5c(%rip),%rsi
xor    %edi,%edi
xor    %eax,%eax
call   10a0 <tst_resm@plt>
xor    %eax,%eax
call   1050 <tst_exit@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xda6(%rip),%rdi
call   1070 <perror@plt>
mov    (%r14),%rcx
lea    0xd9e(%rip),%rsi
mov    $0x1,%edi
mov    %ebp,%edx
xor    %eax,%eax
call   10a0 <tst_resm@plt>
xor    %eax,%eax
call   1050 <tst_exit@plt>
jmp    11be <main+0x3e>
lea    0xda4(%rip),%rdi
call   1070 <perror@plt>
mov    (%r14),%rcx
lea    0xd9b(%rip),%rsi
mov    $0x1,%edi
mov    %ebp,%edx
xor    %eax,%eax
call   10a0 <tst_resm@plt>
mov    %ebp,%edi
call   1060 <rm_shm@plt>
xor    %eax,%eax
call   1050 <tst_exit@plt>
jmp    11e9 <main+0x69>
lea    0xd99(%rip),%rdi
call   1070 <perror@plt>
mov    (%r14),%rcx
lea    0xd90(%rip),%rsi
mov    $0x1,%edi
mov    %ebp,%edx
xor    %eax,%eax
call   10a0 <tst_resm@plt>
mov    %ebp,%edi
call   1060 <rm_shm@plt>
xor    %eax,%eax
call   1050 <tst_exit@plt>
jmp    120e <main+0x8e>
lea    0xd62(%rip),%rdi
call   1070 <perror@plt>
mov    (%r14),%rcx
lea    0xd59(%rip),%rsi
mov    $0x1,%edi
mov    %ebp,%edx
xor    %eax,%eax
call   10a0 <tst_resm@plt>
mov    %ebp,%edi
call   1060 <rm_shm@plt>
xor    %eax,%eax
call   1050 <tst_exit@plt>
jmp    122e <main+0xae>

LLVM IR (-O0):
@errno = global i64 0
@IPC_CREAT = global i32 0
@.str = [7 x i8] c"shmget\00"
@.str.1 = [39 x i8] c"shmget Failed: shmid = %d, errno = %d\0A\00"
@.str.2 = [6 x i8] c"shmat\00"
@.str.3 = [38 x i8] c"shmat Failed: shmid = %d, errno = %d\0A\00"
@.str.4 = [6 x i8] c"shmdt\00"
@.str.5 = [38 x i8] c"shmdt Failed: shmid = %d, errno = %d\0A\00"
@.str.6 = [18 x i8] c"shmdt first time.\00"
@.str.7 = [19 x i8] c"shmdt second time.\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32
store i32 0, i32* %1
store i64 0, i64* @errno
%6 = call i64 (...) @getpid()
store i64 %6, i64* %4
%7 = load i64, i64* %4
%8 = load i32, i32* @IPC_CREAT
%9 = or i32 %8, 438
%10 = call i32 @shmget(i64 %7, i32 16384, i32 %9)
store i32 %10, i32* %2
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %18
12: ; preds = %0
%13 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%14 = load i32, i32* %2
%15 = load i64, i64* @errno
%16 = call i32 (i32, i8*, ...) @tst_resm(i32 1, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i32 %14, i64 %15)
%17 = call i32 (...) @tst_exit()
br label %18
18: ; preds = %12, %0
%19 = call i32 (i32, i8*, ...) @tst_resm(i32 0, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%20 = load i32, i32* %2
%21 = call i64 @shmat(i32 %20, i8* inttoptr (i64 524288 to i8*), i32 0)
%22 = inttoptr i64 %21 to i8*
store i8* %22, i8** %3
%23 = load i8*, i8** %3
%24 = icmp eq i8* %23, inttoptr (i64 -1 to i8*)
br i1 %24, label %25, label %33
25: ; preds = %18
%26 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%27 = load i32, i32* %2
%28 = load i64, i64* @errno
%29 = call i32 (i32, i8*, ...) @tst_resm(i32 1, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0), i32 %27, i64 %28)
%30 = load i32, i32* %2
%31 = call i32 @rm_shm(i32 %30)
%32 = call i32 (...) @tst_exit()
br label %33
33: ; preds = %25, %18
%34 = load i8*, i8** %3
store i8 49, i8* %34
%35 = load i8*, i8** %3
%36 = getelementptr inbounds i8, i8* %35, i64 1
store i8 50, i8* %36
%37 = call i32 (i32, i8*, ...) @tst_resm(i32 0, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%38 = load i8*, i8** %3
%39 = call i32 @shmdt(i8* %38)
store i32 %39, i32* %5
%40 = load i32, i32* %5
%41 = icmp slt i32 %40, 0
br i1 %41, label %42, label %50
42: ; preds = %33
%43 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%44 = load i32, i32* %2
%45 = load i64, i64* @errno
%46 = call i32 (i32, i8*, ...) @tst_resm(i32 1, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.5, i64 0, i64 0), i32 %44, i64 %45)
%47 = load i32, i32* %2
%48 = call i32 @rm_shm(i32 %47)
%49 = call i32 (...) @tst_exit()
br label %50
50: ; preds = %42, %33
%51 = call i32 (i32, i8*, ...) @tst_resm(i32 0, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.6, i64 0, i64 0))
%52 = load i8*, i8** %3
%53 = call i32 @shmdt(i8* %52)
store i32 %53, i32* %5
%54 = load i32, i32* %5
%55 = icmp eq i32 %54, 0
br i1 %55, label %56, label %64
56: ; preds = %50
%57 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%58 = load i32, i32* %2
%59 = load i64, i64* @errno
%60 = call i32 (i32, i8*, ...) @tst_resm(i32 1, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.5, i64 0, i64 0), i32 %58, i64 %59)
%61 = load i32, i32* %2
%62 = call i32 @rm_shm(i32 %61)
%63 = call i32 (...) @tst_exit()
br label %64
64: ; preds = %56, %50
%65 = load i32, i32* %2
%66 = call i32 @rm_shm(i32 %65)
%67 = call i32 (i32, i8*, ...) @tst_resm(i32 0, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.7, i64 0, i64 0))
%68 = call i32 (...) @tst_exit()
ret i32 0
}

LLVM IR (-O2):
@errno = global i64 0
@IPC_CREAT = global i32 0
@.str = [7 x i8] c"shmget\00"
@.str.1 = [39 x i8] c"shmget Failed: shmid = %d, errno = %d\0A\00"
@.str.2 = [6 x i8] c"shmat\00"
@.str.3 = [38 x i8] c"shmat Failed: shmid = %d, errno = %d\0A\00"
@.str.4 = [6 x i8] c"shmdt\00"
@.str.5 = [38 x i8] c"shmdt Failed: shmid = %d, errno = %d\0A\00"
@.str.6 = [18 x i8] c"shmdt first time.\00"
@.str.7 = [19 x i8] c"shmdt second time.\00"
define i32 @main() {
store i64 0, i64* @errno
%1 = tail call i64 (...) @getpid()
%2 = load i32, i32* @IPC_CREAT
%3 = or i32 %2, 438
%4 = tail call i32 @shmget(i64 %1, i32 16384, i32 %3)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %11
6: ; preds = %0
%7 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%8 = load i64, i64* @errno
%9 = tail call i32 (i32, i8*, ...) @tst_resm(i32 1, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i32 %4, i64 %8)
%10 = tail call i32 (...) @tst_exit()
br label %11
11: ; preds = %6, %0
%12 = tail call i32 (i32, i8*, ...) @tst_resm(i32 0, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%13 = tail call i64 @shmat(i32 %4, i8* inttoptr (i64 524288 to i8*), i32 0)
%14 = inttoptr i64 %13 to i8*
%15 = icmp eq i8* %14, inttoptr (i64 -1 to i8*)
br i1 %15, label %16, label %22
16: ; preds = %11
%17 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%18 = load i64, i64* @errno
%19 = tail call i32 (i32, i8*, ...) @tst_resm(i32 1, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0), i32 %4, i64 %18)
%20 = tail call i32 @rm_shm(i32 %4)
%21 = tail call i32 (...) @tst_exit()
br label %22
22: ; preds = %16, %11
store i8 49, i8* %14
%23 = getelementptr inbounds i8, i8* %14, i64 1
store i8 50, i8* %23
%24 = tail call i32 (i32, i8*, ...) @tst_resm(i32 0, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%25 = tail call i32 @shmdt(i8* %14)
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %33
27: ; preds = %22
%28 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%29 = load i64, i64* @errno
%30 = tail call i32 (i32, i8*, ...) @tst_resm(i32 1, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.5, i64 0, i64 0), i32 %4, i64 %29)
%31 = tail call i32 @rm_shm(i32 %4)
%32 = tail call i32 (...) @tst_exit()
br label %33
33: ; preds = %27, %22
%34 = tail call i32 (i32, i8*, ...) @tst_resm(i32 0, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.6, i64 0, i64 0))
%35 = tail call i32 @shmdt(i8* %14)
%36 = icmp eq i32 %35, 0
br i1 %36, label %37, label %43
37: ; preds = %33
%38 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%39 = load i64, i64* @errno
%40 = tail call i32 (i32, i8*, ...) @tst_resm(i32 1, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.5, i64 0, i64 0), i32 %4, i64 %39)
%41 = tail call i32 @rm_shm(i32 %4)
%42 = tail call i32 (...) @tst_exit()
br label %43
43: ; preds = %37, %33
%44 = tail call i32 @rm_shm(i32 %4)
%45 = tail call i32 (i32, i8*, ...) @tst_resm(i32 0, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.7, i64 0, i64 0))
%46 = tail call i32 (...) @tst_exit()
ret i32 0
}