#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;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, char *, int);
int /*<<< orphan*/ perror(char *);
char *shmat(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int shmdt(char *);
int shmget(int, int, int);
int main() {
  int shmid;
  int count, i;
  key_t key;
  char *shmem, *d;
  FILE *f;
  f = fopen("secrets.out", "rw+");
  key = 4186;
  if ((shmid = shmget(key, 2046, 0666)) < 0) {
    perror("There is an error");
    exit(1);
  }
  if ((shmem = shmat(shmid, NULL, 0)) == (char *)-1) {
    perror("There is an error in accessing the shared memory ");
    exit(1);
  }
  while (1) {
    if (*shmem != '\0') {
      count = 0;
      for (d = shmem; *d != '\0'; d++) {
        if (*d == '0' || *d == '1' || *d == '2' || *d == '3' || *d == '4' ||
            *d == '5' || *d == '6' || *d == '7' || *d == '8' || *d == '9') {
          count++;
        }
      }
      f = fopen("secrets.out", "a");
      fprintf(f, "%s - %d", shmem, count);
      fclose(f);
      *shmem = '\0';
    }
  }
  if (shmdt(shmem) == -1) {
    perror("Error in detaching the segment");
    exit(1);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe7a(%rip),%rdi
lea    0xe7f(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x30(%rbp)
movl   $0x105a,-0x14(%rbp)
mov    -0x14(%rbp),%edi
mov    $0x7fe,%esi
mov    $0x1b6,%edx
call   1080 <shmget@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    11d1 <main+0x61>
lea    0xe4e(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x8(%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    1209 <main+0x99>
lea    0xe28(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jmp    120e <main+0x9e>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     133a <main+0x1ca>
movl   $0x0,-0xc(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     12fc <main+0x18c>
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x30,%eax
je     12dd <main+0x16d>
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x31,%eax
je     12dd <main+0x16d>
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x32,%eax
je     12dd <main+0x16d>
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x33,%eax
je     12dd <main+0x16d>
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x34,%eax
je     12dd <main+0x16d>
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x35,%eax
je     12dd <main+0x16d>
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x36,%eax
je     12dd <main+0x16d>
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x37,%eax
je     12dd <main+0x16d>
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x38,%eax
je     12dd <main+0x16d>
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x39,%eax
jne    12e6 <main+0x176>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    12eb <main+0x17b>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    122d <main+0xbd>
lea    0xcfd(%rip),%rdi
lea    0xd4a(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0xc(%rbp),%ecx
lea    0xd31(%rip),%rsi
call   1040 <fprintf@plt>
mov    -0x30(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
jmp    120e <main+0x9e>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
lea    0xe7e(%rip),%rdi
lea    0xe83(%rip),%rsi
call   1050 <fopen@plt>
mov    $0x105a,%edi
mov    $0x7fe,%esi
mov    $0x1b6,%edx
call   1080 <shmget@plt>
test   %eax,%eax
js     1239 <main+0xc9>
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1070 <shmat@plt>
cmp    $0xffffffffffffffff,%rax
je     1242 <main+0xd2>
mov    %rax,%r13
mov    (%rax),%bpl
mov    %rax,%r14
add    $0x1,%r14
lea    0xe81(%rip),%r15
lea    0xe7c(%rip),%r12
nopw   0x0(%rax,%rax,1)
mov    %ebp,%ecx
xor    %ebp,%ebp
test   %cl,%cl
je     11e0 <main+0x70>
mov    %r14,%rax
jmp    11fa <main+0x8a>
nopl   (%rax)
add    $0x1,%ebp
movzbl (%rax),%ecx
add    $0x1,%rax
lea    -0x30(%rcx),%edx
cmp    $0xa,%dl
jb     11f0 <main+0x80>
test   %cl,%cl
jne    11f3 <main+0x83>
lea    0xdf3(%rip),%rdi
mov    %r15,%rsi
call   1050 <fopen@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r12,%rsi
mov    %r13,%rdx
mov    %ebp,%ecx
call   1040 <fprintf@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
movb   $0x0,0x0(%r13)
xor    %ebp,%ebp
jmp    11e0 <main+0x70>
lea    0xdd0(%rip),%rdi
jmp    1249 <main+0xd9>
lea    0xdd9(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [12 x i8] c"secrets.out\00"
@.str.1 = [4 x i8] c"rw+\00"
@.str.2 = [18 x i8] c"There is an error\00"
@.str.3 = [50 x i8] c"There is an error in accessing the shared memory \00"
@.str.4 = [2 x i8] c"a\00"
@.str.5 = [8 x i8] c"%s - %d\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32*
store i32 0, i32* %1
%9 = call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
store i32* %9, i32** %8
store i32 4186, i32* %5
%10 = load i32, i32* %5
%11 = call i32 @shmget(i32 %10, i32 2046, i32 438)
store i32 %11, i32* %2
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %0
%14 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %0
%17 = load i32, i32* %2
%18 = call i8* @shmat(i32 %17, i32* null, i32 0)
store i8* %18, i8** %6
%19 = icmp eq i8* %18, inttoptr (i64 -1 to i8*)
br i1 %19, label %20, label %23
20: ; preds = %16
%21 = call i32 @perror(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.3, i64 0, i64 0))
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %16
br label %24
24: ; preds = %23, %102
%25 = load i8*, i8** %6
%26 = load i8, i8* %25
%27 = sext i8 %26 to i32
%28 = icmp ne i32 %27, 0
br i1 %28, label %29, label %102
29: ; preds = %24
store i32 0, i32* %3
%30 = load i8*, i8** %6
store i8* %30, i8** %7
br label %31
31: ; preds = %90, %29
%32 = load i8*, i8** %7
%33 = load i8, i8* %32
%34 = sext i8 %33 to i32
%35 = icmp ne i32 %34, 0
br i1 %35, label %36, label %93
36: ; preds = %31
%37 = load i8*, i8** %7
%38 = load i8, i8* %37
%39 = sext i8 %38 to i32
%40 = icmp eq i32 %39, 48
br i1 %40, label %86, label %41
41: ; preds = %36
%42 = load i8*, i8** %7
%43 = load i8, i8* %42
%44 = sext i8 %43 to i32
%45 = icmp eq i32 %44, 49
br i1 %45, label %86, label %46
46: ; preds = %41
%47 = load i8*, i8** %7
%48 = load i8, i8* %47
%49 = sext i8 %48 to i32
%50 = icmp eq i32 %49, 50
br i1 %50, label %86, label %51
51: ; preds = %46
%52 = load i8*, i8** %7
%53 = load i8, i8* %52
%54 = sext i8 %53 to i32
%55 = icmp eq i32 %54, 51
br i1 %55, label %86, label %56
56: ; preds = %51
%57 = load i8*, i8** %7
%58 = load i8, i8* %57
%59 = sext i8 %58 to i32
%60 = icmp eq i32 %59, 52
br i1 %60, label %86, label %61
61: ; preds = %56
%62 = load i8*, i8** %7
%63 = load i8, i8* %62
%64 = sext i8 %63 to i32
%65 = icmp eq i32 %64, 53
br i1 %65, label %86, label %66
66: ; preds = %61
%67 = load i8*, i8** %7
%68 = load i8, i8* %67
%69 = sext i8 %68 to i32
%70 = icmp eq i32 %69, 54
br i1 %70, label %86, label %71
71: ; preds = %66
%72 = load i8*, i8** %7
%73 = load i8, i8* %72
%74 = sext i8 %73 to i32
%75 = icmp eq i32 %74, 55
br i1 %75, label %86, label %76
76: ; preds = %71
%77 = load i8*, i8** %7
%78 = load i8, i8* %77
%79 = sext i8 %78 to i32
%80 = icmp eq i32 %79, 56
br i1 %80, label %86, label %81
81: ; preds = %76
%82 = load i8*, i8** %7
%83 = load i8, i8* %82
%84 = sext i8 %83 to i32
%85 = icmp eq i32 %84, 57
br i1 %85, label %86, label %89
86: ; preds = %81, %76, %71, %66, %61, %56, %51, %46, %41, %36
%87 = load i32, i32* %3
%88 = add nsw i32 %87, 1
store i32 %88, i32* %3
br label %89
89: ; preds = %86, %81
br label %90
90: ; preds = %89
%91 = load i8*, i8** %7
%92 = getelementptr inbounds i8, i8* %91, i32 1
store i8* %92, i8** %7
br label %31
93: ; preds = %31
%94 = call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %94, i32** %8
%95 = load i32*, i32** %8
%96 = load i8*, i8** %6
%97 = load i32, i32* %3
%98 = call i32 @fprintf(i32* %95, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0), i8* %96, i32 %97)
%99 = load i32*, i32** %8
%100 = call i32 @fclose(i32* %99)
%101 = load i8*, i8** %6
store i8 0, i8* %101
br label %102
102: ; preds = %93, %24
br label %24
}

LLVM IR (-O2):
@.str = [12 x i8] c"secrets.out\00"
@.str.1 = [4 x i8] c"rw+\00"
@.str.2 = [18 x i8] c"There is an error\00"
@.str.3 = [50 x i8] c"There is an error in accessing the shared memory \00"
@.str.4 = [2 x i8] c"a\00"
@.str.5 = [8 x i8] c"%s - %d\00"
define i32 @main() {
%1 = tail call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%2 = tail call i32 @shmget(i32 4186, i32 2046, i32 438)
%3 = icmp slt i32 %2, 0
br i1 %3, label %4, label %7
4: ; preds = %0
%5 = tail call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
%6 = tail call i32 @exit(i32 1)
unreachable
7: ; preds = %0
%8 = tail call i8* @shmat(i32 %2, i32* null, i32 0)
%9 = icmp eq i8* %8, inttoptr (i64 -1 to i8*)
br i1 %9, label %12, label %10
10: ; preds = %7
%11 = load i8, i8* %8
br label %15
12: ; preds = %7
%13 = tail call i32 @perror(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.3, i64 0, i64 0))
%14 = tail call i32 @exit(i32 1)
unreachable
15: ; preds = %32, %10
%16 = phi i8 [ %11, %10 ], [ 0, %32 ]
%17 = icmp eq i8 %16, 0
br i1 %17, label %32, label %18
18: ; preds = %15, %24
%19 = phi i8 [ %27, %24 ], [ %16, %15 ]
%20 = phi i32 [ %25, %24 ], [ 0, %15 ]
%21 = phi i8* [ %26, %24 ], [ %8, %15 ]
switch i8 %19, label %24 [
i8 0, label %28
i8 48, label %22
i8 49, label %22
i8 50, label %22
i8 51, label %22
i8 52, label %22
i8 53, label %22
i8 54, label %22
i8 55, label %22
i8 56, label %22
i8 57, label %22
]
22: ; preds = %18, %18, %18, %18, %18, %18, %18, %18, %18, %18
%23 = add nsw i32 %20, 1
br label %24
24: ; preds = %18, %22
%25 = phi i32 [ %23, %22 ], [ %20, %18 ]
%26 = getelementptr inbounds i8, i8* %21, i64 1
%27 = load i8, i8* %26
br label %18
28: ; preds = %18
%29 = tail call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%30 = tail call i32 @fprintf(i32* %29, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0), i8* %8, i32 %20)
%31 = tail call i32 @fclose(i32* %29)
store i8 0, i8* %8
br label %32
32: ; preds = %28, %15
br label %15
}