#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*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int ftok(char *, char);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
char *shmat(int, void *, int /*<<< orphan*/);
int shmdt(char *);
int shmget(int, int, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strncpy(char *, char *, int);
int main(int argc, char **argv) {
  key_t key;
  int shmid;
  char *data;
  if (argc > 2) {
    fprintf(stderr, "usage: shmdemo [data_to_write]\n");
    exit(1);
  }
  if ((key = ftok("shmdemo.c", 'R')) == -1) {
    perror("ftok");
    exit(1);
  }
  if ((shmid = shmget(key, 1024, 0644 | IPC_CREAT)) == -1) {
    perror("shmget");
    exit(1);
  }
  data = shmat(shmid, (void *)0, 0);
  if (data == (char *)(-1)) {
    perror("shmat");
    exit(1);
  }
  if (argc == 2) {
    printf("writing to segment: \"%s\"\n", argv[1]);
    strncpy(data, argv[1], 1024);
  } else
    printf("segment contains: \"%s\"\n", data);
  if (shmdt(data) == -1) {
    perror("shmdt");
    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)
jle    11cf <main+0x3f>
mov    0x2e29(%rip),%rax
mov    (%rax),%edi
lea    0xe40(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    0xe4a(%rip),%rdi
mov    $0x52,%esi
call   10b0 <ftok@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    1202 <main+0x72>
lea    0xe37(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x14(%rbp),%edi
mov    0x2dcc(%rip),%rax
mov    (%rax),%edx
or     $0x1a4,%edx
mov    $0x400,%esi
call   1090 <shmget@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    1240 <main+0xb0>
lea    0xdfe(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x18(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1080 <shmat@plt>
mov    %rax,-0x20(%rbp)
mov    $0xffffffffffffffff,%rax
cmp    %rax,-0x20(%rbp)
jne    1279 <main+0xe9>
lea    0xdcc(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
cmpl   $0x2,-0x8(%rbp)
jne    12b2 <main+0x122>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xdaa(%rip),%rdi
call   1050 <printf@plt>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
mov    $0x400,%edx
call   1030 <strncpy@plt>
jmp    12c2 <main+0x132>
mov    -0x20(%rbp),%rsi
lea    0xd99(%rip),%rdi
call   1050 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1040 <shmdt@plt>
cmp    $0xffffffff,%eax
jne    12ea <main+0x15a>
lea    0xd93(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
cmp    $0x3,%edi
jge    123e <main+0xae>
mov    %rsi,%r14
mov    %edi,%ebp
lea    0xe77(%rip),%rdi
mov    $0x52,%esi
call   10b0 <ftok@plt>
cmp    $0xffffffff,%eax
je     125d <main+0xcd>
mov    0x2e15(%rip),%rcx
mov    $0x1a4,%edx
or     (%rcx),%edx
mov    %eax,%edi
mov    $0x400,%esi
call   1090 <shmget@plt>
cmp    $0xffffffff,%eax
je     1266 <main+0xd6>
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1080 <shmat@plt>
cmp    $0xffffffffffffffff,%rax
je     126f <main+0xdf>
mov    %rax,%rbx
cmp    $0x2,%ebp
jne    121b <main+0x8b>
mov    0x8(%r14),%rsi
lea    0xe39(%rip),%rdi
call   1050 <printf@plt>
mov    0x8(%r14),%rsi
mov    %rbx,%rdi
mov    $0x400,%edx
call   1030 <strncpy@plt>
jmp    122a <main+0x9a>
lea    0xe34(%rip),%rdi
mov    %rbx,%rsi
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1040 <shmdt@plt>
cmp    $0xffffffff,%eax
je     1278 <main+0xe8>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2d9b(%rip),%rax
mov    (%rax),%edi
lea    0xdb2(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    0xdc6(%rip),%rdi
jmp    127f <main+0xef>
lea    0xdc2(%rip),%rdi
jmp    127f <main+0xef>
lea    0xdc0(%rip),%rdi
jmp    127f <main+0xef>
lea    0xdef(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [32 x i8] c"usage: shmdemo [data_to_write]\0A\00"
@.str.1 = [10 x i8] c"shmdemo.c\00"
@.str.2 = [5 x i8] c"ftok\00"
@IPC_CREAT = global i32 0
@.str.3 = [7 x i8] c"shmget\00"
@.str.4 = [6 x i8] c"shmat\00"
@.str.5 = [26 x i8] c"writing to segment: \22%s\22\0A\00"
@.str.6 = [24 x i8] c"segment contains: \22%s\22\0A\00"
@.str.7 = [6 x i8] c"shmdt\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 i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp sgt i32 %9, 2
br i1 %10, label %11, label %15
11: ; preds = %2
%12 = load i32, i32* @stderr
%13 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %2
%16 = call i32 @ftok(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8 signext 82)
store i32 %16, i32* %6
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %21
18: ; preds = %15
%19 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %15
%22 = load i32, i32* %6
%23 = load i32, i32* @IPC_CREAT
%24 = or i32 420, %23
%25 = call i32 @shmget(i32 %22, i32 1024, i32 %24)
store i32 %25, i32* %7
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %30
27: ; preds = %21
%28 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %21
%31 = load i32, i32* %7
%32 = call i8* @shmat(i32 %31, i8* null, i32 0)
store i8* %32, i8** %8
%33 = load i8*, i8** %8
%34 = icmp eq i8* %33, inttoptr (i64 -1 to i8*)
br i1 %34, label %35, label %38
35: ; preds = %30
%36 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%37 = call i32 @exit(i32 1)
unreachable
38: ; preds = %30
%39 = load i32, i32* %4
%40 = icmp eq i32 %39, 2
br i1 %40, label %41, label %51
41: ; preds = %38
%42 = load i8**, i8*** %5
%43 = getelementptr inbounds i8*, i8** %42, i64 1
%44 = load i8*, i8** %43
%45 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0), i8* %44)
%46 = load i8*, i8** %8
%47 = load i8**, i8*** %5
%48 = getelementptr inbounds i8*, i8** %47, i64 1
%49 = load i8*, i8** %48
%50 = call i32 @strncpy(i8* %46, i8* %49, i32 1024)
br label %54
51: ; preds = %38
%52 = load i8*, i8** %8
%53 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0), i8* %52)
br label %54
54: ; preds = %51, %41
%55 = load i8*, i8** %8
%56 = call i32 @shmdt(i8* %55)
%57 = icmp eq i32 %56, -1
br i1 %57, label %58, label %61
58: ; preds = %54
%59 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0))
%60 = call i32 @exit(i32 1)
unreachable
61: ; preds = %54
ret i32 0
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [32 x i8] c"usage: shmdemo [data_to_write]\0A\00"
@.str.1 = [10 x i8] c"shmdemo.c\00"
@.str.2 = [5 x i8] c"ftok\00"
@IPC_CREAT = global i32 0
@.str.3 = [7 x i8] c"shmget\00"
@.str.4 = [6 x i8] c"shmat\00"
@.str.5 = [26 x i8] c"writing to segment: \22%s\22\0A\00"
@.str.6 = [24 x i8] c"segment contains: \22%s\22\0A\00"
@.str.7 = [6 x i8] c"shmdt\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp sgt i32 %0, 2
br i1 %3, label %4, label %8
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %2
%9 = tail call i32 @ftok(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8 signext 82)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %14
11: ; preds = %8
%12 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %8
%15 = load i32, i32* @IPC_CREAT
%16 = or i32 %15, 420
%17 = tail call i32 @shmget(i32 %9, i32 1024, i32 %16)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %22
19: ; preds = %14
%20 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%21 = tail call i32 @exit(i32 1)
unreachable
22: ; preds = %14
%23 = tail call i8* @shmat(i32 %17, i8* null, i32 0)
%24 = icmp eq i8* %23, inttoptr (i64 -1 to i8*)
br i1 %24, label %25, label %28
25: ; preds = %22
%26 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%27 = tail call i32 @exit(i32 1)
unreachable
28: ; preds = %22
%29 = icmp eq i32 %0, 2
br i1 %29, label %30, label %36
30: ; preds = %28
%31 = getelementptr inbounds i8*, i8** %1, i64 1
%32 = load i8*, i8** %31
%33 = tail call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0), i8* %32)
%34 = load i8*, i8** %31
%35 = tail call i32 @strncpy(i8* %23, i8* %34, i32 1024)
br label %38
36: ; preds = %28
%37 = tail call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0), i8* %23)
br label %38
38: ; preds = %36, %30
%39 = tail call i32 @shmdt(i8* %23)
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %44
41: ; preds = %38
%42 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0))
%43 = tail call i32 @exit(i32 1)
unreachable
44: ; preds = %38
ret i32 0
}