#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 IPC_CREAT;
int IPC_EXCL;
int /*<<< orphan*/ O_RDONLY;
scalar_t__ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ ftok(char *, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int read(int, char *, int);
scalar_t__ shmat(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
scalar_t__ shmdt(char *);
int shmget(int /*<<< orphan*/, int, int);
int main() {
  int shmid;
  int fd;
  int size;
  int i;
  char pathname[] = "2a.c";
  char string[1738];
  char *shstring;
  key_t key;
  if ((fd = open("2a.c", O_RDONLY)) < 0) {
    printf("Can\'t open file\n");
    exit(-1);
  }
  size = read(fd, string, 1738);
  if (size != 1738) {
    printf("Can\'t read all string\n");
    exit(-1);
  }
  if (close(fd) < 0)
    printf("Can\'t close file\n");
  if ((key = ftok(pathname, 0)) < 0) {
    printf("Can\'t generate key\n");
    exit(-1);
  }
  if ((shmid = shmget(key, 1738 * sizeof(char), 0666 | IPC_CREAT | IPC_EXCL)) <
      0) {
    printf("Can\'t create shared memory\n");
    exit(-1);
  }
  if ((shstring = (char *)shmat(shmid, NULL, 0)) == (char *)(-1)) {
    printf("Can't attach shared memory\n");
    exit(-1);
  }
  for (i = 0; i < 1738; i++)
    shstring[i] = string[i];
  if (shmdt(shstring) < 0) {
    printf("Can't detach shared memory\n");
    exit(-1);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x700,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xe58(%rip),%eax
mov    %eax,-0x19(%rbp)
mov    0xe53(%rip),%al
mov    %al,-0x15(%rbp)
mov    0x2e25(%rip),%rax
mov    (%rax),%esi
lea    0xe3c(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    11eb <main+0x5b>
lea    0xe29(%rip),%rdi
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
mov    -0xc(%rbp),%edi
lea    -0x6f0(%rbp),%rsi
mov    $0x6ca,%edx
call   1060 <read@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x6ca,-0x10(%rbp)
je     1225 <main+0x95>
lea    0xe00(%rip),%rdi
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
cmp    $0x0,%rax
jge    1243 <main+0xb3>
lea    0xdef(%rip),%rdi
call   1040 <printf@plt>
lea    -0x19(%rbp),%rdi
xor    %esi,%esi
call   10b0 <ftok@plt>
mov    %eax,-0x6fc(%rbp)
cmp    $0x0,%eax
jge    1273 <main+0xe3>
lea    0xddb(%rip),%rdi
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
mov    -0x6fc(%rbp),%edi
mov    0x2d50(%rip),%rax
mov    (%rax),%edx
or     $0x1b6,%edx
mov    0x2d61(%rip),%rax
or     (%rax),%edx
mov    $0x6ca,%esi
call   1090 <shmget@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    12bd <main+0x12d>
lea    0xda5(%rip),%rdi
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
mov    -0x8(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1080 <shmat@plt>
mov    %rax,-0x6f8(%rbp)
mov    $0xffffffffffffffff,%rcx
cmp    %rcx,%rax
jne    12f8 <main+0x168>
lea    0xd86(%rip),%rdi
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
movl   $0x0,-0x14(%rbp)
cmpl   $0x6ca,-0x14(%rbp)
jge    1333 <main+0x1a3>
movslq -0x14(%rbp),%rax
mov    -0x6f0(%rbp,%rax,1),%dl
mov    -0x6f8(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    12ff <main+0x16f>
mov    -0x6f8(%rbp),%rdi
call   1030 <shmdt@plt>
cmp    $0x0,%rax
jge    135f <main+0x1cf>
lea    0xd3b(%rip),%rdi
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
xor    %eax,%eax
add    $0x700,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x6e0,%rsp
movb   $0x0,0xc(%rsp)
movl   $0x632e6132,0x8(%rsp)
63
lea    0xe44(%rip),%rdi
mov    0x2e1d(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
test   %eax,%eax
js     127e <main+0xde>
mov    %eax,%ebx
lea    0x10(%rsp),%rsi
mov    %eax,%edi
mov    $0x6ca,%edx
call   1060 <read@plt>
cmp    $0x6ca,%eax
jne    1287 <main+0xe7>
mov    %ebx,%edi
call   1050 <close@plt>
test   %rax,%rax
jns    1208 <main+0x68>
lea    0xe66(%rip),%rdi
call   1030 <puts@plt>
lea    0x8(%rsp),%rdi
xor    %esi,%esi
call   10c0 <ftok@plt>
test   %eax,%eax
js     1290 <main+0xf0>
mov    0x2db1(%rip),%rcx
mov    (%rcx),%edx
mov    0x2dc8(%rip),%rcx
or     (%rcx),%edx
or     $0x1b6,%edx
mov    %eax,%edi
mov    $0x6ca,%esi
call   10a0 <shmget@plt>
test   %eax,%eax
js     1299 <main+0xf9>
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1090 <shmat@plt>
cmp    $0xffffffffffffffff,%rax
je     12a2 <main+0x102>
mov    %rax,%rbx
lea    0x10(%rsp),%rsi
mov    $0x6ca,%edx
mov    %rax,%rdi
call   1070 <memcpy@plt>
mov    %rbx,%rdi
call   1040 <shmdt@plt>
test   %rax,%rax
js     12ab <main+0x10b>
xor    %eax,%eax
add    $0x6e0,%rsp
pop    %rbx
ret
lea    0xe0b(%rip),%rdi
jmp    12b2 <main+0x112>
lea    0xdec(%rip),%rdi
jmp    12b2 <main+0x112>
lea    0xdbf(%rip),%rdi
jmp    12b2 <main+0x112>
lea    0xd9b(%rip),%rdi
jmp    12b2 <main+0x112>
lea    0xd77(%rip),%rdi
jmp    12b2 <main+0x112>
lea    0xd53(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@__const.main.pathname = [5 x i8] c"2a.c\00"
@.str = [5 x i8] c"2a.c\00"
@O_RDONLY = global i32 0
@.str.1 = [17 x i8] c"Can't open file\0A\00"
@.str.2 = [23 x i8] c"Can't read all string\0A\00"
@.str.3 = [18 x i8] c"Can't close file\0A\00"
@.str.4 = [20 x i8] c"Can't generate key\0A\00"
@IPC_CREAT = global i32 0
@IPC_EXCL = global i32 0
@.str.5 = [28 x i8] c"Can't create shared memory\0A\00"
@.str.6 = [28 x i8] c"Can't attach shared memory\0A\00"
@.str.7 = [28 x i8] c"Can't detach shared memory\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [5 x i8]
%7 = alloca [1738 x i8]
%8 = alloca i8*
%9 = alloca i32
store i32 0, i32* %1
%10 = bitcast [5 x i8]* %6 to i8*
%11 = load i32, i32* @O_RDONLY
%12 = call i32 @open(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %11)
store i32 %12, i32* %3
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %0
%15 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%16 = call i32 @exit(i32 -1)
unreachable
17: ; preds = %0
%18 = load i32, i32* %3
%19 = getelementptr inbounds [1738 x i8], [1738 x i8]* %7, i64 0, i64 0
%20 = call i32 @read(i32 %18, i8* %19, i32 1738)
store i32 %20, i32* %4
%21 = load i32, i32* %4
%22 = icmp ne i32 %21, 1738
br i1 %22, label %23, label %26
23: ; preds = %17
%24 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
%25 = call i32 @exit(i32 -1)
unreachable
26: ; preds = %17
%27 = load i32, i32* %3
%28 = call i64 @close(i32 %27)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %26
%31 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
br label %32
32: ; preds = %30, %26
%33 = getelementptr inbounds [5 x i8], [5 x i8]* %6, i64 0, i64 0
%34 = call i32 @ftok(i8* %33, i32 0)
store i32 %34, i32* %9
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %32
%37 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0))
%38 = call i32 @exit(i32 -1)
unreachable
39: ; preds = %32
%40 = load i32, i32* %9
%41 = load i32, i32* @IPC_CREAT
%42 = or i32 438, %41
%43 = load i32, i32* @IPC_EXCL
%44 = or i32 %42, %43
%45 = call i32 @shmget(i32 %40, i32 1738, i32 %44)
store i32 %45, i32* %2
%46 = icmp slt i32 %45, 0
br i1 %46, label %47, label %50
47: ; preds = %39
%48 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0))
%49 = call i32 @exit(i32 -1)
unreachable
50: ; preds = %39
%51 = load i32, i32* %2
%52 = call i64 @shmat(i32 %51, i32* null, i32 0)
%53 = inttoptr i64 %52 to i8*
store i8* %53, i8** %8
%54 = icmp eq i8* %53, inttoptr (i64 -1 to i8*)
br i1 %54, label %55, label %58
55: ; preds = %50
%56 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.6, i64 0, i64 0))
%57 = call i32 @exit(i32 -1)
unreachable
58: ; preds = %50
store i32 0, i32* %5
br label %59
59: ; preds = %71, %58
%60 = load i32, i32* %5
%61 = icmp slt i32 %60, 1738
br i1 %61, label %62, label %74
62: ; preds = %59
%63 = load i32, i32* %5
%64 = sext i32 %63 to i64
%65 = getelementptr inbounds [1738 x i8], [1738 x i8]* %7, i64 0, i64 %64
%66 = load i8, i8* %65
%67 = load i8*, i8** %8
%68 = load i32, i32* %5
%69 = sext i32 %68 to i64
%70 = getelementptr inbounds i8, i8* %67, i64 %69
store i8 %66, i8* %70
br label %71
71: ; preds = %62
%72 = load i32, i32* %5
%73 = add nsw i32 %72, 1
store i32 %73, i32* %5
br label %59
74: ; preds = %59
%75 = load i8*, i8** %8
%76 = call i64 @shmdt(i8* %75)
%77 = icmp slt i64 %76, 0
br i1 %77, label %78, label %81
78: ; preds = %74
%79 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.7, i64 0, i64 0))
%80 = call i32 @exit(i32 -1)
unreachable
81: ; preds = %74
ret i32 0
}

