#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*/ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ getchar();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
char *shmat(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ shmdt(char *);
int shmget(int, int, int);
int /*<<< orphan*/ stdin;
main() {
  int shmid;
  char *msg;
  if ((shmid = shmget(10, 1024, 666)) == -1) {
    perror("shmget");
    exit(1);
  }
  msg = shmat(shmid, 0, 0);
  printf("Enter the data you want to write into shared memory\n");
  fgets(msg, 1024, stdin);
  printf("Data successfully written\n");
  getchar();
  shmdt(msg);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0xa,%edi
mov    $0x400,%esi
mov    $0x29a,%edx
call   1090 <shmget@plt>
mov    %eax,-0x4(%rbp)
cmp    $0xffffffff,%eax
jne    11be <main+0x3e>
lea    0xe51(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x4(%rbp),%edi
xor    %edx,%edx
mov    %edx,%esi
call   1080 <shmat@plt>
mov    %rax,-0x10(%rbp)
lea    0xe32(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
mov    0x2e03(%rip),%rax
mov    (%rax),%edx
mov    $0x400,%esi
call   1050 <fgets@plt>
lea    0xe44(%rip),%rdi
call   1040 <printf@plt>
mov    $0x0,%al
call   1060 <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
mov    $0x29a,%edx
call   1090 <shmget@plt>
cmp    $0xffffffff,%eax
je     11e9 <main+0x69>
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1080 <shmat@plt>
mov    %rax,%rbx
lea    0xe58(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e2d(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    $0x400,%esi
call   1050 <fgets@plt>
lea    0xe6a(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1060 <getchar@plt>
mov    %rbx,%rdi
call   1040 <shmdt@plt>
xor    %eax,%eax
pop    %rbx
ret
lea    0xe10(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [7 x i8] c"shmget\00"
@.str.1 = [53 x i8] c"Enter the data you want to write into shared memory\0A\00"
@stdin = global i32 0
@.str.2 = [27 x i8] c"Data successfully written\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = call i32 @shmget(i32 10, i32 1024, i32 666)
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 = call i32 @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.1, i64 0, i64 0))
%12 = load i8*, i8** %2
%13 = load i32, i32* @stdin
%14 = call i32 @fgets(i8* %12, i32 1024, i32 %13)
%15 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
%16 = call i32 (...) @getchar()
%17 = load i8*, i8** %2
%18 = call i32 @shmdt(i8* %17)
ret i32 0
}

LLVM IR (-O2):
@.str = [7 x i8] c"shmget\00"
@stdin = global i32 0
@str = [52 x i8] c"Enter the data you want to write into shared memory\00"
@str.3 = [26 x i8] c"Data successfully written\00"
define i32 @main() {
%1 = tail call i32 @shmget(i32 10, i32 1024, i32 666)
%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 @puts(i8* dereferenceable(1) getelementptr inbounds ([52 x i8], [52 x i8]* @str, i64 0, i64 0))
%9 = load i32, i32* @stdin
%10 = tail call i32 @fgets(i8* %7, i32 1024, i32 %9)
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str.3, i64 0, i64 0))
%12 = tail call i32 (...) @getchar()
%13 = tail call i32 @shmdt(i8* %7)
ret i32 0
}