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

/* Variables and functions */
scalar_t__ factorial(int);
uint64_t alternating_factorial(int n) {
  uint64_t result = 0;
  int i;
  for (i = 1; i <= n; i++) {
    if ((n - i) % 2 == 0) {
      result += factorial(i);
    } else {
      result -= factorial(i);
    }
  }
  return result;
}


x86 assembly (clang, -O0):
<alternating_factorial>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
movl   $0x1,-0xc(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x4(%rbp),%eax
jg     118e <alternating_factorial+0x7e>
mov    -0x4(%rbp),%eax
sub    -0xc(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    1166 <alternating_factorial+0x56>
mov    -0xc(%rbp),%edi
call   1030 <factorial@plt>
mov    %rax,%rcx
movslq -0x8(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x8(%rbp)
jmp    117b <alternating_factorial+0x6b>
mov    -0xc(%rbp),%edi
call   1030 <factorial@plt>
mov    %rax,%rcx
movslq -0x8(%rbp),%rax
sub    %rcx,%rax
mov    %eax,-0x8(%rbp)
jmp    1180 <alternating_factorial+0x70>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1129 <alternating_factorial+0x19>
mov    -0x8(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<alternating_factorial>:
push   %rbp
push   %r14
push   %rbx
test   %edi,%edi
jle    114e <alternating_factorial+0x3e>
mov    %edi,%ebx
add    $0xffffffff,%ebx
xor    %r14d,%r14d
mov    $0x1,%ebp
cs nopw 0x0(%rax,%rax,1)
nop
mov    %ebp,%edi
call   1030 <factorial@plt>
mov    %eax,%ecx
neg    %ecx
test   $0x1,%bl
cmove  %eax,%ecx
add    %ecx,%r14d
add    $0x1,%ebp
add    $0xffffffff,%ebx
jb     1130 <alternating_factorial+0x20>
jmp    1151 <alternating_factorial+0x41>
xor    %r14d,%r14d
mov    %r14d,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @alternating_factorial(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %2
store i32 0, i32* %3
store i32 1, i32* %4
br label %5
5: ; preds = %30, %1
%6 = load i32, i32* %4
%7 = load i32, i32* %2
%8 = icmp sle i32 %6, %7
br i1 %8, label %9, label %33
9: ; preds = %5
%10 = load i32, i32* %2
%11 = load i32, i32* %4
%12 = sub nsw i32 %10, %11
%13 = srem i32 %12, 2
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %22
15: ; preds = %9
%16 = load i32, i32* %4
%17 = call i64 @factorial(i32 %16)
%18 = load i32, i32* %3
%19 = sext i32 %18 to i64
%20 = add nsw i64 %19, %17
%21 = trunc i64 %20 to i32
store i32 %21, i32* %3
br label %29
22: ; preds = %9
%23 = load i32, i32* %4
%24 = call i64 @factorial(i32 %23)
%25 = load i32, i32* %3
%26 = sext i32 %25 to i64
%27 = sub nsw i64 %26, %24
%28 = trunc i64 %27 to i32
store i32 %28, i32* %3
br label %29
29: ; preds = %22, %15
br label %30
30: ; preds = %29
%31 = load i32, i32* %4
%32 = add nsw i32 %31, 1
store i32 %32, i32* %4
br label %5
33: ; preds = %5
%34 = load i32, i32* %3
ret i32 %34
}

LLVM IR (-O2):
define i32 @alternating_factorial(i32 %0) {
%2 = icmp slt i32 %0, 1
br i1 %2, label %16, label %3
3: ; preds = %1, %3
%4 = phi i32 [ %14, %3 ], [ 1, %1 ]
%5 = phi i32 [ %13, %3 ], [ 0, %1 ]
%6 = sub nsw i32 %0, %4
%7 = and i32 %6, 1
%8 = icmp eq i32 %7, 0
%9 = tail call i64 @factorial(i32 %4)
%10 = trunc i64 %9 to i32
%11 = sub i32 0, %10
%12 = select i1 %8, i32 %10, i32 %11
%13 = add i32 %5, %12
%14 = add nuw i32 %4, 1
%15 = icmp eq i32 %4, %0
br i1 %15, label %16, label %3
16: ; preds = %3, %1
%17 = phi i32 [ 0, %1 ], [ %13, %3 ]
ret i32 %17
}