#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 /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, char);
scalar_t__ shmat(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int shmget(int, int, int);
void server() {
  key_t key;
  int shmid = 0;
  char *addr = NULL;
  int i = 0;
  key = 75;
  if ((shmid = shmget(key, 1024, 0777)) == -1) {
    perror("server shmget error");
    exit(1);
  }
  if ((addr = (char *)shmat(shmid, NULL, 0)) == (char *)-1) {
    perror("server shmid error");
    exit(1);
  }
  while (1) {
    while (*addr != 'a')
      ;
    *addr = 'b';
    i++;
    printf("*addr+%d = %c\n", i, *addr + i);
    if (i == 5) {
      break;
    }
  }
}


x86 assembly (clang, -O0):
<server>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x4b,-0x4(%rbp)
mov    -0x4(%rbp),%edi
mov    $0x400,%esi
mov    $0x1ff,%edx
call   1060 <shmget@plt>
mov    %eax,-0x8(%rbp)
cmp    $0xffffffff,%eax
jne    11a9 <server+0x59>
lea    0xe66(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x8(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1050 <shmat@plt>
mov    %rax,-0x10(%rbp)
mov    $0xffffffffffffffff,%rcx
cmp    %rcx,%rax
jne    11e1 <server+0x91>
lea    0xe42(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
jmp    11e6 <server+0x96>
jmp    11eb <server+0x9b>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x61,%eax
je     1200 <server+0xb0>
jmp    11eb <server+0x9b>
mov    -0x10(%rbp),%rax
movb   $0x62,(%rax)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%esi
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
add    -0x14(%rbp),%eax
lea    0xe03(%rip),%rdi
movsbl %al,%edx
call   1030 <printf@plt>
cmpl   $0x5,-0x14(%rbp)
jne    123b <server+0xeb>
jmp    1240 <server+0xf0>
jmp    11e6 <server+0x96>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<server>:
push   %rbx
mov    $0x4b,%edi
mov    $0x400,%esi
mov    $0x1ff,%edx
call   1060 <shmget@plt>
cmp    $0xffffffff,%eax
je     1232 <server+0xe2>
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1050 <shmat@plt>
cmp    $0xffffffffffffffff,%rax
je     123b <server+0xeb>
mov    %rax,%rbx
cmpb   $0x61,(%rax)
jne    1230 <server+0xe0>
movb   $0x62,(%rbx)
lea    0xe8e(%rip),%rdi
mov    $0x1,%esi
mov    $0x63,%edx
call   1030 <printf@plt>
cmpb   $0x61,(%rbx)
jne    1230 <server+0xe0>
movb   $0x62,(%rbx)
lea    0xe6c(%rip),%rdi
mov    $0x2,%esi
mov    $0x64,%edx
call   1030 <printf@plt>
cmpb   $0x61,(%rbx)
jne    1230 <server+0xe0>
movb   $0x62,(%rbx)
lea    0xe4e(%rip),%rdi
mov    $0x3,%esi
mov    $0x65,%edx
call   1030 <printf@plt>
cmpb   $0x61,(%rbx)
jne    1230 <server+0xe0>
movb   $0x62,(%rbx)
lea    0xe30(%rip),%rdi
mov    $0x4,%esi
mov    $0x66,%edx
call   1030 <printf@plt>
cmpb   $0x61,(%rbx)
jne    1230 <server+0xe0>
movb   $0x62,(%rbx)
lea    0xe12(%rip),%rdi
mov    $0x5,%esi
mov    $0x67,%edx
pop    %rbx
jmp    1030 <printf@plt>
cs nopw 0x0(%rax,%rax,1)
nop
jmp    1230 <server+0xe0>
lea    0xdc7(%rip),%rdi
jmp    1242 <server+0xf2>
lea    0xdd2(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [20 x i8] c"server shmget error\00"
@.str.1 = [19 x i8] c"server shmid error\00"
@.str.2 = [15 x i8] c"*addr+%d = %c\0A\00"
define void @server() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
store i32 0, i32* %2
store i8* null, i8** %3
store i32 0, i32* %4
store i32 75, i32* %1
%5 = load i32, i32* %1
%6 = call i32 @shmget(i32 %5, i32 1024, i32 511)
store i32 %6, i32* %2
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %11
8: ; preds = %0
%9 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%10 = call i32 @exit(i32 1)
unreachable
11: ; preds = %0
%12 = load i32, i32* %2
%13 = call i64 @shmat(i32 %12, i32* null, i32 0)
%14 = inttoptr i64 %13 to i8*
store i8* %14, i8** %3
%15 = icmp eq i8* %14, inttoptr (i64 -1 to i8*)
br i1 %15, label %16, label %19
16: ; preds = %11
%17 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %11
br label %20
20: ; preds = %19, %42
br label %21
21: ; preds = %26, %20
%22 = load i8*, i8** %3
%23 = load i8, i8* %22
%24 = sext i8 %23 to i32
%25 = icmp ne i32 %24, 97
br i1 %25, label %26, label %27
26: ; preds = %21
br label %21
27: ; preds = %21
%28 = load i8*, i8** %3
store i8 98, i8* %28
%29 = load i32, i32* %4
%30 = add nsw i32 %29, 1
store i32 %30, i32* %4
%31 = load i32, i32* %4
%32 = load i8*, i8** %3
%33 = load i8, i8* %32
%34 = sext i8 %33 to i32
%35 = load i32, i32* %4
%36 = add nsw i32 %34, %35
%37 = trunc i32 %36 to i8
%38 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 %31, i8 signext %37)
%39 = load i32, i32* %4
%40 = icmp eq i32 %39, 5
br i1 %40, label %41, label %42
41: ; preds = %27
br label %43
42: ; preds = %27
br label %20
43: ; preds = %41
ret void
}

LLVM IR (-O2):
@.str = [20 x i8] c"server shmget error\00"
@.str.1 = [19 x i8] c"server shmid error\00"
@.str.2 = [15 x i8] c"*addr+%d = %c\0A\00"
define void @server() {
%1 = tail call i32 @shmget(i32 75, i32 1024, i32 511)
%2 = icmp eq i32 %1, -1
br i1 %2, label %3, label %6
3: ; preds = %0
%4 = tail call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%5 = tail call i32 @exit(i32 1)
unreachable
6: ; preds = %0
%7 = tail call i64 @shmat(i32 %1, i32* null, i32 0)
%8 = inttoptr i64 %7 to i8*
%9 = icmp eq i8* %8, inttoptr (i64 -1 to i8*)
br i1 %9, label %14, label %10
10: ; preds = %6
%11 = load i8, i8* %8
%12 = icmp eq i8 %11, 97
br i1 %12, label %18, label %13
13: ; preds = %30, %26, %22, %18, %10
br label %17
14: ; preds = %6
%15 = tail call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
%16 = tail call i32 @exit(i32 1)
unreachable
17: ; preds = %13, %17
br label %17
18: ; preds = %10
store i8 98, i8* %8
%19 = tail call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 1, i8 signext 99)
%20 = load i8, i8* %8
%21 = icmp eq i8 %20, 97
br i1 %21, label %22, label %13
22: ; preds = %18
store i8 98, i8* %8
%23 = tail call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 2, i8 signext 100)
%24 = load i8, i8* %8
%25 = icmp eq i8 %24, 97
br i1 %25, label %26, label %13
26: ; preds = %22
store i8 98, i8* %8
%27 = tail call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 3, i8 signext 101)
%28 = load i8, i8* %8
%29 = icmp eq i8 %28, 97
br i1 %29, label %30, label %13
30: ; preds = %26
store i8 98, i8* %8
%31 = tail call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 4, i8 signext 102)
%32 = load i8, i8* %8
%33 = icmp eq i8 %32, 97
br i1 %33, label %34, label %13
34: ; preds = %30
store i8 98, i8* %8
%35 = tail call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 5, i8 signext 103)
ret void
}