LLVM IR (-O2):
@.str = [5 x i8] c"2a.c\00"
@O_RDONLY = global i32 0
@IPC_CREAT = global i32 0
@IPC_EXCL = global i32 0
@str = [27 x i8] c"Can't detach shared memory\00"
@str.8 = [27 x i8] c"Can't attach shared memory\00"
@str.9 = [27 x i8] c"Can't create shared memory\00"
@str.10 = [19 x i8] c"Can't generate key\00"
@str.11 = [17 x i8] c"Can't close file\00"
@str.12 = [22 x i8] c"Can't read all string\00"
@str.13 = [16 x i8] c"Can't open file\00"
define i32 @main() {
%1 = alloca [5 x i8]
%2 = alloca [1738 x i8]
%3 = getelementptr inbounds [1738 x i8], [1738 x i8]* %2, i64 0, i64 0
%4 = getelementptr inbounds [5 x i8], [5 x i8]* %1, i64 0, i64 0
%5 = getelementptr inbounds [1738 x i8], [1738 x i8]* %2, i64 0, i64 0
%6 = load i32, i32* @O_RDONLY
%7 = tail call i32 @open(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %0
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.13, i64 0, i64 0))
%11 = tail call i32 @exit(i32 -1)
unreachable
12: ; preds = %0
%13 = call i32 @read(i32 %7, i8* %5, i32 1738)
%14 = icmp eq i32 %13, 1738
br i1 %14, label %18, label %15
15: ; preds = %12
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str.12, i64 0, i64 0))
%17 = tail call i32 @exit(i32 -1)
unreachable
18: ; preds = %12
%19 = tail call i64 @close(i32 %7)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %18
%22 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str.11, i64 0, i64 0))
br label %23
23: ; preds = %21, %18
%24 = call i32 @ftok(i8* %4, i32 0)
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %23
%27 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.10, i64 0, i64 0))
%28 = call i32 @exit(i32 -1)
unreachable
29: ; preds = %23
%30 = load i32, i32* @IPC_CREAT
%31 = load i32, i32* @IPC_EXCL
%32 = or i32 %30, %31
%33 = or i32 %32, 438
%34 = call i32 @shmget(i32 %24, i32 1738, i32 %33)
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %29
%37 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.9, i64 0, i64 0))
%38 = call i32 @exit(i32 -1)
unreachable
39: ; preds = %29
%40 = call i64 @shmat(i32 %34, i32* null, i32 0)
%41 = inttoptr i64 %40 to i8*
%42 = icmp eq i8* %41, inttoptr (i64 -1 to i8*)
br i1 %42, label %46, label %43
43: ; preds = %39
%44 = call i64 @shmdt(i8* %41)
%45 = icmp slt i64 %44, 0
br i1 %45, label %49, label %52
46: ; preds = %39
%47 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.8, i64 0, i64 0))
%48 = call i32 @exit(i32 -1)
unreachable
49: ; preds = %43
%50 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str, i64 0, i64 0))
%51 = call i32 @exit(i32 -1)
unreachable
52: ; preds = %43
ret i32 0
}