#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 factorial(int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ scanf(char *, long long *);
int main() {
  long long int n, sum = 0;
  printf("Enter a Natural number\n");
  scanf("%lli", &n);
  int sign = 1;
  for (int i = 2; i <= n; i++) {
    sum = sum + (sign * factorial(n) / factorial(i));
    sign = -sign;
  }
  printf("Number of derangements possible for %lli are %lli\n", n, sum);
  return (0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
movq   $0x0,-0x18(%rbp)
lea    0xeb2(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xebc(%rip),%rdi
lea    -0x10(%rbp),%rsi
call   1050 <scanf@plt>
movl   $0x1,-0x1c(%rbp)
movl   $0x2,-0x20(%rbp)
movslq -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
jg     11db <main+0xab>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    %eax,%edi
call   1030 <factorial@plt>
mov    %eax,%ecx
mov    -0x30(%rbp),%eax
imul   %ecx,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x20(%rbp),%edi
call   1030 <factorial@plt>
mov    %eax,%ecx
mov    -0x2c(%rbp),%eax
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
xor    %eax,%eax
sub    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1173 <main+0x43>
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    0xe33(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
lea    0xeea(%rip),%rdi
call   1030 <puts@plt>
lea    0xea6(%rip),%rdi
mov    %rsp,%rsi
call   1060 <scanf@plt>
mov    (%rsp),%rsi
cmp    $0x2,%rsi
jge    1171 <main+0x31>
xor    %r14d,%r14d
jmp    11b0 <main+0x70>
mov    $0x1,%r15d
mov    $0x2,%ebx
xor    %r14d,%r14d
nop
mov    %esi,%edi
call   1040 <factorial@plt>
mov    %eax,%ebp
imul   %r15d,%ebp
mov    %ebx,%edi
call   1040 <factorial@plt>
mov    %eax,%ecx
mov    %ebp,%eax
cltd
idiv   %ecx
cltq
add    %rax,%r14
neg    %r15d
add    $0x1,%rbx
mov    (%rsp),%rsi
cmp    %rbx,%rsi
jge    1180 <main+0x40>
lea    0xe4e(%rip),%rdi
mov    %r14,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [24 x i8] c"Enter a Natural number\0A\00"
@.str.1 = [5 x i8] c"%lli\00"
@.str.2 = [51 x i8] c"Number of derangements possible for %lli are %lli\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i64
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* %1
store i64 0, i64* %3
%6 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%7 = call i32 @scanf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i64* %2)
store i32 1, i32* %4
store i32 2, i32* %5
br label %8
8: ; preds = %27, %0
%9 = load i32, i32* %5
%10 = sext i32 %9 to i64
%11 = load i64, i64* %2
%12 = icmp sle i64 %10, %11
br i1 %12, label %13, label %30
13: ; preds = %8
%14 = load i64, i64* %3
%15 = load i32, i32* %4
%16 = load i64, i64* %2
%17 = trunc i64 %16 to i32
%18 = call i32 @factorial(i32 %17)
%19 = mul nsw i32 %15, %18
%20 = load i32, i32* %5
%21 = call i32 @factorial(i32 %20)
%22 = sdiv i32 %19, %21
%23 = sext i32 %22 to i64
%24 = add nsw i64 %14, %23
store i64 %24, i64* %3
%25 = load i32, i32* %4
%26 = sub nsw i32 0, %25
store i32 %26, i32* %4
br label %27
27: ; preds = %13
%28 = load i32, i32* %5
%29 = add nsw i32 %28, 1
store i32 %29, i32* %5
br label %8
30: ; preds = %8
%31 = load i64, i64* %2
%32 = load i64, i64* %3
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.2, i64 0, i64 0), i64 %31, i64 %32)
ret i32 0
}

LLVM IR (-O2):
@.str.1 = [5 x i8] c"%lli\00"
@.str.2 = [51 x i8] c"Number of derangements possible for %lli are %lli\0A\00"
@str = [23 x i8] c"Enter a Natural number\00"
define i32 @main() {
%1 = alloca i64
%2 = bitcast i64* %1 to i8*
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str, i64 0, i64 0))
%4 = call i32 @scanf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i64* %1)
%5 = load i64, i64* %1
%6 = icmp slt i64 %5, 2
br i1 %6, label %7, label %11
7: ; preds = %11, %0
%8 = phi i64 [ 0, %0 ], [ %23, %11 ]
%9 = phi i64 [ %5, %0 ], [ %26, %11 ]
%10 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([51 x i8], [51 x i8]* @.str.2, i64 0, i64 0), i64 %9, i64 %8)
ret i32 0
11: ; preds = %0, %11
%12 = phi i64 [ %25, %11 ], [ 2, %0 ]
%13 = phi i64 [ %26, %11 ], [ %5, %0 ]
%14 = phi i32 [ %24, %11 ], [ 1, %0 ]
%15 = phi i64 [ %23, %11 ], [ 0, %0 ]
%16 = trunc i64 %13 to i32
%17 = call i32 @factorial(i32 %16)
%18 = mul nsw i32 %17, %14
%19 = trunc i64 %12 to i32
%20 = call i32 @factorial(i32 %19)
%21 = sdiv i32 %18, %20
%22 = sext i32 %21 to i64
%23 = add nsw i64 %15, %22
%24 = sub nsw i32 0, %14
%25 = add nuw i64 %12, 1
%26 = load i64, i64* %1
%27 = icmp slt i64 %26, %25
br i1 %27, label %7, label %11
}