#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 sembuf {
  int sem_op;
  scalar_t__ sem_flg;
  scalar_t__ sem_num;
};
typedef int key_t;

/* Variables and functions */
int /*<<< orphan*/ SHM_RDONLY;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int ftok(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int semget(int, int, int /*<<< orphan*/);
scalar_t__ semop(int, struct sembuf *, int);
char *shmat(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int shmget(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int main(int argc, char *argv[]) {
  key_t key;
  int sem;
  int shm;
  struct sembuf sembuf;
  char *chaine = NULL;
  if (argc != 2) {
    fprintf(stderr, "Syntaxe : %s fichier_clé \n", argv[0]);
    exit(1);
  }
  if ((key = ftok(argv[1], 0)) == -1) {
    perror("ftok");
    exit(1);
  }
  if (((sem = semget(key, 1, 0)) == -1) || ((shm = shmget(key, 0, 0)) == -1)) {
    perror("semget/shmget");
    exit(1);
  }
  if ((chaine = shmat(shm, NULL, SHM_RDONLY)) == NULL) {
    perror("shmat");
    exit(1);
  }
  sembuf.sem_num = 0;
  sembuf.sem_op = -1;
  sembuf.sem_flg = 0;
  if (semop(sem, &sembuf, 1) < 0) {
    perror("semop");
    exit(1);
  }
  fprintf(stdout, "%s\n", chaine);
  sembuf.sem_op = 1;
  if (semop(sem, &sembuf, 1) < 0) {
    perror("semop");
    exit(1);
  }
  return (0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x40(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11ce <main+0x4e>
mov    0x2e31(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe41(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
xor    %esi,%esi
call   10a0 <ftok@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    11ff <main+0x7f>
lea    0xe2c(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x14(%rbp),%edi
mov    $0x1,%esi
xor    %edx,%edx
call   1030 <semget@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
je     1232 <main+0xb2>
mov    -0x14(%rbp),%edi
xor    %edx,%edx
mov    %edx,%esi
call   1070 <shmget@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
jne    1248 <main+0xc8>
lea    0xde8(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x1c(%rbp),%edi
mov    0x2d86(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1060 <shmat@plt>
mov    %rax,-0x40(%rbp)
cmp    $0x0,%rax
jne    1281 <main+0x101>
lea    0xdbd(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
movq   $0x0,-0x28(%rbp)
movl   $0xffffffff,-0x38(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x18(%rbp),%edi
lea    -0x38(%rbp),%rsi
mov    $0x1,%edx
call   1080 <semop@plt>
cmp    $0x0,%rax
jge    12c9 <main+0x149>
lea    0xd7b(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    0x2d00(%rip),%rax
mov    (%rax),%edi
mov    -0x40(%rbp),%rdx
lea    0xd5e(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0x1,-0x38(%rbp)
mov    -0x18(%rbp),%edi
lea    -0x38(%rbp),%rsi
mov    $0x1,%edx
call   1080 <semop@plt>
cmp    $0x0,%rax
jge    131a <main+0x19a>
lea    0xd2a(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
cmp    $0x2,%edi
jne    125d <main+0xdd>
mov    0x8(%rsi),%rdi
xor    %esi,%esi
call   10a0 <ftok@plt>
cmp    $0xffffffff,%eax
je     127f <main+0xff>
mov    %eax,%ebx
mov    %eax,%edi
mov    $0x1,%esi
xor    %edx,%edx
call   1030 <semget@plt>
cmp    $0xffffffff,%eax
je     124b <main+0xcb>
mov    %eax,%ebp
mov    %ebx,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1070 <shmget@plt>
cmp    $0xffffffff,%eax
je     124b <main+0xcb>
mov    0x2e03(%rip),%rcx
mov    (%rcx),%edx
mov    %eax,%edi
xor    %esi,%esi
call   1060 <shmat@plt>
test   %rax,%rax
je     1288 <main+0x108>
mov    %rax,%rbx
movl   $0xffffffff,(%rsp)
xorps  %xmm0,%xmm0
movups %xmm0,0x8(%rsp)
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x1,%edx
call   1080 <semop@plt>
test   %rax,%rax
js     1254 <main+0xd4>
mov    0x2dba(%rip),%rax
mov    (%rax),%edi
lea    0xe1c(%rip),%rsi
mov    %rbx,%rdx
call   1040 <fprintf@plt>
movl   $0x1,(%rsp)
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x1,%edx
call   1080 <semop@plt>
test   %rax,%rax
js     1254 <main+0xd4>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xdcf(%rip),%rdi
jmp    128f <main+0x10f>
lea    0xdda(%rip),%rdi
jmp    128f <main+0x10f>
mov    0x2d7c(%rip),%rax
mov    (%rax),%edi
mov    (%rsi),%rdx
lea    0xd90(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    0xd96(%rip),%rdi
jmp    128f <main+0x10f>
lea    0xda0(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.sembuf = type { i32, i64, i64 }
@stderr = global i32 0
@.str = [28 x i8] c"Syntaxe : %s fichier_cl\C3\A9 \0A\00"
@.str.1 = [5 x i8] c"ftok\00"
@.str.2 = [14 x i8] c"semget/shmget\00"
@SHM_RDONLY = global i32 0
@.str.3 = [6 x i8] c"shmat\00"
@.str.4 = [6 x i8] c"semop\00"
@stdout = global i32 0
@.str.5 = [4 x i8] c"%s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.sembuf
%10 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* null, i8** %10
%11 = load i32, i32* %4
%12 = icmp ne i32 %11, 2
br i1 %12, label %13, label %20
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 0
%17 = load i8*, i8** %16
%18 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %17)
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %2
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 1
%23 = load i8*, i8** %22
%24 = call i32 @ftok(i8* %23, i32 0)
store i32 %24, i32* %6
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %29
26: ; preds = %20
%27 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %20
%30 = load i32, i32* %6
%31 = call i32 @semget(i32 %30, i32 1, i32 0)
store i32 %31, i32* %7
%32 = icmp eq i32 %31, -1
br i1 %32, label %37, label %33
33: ; preds = %29
%34 = load i32, i32* %6
%35 = call i32 @shmget(i32 %34, i32 0, i32 0)
store i32 %35, i32* %8
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %40
37: ; preds = %33, %29
%38 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%39 = call i32 @exit(i32 1)
unreachable
40: ; preds = %33
%41 = load i32, i32* %8
%42 = load i32, i32* @SHM_RDONLY
%43 = call i8* @shmat(i32 %41, i32* null, i32 %42)
store i8* %43, i8** %10
%44 = icmp eq i8* %43, null
br i1 %44, label %45, label %48
45: ; preds = %40
%46 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%47 = call i32 @exit(i32 1)
unreachable
48: ; preds = %40
%49 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %9, i32 0, i32 2
store i64 0, i64* %49
%50 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %9, i32 0, i32 0
store i32 -1, i32* %50
%51 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %9, i32 0, i32 1
store i64 0, i64* %51
%52 = load i32, i32* %7
%53 = call i64 @semop(i32 %52, %struct.sembuf* %9, i32 1)
%54 = icmp slt i64 %53, 0
br i1 %54, label %55, label %58
55: ; preds = %48
%56 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%57 = call i32 @exit(i32 1)
unreachable
58: ; preds = %48
%59 = load i32, i32* @stdout
%60 = load i8*, i8** %10
%61 = call i32 @fprintf(i32 %59, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i8* %60)
%62 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %9, i32 0, i32 0
store i32 1, i32* %62
%63 = load i32, i32* %7
%64 = call i64 @semop(i32 %63, %struct.sembuf* %9, i32 1)
%65 = icmp slt i64 %64, 0
br i1 %65, label %66, label %69
66: ; preds = %58
%67 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%68 = call i32 @exit(i32 1)
unreachable
69: ; preds = %58
ret i32 0
}

LLVM IR (-O2):
%struct.sembuf = type { i32, i64, i64 }
@stderr = global i32 0
@.str = [28 x i8] c"Syntaxe : %s fichier_cl\C3\A9 \0A\00"
@.str.1 = [5 x i8] c"ftok\00"
@.str.2 = [14 x i8] c"semget/shmget\00"
@SHM_RDONLY = global i32 0
@.str.3 = [6 x i8] c"shmat\00"
@.str.4 = [6 x i8] c"semop\00"
@stdout = global i32 0
@.str.5 = [4 x i8] c"%s\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sembuf
%4 = bitcast %struct.sembuf* %3 to i8*
%5 = icmp eq i32 %0, 2
br i1 %5, label %11, label %6
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = load i8*, i8** %1
%9 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %8)
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %2
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%13 = load i8*, i8** %12
%14 = tail call i32 @ftok(i8* %13, i32 0)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %19
16: ; preds = %11
%17 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%18 = tail call i32 @exit(i32 1)
unreachable
19: ; preds = %11
%20 = tail call i32 @semget(i32 %14, i32 1, i32 0)
%21 = icmp eq i32 %20, -1
br i1 %21, label %25, label %22
22: ; preds = %19
%23 = tail call i32 @shmget(i32 %14, i32 0, i32 0)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %28
25: ; preds = %22, %19
%26 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%27 = tail call i32 @exit(i32 1)
unreachable
28: ; preds = %22
%29 = load i32, i32* @SHM_RDONLY
%30 = tail call i8* @shmat(i32 %23, i32* null, i32 %29)
%31 = icmp eq i8* %30, null
br i1 %31, label %32, label %35
32: ; preds = %28
%33 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%34 = tail call i32 @exit(i32 1)
unreachable
35: ; preds = %28
%36 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %3, i64 0, i32 0
store i32 -1, i32* %36
%37 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %3, i64 0, i32 1
%38 = bitcast i64* %37 to <2 x i64>*
store <2 x i64> zeroinitializer, <2 x i64>* %38
%39 = call i64 @semop(i32 %20, %struct.sembuf* %3, i32 1)
%40 = icmp slt i64 %39, 0
br i1 %40, label %41, label %44
41: ; preds = %35
%42 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%43 = call i32 @exit(i32 1)
unreachable
44: ; preds = %35
%45 = load i32, i32* @stdout
%46 = call i32 @fprintf(i32 %45, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i8* %30)
store i32 1, i32* %36
%47 = call i64 @semop(i32 %20, %struct.sembuf* %3, i32 1)
%48 = icmp slt i64 %47, 0
br i1 %48, label %49, label %52
49: ; preds = %44
%50 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%51 = call i32 @exit(i32 1)
unreachable
52: ; preds = %44
ret i32 0
}