#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 */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
char *shmat(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
scalar_t__ shmdt(char *);
int shmget(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(int argc, char **argv) {
  int shmid;
  char *mem;
  if ((shmid = shmget((key_t)217, 0, 0)) == -1) {
    perror("Dans shmget");
    exit(1);
  }
  if ((mem = shmat(shmid, NULL, 0)) == (char *)-1) {
    perror("shmat");
    exit(2);
  }
  printf("lu: %s\n", mem);
  printf("la mem est %p\n", mem);
  if (shmdt(mem)) {
    perror("shmdt");
    exit(3);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0xd9,%edi
xor    %edx,%edx
mov    %edx,%esi
call   1070 <shmget@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    119f <main+0x3f>
lea    0xe70(%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    11d7 <main+0x77>
lea    0xe44(%rip),%rdi
call   1050 <perror@plt>
mov    $0x2,%edi
call   1080 <exit@plt>
mov    -0x20(%rbp),%rsi
lea    0xe30(%rip),%rdi
call   1040 <printf@plt>
mov    -0x20(%rbp),%rsi
lea    0xe28(%rip),%rdi
call   1040 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <shmdt@plt>
cmp    $0x0,%rax
je     1220 <main+0xc0>
lea    0xe18(%rip),%rdi
call   1050 <perror@plt>
mov    $0x3,%edi
call   1080 <exit@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    $0xd9,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1070 <shmget@plt>
cmp    $0xffffffff,%eax
je     11b7 <main+0x57>
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1060 <shmat@plt>
cmp    $0xffffffffffffffff,%rax
je     11cd <main+0x6d>
mov    %rax,%rbx
lea    0xe83(%rip),%rdi
mov    %rax,%rsi
call   1040 <printf@plt>
lea    0xe7c(%rip),%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
mov    %rbx,%rdi
call   1030 <shmdt@plt>
test   %rax,%rax
jne    11e3 <main+0x83>
xor    %eax,%eax
pop    %rbx
ret
lea    0xe42(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    0xe38(%rip),%rdi
call   1050 <perror@plt>
mov    $0x2,%edi
call   1080 <exit@plt>
lea    0xe3f(%rip),%rdi
call   1050 <perror@plt>
mov    $0x3,%edi
call   1080 <exit@plt>

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

LLVM IR (-O2):
@.str = [12 x i8] c"Dans shmget\00"
@.str.1 = [6 x i8] c"shmat\00"
@.str.2 = [8 x i8] c"lu: %s\0A\00"
@.str.3 = [15 x i8] c"la mem est %p\0A\00"
@.str.4 = [6 x i8] c"shmdt\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 @shmget(i32 217, i32 0, i32 0)
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %8
5: ; preds = %2
%6 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %2
%9 = tail call i8* @shmat(i32 %3, i32* null, i32 0)
%10 = icmp eq i8* %9, inttoptr (i64 -1 to i8*)
br i1 %10, label %11, label %14
11: ; preds = %8
%12 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%13 = tail call i32 @exit(i32 2)
unreachable
14: ; preds = %8
%15 = tail call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* %9)
%16 = tail call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i8* %9)
%17 = tail call i64 @shmdt(i8* %9)
%18 = icmp eq i64 %17, 0
br i1 %18, label %22, label %19
19: ; preds = %14
%20 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%21 = tail call i32 @exit(i32 3)
unreachable
22: ; preds = %14
ret i32 0
}