#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 atoi(char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int *shmat(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int shmget(int, int, int);
scalar_t__ strcmp(char *, char *);
int main(int argc, char *argv[]) {
  int shmid;
  key_t key;
  int *shm;
  if (argc < 2) {
    printf("shm check | update | [0 - 255]\n");
    exit(0);
  }
  key = 1024;
  if ((shmid = shmget(key, 16, IPC_CREAT | 0666)) < 0) {
    perror("shmget");
    exit(1);
  }
  if ((shm = shmat(shmid, NULL, 0)) == (int *)-1) {
    perror("shmat");
    exit(1);
  }
  if (strcmp(argv[1], "check") == 0)
    exit(*shm);
  if (strcmp(argv[1], "update") == 0)
    exit(++(*shm));
  *shm = atoi(argv[1]);
  exit(*shm);
}


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)
jge    11a3 <main+0x33>
lea    0xe69(%rip),%rdi
call   1030 <printf@plt>
xor    %edi,%edi
call   1090 <exit@plt>
movl   $0x400,-0x18(%rbp)
mov    -0x18(%rbp),%edi
mov    0x2e2c(%rip),%rax
mov    (%rax),%edx
or     $0x1b6,%edx
mov    $0x10,%esi
call   1070 <shmget@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    11e8 <main+0x78>
lea    0xe47(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x14(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1060 <shmat@plt>
mov    %rax,-0x20(%rbp)
mov    $0xffffffffffffffff,%rcx
cmp    %rcx,%rax
jne    1220 <main+0xb0>
lea    0xe16(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xdfe(%rip),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%rax
jne    1249 <main+0xd9>
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xddb(%rip),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%rax
jne    1277 <main+0x107>
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
add    $0x1,%edi
mov    %edi,(%rax)
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <atoi@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
cmp    $0x1,%edi
jg     118c <main+0x1c>
lea    0xe9a(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   1090 <exit@plt>
mov    %rsi,%rbx
mov    0x2e4a(%rip),%rax
mov    $0x1b6,%edx
or     (%rax),%edx
mov    $0x400,%edi
mov    $0x10,%esi
call   1070 <shmget@plt>
test   %eax,%eax
jns    11b9 <main+0x49>
lea    0xe49(%rip),%rdi
jmp    11d1 <main+0x61>
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1060 <shmat@plt>
cmp    $0xffffffffffffffff,%rax
jne    11e0 <main+0x70>
lea    0xe36(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    %rax,%r14
mov    0x8(%rbx),%rdi
lea    0xe1f(%rip),%rsi
call   1040 <strcmp@plt>
test   %rax,%rax
jne    1200 <main+0x90>
mov    (%r14),%edi
call   1090 <exit@plt>
mov    0x8(%rbx),%rdi
lea    0xe08(%rip),%rsi
call   1040 <strcmp@plt>
test   %rax,%rax
jne    1223 <main+0xb3>
mov    (%r14),%edi
add    $0x1,%edi
mov    %edi,(%r14)
call   1090 <exit@plt>
mov    0x8(%rbx),%rdi
call   1080 <atoi@plt>
mov    %eax,(%r14)
mov    %eax,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [32 x i8] c"shm check | update | [0 - 255]\0A\00"
@IPC_CREAT = global i32 0
@.str.1 = [7 x i8] c"shmget\00"
@.str.2 = [6 x i8] c"shmat\00"
@.str.3 = [6 x i8] c"check\00"
@.str.4 = [7 x i8] c"update\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%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 slt i32 %9, 2
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
%13 = call i32 @exit(i32 0)
unreachable
14: ; preds = %2
store i32 1024, i32* %7
%15 = load i32, i32* %7
%16 = load i32, i32* @IPC_CREAT
%17 = or i32 %16, 438
%18 = call i32 @shmget(i32 %15, i32 16, i32 %17)
store i32 %18, i32* %6
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %14
%21 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %14
%24 = load i32, i32* %6
%25 = call i32* @shmat(i32 %24, i32* null, i32 0)
store i32* %25, i32** %8
%26 = icmp eq i32* %25, inttoptr (i64 -1 to i32*)
br i1 %26, label %27, label %30
27: ; preds = %23
%28 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %23
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 1
%33 = load i8*, i8** %32
%34 = call i64 @strcmp(i8* %33, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%35 = icmp eq i64 %34, 0
br i1 %35, label %36, label %40
36: ; preds = %30
%37 = load i32*, i32** %8
%38 = load i32, i32* %37
%39 = call i32 @exit(i32 %38)
unreachable
40: ; preds = %30
%41 = load i8**, i8*** %5
%42 = getelementptr inbounds i8*, i8** %41, i64 1
%43 = load i8*, i8** %42
%44 = call i64 @strcmp(i8* %43, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%45 = icmp eq i64 %44, 0
br i1 %45, label %46, label %51
46: ; preds = %40
%47 = load i32*, i32** %8
%48 = load i32, i32* %47
%49 = add nsw i32 %48, 1
store i32 %49, i32* %47
%50 = call i32 @exit(i32 %49)
unreachable
51: ; preds = %40
%52 = load i8**, i8*** %5
%53 = getelementptr inbounds i8*, i8** %52, i64 1
%54 = load i8*, i8** %53
%55 = call i32 @atoi(i8* %54)
%56 = load i32*, i32** %8
store i32 %55, i32* %56
%57 = load i32*, i32** %8
%58 = load i32, i32* %57
%59 = call i32 @exit(i32 %58)
unreachable
}

LLVM IR (-O2):
@IPC_CREAT = global i32 0
@.str.1 = [7 x i8] c"shmget\00"
@.str.2 = [6 x i8] c"shmat\00"
@.str.3 = [6 x i8] c"check\00"
@.str.4 = [7 x i8] c"update\00"
@str = [31 x i8] c"shm check | update | [0 - 255]\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 2
br i1 %3, label %4, label %7
4: ; preds = %2
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str, i64 0, i64 0))
%6 = tail call i32 @exit(i32 0)
unreachable
7: ; preds = %2
%8 = load i32, i32* @IPC_CREAT
%9 = or i32 %8, 438
%10 = tail call i32 @shmget(i32 1024, i32 16, i32 %9)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %7
%13 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%14 = tail call i32 @exit(i32 1)
unreachable
15: ; preds = %7
%16 = tail call i32* @shmat(i32 %10, i32* null, i32 0)
%17 = icmp eq i32* %16, inttoptr (i64 -1 to i32*)
br i1 %17, label %18, label %21
18: ; preds = %15
%19 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%20 = tail call i32 @exit(i32 1)
unreachable
21: ; preds = %15
%22 = getelementptr inbounds i8*, i8** %1, i64 1
%23 = load i8*, i8** %22
%24 = tail call i64 @strcmp(i8* %23, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %21
%27 = load i32, i32* %16
%28 = tail call i32 @exit(i32 %27)
unreachable
29: ; preds = %21
%30 = load i8*, i8** %22
%31 = tail call i64 @strcmp(i8* %30, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%32 = icmp eq i64 %31, 0
br i1 %32, label %33, label %37
33: ; preds = %29
%34 = load i32, i32* %16
%35 = add nsw i32 %34, 1
store i32 %35, i32* %16
%36 = tail call i32 @exit(i32 %35)
unreachable
37: ; preds = %29
%38 = load i8*, i8** %22
%39 = tail call i32 @atoi(i8* %38)
store i32 %39, i32* %16
%40 = tail call i32 @exit(i32 %39)
unreachable
}