#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 /*<<< orphan*/ key_t;

/* Variables and functions */
scalar_t__ IPC_CREAT;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
char *shmat(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int shmget(int /*<<< orphan*/, int, scalar_t__);
int /*<<< orphan*/ strcpy(char *, char *);
int main(int argc, char **argv) {
  int shmid;
  char *mem;
  if ((shmid = shmget((key_t)217, 1000, 0750 + IPC_CREAT)) == -1) {
    perror("shmget");
    exit(1);
  }
  if ((mem = shmat(shmid, NULL, 0)) == (char *)-1) {
    perror("shmat");
    exit(2);
  }
  printf("la mem est %p\n", mem);
  strcpy(mem, "je suis le programme p1");
  exit(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)
mov    0x2e63(%rip),%rax
mov    (%rax),%rdx
add    $0x1e8,%rdx
mov    $0xd9,%edi
mov    $0x3e8,%esi
call   1070 <shmget@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    11b8 <main+0x58>
lea    0xe57(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x14(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1060 <shmat@plt>
mov    %rax,-0x20(%rbp)
mov    $0xffffffffffffffff,%rcx
cmp    %rcx,%rax
jne    11f0 <main+0x90>
lea    0xe26(%rip),%rdi
call   1050 <perror@plt>
mov    $0x2,%edi
call   1080 <exit@plt>
mov    -0x20(%rbp),%rsi
lea    0xe12(%rip),%rdi
call   1040 <printf@plt>
mov    -0x20(%rbp),%rdi
lea    0xe11(%rip),%rsi
call   1030 <strcpy@plt>
xor    %edi,%edi
call   1080 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    0x2e78(%rip),%rax
mov    $0x1e8,%edx
add    (%rax),%rdx
mov    $0xd9,%edi
mov    $0x3e8,%esi
call   1070 <shmget@plt>
cmp    $0xffffffff,%eax
jne    119a <main+0x3a>
lea    0xe75(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1060 <shmat@plt>
cmp    $0xffffffffffffffff,%rax
jne    11c1 <main+0x61>
lea    0xe55(%rip),%rdi
call   1050 <perror@plt>
mov    $0x2,%edi
call   1080 <exit@plt>
mov    %rax,%rbx
lea    0xe42(%rip),%rdi
mov    %rax,%rsi
call   1040 <printf@plt>
lea    0xe42(%rip),%rsi
mov    %rbx,%rdi
call   1030 <strcpy@plt>
xor    %edi,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@IPC_CREAT = global i64 0
@.str = [7 x i8] c"shmget\00"
@.str.1 = [6 x i8] c"shmat\00"
@.str.2 = [15 x i8] c"la mem est %p\0A\00"
@.str.3 = [24 x i8] c"je suis le programme p1\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i64, i64* @IPC_CREAT
%9 = add nsw i64 488, %8
%10 = call i32 @shmget(i32 217, i32 1000, i64 %9)
store i32 %10, i32* %6
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %2
%16 = load i32, i32* %6
%17 = call i8* @shmat(i32 %16, i32* null, i32 0)
store i8* %17, i8** %7
%18 = icmp eq i8* %17, inttoptr (i64 -1 to i8*)
br i1 %18, label %19, label %22
19: ; preds = %15
%20 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%21 = call i32 @exit(i32 2)
unreachable
22: ; preds = %15
%23 = load i8*, i8** %7
%24 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* %23)
%25 = load i8*, i8** %7
%26 = call i32 @strcpy(i8* %25, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
%27 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@IPC_CREAT = global i64 0
@.str = [7 x i8] c"shmget\00"
@.str.1 = [6 x i8] c"shmat\00"
@.str.2 = [15 x i8] c"la mem est %p\0A\00"
@.str.3 = [24 x i8] c"je suis le programme p1\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = load i64, i64* @IPC_CREAT
%4 = add nsw i64 %3, 488
%5 = tail call i32 @shmget(i32 217, i32 1000, i64 %4)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %10
7: ; preds = %2
%8 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %2
%11 = tail call i8* @shmat(i32 %5, i32* null, i32 0)
%12 = icmp eq i8* %11, inttoptr (i64 -1 to i8*)
br i1 %12, label %13, label %16
13: ; preds = %10
%14 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%15 = tail call i32 @exit(i32 2)
unreachable
16: ; preds = %10
%17 = tail call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* %11)
%18 = tail call i32 @strcpy(i8* %11, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
%19 = tail call i32 @exit(i32 0)
unreachable
}