#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);
void zero() {
  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);
    }
  }
}


x86 assembly (clang, -O0):
<zero>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xea1(%rip),%rdi
mov    $0x1,%esi
call   1070 <ftok@plt>
mov    %eax,%edi
mov    0x2e66(%rip),%rax
mov    (%rax),%edx
or     $0x1b6,%edx
mov    $0x400,%esi
call   1060 <shmget@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0xffffffff,-0x4(%rbp)
je     1203 <zero+0xb3>
mov    -0x4(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1050 <shmat@plt>
mov    %rax,-0x10(%rbp)
mov    -0x4(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1050 <shmat@plt>
mov    %rax,-0x18(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e21(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
mov    %eax,%edx
call   1040 <shmctl@plt>
mov    $0xffffffffffffffff,%rax
cmp    %rax,-0x10(%rbp)
je     11e4 <zero+0x94>
mov    -0x10(%rbp),%rdi
call   1030 <shmdt@plt>
mov    $0xffffffffffffffff,%rax
cmp    %rax,-0x18(%rbp)
je     11fe <zero+0xae>
mov    -0x18(%rbp),%rdi
call   1030 <shmdt@plt>
jmp    1203 <zero+0xb3>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<zero>:
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     11d6 <zero+0x86>
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 <zero+0x73>
mov    %r15,%rdi
call   1030 <shmdt@plt>
cmp    $0xffffffffffffffff,%r14
je     11d6 <zero+0x86>
mov    %r14,%rdi
pop    %rbx
pop    %r14
pop    %r15
jmp    1030 <shmdt@plt>
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 void @zero() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i8*
%4 = call i32 @ftok(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 1)
%5 = load i32, i32* @IPC_CREAT
%6 = or i32 %5, 438
%7 = call i32 @shmget(i32 %4, i32 1024, i32 %6)
store i32 %7, i32* %1
%8 = load i32, i32* %1
%9 = icmp ne i32 %8, -1
br i1 %9, label %10, label %30
10: ; preds = %0
%11 = load i32, i32* %1
%12 = call i8* @shmat(i32 %11, i32* null, i32 0)
store i8* %12, i8** %2
%13 = load i32, i32* %1
%14 = call i8* @shmat(i32 %13, i32* null, i32 0)
store i8* %14, i8** %3
%15 = load i32, i32* %1
%16 = load i32, i32* @IPC_RMID
%17 = call i32 @shmctl(i32 %15, i32 %16, i32* null)
%18 = load i8*, i8** %2
%19 = icmp ne i8* %18, inttoptr (i64 -1 to i8*)
br i1 %19, label %20, label %23
20: ; preds = %10
%21 = load i8*, i8** %2
%22 = call i32 @shmdt(i8* %21)
br label %23
23: ; preds = %20, %10
%24 = load i8*, i8** %3
%25 = icmp ne i8* %24, inttoptr (i64 -1 to i8*)
br i1 %25, label %26, label %29
26: ; preds = %23
%27 = load i8*, i8** %3
%28 = call i32 @shmdt(i8* %27)
br label %29
29: ; preds = %26, %23
br label %30
30: ; preds = %29, %0
ret void
}

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 void @zero() {
%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 void
}