#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*/ IPC_RMID;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ memcpy(char *, char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
char *shmat(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ shmctl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ shmdt(char *);
int shmget(int, int, int);
int strlen(char *);
char toupper(char);
int main(int argc, char *argv[]) {
  printf("Program 2: shm1 --> konverterer --> shm2\n");
  int shmid1;
  int shmid2;
  key_t key = 1234;
  key_t key1 = 4321;
  char *shm1;
  char *shm2;
  char *s;
  char buffer[150];
  shmid1 = shmget(key, 500, 0666);
  if (shmid1 < 0) {
    perror("shmget");
    exit(1);
  }
  shm1 = shmat(shmid1, NULL, 0);
  if (shm1 == (char *)-1) {
    perror("shmat");
    exit(1);
  }
  int i;
  for (s = shm1; *s != 0; s++) {
    i++;
  }
  memcpy(buffer, shm1, i);
  shmctl(shmid1, IPC_RMID, 0);
  shmdt(shm1);
  int o;
  for (o = 0; o < i; o++) {
    buffer[o] = toupper(buffer[o]);
  }
  shmid2 = shmget(key1, 500, IPC_CREAT | 0666);
  if (shmid2 < 0) {
    perror("shmget");
    exit(1);
  }
  shm2 = shmat(shmid2, NULL, 0);
  if (shm2 == (char *)-1) {
    perror("shmat");
    exit(1);
  }
  memcpy(shm2, buffer, strlen(buffer));
  shmdt(shm2);
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xf0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe40(%rip),%rdi
call   1060 <printf@plt>
movl   $0x4d2,-0x1c(%rbp)
movl   $0x10e1,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
mov    $0x1f4,%esi
mov    $0x1b6,%edx
call   10b0 <shmget@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1208 <main+0x68>
lea    0xe31(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x14(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   10a0 <shmat@plt>
mov    %rax,-0x28(%rbp)
mov    $0xffffffffffffffff,%rax
cmp    %rax,-0x28(%rbp)
jne    1241 <main+0xa1>
lea    0xdff(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     1279 <main+0xd9>
mov    -0xd4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xd4(%rbp)
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
jmp    1249 <main+0xa9>
lea    -0xd0(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0xd4(%rbp),%edx
call   1070 <memcpy@plt>
mov    -0x14(%rbp),%edi
mov    0x2d47(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <shmctl@plt>
mov    -0x28(%rbp),%rdi
call   1040 <shmdt@plt>
movl   $0x0,-0xd8(%rbp)
mov    -0xd8(%rbp),%eax
cmp    -0xd4(%rbp),%eax
jge    12ff <main+0x15f>
movslq -0xd8(%rbp),%rax
movsbl -0xd0(%rbp,%rax,1),%edi
ff
call   1030 <toupper@plt>
mov    %al,%cl
movslq -0xd8(%rbp),%rax
mov    %cl,-0xd0(%rbp,%rax,1)
mov    -0xd8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xd8(%rbp)
jmp    12b5 <main+0x115>
mov    -0x20(%rbp),%edi
mov    0x2ccf(%rip),%rax
mov    (%rax),%edx
or     $0x1b6,%edx
mov    $0x1f4,%esi
call   10b0 <shmget@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    133e <main+0x19e>
lea    0xcfb(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x18(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   10a0 <shmat@plt>
mov    %rax,-0x30(%rbp)
mov    $0xffffffffffffffff,%rax
cmp    %rax,-0x30(%rbp)
jne    1377 <main+0x1d7>
lea    0xcc9(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0xe8(%rbp)
lea    -0xd0(%rbp),%rax
mov    %rax,-0xe0(%rbp)
lea    -0xd0(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0xe8(%rbp),%rdi
mov    -0xe0(%rbp),%rsi
mov    %eax,%edx
call   1070 <memcpy@plt>
mov    -0x30(%rbp),%rdi
call   1040 <shmdt@plt>
xor    %edi,%edi
call   10c0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0xa0,%rsp
lea    0xe5b(%rip),%rdi
call   1040 <puts@plt>
mov    $0x4d2,%edi
mov    $0x1f4,%esi
mov    $0x1b6,%edx
call   10b0 <shmget@plt>
test   %eax,%eax
js     1276 <main+0xd6>
mov    %eax,%ebx
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   10a0 <shmat@plt>
cmp    $0xffffffffffffffff,%rax
je     1290 <main+0xf0>
mov    %rax,%r14
cmpb   $0x0,(%rax)
je     120c <main+0x6c>
mov    %r14,%rax
add    $0x1,%rax
nopl   0x0(%rax)
add    $0x1,%ebp
cmpb   $0x0,(%rax)
lea    0x1(%rax),%rax
jne    1200 <main+0x60>
mov    %rsp,%rdi
mov    %r14,%rsi
mov    %ebp,%edx
call   1070 <memcpy@plt>
mov    0x2dc0(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1080 <shmctl@plt>
mov    %r14,%rdi
call   1050 <shmdt@plt>
test   %ebp,%ebp
jle    1255 <main+0xb5>
mov    %ebp,%ebx
xor    %ebp,%ebp
nopl   0x0(%rax,%rax,1)
movsbl (%rsp,%rbp,1),%edi
call   1030 <toupper@plt>
mov    %al,(%rsp,%rbp,1)
add    $0x1,%rbp
cmp    %rbp,%rbx
jne    1240 <main+0xa0>
mov    0x2d7c(%rip),%rax
mov    $0x1b6,%edx
or     (%rax),%edx
mov    $0x10e1,%edi
mov    $0x1f4,%esi
call   10b0 <shmget@plt>
test   %eax,%eax
jns    127f <main+0xdf>
lea    0xd83(%rip),%rdi
jmp    1297 <main+0xf7>
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   10a0 <shmat@plt>
cmp    $0xffffffffffffffff,%rax
jne    12a6 <main+0x106>
lea    0xd70(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    %rax,%rbx
mov    %rsp,%rbp
mov    %rbp,%rdi
call   1060 <strlen@plt>
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    %eax,%edx
call   1070 <memcpy@plt>
mov    %rbx,%rdi
call   1050 <shmdt@plt>
xor    %edi,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@.str = [42 x i8] c"Program 2: shm1 --> konverterer --> shm2\0A\00"
@.str.1 = [7 x i8] c"shmget\00"
@.str.2 = [6 x i8] c"shmat\00"
@IPC_RMID = global i32 0
@IPC_CREAT = global i32 0
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 i32
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca [150 x i8]
%14 = alloca i32
%15 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%16 = call i32 @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0))
store i32 1234, i32* %8
store i32 4321, i32* %9
%17 = load i32, i32* %8
%18 = call i32 @shmget(i32 %17, i32 500, i32 438)
store i32 %18, i32* %6
%19 = load i32, i32* %6
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %2
%22 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %2
%25 = load i32, i32* %6
%26 = call i8* @shmat(i32 %25, i32* null, i32 0)
store i8* %26, i8** %10
%27 = load i8*, i8** %10
%28 = icmp eq i8* %27, inttoptr (i64 -1 to i8*)
br i1 %28, label %29, label %32
29: ; preds = %24
%30 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %24
%33 = load i8*, i8** %10
store i8* %33, i8** %12
br label %34
34: ; preds = %42, %32
%35 = load i8*, i8** %12
%36 = load i8, i8* %35
%37 = sext i8 %36 to i32
%38 = icmp ne i32 %37, 0
br i1 %38, label %39, label %45
39: ; preds = %34
%40 = load i32, i32* %14
%41 = add nsw i32 %40, 1
store i32 %41, i32* %14
br label %42
42: ; preds = %39
%43 = load i8*, i8** %12
%44 = getelementptr inbounds i8, i8* %43, i32 1
store i8* %44, i8** %12
br label %34
45: ; preds = %34
%46 = getelementptr inbounds [150 x i8], [150 x i8]* %13, i64 0, i64 0
%47 = load i8*, i8** %10
%48 = load i32, i32* %14
%49 = call i32 @memcpy(i8* %46, i8* %47, i32 %48)
%50 = load i32, i32* %6
%51 = load i32, i32* @IPC_RMID
%52 = call i32 @shmctl(i32 %50, i32 %51, i32 0)
%53 = load i8*, i8** %10
%54 = call i32 @shmdt(i8* %53)
store i32 0, i32* %15
br label %55
55: ; preds = %68, %45
%56 = load i32, i32* %15
%57 = load i32, i32* %14
%58 = icmp slt i32 %56, %57
br i1 %58, label %59, label %71
59: ; preds = %55
%60 = load i32, i32* %15
%61 = sext i32 %60 to i64
%62 = getelementptr inbounds [150 x i8], [150 x i8]* %13, i64 0, i64 %61
%63 = load i8, i8* %62
%64 = call signext i8 @toupper(i8 signext %63)
%65 = load i32, i32* %15
%66 = sext i32 %65 to i64
%67 = getelementptr inbounds [150 x i8], [150 x i8]* %13, i64 0, i64 %66
store i8 %64, i8* %67
br label %68
68: ; preds = %59
%69 = load i32, i32* %15
%70 = add nsw i32 %69, 1
store i32 %70, i32* %15
br label %55
71: ; preds = %55
%72 = load i32, i32* %9
%73 = load i32, i32* @IPC_CREAT
%74 = or i32 %73, 438
%75 = call i32 @shmget(i32 %72, i32 500, i32 %74)
store i32 %75, i32* %7
%76 = load i32, i32* %7
%77 = icmp slt i32 %76, 0
br i1 %77, label %78, label %81
78: ; preds = %71
%79 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%80 = call i32 @exit(i32 1)
unreachable
81: ; preds = %71
%82 = load i32, i32* %7
%83 = call i8* @shmat(i32 %82, i32* null, i32 0)
store i8* %83, i8** %11
%84 = load i8*, i8** %11
%85 = icmp eq i8* %84, inttoptr (i64 -1 to i8*)
br i1 %85, label %86, label %89
86: ; preds = %81
%87 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%88 = call i32 @exit(i32 1)
unreachable
89: ; preds = %81
%90 = load i8*, i8** %11
%91 = getelementptr inbounds [150 x i8], [150 x i8]* %13, i64 0, i64 0
%92 = getelementptr inbounds [150 x i8], [150 x i8]* %13, i64 0, i64 0
%93 = call i32 @strlen(i8* %92)
%94 = call i32 @memcpy(i8* %90, i8* %91, i32 %93)
%95 = load i8*, i8** %11
%96 = call i32 @shmdt(i8* %95)
%97 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str.1 = [7 x i8] c"shmget\00"
@.str.2 = [6 x i8] c"shmat\00"
@IPC_RMID = global i32 0
@IPC_CREAT = global i32 0
@str = [41 x i8] c"Program 2: shm1 --> konverterer --> shm2\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [150 x i8]
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @str, i64 0, i64 0))
%5 = getelementptr inbounds [150 x i8], [150 x i8]* %3, i64 0, i64 0
%6 = tail call i32 @shmget(i32 1234, i32 500, i32 438)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %2
%9 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %2
%12 = tail call i8* @shmat(i32 %6, i32* null, i32 0)
%13 = icmp eq i8* %12, inttoptr (i64 -1 to i8*)
br i1 %13, label %17, label %14
14: ; preds = %11
%15 = load i8, i8* %12
%16 = icmp eq i8 %15, 0
br i1 %16, label %27, label %20
17: ; preds = %11
%18 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%19 = tail call i32 @exit(i32 1)
unreachable
20: ; preds = %14, %20
%21 = phi i32 [ %23, %20 ], [ undef, %14 ]
%22 = phi i8* [ %24, %20 ], [ %12, %14 ]
%23 = add nsw i32 %21, 1
%24 = getelementptr inbounds i8, i8* %22, i64 1
%25 = load i8, i8* %24
%26 = icmp eq i8 %25, 0
br i1 %26, label %27, label %20
27: ; preds = %20, %14
%28 = phi i32 [ undef, %14 ], [ %23, %20 ]
%29 = call i32 @memcpy(i8* %5, i8* %12, i32 %28)
%30 = load i32, i32* @IPC_RMID
%31 = call i32 @shmctl(i32 %6, i32 %30, i32 0)
%32 = call i32 @shmdt(i8* %12)
%33 = icmp sgt i32 %28, 0
br i1 %33, label %34, label %43
34: ; preds = %27
%35 = zext i32 %28 to i64
br label %36
36: ; preds = %34, %36
%37 = phi i64 [ 0, %34 ], [ %41, %36 ]
%38 = getelementptr inbounds [150 x i8], [150 x i8]* %3, i64 0, i64 %37
%39 = load i8, i8* %38
%40 = call signext i8 @toupper(i8 signext %39)
store i8 %40, i8* %38
%41 = add nuw nsw i64 %37, 1
%42 = icmp eq i64 %41, %35
br i1 %42, label %43, label %36
43: ; preds = %36, %27
%44 = load i32, i32* @IPC_CREAT
%45 = or i32 %44, 438
%46 = call i32 @shmget(i32 4321, i32 500, i32 %45)
%47 = icmp slt i32 %46, 0
br i1 %47, label %48, label %51
48: ; preds = %43
%49 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%50 = call i32 @exit(i32 1)
unreachable
51: ; preds = %43
%52 = call i8* @shmat(i32 %46, i32* null, i32 0)
%53 = icmp eq i8* %52, inttoptr (i64 -1 to i8*)
br i1 %53, label %54, label %57
54: ; preds = %51
%55 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%56 = call i32 @exit(i32 1)
unreachable
57: ; preds = %51
%58 = call i32 @strlen(i8* %5)
%59 = call i32 @memcpy(i8* %52, i8* %5, i32 %58)
%60 = call i32 @shmdt(i8* %52)
%61 = call i32 @exit(i32 0)
unreachable
}