#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 IPC_CREAT;
int /*<<< orphan*/ IPC_RMID;
int /*<<< orphan*/ ftok(char *, int);
void *shmat(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ shmctl(int, int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ shmdt(void *);
int shmget(int /*<<< orphan*/, int, int);
int main(void) {
  int shmid =
      shmget(ftok("sp-rtrace/tests/shmseg_test", 1), 1024, IPC_CREAT | 0666);
  if (shmid != -1) {
    void *ptr = shmat(shmid, NULL, 0);
    void *ptr2 = shmat(shmid, NULL, 0);
    shmctl(shmid, IPC_RMID, NULL);
    if (ptr != (void *)-1) {
      shmdt(ptr);
    }
    if (ptr2 != (void *)-1) {
      shmdt(ptr2);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe9a(%rip),%rdi
mov    $0x1,%esi
call   1070 <ftok@plt>
mov    %eax,%edi
mov    0x2e5f(%rip),%rax
mov    (%rax),%edx
or     $0x1b6,%edx
mov    $0x400,%esi
call   1060 <shmget@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
je     120a <main+0xba>
mov    -0x8(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1050 <shmat@plt>
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1050 <shmat@plt>
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e1a(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
mov    %eax,%edx
call   1040 <shmctl@plt>
mov    $0xffffffffffffffff,%rax
cmp    %rax,-0x10(%rbp)
je     11eb <main+0x9b>
mov    -0x10(%rbp),%rdi
call   1030 <shmdt@plt>
mov    $0xffffffffffffffff,%rax
cmp    %rax,-0x18(%rbp)
je     1205 <main+0xb5>
mov    -0x18(%rbp),%rdi
call   1030 <shmdt@plt>
jmp    120a <main+0xba>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
lea    0xea4(%rip),%rdi
mov    $0x1,%esi
call   1070 <ftok@plt>
mov    0x2e6b(%rip),%rcx
mov    $0x1b6,%edx
or     (%rcx),%edx
mov    %eax,%edi
mov    $0x400,%esi
call   1060 <shmget@plt>
cmp    $0xffffffff,%eax
je     11d1 <main+0x81>
mov    %eax,%ebx
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1050 <shmat@plt>
mov    %rax,%r15
mov    %ebx,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1050 <shmat@plt>
mov    %rax,%r14
mov    0x2e36(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1040 <shmctl@plt>
cmp    $0xffffffffffffffff,%r15
je     11c3 <main+0x73>
mov    %r15,%rdi
call   1030 <shmdt@plt>
cmp    $0xffffffffffffffff,%r14
je     11d1 <main+0x81>
mov    %r14,%rdi
call   1030 <shmdt@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [28 x i8] c"sp-rtrace/tests/shmseg_test\00"
@IPC_CREAT = global i32 0
@IPC_RMID = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
store i32 0, i32* %1
%5 = call i32 @ftok(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 1)
%6 = load i32, i32* @IPC_CREAT
%7 = or i32 %6, 438
%8 = call i32 @shmget(i32 %5, i32 1024, i32 %7)
store i32 %8, i32* %2
%9 = load i32, i32* %2
%10 = icmp ne i32 %9, -1
br i1 %10, label %11, label %31
11: ; preds = %0
%12 = load i32, i32* %2
%13 = call i8* @shmat(i32 %12, i32* null, i32 0)
store i8* %13, i8** %3
%14 = load i32, i32* %2
%15 = call i8* @shmat(i32 %14, i32* null, i32 0)
store i8* %15, i8** %4
%16 = load i32, i32* %2
%17 = load i32, i32* @IPC_RMID
%18 = call i32 @shmctl(i32 %16, i32 %17, i32* null)
%19 = load i8*, i8** %3
%20 = icmp ne i8* %19, inttoptr (i64 -1 to i8*)
br i1 %20, label %21, label %24
21: ; preds = %11
%22 = load i8*, i8** %3
%23 = call i32 @shmdt(i8* %22)
br label %24
24: ; preds = %21, %11
%25 = load i8*, i8** %4
%26 = icmp ne i8* %25, inttoptr (i64 -1 to i8*)
br i1 %26, label %27, label %30
27: ; preds = %24
%28 = load i8*, i8** %4
%29 = call i32 @shmdt(i8* %28)
br label %30
30: ; preds = %27, %24
br label %31
31: ; preds = %30, %0
ret i32 0
}

LLVM IR (-O2):
@.str = [28 x i8] c"sp-rtrace/tests/shmseg_test\00"
@IPC_CREAT = global i32 0
@IPC_RMID = global i32 0
define i32 @main() {
%1 = tail call i32 @ftok(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 1)
%2 = load i32, i32* @IPC_CREAT
%3 = or i32 %2, 438
%4 = tail call i32 @shmget(i32 %1, i32 1024, i32 %3)
%5 = icmp eq i32 %4, -1
br i1 %5, label %18, label %6
6: ; preds = %0
%7 = tail call i8* @shmat(i32 %4, i32* null, i32 0)
%8 = tail call i8* @shmat(i32 %4, i32* null, i32 0)
%9 = load i32, i32* @IPC_RMID
%10 = tail call i32 @shmctl(i32 %4, i32 %9, i32* null)
%11 = icmp eq i8* %7, inttoptr (i64 -1 to i8*)
br i1 %11, label %14, label %12
12: ; preds = %6
%13 = tail call i32 @shmdt(i8* %7)
br label %14
14: ; preds = %12, %6
%15 = icmp eq i8* %8, inttoptr (i64 -1 to i8*)
br i1 %15, label %18, label %16
16: ; preds = %14
%17 = tail call i32 @shmdt(i8* %8)
br label %18
18: ; preds = %14, %16, %0
ret i32 0
}