#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 */

/* Variables and functions */
int atoi(char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, ...);
int getpid();
int /*<<< orphan*/ isdigit(char);
int /*<<< orphan*/ printf(char *, char *, ...);
int *shmat(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ shmdt(int *);
int /*<<< orphan*/ stderr;
int strlen(char *);
int main(int argc, char **argv) {
  if (argc != 3) {
    printf("Usage: %s <shm> <number>\n", argv[0]);
    return (-1);
  }
  int pid = getpid();
  printf("%s process [%d]: starting.\n", argv[0], pid);
  int str_size = strlen(argv[2]);
  for (int o = 0; o < str_size; ++o) {
    if (!(isdigit(argv[2][o]))) {
      fprintf(stderr, "%s process [%d]: %s is not a valid input.\n", argv[0],
              pid, argv[2]);
      return (2);
    }
  }
  int shm = atoi(argv[1]);
  int *share;
  if ((share = shmat(shm, NULL, 0)) == NULL) {
    fprintf(stderr, "%s: shmat error.\n", argv[0]);
    return (1);
  }
  printf("%s process [%d]: result %d read from shared memort.\n", argv[0], pid,
         *share);
  int one = atoi(argv[2]);
  int result = ((one * (*share)) & 1) ? 1 : 0;
  if (result) {
    printf("%s process [%d]: %d is odd.\n", argv[0], pid, one * (*share));
  } else {
    printf("%s process [%d]: %d is even.\n", argv[0], pid, one * (*share));
  }
  *share = result;
  printf("%s process [%d]: wrote result (%d) to shared memory.\n", argv[0], pid,
         result);
  shmdt(share);
  return (0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     11c1 <main+0x41>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe52(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1394 <main+0x214>
mov    $0x0,%al
call   1050 <getpid@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
mov    -0x14(%rbp),%edx
lea    0xe3e(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1060 <strlen@plt>
mov    %eax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    126c <main+0xec>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
movslq -0x1c(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1030 <isdigit@plt>
cmp    $0x0,%eax
jne    1259 <main+0xd9>
mov    0x2db5(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
mov    -0x14(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%r8
lea    0xdf0(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
movl   $0x2,-0x4(%rbp)
jmp    1394 <main+0x214>
jmp    125e <main+0xde>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11fa <main+0x7a>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10a0 <atoi@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1090 <shmat@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    12c2 <main+0x142>
mov    0x2d41(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xdb2(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1394 <main+0x214>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
mov    -0x14(%rbp),%edx
mov    -0x28(%rbp),%rax
mov    (%rax),%ecx
lea    0xd9a(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10a0 <atoi@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%edx
mov    -0x28(%rbp),%rax
imul   (%rax),%edx
and    $0x1,%edx
xor    %eax,%eax
mov    $0x1,%ecx
cmp    $0x0,%edx
cmovne %ecx,%eax
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
je     133e <main+0x1be>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
mov    -0x14(%rbp),%edx
mov    -0x2c(%rbp),%ecx
mov    -0x28(%rbp),%rax
imul   (%rax),%ecx
lea    0xd76(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    1360 <main+0x1e0>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
mov    -0x14(%rbp),%edx
mov    -0x2c(%rbp),%ecx
mov    -0x28(%rbp),%rax
imul   (%rax),%ecx
lea    0xd6c(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x30(%rbp),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
mov    -0x14(%rbp),%edx
mov    -0x30(%rbp),%ecx
lea    0xd66(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x28(%rbp),%rdi
call   1040 <shmdt@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r13
cmp    $0x3,%edi
jne    1280 <main+0x100>
xor    %eax,%eax
call   1050 <getpid@plt>
mov    %eax,%r14d
mov    0x0(%r13),%rsi
lea    0xe6e(%rip),%rdi
mov    %eax,%edx
xor    %eax,%eax
call   1070 <printf@plt>
mov    0x10(%r13),%rdi
call   1060 <strlen@plt>
test   %eax,%eax
jle    11ee <main+0x6e>
mov    %eax,%ebp
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
mov    0x10(%r13),%rax
movsbl (%rax,%rbx,1),%edi
call   1030 <isdigit@plt>
test   %eax,%eax
je     129a <main+0x11a>
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    11d0 <main+0x50>
mov    0x8(%r13),%rdi
call   10a0 <atoi@plt>
xor    %r12d,%r12d
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1090 <shmat@plt>
test   %rax,%rax
je     12c4 <main+0x144>
mov    %rax,%r15
mov    0x0(%r13),%rsi
mov    (%rax),%ecx
lea    0xe55(%rip),%rdi
mov    %r14d,%edx
xor    %eax,%eax
call   1070 <printf@plt>
mov    0x10(%r13),%rdi
call   10a0 <atoi@plt>
imul   (%r15),%eax
mov    %eax,%ebp
and    $0x1,%ebp
mov    0x0(%r13),%rsi
lea    0xe80(%rip),%rcx
lea    0xe5c(%rip),%rdi
cmove  %rcx,%rdi
mov    %r14d,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1070 <printf@plt>
mov    %ebp,(%r15)
mov    0x0(%r13),%rsi
lea    0xe79(%rip),%rdi
mov    %r14d,%edx
mov    %ebp,%ecx
xor    %eax,%eax
call   1070 <printf@plt>
mov    %r15,%rdi
call   1040 <shmdt@plt>
jmp    12e5 <main+0x165>
mov    0x0(%r13),%rsi
lea    0xd75(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0xffffffff,%r12d
jmp    12e5 <main+0x165>
mov    0x2d3f(%rip),%rax
mov    (%rax),%edi
mov    0x0(%r13),%rdx
mov    0x10(%r13),%r8
lea    0xd84(%rip),%rsi
mov    %r14d,%ecx
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    $0x2,%r12d
jmp    12e5 <main+0x165>
mov    0x2d15(%rip),%rax
mov    (%rax),%edi
mov    0x0(%r13),%rdx
lea    0xd89(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    $0x1,%r12d
mov    %r12d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [26 x i8] c"Usage: %s <shm> <number>\0A\00"
@.str.1 = [28 x i8] c"%s process [%d]: starting.\0A\00"
@stderr = global i32 0
@.str.2 = [43 x i8] c"%s process [%d]: %s is not a valid input.\0A\00"
@.str.3 = [18 x i8] c"%s: shmat error.\0A\00"
@.str.4 = [53 x i8] c"%s process [%d]: result %d read from shared memort.\0A\00"
@.str.5 = [29 x i8] c"%s process [%d]: %d is odd.\0A\00"
@.str.6 = [30 x i8] c"%s process [%d]: %d is even.\0A\00"
@.str.7 = [54 x i8] c"%s process [%d]: wrote result (%d) to shared memory.\0A\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 i32
%9 = alloca i32
%10 = alloca i32*
%11 = alloca i32
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i32, i32* %4
%14 = icmp ne i32 %13, 3
br i1 %14, label %15, label %20
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 0
%18 = load i8*, i8** %17
%19 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %18)
store i32 -1, i32* %3
br label %126
20: ; preds = %2
%21 = call i32 (...) @getpid()
store i32 %21, i32* %6
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 0
%24 = load i8*, i8** %23
%25 = load i32, i32* %6
%26 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* %24, i32 %25)
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 2
%29 = load i8*, i8** %28
%30 = call i32 @strlen(i8* %29)
store i32 %30, i32* %7
store i32 0, i32* %8
br label %31
31: ; preds = %56, %20
%32 = load i32, i32* %8
%33 = load i32, i32* %7
%34 = icmp slt i32 %32, %33
br i1 %34, label %35, label %59
35: ; preds = %31
%36 = load i8**, i8*** %5
%37 = getelementptr inbounds i8*, i8** %36, i64 2
%38 = load i8*, i8** %37
%39 = load i32, i32* %8
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i8, i8* %38, i64 %40
%42 = load i8, i8* %41
%43 = call i32 @isdigit(i8 signext %42)
%44 = icmp ne i32 %43, 0
br i1 %44, label %55, label %45
45: ; preds = %35
%46 = load i32, i32* @stderr
%47 = load i8**, i8*** %5
%48 = getelementptr inbounds i8*, i8** %47, i64 0
%49 = load i8*, i8** %48
%50 = load i32, i32* %6
%51 = load i8**, i8*** %5
%52 = getelementptr inbounds i8*, i8** %51, i64 2
%53 = load i8*, i8** %52
%54 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %46, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0), i8* %49, i32 %50, i8* %53)
store i32 2, i32* %3
br label %126
55: ; preds = %35
br label %56
56: ; preds = %55
%57 = load i32, i32* %8
%58 = add nsw i32 %57, 1
store i32 %58, i32* %8
br label %31
59: ; preds = %31
%60 = load i8**, i8*** %5
%61 = getelementptr inbounds i8*, i8** %60, i64 1
%62 = load i8*, i8** %61
%63 = call i32 @atoi(i8* %62)
store i32 %63, i32* %9
%64 = load i32, i32* %9
%65 = call i32* @shmat(i32 %64, i32* null, i32 0)
store i32* %65, i32** %10
%66 = icmp eq i32* %65, null
br i1 %66, label %67, label %73
67: ; preds = %59
%68 = load i32, i32* @stderr
%69 = load i8**, i8*** %5
%70 = getelementptr inbounds i8*, i8** %69, i64 0
%71 = load i8*, i8** %70
%72 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %68, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i8* %71)
store i32 1, i32* %3
br label %126
73: ; preds = %59
%74 = load i8**, i8*** %5
%75 = getelementptr inbounds i8*, i8** %74, i64 0
%76 = load i8*, i8** %75
%77 = load i32, i32* %6
%78 = load i32*, i32** %10
%79 = load i32, i32* %78
%80 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.4, i64 0, i64 0), i8* %76, i32 %77, i32 %79)
%81 = load i8**, i8*** %5
%82 = getelementptr inbounds i8*, i8** %81, i64 2
%83 = load i8*, i8** %82
%84 = call i32 @atoi(i8* %83)
store i32 %84, i32* %11
%85 = load i32, i32* %11
%86 = load i32*, i32** %10
%87 = load i32, i32* %86
%88 = mul nsw i32 %85, %87
%89 = and i32 %88, 1
%90 = icmp ne i32 %89, 0
%91 = zext i1 %90 to i64
%92 = select i1 %90, i32 1, i32 0
store i32 %92, i32* %12
%93 = load i32, i32* %12
%94 = icmp ne i32 %93, 0
br i1 %94, label %95, label %105
95: ; preds = %73
%96 = load i8**, i8*** %5
%97 = getelementptr inbounds i8*, i8** %96, i64 0
%98 = load i8*, i8** %97
%99 = load i32, i32* %6
%100 = load i32, i32* %11
%101 = load i32*, i32** %10
%102 = load i32, i32* %101
%103 = mul nsw i32 %100, %102
%104 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.5, i64 0, i64 0), i8* %98, i32 %99, i32 %103)
br label %115
105: ; preds = %73
%106 = load i8**, i8*** %5
%107 = getelementptr inbounds i8*, i8** %106, i64 0
%108 = load i8*, i8** %107
%109 = load i32, i32* %6
%110 = load i32, i32* %11
%111 = load i32*, i32** %10
%112 = load i32, i32* %111
%113 = mul nsw i32 %110, %112
%114 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.6, i64 0, i64 0), i8* %108, i32 %109, i32 %113)
br label %115
115: ; preds = %105, %95
%116 = load i32, i32* %12
%117 = load i32*, i32** %10
store i32 %116, i32* %117
%118 = load i8**, i8*** %5
%119 = getelementptr inbounds i8*, i8** %118, i64 0
%120 = load i8*, i8** %119
%121 = load i32, i32* %6
%122 = load i32, i32* %12
%123 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.7, i64 0, i64 0), i8* %120, i32 %121, i32 %122)
%124 = load i32*, i32** %10
%125 = call i32 @shmdt(i32* %124)
store i32 0, i32* %3
br label %126
126: ; preds = %115, %67, %45, %15
%127 = load i32, i32* %3
ret i32 %127
}

LLVM IR (-O2):
@.str = [26 x i8] c"Usage: %s <shm> <number>\0A\00"
@.str.1 = [28 x i8] c"%s process [%d]: starting.\0A\00"
@stderr = global i32 0
@.str.2 = [43 x i8] c"%s process [%d]: %s is not a valid input.\0A\00"
@.str.3 = [18 x i8] c"%s: shmat error.\0A\00"
@.str.4 = [53 x i8] c"%s process [%d]: result %d read from shared memort.\0A\00"
@.str.5 = [29 x i8] c"%s process [%d]: %d is odd.\0A\00"
@.str.6 = [30 x i8] c"%s process [%d]: %d is even.\0A\00"
@.str.7 = [54 x i8] c"%s process [%d]: wrote result (%d) to shared memory.\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 3
br i1 %3, label %7, label %4
4: ; preds = %2
%5 = load i8*, i8** %1
%6 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %5)
br label %60
7: ; preds = %2
%8 = tail call i32 (...) @getpid()
%9 = load i8*, i8** %1
%10 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* %9, i32 %8)
%11 = getelementptr inbounds i8*, i8** %1, i64 2
%12 = load i8*, i8** %11
%13 = tail call i32 @strlen(i8* %12)
%14 = icmp sgt i32 %13, 0
br i1 %14, label %15, label %32
15: ; preds = %7
%16 = zext i32 %13 to i64
br label %19
17: ; preds = %19
%18 = icmp eq i64 %26, %16
br i1 %18, label %32, label %19
19: ; preds = %15, %17
%20 = phi i64 [ 0, %15 ], [ %26, %17 ]
%21 = load i8*, i8** %11
%22 = getelementptr inbounds i8, i8* %21, i64 %20
%23 = load i8, i8* %22
%24 = tail call i32 @isdigit(i8 signext %23)
%25 = icmp eq i32 %24, 0
%26 = add nuw nsw i64 %20, 1
br i1 %25, label %27, label %17
27: ; preds = %19
%28 = load i32, i32* @stderr
%29 = load i8*, i8** %1
%30 = load i8*, i8** %11
%31 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0), i8* %29, i32 %8, i8* %30)
br label %60
32: ; preds = %17, %7
%33 = getelementptr inbounds i8*, i8** %1, i64 1
%34 = load i8*, i8** %33
%35 = tail call i32 @atoi(i8* %34)
%36 = tail call i32* @shmat(i32 %35, i32* null, i32 0)
%37 = icmp eq i32* %36, null
br i1 %37, label %38, label %42
38: ; preds = %32
%39 = load i32, i32* @stderr
%40 = load i8*, i8** %1
%41 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i8* %40)
br label %60
42: ; preds = %32
%43 = load i8*, i8** %1
%44 = load i32, i32* %36
%45 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.4, i64 0, i64 0), i8* %43, i32 %8, i32 %44)
%46 = load i8*, i8** %11
%47 = tail call i32 @atoi(i8* %46)
%48 = load i32, i32* %36
%49 = mul nsw i32 %48, %47
%50 = and i32 %49, 1
%51 = icmp eq i32 %50, 0
%52 = xor i1 %51, true
%53 = zext i1 %52 to i32
%54 = load i8*, i8** %1
%55 = select i1 %51, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.5, i64 0, i64 0)
%56 = tail call i32 (i8*, i8*, ...) @printf(i8* %55, i8* %54, i32 %8, i32 %49)
store i32 %53, i32* %36
%57 = load i8*, i8** %1
%58 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.7, i64 0, i64 0), i8* %57, i32 %8, i32 %53)
%59 = tail call i32 @shmdt(i32* %36)
br label %60
60: ; preds = %27, %42, %38, %4
%61 = phi i32 [ -1, %4 ], [ 1, %38 ], [ 0, %42 ], [ 2, %27 ]
ret i32 %61
}