#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 key_t;

/* Variables and functions */
int IPC_CREAT;
int /*<<< orphan*/ exit(int);
int ftok(char *, char);
int /*<<< orphan*/ memcpy(char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
char *shmat(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int shmget(int, int, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ strlen(char *);
int main(int argc, char *argv[]) {
  int shmid;
  key_t key;
  char *shm, *s;
  char *message = "The server sends a line of text0";
  if ((key = ftok("Makefile", 'R')) == -1) {
    perror("ftok");
    exit(1);
  }
  if ((shmid = shmget(key, 100, IPC_CREAT | 0666)) < 0) {
    perror("shmget");
    exit(1);
  }
  if ((shm = shmat(shmid, NULL, 0)) == (char *)-1) {
    perror("shmat");
    exit(1);
  }
  memcpy(shm, message, strlen(message));
  printf("Server side message is: %s\n", message);
  printf("Waiting for Client to change the memory\n");
  while (*shm != '*')
    sleep(1);
  sleep(1);
  printf("\nClient changed the memory, and the message is: \n");
  for (s = shm; *s != '0'; s++)
    printf("%c", *s);
  sleep(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)
lea    0xe53(%rip),%rax
mov    %rax,-0x30(%rbp)
lea    0xe69(%rip),%rdi
mov    $0x52,%esi
call   10a0 <ftok@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    11e4 <main+0x54>
lea    0xe55(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x18(%rbp),%edi
mov    0x2df2(%rip),%rax
mov    (%rax),%edx
or     $0x1b6,%edx
mov    $0x64,%esi
call   1080 <shmget@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    1222 <main+0x92>
lea    0xe1c(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x14(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1070 <shmat@plt>
mov    %rax,-0x20(%rbp)
mov    $0xffffffffffffffff,%rcx
cmp    %rcx,%rax
jne    125a <main+0xca>
lea    0xdeb(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x40(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    %eax,%edx
call   1050 <memcpy@plt>
mov    -0x30(%rbp),%rsi
lea    0xdaf(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xdbd(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2a,%eax
je     12c1 <main+0x131>
mov    $0x1,%edi
call   10b0 <sleep@plt>
jmp    12a2 <main+0x112>
mov    $0x1,%edi
call   10b0 <sleep@plt>
lea    0xdaf(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x30,%eax
je     1317 <main+0x187>
mov    -0x28(%rbp),%rax
movsbl (%rax),%esi
lea    0xdb4(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    12e1 <main+0x151>
mov    $0x1,%edi
call   10b0 <sleep@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
lea    0xe66(%rip),%rdi
mov    $0x52,%esi
call   10c0 <ftok@plt>
cmp    $0xffffffff,%eax
je     12a7 <main+0xf7>
mov    0x2e0b(%rip),%rcx
mov    $0x1b6,%edx
or     (%rcx),%edx
mov    %eax,%edi
mov    $0x64,%esi
call   10a0 <shmget@plt>
test   %eax,%eax
js     12b0 <main+0x100>
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1090 <shmat@plt>
cmp    $0xffffffffffffffff,%rax
je     12b9 <main+0x109>
mov    %rax,%rbx
lea    0xdf1(%rip),%r14
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %eax,%edx
call   1070 <memcpy@plt>
lea    0xe11(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe1c(%rip),%rdi
call   1040 <puts@plt>
cmpb   $0x2a,(%rbx)
je     125f <main+0xaf>
cs nopw 0x0(%rax,%rax,1)
mov    $0x1,%edi
call   10d0 <sleep@plt>
cmpb   $0x2a,(%rbx)
jne    1250 <main+0xa0>
mov    $0x1,%edi
call   10d0 <sleep@plt>
lea    0xe10(%rip),%rdi
call   1040 <puts@plt>
mov    (%rbx),%al
cmp    $0x30,%al
je     1293 <main+0xe3>
add    $0x1,%rbx
nop
movsbl %al,%edi
call   1030 <putchar@plt>
movzbl (%rbx),%eax
add    $0x1,%rbx
cmp    $0x30,%al
jne    1280 <main+0xd0>
mov    $0x1,%edi
call   10d0 <sleep@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xd7c(%rip),%rdi
jmp    12c0 <main+0x110>
lea    0xd78(%rip),%rdi
jmp    12c0 <main+0x110>
lea    0xd76(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [33 x i8] c"The server sends a line of text0\00"
@.str.1 = [9 x i8] c"Makefile\00"
@.str.2 = [5 x i8] c"ftok\00"
@IPC_CREAT = global i32 0
@.str.3 = [7 x i8] c"shmget\00"
@.str.4 = [6 x i8] c"shmat\00"
@.str.5 = [28 x i8] c"Server side message is: %s\0A\00"
@.str.6 = [41 x i8] c"Waiting for Client to change the memory\0A\00"
@.str.7 = [50 x i8] c"\0AClient changed the memory, and the message is: \0A\00"
@.str.8 = [3 x i8] c"%c\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 i8*
%9 = alloca i8*
%10 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i8** %10
%11 = call i32 @ftok(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8 signext 82)
store i32 %11, i32* %7
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %2
%17 = load i32, i32* %7
%18 = load i32, i32* @IPC_CREAT
%19 = or i32 %18, 438
%20 = call i32 @shmget(i32 %17, i32 100, i32 %19)
store i32 %20, i32* %6
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %16
%23 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %16
%26 = load i32, i32* %6
%27 = call i8* @shmat(i32 %26, i32* null, i32 0)
store i8* %27, i8** %8
%28 = icmp eq i8* %27, inttoptr (i64 -1 to i8*)
br i1 %28, label %29, label %32
29: ; preds = %25
%30 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %25
%33 = load i8*, i8** %8
%34 = load i8*, i8** %10
%35 = load i8*, i8** %10
%36 = call i32 @strlen(i8* %35)
%37 = call i32 @memcpy(i8* %33, i8* %34, i32 %36)
%38 = load i8*, i8** %10
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0), i8* %38)
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.6, i64 0, i64 0))
br label %41
41: ; preds = %46, %32
%42 = load i8*, i8** %8
%43 = load i8, i8* %42
%44 = sext i8 %43 to i32
%45 = icmp ne i32 %44, 42
br i1 %45, label %46, label %48
46: ; preds = %41
%47 = call i32 @sleep(i32 1)
br label %41
48: ; preds = %41
%49 = call i32 @sleep(i32 1)
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.7, i64 0, i64 0))
%51 = load i8*, i8** %8
store i8* %51, i8** %9
br label %52
52: ; preds = %62, %48
%53 = load i8*, i8** %9
%54 = load i8, i8* %53
%55 = sext i8 %54 to i32
%56 = icmp ne i32 %55, 48
br i1 %56, label %57, label %65
57: ; preds = %52
%58 = load i8*, i8** %9
%59 = load i8, i8* %58
%60 = sext i8 %59 to i32
%61 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.8, i64 0, i64 0), i32 %60)
br label %62
62: ; preds = %57
%63 = load i8*, i8** %9
%64 = getelementptr inbounds i8, i8* %63, i32 1
store i8* %64, i8** %9
br label %52
65: ; preds = %52
%66 = call i32 @sleep(i32 1)
ret i32 0
}

LLVM IR (-O2):
@.str = [33 x i8] c"The server sends a line of text0\00"
@.str.1 = [9 x i8] c"Makefile\00"
@.str.2 = [5 x i8] c"ftok\00"
@IPC_CREAT = global i32 0
@.str.3 = [7 x i8] c"shmget\00"
@.str.4 = [6 x i8] c"shmat\00"
@.str.5 = [28 x i8] c"Server side message is: %s\0A\00"
@str = [40 x i8] c"Waiting for Client to change the memory\00"
@str.9 = [49 x i8] c"\0AClient changed the memory, and the message is: \00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 @ftok(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8 signext 82)
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %8
5: ; preds = %2
%6 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %2
%9 = load i32, i32* @IPC_CREAT
%10 = or i32 %9, 438
%11 = tail call i32 @shmget(i32 %3, i32 100, i32 %10)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %8
%14 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%15 = tail call i32 @exit(i32 1)
unreachable
16: ; preds = %8
%17 = tail call i8* @shmat(i32 %11, i32* null, i32 0)
%18 = icmp eq i8* %17, inttoptr (i64 -1 to i8*)
br i1 %18, label %19, label %22
19: ; preds = %16
%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 1)
unreachable
22: ; preds = %16
%23 = tail call i32 @strlen(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
%24 = tail call i32 @memcpy(i8* %17, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i32 %23)
%25 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
%26 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @str, i64 0, i64 0))
%27 = load i8, i8* %17
%28 = icmp eq i8 %27, 42
br i1 %28, label %33, label %29
29: ; preds = %22, %29
%30 = tail call i32 @sleep(i32 1)
%31 = load i8, i8* %17
%32 = icmp eq i8 %31, 42
br i1 %32, label %33, label %29
33: ; preds = %29, %22
%34 = tail call i32 @sleep(i32 1)
%35 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([49 x i8], [49 x i8]* @str.9, i64 0, i64 0))
%36 = load i8, i8* %17
%37 = icmp eq i8 %36, 48
br i1 %37, label %46, label %38
38: ; preds = %33, %38
%39 = phi i8 [ %44, %38 ], [ %36, %33 ]
%40 = phi i8* [ %43, %38 ], [ %17, %33 ]
%41 = sext i8 %39 to i32
%42 = tail call i32 @putchar(i32 %41)
%43 = getelementptr inbounds i8, i8* %40, i64 1
%44 = load i8, i8* %43
%45 = icmp eq i8 %44, 48
br i1 %45, label %46, label %38
46: ; preds = %38, %33
%47 = tail call i32 @sleep(i32 1)
ret i32 0
}