#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 int key_t;

/* Variables and functions */
int IPC_CREAT;
int /*<<< orphan*/ ePrintf(char *, int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int ftok(char *, char);
int *shmat(int, int /*<<< orphan*/, int /*<<< orphan*/);
int shmget(int, int, int);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int mmGetId(char *path, char key) {
  int shmid, *shmptr;
  key_t shmkey;
  shmkey = ftok(path, key);
  if (shmkey == -1) {
    ePrintf("ftok: %s\n", strerror(errno));
    return -1;
  }
  shmid = shmget(shmkey, 2048, 0640 | IPC_CREAT);
  if (shmid < 0) {
    ePrintf("ftok: %s\n", strerror(errno));
    return -1;
  }
  shmptr = shmat(shmid, 0, 0);
  if (shmptr == (int *)-1) {
    ePrintf("ftok: %s\n", strerror(errno));
    return -1;
  }
  if (*shmptr == 0)
    *shmptr = 2048;
  else if (*shmptr != 2048) {
    shmid = shmget(shmkey, *shmptr, 0640 | IPC_CREAT);
    if (shmid < 0) {
      ePrintf("ftok: %s\n", strerror(errno));
      return -1;
    }
  }
  return shmid;
}


x86 assembly (clang, -O0):
<mmGetId>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %sil,%al
mov    %rdi,-0x10(%rbp)
mov    %al,-0x11(%rbp)
mov    -0x10(%rbp),%rdi
movsbl -0x11(%rbp),%esi
call   1060 <ftok@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    11a4 <mmGetId+0x54>
mov    0x2e5d(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    %eax,%esi
lea    0xe6d(%rip),%rdi
call   1030 <ePrintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12cc <mmGetId+0x17c>
mov    -0x24(%rbp),%edi
mov    0x2e2a(%rip),%rax
mov    (%rax),%edx
or     $0x1a0,%edx
mov    $0x800,%esi
call   1050 <shmget@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11f5 <mmGetId+0xa5>
mov    0x2e0c(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    %eax,%esi
lea    0xe1c(%rip),%rdi
call   1030 <ePrintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12cc <mmGetId+0x17c>
mov    -0x18(%rbp),%edi
xor    %edx,%edx
mov    %edx,%esi
call   1040 <shmat@plt>
mov    %rax,-0x20(%rbp)
mov    $0xffffffffffffffff,%rax
cmp    %rax,-0x20(%rbp)
jne    123e <mmGetId+0xee>
mov    0x2dc3(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    %eax,%esi
lea    0xdd3(%rip),%rdi
call   1030 <ePrintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12cc <mmGetId+0x17c>
mov    -0x20(%rbp),%rax
cmpl   $0x0,(%rax)
jne    125a <mmGetId+0x10a>
mov    -0x20(%rbp),%rax
movl   $0x800,(%rax)
jmp    12c6 <mmGetId+0x176>
mov    -0x20(%rbp),%rax
cmpl   $0x800,(%rax)
je     12c1 <mmGetId+0x171>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%rax
mov    (%rax),%esi
mov    0x2d5e(%rip),%rax
mov    (%rax),%edx
or     $0x1a0,%edx
call   1050 <shmget@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    12bc <mmGetId+0x16c>
mov    0x2d45(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    %eax,%esi
lea    0xd55(%rip),%rdi
call   1030 <ePrintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12cc <mmGetId+0x17c>
jmp    12c1 <mmGetId+0x171>
jmp    12c6 <mmGetId+0x176>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mmGetId>:
push   %rbp
push   %r14
push   %rbx
call   1060 <ftok@plt>
cmp    $0xffffffff,%eax
je     11bd <mmGetId+0x6d>
mov    %eax,%ebp
mov    0x2e71(%rip),%r14
mov    $0x1a0,%edx
or     (%r14),%edx
mov    %eax,%edi
mov    $0x800,%esi
call   1050 <shmget@plt>
test   %eax,%eax
js     11bd <mmGetId+0x6d>
mov    %eax,%ebx
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1040 <shmat@plt>
cmp    $0xffffffffffffffff,%rax
je     11bd <mmGetId+0x6d>
mov    (%rax),%esi
cmp    $0x800,%esi
je     11de <mmGetId+0x8e>
test   %esi,%esi
jne    11a8 <mmGetId+0x58>
movl   $0x800,(%rax)
jmp    11de <mmGetId+0x8e>
mov    $0x1a0,%edx
or     (%r14),%edx
mov    %ebp,%edi
call   1050 <shmget@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    11de <mmGetId+0x8e>
mov    0x2e1c(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe2e(%rip),%rdi
mov    %eax,%esi
call   1030 <ePrintf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [10 x i8] c"ftok: %s\0A\00"
@errno = global i32 0
@IPC_CREAT = global i32 0
define i32 @mmGetId(i8* %0, i8 signext %1) {
%3 = tail call i32 @ftok(i8* %0, i8 signext %1)
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %9
5: ; preds = %2
%6 = load i32, i32* @errno
%7 = tail call i32 @strerror(i32 %6)
%8 = tail call i32 @ePrintf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %7)
br label %37
9: ; preds = %2
%10 = load i32, i32* @IPC_CREAT
%11 = or i32 %10, 416
%12 = tail call i32 @shmget(i32 %3, i32 2048, i32 %11)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %9
%15 = load i32, i32* @errno
%16 = tail call i32 @strerror(i32 %15)
%17 = tail call i32 @ePrintf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %16)
br label %37
18: ; preds = %9
%19 = tail call i32* @shmat(i32 %12, i32 0, i32 0)
%20 = icmp eq i32* %19, inttoptr (i64 -1 to i32*)
br i1 %20, label %21, label %25
21: ; preds = %18
%22 = load i32, i32* @errno
%23 = tail call i32 @strerror(i32 %22)
%24 = tail call i32 @ePrintf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %23)
br label %37
25: ; preds = %18
%26 = load i32, i32* %19
switch i32 %26, label %28 [
i32 0, label %27
i32 2048, label %37
]
27: ; preds = %25
store i32 2048, i32* %19
br label %37
28: ; preds = %25
%29 = load i32, i32* @IPC_CREAT
%30 = or i32 %29, 416
%31 = tail call i32 @shmget(i32 %3, i32 %26, i32 %30)
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %37
33: ; preds = %28
%34 = load i32, i32* @errno
%35 = tail call i32 @strerror(i32 %34)
%36 = tail call i32 @ePrintf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %35)
br label %37
37: ; preds = %27, %28, %25, %33, %21, %14, %5
%38 = phi i32 [ -1, %5 ], [ -1, %14 ], [ -1, %21 ], [ -1, %33 ], [ %12, %27 ], [ %31, %28 ], [ %12, %25 ]
ret i32 %38
}