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