#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 */

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ getchar();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
char *shmat(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ shmdt(char *);
int shmget(int, int, int /*<<< orphan*/);
main() {
  int shmid;
  char *msg;
  if ((shmid = shmget(10, 1024, 0)) == -1) {
    perror("shmget");
    exit(1);
  }
  msg = shmat(shmid, 0, 0);
  printf("Data written in the shared memory is:%s\n", msg);
  getchar();
  shmdt(msg);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0xa,%edi
mov    $0x400,%esi
xor    %edx,%edx
call   1080 <shmget@plt>
mov    %eax,-0x4(%rbp)
cmp    $0xffffffff,%eax
jne    11ab <main+0x3b>
lea    0xe64(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x4(%rbp),%edi
xor    %edx,%edx
mov    %edx,%esi
call   1070 <shmat@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rsi
lea    0xe41(%rip),%rdi
call   1040 <printf@plt>
mov    $0x0,%al
call   1050 <getchar@plt>
mov    -0x10(%rbp),%rdi
call   1030 <shmdt@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    $0xa,%edi
mov    $0x400,%esi
xor    %edx,%edx
call   1080 <shmget@plt>
cmp    $0xffffffff,%eax
je     11b7 <main+0x47>
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1070 <shmat@plt>
mov    %rax,%rbx
lea    0xe6b(%rip),%rdi
mov    %rax,%rsi
call   1040 <printf@plt>
xor    %eax,%eax
call   1050 <getchar@plt>
mov    %rbx,%rdi
call   1030 <shmdt@plt>
xor    %eax,%eax
pop    %rbx
ret
lea    0xe42(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [7 x i8] c"shmget\00"
@.str.1 = [41 x i8] c"Data written in the shared memory is:%s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = call i32 @shmget(i32 10, i32 1024, i32 0)
store i32 %3, i32* %1
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %8
5: ; preds = %0
%6 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%7 = call i32 @exit(i32 1)
unreachable
8: ; preds = %0
%9 = load i32, i32* %1
%10 = call i8* @shmat(i32 %9, i32 0, i32 0)
store i8* %10, i8** %2
%11 = load i8*, i8** %2
%12 = call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), i8* %11)
%13 = call i32 (...) @getchar()
%14 = load i8*, i8** %2
%15 = call i32 @shmdt(i8* %14)
ret i32 0
}

LLVM IR (-O2):
@.str = [7 x i8] c"shmget\00"
@.str.1 = [41 x i8] c"Data written in the shared memory is:%s\0A\00"
define i32 @main() {
%1 = tail call i32 @shmget(i32 10, i32 1024, i32 0)
%2 = icmp eq i32 %1, -1
br i1 %2, label %3, label %6
3: ; preds = %0
%4 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%5 = tail call i32 @exit(i32 1)
unreachable
6: ; preds = %0
%7 = tail call i8* @shmat(i32 %1, i32 0, i32 0)
%8 = tail call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), i8* %7)
%9 = tail call i32 (...) @getchar()
%10 = tail call i32 @shmdt(i8* %7)
ret i32 0
}