#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 */
struct shmid_ds {
  int dummy;
};
typedef int /*<<< orphan*/ key_t;

/* Variables and functions */
int EXIT_SUCCESS;
int IPC_CREAT;
int /*<<< orphan*/ IPC_RMID;
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, void *);
void *shmat(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ shmctl(int, int /*<<< orphan*/, struct shmid_ds *);
int /*<<< orphan*/ shmdt(void *);
int shmget(int /*<<< orphan*/, int, int);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ strcpy(void *, char *);
int main(int argc, char **argv) {
  char buffer[100 + 1] = {'\0'};
  int shm_id = shmget((key_t)10, 100, 0644 | IPC_CREAT);
  void *p = shmat(shm_id, NULL, 0);
  if (p == (void *)-1) {
    perror("shmat");
  }
  printf("\nget share memory %X\n", p);
  while (1) {
    fgets(buffer, 100, stdin);
    strcpy(p, buffer);
  }
  shmdt(p);
  shmctl(shm_id, IPC_RMID, (struct shmid_ds *)0);
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x80(%rbp),%rdi
xor    %esi,%esi
mov    $0x65,%edx
call   1050 <memset@plt>
mov    0x2e38(%rip),%rax
mov    (%rax),%edx
or     $0x1a4,%edx
mov    $0xa,%edi
mov    $0x64,%esi
call   1090 <shmget@plt>
mov    %eax,-0x84(%rbp)
mov    -0x84(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1080 <shmat@plt>
mov    %rax,-0x90(%rbp)
mov    $0xffffffffffffffff,%rax
cmp    %rax,-0x90(%rbp)
jne    11f5 <main+0x85>
lea    0xe10(%rip),%rdi
call   1070 <perror@plt>
mov    -0x90(%rbp),%rsi
lea    0xe03(%rip),%rdi
call   1040 <printf@plt>
lea    -0x80(%rbp),%rdi
mov    0x2dd5(%rip),%rax
mov    (%rax),%edx
mov    $0x64,%esi
call   1060 <fgets@plt>
mov    -0x90(%rbp),%rdi
lea    -0x80(%rbp),%rsi
call   1030 <strcpy@plt>
jmp    1208 <main+0x98>

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x70,%rsp
xorps  %xmm0,%xmm0
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
movaps %xmm0,(%rsp)
movq   $0x0,0x5d(%rsp)
mov    0x2e3f(%rip),%rax
mov    $0x1a4,%edx
or     (%rax),%edx
mov    $0xa,%edi
mov    $0x64,%esi
call   1080 <shmget@plt>
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1070 <shmat@plt>
mov    %rax,%r14
cmp    $0xffffffffffffffff,%rax
je     11fd <main+0x9d>
lea    0xe3c(%rip),%rdi
mov    %r14,%rsi
call   1040 <printf@plt>
mov    0x2e0f(%rip),%r15
mov    %rsp,%rbx
nopl   0x0(%rax)
mov    (%r15),%edx
mov    %rbx,%rdi
mov    $0x64,%esi
call   1050 <fgets@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1030 <strcpy@plt>
jmp    11e0 <main+0x80>
lea    0xdfc(%rip),%rdi
call   1060 <perror@plt>
jmp    11c3 <main+0x63>

LLVM IR (-O0):
@IPC_CREAT = global i32 0
@.str = [6 x i8] c"shmat\00"
@.str.1 = [22 x i8] c"\0Aget share memory %X\0A\00"
@stdin = global i32 0
@EXIT_SUCCESS = global i32 0
@IPC_RMID = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [101 x i8]
%7 = alloca i32
%8 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = bitcast [101 x i8]* %6 to i8*
%10 = load i32, i32* @IPC_CREAT
%11 = or i32 420, %10
%12 = call i32 @shmget(i32 10, i32 100, i32 %11)
store i32 %12, i32* %7
%13 = load i32, i32* %7
%14 = call i8* @shmat(i32 %13, i32* null, i32 0)
store i8* %14, i8** %8
%15 = load i8*, i8** %8
%16 = icmp eq i8* %15, inttoptr (i64 -1 to i8*)
br i1 %16, label %17, label %19
17: ; preds = %2
%18 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %19
19: ; preds = %17, %2
%20 = load i8*, i8** %8
%21 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %20)
br label %22
22: ; preds = %19, %22
%23 = getelementptr inbounds [101 x i8], [101 x i8]* %6, i64 0, i64 0
%24 = load i32, i32* @stdin
%25 = call i32 @fgets(i8* %23, i32 100, i32 %24)
%26 = load i8*, i8** %8
%27 = getelementptr inbounds [101 x i8], [101 x i8]* %6, i64 0, i64 0
%28 = call i32 @strcpy(i8* %26, i8* %27)
br label %22
}

LLVM IR (-O2):
@IPC_CREAT = global i32 0
@.str = [6 x i8] c"shmat\00"
@.str.1 = [22 x i8] c"\0Aget share memory %X\0A\00"
@stdin = global i32 0
@EXIT_SUCCESS = global i32 0
@IPC_RMID = global i32 0
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [101 x i8]
%4 = getelementptr inbounds [101 x i8], [101 x i8]* %3, i64 0, i64 0
%5 = load i32, i32* @IPC_CREAT
%6 = or i32 %5, 420
%7 = tail call i32 @shmget(i32 10, i32 100, i32 %6)
%8 = tail call i8* @shmat(i32 %7, i32* null, i32 0)
%9 = icmp eq i8* %8, inttoptr (i64 -1 to i8*)
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %12
12: ; preds = %10, %2
%13 = tail call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %8)
br label %14
14: ; preds = %14, %12
%15 = load i32, i32* @stdin
%16 = call i32 @fgets(i8* %4, i32 100, i32 %15)
%17 = call i32 @strcpy(i8* %8, i8* %4)
br label %14
}