#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;
int /*<<< orphan*/ IPC_RMID;
int /*<<< orphan*/ exit(int);
scalar_t__ ftok(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int shmctl(int, int /*<<< orphan*/, int /*<<< orphan*/ *);
int shmget(scalar_t__, int, int);
int main(int argc, char *argv[]) {
  if (argc != 2) {
    printf("error args!\n");
    exit(0);
  }
  key_t skey = ftok(argv[1], 1);
  if (skey < 0) {
    perror("ftok");
    exit(-1);
  }
  printf("skey: %#x\n", skey);
  int shmid = shmget(skey, 4096, IPC_CREAT | 0600);
  if (shmid < 0) {
    perror("shmget");
    exit(-1);
  }
  printf("shmid: %d\n", shmid);
  int ret = shmctl(shmid, IPC_RMID, NULL);
  if (ret < 0) {
    perror("shmctl");
    exit(-1);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     1195 <main+0x35>
lea    0xe79(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %edi,%edi
call   1070 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    $0x1,%esi
call   1080 <ftok@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    11cc <main+0x6c>
lea    0xe50(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
mov    -0x18(%rbp),%rsi
lea    0xe3b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%rdi
mov    0x2def(%rip),%rax
mov    (%rax),%edx
or     $0x180,%edx
mov    $0x1000,%esi
call   1060 <shmget@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    121e <main+0xbe>
lea    0xe0e(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
mov    -0x1c(%rbp),%esi
lea    0xdfc(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x1c(%rbp),%edi
mov    0x2da7(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
mov    %eax,%edx
call   1040 <shmctl@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    1267 <main+0x107>
lea    0xdd7(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
cmp    $0x2,%edi
jne    11e8 <main+0x78>
mov    0x8(%rsi),%rdi
mov    $0x1,%esi
call   1090 <ftok@plt>
test   %rax,%rax
js     11fb <main+0x8b>
mov    %rax,%rbx
lea    0xe72(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2e34(%rip),%rax
mov    $0x180,%edx
or     (%rax),%edx
mov    %rbx,%rdi
mov    $0x1000,%esi
call   1070 <shmget@plt>
test   %eax,%eax
js     1204 <main+0x94>
mov    %eax,%ebx
lea    0xe52(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2e0b(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1050 <shmctl@plt>
test   %eax,%eax
js     120d <main+0x9d>
xor    %eax,%eax
pop    %rbx
ret
lea    0xe3a(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   1080 <exit@plt>
lea    0xdfe(%rip),%rdi
jmp    1214 <main+0xa4>
lea    0xe05(%rip),%rdi
jmp    1214 <main+0xa4>
lea    0xe0e(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [13 x i8] c"error args!\0A\00"
@.str.1 = [5 x i8] c"ftok\00"
@.str.2 = [11 x i8] c"skey: %#x\0A\00"
@IPC_CREAT = global i32 0
@.str.3 = [7 x i8] c"shmget\00"
@.str.4 = [11 x i8] c"shmid: %d\0A\00"
@IPC_RMID = global i32 0
@.str.5 = [7 x i8] c"shmctl\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp ne i32 %9, 2
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%13 = call i32 @exit(i32 0)
unreachable
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
%18 = call i64 @ftok(i8* %17, i32 1)
store i64 %18, i64* %6
%19 = load i64, i64* %6
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %14
%22 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%23 = call i32 @exit(i32 -1)
unreachable
24: ; preds = %14
%25 = load i64, i64* %6
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i64 %25)
%27 = load i64, i64* %6
%28 = load i32, i32* @IPC_CREAT
%29 = or i32 %28, 384
%30 = call i32 @shmget(i64 %27, i32 4096, i32 %29)
store i32 %30, i32* %7
%31 = load i32, i32* %7
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %24
%34 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%35 = call i32 @exit(i32 -1)
unreachable
36: ; preds = %24
%37 = load i32, i32* %7
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i32 %37)
%39 = load i32, i32* %7
%40 = load i32, i32* @IPC_RMID
%41 = call i32 @shmctl(i32 %39, i32 %40, i32* null)
store i32 %41, i32* %8
%42 = load i32, i32* %8
%43 = icmp slt i32 %42, 0
br i1 %43, label %44, label %47
44: ; preds = %36
%45 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
%46 = call i32 @exit(i32 -1)
unreachable
47: ; preds = %36
ret i32 0
}

LLVM IR (-O2):
@.str.1 = [5 x i8] c"ftok\00"
@.str.2 = [11 x i8] c"skey: %#x\0A\00"
@IPC_CREAT = global i32 0
@.str.3 = [7 x i8] c"shmget\00"
@.str.4 = [11 x i8] c"shmid: %d\0A\00"
@IPC_RMID = global i32 0
@.str.5 = [7 x i8] c"shmctl\00"
@str = [12 x i8] c"error args!\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 2
br i1 %3, label %7, label %4
4: ; preds = %2
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
%6 = tail call i32 @exit(i32 0)
unreachable
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = tail call i64 @ftok(i8* %9, i32 1)
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %7
%13 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%14 = tail call i32 @exit(i32 -1)
unreachable
15: ; preds = %7
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i64 %10)
%17 = load i32, i32* @IPC_CREAT
%18 = or i32 %17, 384
%19 = tail call i32 @shmget(i64 %10, i32 4096, i32 %18)
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %15
%22 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%23 = tail call i32 @exit(i32 -1)
unreachable
24: ; preds = %15
%25 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i32 %19)
%26 = load i32, i32* @IPC_RMID
%27 = tail call i32 @shmctl(i32 %19, i32 %26, i32* null)
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %24
%30 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
%31 = tail call i32 @exit(i32 -1)
unreachable
32: ; preds = %24
ret i32 0
}