#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 pid_t;

/* Variables and functions */
int EXIT_FAILURE;
int EXIT_SUCCESS;
int IPC_CREAT;
int /*<<< orphan*/ IPC_PRIVATE;
int /*<<< orphan*/ IPC_RMID;
int atoi(char *);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
double *shmat(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ shmctl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int shmget(int /*<<< orphan*/, int, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ wait(int /*<<< orphan*/);
int main(int argc, char **argv) {
  int n, i;
  pid_t pid;
  double d;
  double *shared;
  int shmid, iproc;
  if (argc != 2)
    n = 10;
  else
    n = atoi(argv[1]);
  d = 0.0;
  shmid = shmget(IPC_PRIVATE, sizeof(double), (IPC_CREAT | 0600));
  shared = shmat(shmid, 0, 0);
  shmctl(shmid, IPC_RMID, 0);
  iproc = 0;
  if ((pid = fork()) == -1) {
    fprintf(stderr, "The fork failed!\n");
    exit(EXIT_FAILURE);
  } else {
    if (pid != 0)
      iproc = 1;
  }
  for (i = iproc + 1; i <= n; i += 2)
    d += (double)i;
  if (pid == 0) {
    *shared = d;
    exit(EXIT_SUCCESS);
  } else {
    wait(0);
    d = d + *shared;
    fprintf(stdout, "result = %f\n", d);
  }
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11ac <main+0x2c>
movl   $0xa,-0x14(%rbp)
jmp    11bc <main+0x3c>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1070 <atoi@plt>
mov    %eax,-0x14(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x28(%rbp)
mov    0x2e1d(%rip),%rax
mov    (%rax),%edi
mov    0x2ddc(%rip),%rax
mov    (%rax),%edx
or     $0x180,%edx
mov    $0x8,%esi
call   1060 <shmget@plt>
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%edi
xor    %edx,%edx
mov    %edx,%esi
call   1050 <shmat@plt>
mov    %rax,-0x30(%rbp)
mov    -0x34(%rbp),%edi
mov    0x2dc5(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <shmctl@plt>
movl   $0x0,-0x38(%rbp)
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
jne    124b <main+0xcb>
mov    0x2dab(%rip),%rax
mov    (%rax),%edi
lea    0xdca(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    0x2d8c(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
cmpl   $0x0,-0x1c(%rbp)
je     125c <main+0xdc>
movl   $0x1,-0x38(%rbp)
jmp    1261 <main+0xe1>
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jg     1293 <main+0x113>
cvtsi2sdl -0x18(%rbp),%xmm0
addsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x28(%rbp)
mov    -0x18(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x18(%rbp)
jmp    126a <main+0xea>
cmpl   $0x0,-0x1c(%rbp)
jne    12b8 <main+0x138>
movsd  -0x28(%rbp),%xmm0
mov    -0x30(%rbp),%rax
movsd  %xmm0,(%rax)
mov    0x2d07(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
xor    %edi,%edi
call   1090 <wait@plt>
movsd  -0x28(%rbp),%xmm0
mov    -0x30(%rbp),%rax
addsd  (%rax),%xmm0
movsd  %xmm0,-0x28(%rbp)
mov    0x2ce8(%rip),%rax
mov    (%rax),%edi
movsd  -0x28(%rbp),%xmm0
lea    0xd2c(%rip),%rsi
mov    $0x1,%al
call   1030 <fprintf@plt>
mov    0x2cc4(%rip),%rax
mov    (%rax),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    $0xa,%ebx
cmp    $0x2,%edi
jne    119d <main+0x1d>
mov    0x8(%rsi),%rdi
call   1070 <atoi@plt>
mov    %eax,%ebx
mov    0x2e44(%rip),%rax
mov    (%rax),%edi
mov    0x2e03(%rip),%rax
mov    $0x180,%edx
or     (%rax),%edx
mov    $0x8,%esi
call   1060 <shmget@plt>
mov    %eax,%ebp
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1050 <shmat@plt>
mov    %rax,%r14
mov    0x2df3(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
xor    %edx,%edx
call   1040 <shmctl@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
mov    $0x1,%ecx
test   %eax,%eax
je     11fe <main+0x7e>
cmp    $0xffffffff,%eax
je     1280 <main+0x100>
mov    $0x2,%ecx
xorpd  %xmm1,%xmm1
cmp    %ebx,%ecx
jg     1222 <main+0xa2>
xorpd  %xmm1,%xmm1
nopw   0x0(%rax,%rax,1)
xorps  %xmm0,%xmm0
cvtsi2sd %ecx,%xmm0
addsd  %xmm0,%xmm1
add    $0x2,%ecx
cmp    %ebx,%ecx
jle    1210 <main+0x90>
movsd  %xmm1,0x8(%rsp)
test   %eax,%eax
je     1267 <main+0xe7>
xor    %edi,%edi
call   1090 <wait@plt>
movsd  0x8(%rsp),%xmm0
addsd  (%r14),%xmm0
mov    0x2d7b(%rip),%rax
mov    (%rax),%edi
lea    0xdc4(%rip),%rsi
mov    $0x1,%al
call   1030 <fprintf@plt>
mov    0x2d5c(%rip),%rax
mov    (%rax),%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
movsd  0x8(%rsp),%xmm0
movsd  %xmm0,(%r14)
mov    0x2d3f(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    0x2d51(%rip),%rax
mov    (%rax),%edi
lea    0xd70(%rip),%rsi
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    0x2d32(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@IPC_PRIVATE = global i32 0
@IPC_CREAT = global i32 0
@IPC_RMID = global i32 0
@stderr = global i32 0
@.str = [18 x i8] c"The fork failed!\0A\00"
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
@stdout = global i32 0
@.str.1 = [13 x i8] c"result = %f\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 double
%10 = alloca double*
%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, 2
br i1 %14, label %15, label %16
15: ; preds = %2
store i32 10, i32* %6
br label %21
16: ; preds = %2
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
%20 = call i32 @atoi(i8* %19)
store i32 %20, i32* %6
br label %21
21: ; preds = %16, %15
store double 0.000000e+00, double* %9
%22 = load i32, i32* @IPC_PRIVATE
%23 = load i32, i32* @IPC_CREAT
%24 = or i32 %23, 384
%25 = call i32 @shmget(i32 %22, i32 8, i32 %24)
store i32 %25, i32* %11
%26 = load i32, i32* %11
%27 = call double* @shmat(i32 %26, i32 0, i32 0)
store double* %27, double** %10
%28 = load i32, i32* %11
%29 = load i32, i32* @IPC_RMID
%30 = call i32 @shmctl(i32 %28, i32 %29, i32 0)
store i32 0, i32* %12
%31 = call i32 (...) @fork()
store i32 %31, i32* %8
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %38
33: ; preds = %21
%34 = load i32, i32* @stderr
%35 = call i32 (i32, i8*, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%36 = load i32, i32* @EXIT_FAILURE
%37 = call i32 @exit(i32 %36)
unreachable
38: ; preds = %21
%39 = load i32, i32* %8
%40 = icmp ne i32 %39, 0
br i1 %40, label %41, label %42
41: ; preds = %38
store i32 1, i32* %12
br label %42
42: ; preds = %41, %38
br label %43
43: ; preds = %42
%44 = load i32, i32* %12
%45 = add nsw i32 %44, 1
store i32 %45, i32* %7
br label %46
46: ; preds = %55, %43
%47 = load i32, i32* %7
%48 = load i32, i32* %6
%49 = icmp sle i32 %47, %48
br i1 %49, label %50, label %58
50: ; preds = %46
%51 = load i32, i32* %7
%52 = sitofp i32 %51 to double
%53 = load double, double* %9
%54 = fadd double %53, %52
store double %54, double* %9
br label %55
55: ; preds = %50
%56 = load i32, i32* %7
%57 = add nsw i32 %56, 2
store i32 %57, i32* %7
br label %46
58: ; preds = %46
%59 = load i32, i32* %8
%60 = icmp eq i32 %59, 0
br i1 %60, label %61, label %66
61: ; preds = %58
%62 = load double, double* %9
%63 = load double*, double** %10
store double %62, double* %63
%64 = load i32, i32* @EXIT_SUCCESS
%65 = call i32 @exit(i32 %64)
unreachable
66: ; preds = %58
%67 = call i32 @wait(i32 0)
%68 = load double, double* %9
%69 = load double*, double** %10
%70 = load double, double* %69
%71 = fadd double %68, %70
store double %71, double* %9
%72 = load i32, i32* @stdout
%73 = load double, double* %9
%74 = call i32 (i32, i8*, ...) @fprintf(i32 %72, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), double %73)
br label %75
75: ; preds = %66
%76 = load i32, i32* @EXIT_SUCCESS
ret i32 %76
}

LLVM IR (-O2):
@IPC_PRIVATE = global i32 0
@IPC_CREAT = global i32 0
@IPC_RMID = global i32 0
@stderr = global i32 0
@.str = [18 x i8] c"The fork failed!\0A\00"
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
@stdout = global i32 0
@.str.1 = [13 x i8] c"result = %f\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 2
br i1 %3, label %4, label %8
4: ; preds = %2
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
%7 = tail call i32 @atoi(i8* %6)
br label %8
8: ; preds = %2, %4
%9 = phi i32 [ %7, %4 ], [ 10, %2 ]
%10 = load i32, i32* @IPC_PRIVATE
%11 = load i32, i32* @IPC_CREAT
%12 = or i32 %11, 384
%13 = tail call i32 @shmget(i32 %10, i32 8, i32 %12)
%14 = tail call double* @shmat(i32 %13, i32 0, i32 0)
%15 = load i32, i32* @IPC_RMID
%16 = tail call i32 @shmctl(i32 %13, i32 %15, i32 0)
%17 = tail call i32 (...) @fork()
switch i32 %17, label %23 [
i32 -1, label %18
i32 0, label %24
]
18: ; preds = %8
%19 = load i32, i32* @stderr
%20 = tail call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%21 = load i32, i32* @EXIT_FAILURE
%22 = tail call i32 @exit(i32 %21)
unreachable
23: ; preds = %8
br label %24
24: ; preds = %8, %23
%25 = phi i32 [ 2, %23 ], [ 1, %8 ]
%26 = icmp sgt i32 %25, %9
br i1 %26, label %34, label %27
27: ; preds = %24, %27
%28 = phi i32 [ %32, %27 ], [ %25, %24 ]
%29 = phi double [ %31, %27 ], [ 0.000000e+00, %24 ]
%30 = sitofp i32 %28 to double
%31 = fadd double %29, %30
%32 = add nuw nsw i32 %28, 2
%33 = icmp sgt i32 %32, %9
br i1 %33, label %34, label %27
34: ; preds = %27, %24
%35 = phi double [ 0.000000e+00, %24 ], [ %31, %27 ]
%36 = icmp eq i32 %17, 0
br i1 %36, label %37, label %40
37: ; preds = %34
store double %35, double* %14
%38 = load i32, i32* @EXIT_SUCCESS
%39 = tail call i32 @exit(i32 %38)
unreachable
40: ; preds = %34
%41 = tail call i32 @wait(i32 0)
%42 = load double, double* %14
%43 = fadd double %35, %42
%44 = load i32, i32* @stdout
%45 = tail call i32 (i32, i8*, ...) @fprintf(i32 %44, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), double %43)
%46 = load i32, i32* @EXIT_SUCCESS
ret i32 %46
}