factorial(int a) {
  int c, b;
  c = 1;
  if (a > 12 || a < 1)
    c = 0;
  else
    for (b = 1; b <= a; b++)
      c = c * b;
  return c;
}


x86 assembly (clang, -O0):
<factorial>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x4(%rbp)
movl   $0x1,-0x8(%rbp)
cmpl   $0xc,-0x4(%rbp)
jg     1122 <factorial+0x22>
cmpl   $0x1,-0x4(%rbp)
jge    112e <factorial+0x2e>
movl   $0x0,-0x8(%rbp)
jmp    115e <factorial+0x5e>
movl   $0x1,-0xc(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x4(%rbp),%eax
jg     1159 <factorial+0x59>
mov    -0x8(%rbp),%eax
imul   -0xc(%rbp),%eax
mov    %eax,-0x8(%rbp)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1135 <factorial+0x35>
jmp    115e <factorial+0x5e>
mov    -0x8(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<factorial>:
lea    -0xd(%rdi),%ecx
xor    %eax,%eax
cmp    $0xfffffff4,%ecx
jb     11b2 <factorial+0xb2>
lea    0x3(%rdi),%eax
and    $0xfffffffc,%eax
add    $0xffffffff,%edi
movd   %edi,%xmm0
pshufd $0x0,%xmm0,%xmm0
cmp    $0x4,%eax
je     1141 <factorial+0x41>
cmp    $0x8,%eax
jne    1155 <factorial+0x55>
movdqa 0xeee(%rip),%xmm1
movdqa 0xec6(%rip),%xmm2
mov    $0x4,%eax
jmp    116a <factorial+0x6a>
movdqa 0xeb7(%rip),%xmm1
movdqa 0xebf(%rip),%xmm2
xor    %eax,%eax
jmp    116a <factorial+0x6a>
movdqa 0xed3(%rip),%xmm1
movdqa 0xebb(%rip),%xmm2
mov    $0x8,%eax
movd   %eax,%xmm3
pshufd $0x0,%xmm3,%xmm3
pxor   0xec5(%rip),%xmm0
por    0xecd(%rip),%xmm3
pcmpgtd %xmm0,%xmm3
pand   %xmm3,%xmm2
pandn  %xmm1,%xmm3
por    %xmm2,%xmm3
pshufd $0xee,%xmm3,%xmm0
pshufd $0xff,%xmm3,%xmm1
pshufd $0xf5,%xmm3,%xmm2
pmuludq %xmm1,%xmm2
pmuludq %xmm3,%xmm0
pmuludq %xmm2,%xmm0
movd   %xmm0,%eax
ret

LLVM IR (-O0):
define i32 @factorial(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %2
store i32 1, i32* %3
%5 = load i32, i32* %2
%6 = icmp sgt i32 %5, 12
br i1 %6, label %10, label %7
7: ; preds = %1
%8 = load i32, i32* %2
%9 = icmp slt i32 %8, 1
br i1 %9, label %10, label %11
10: ; preds = %7, %1
store i32 0, i32* %3
br label %24
11: ; preds = %7
store i32 1, i32* %4
br label %12
12: ; preds = %20, %11
%13 = load i32, i32* %4
%14 = load i32, i32* %2
%15 = icmp sle i32 %13, %14
br i1 %15, label %16, label %23
16: ; preds = %12
%17 = load i32, i32* %3
%18 = load i32, i32* %4
%19 = mul nsw i32 %17, %18
store i32 %19, i32* %3
br label %20
20: ; preds = %16
%21 = load i32, i32* %4
%22 = add nsw i32 %21, 1
store i32 %22, i32* %4
br label %12
23: ; preds = %12
br label %24
24: ; preds = %23, %10
%25 = load i32, i32* %3
ret i32 %25
}

LLVM IR (-O2):
define i32 @factorial(i32 %0) {
%2 = add i32 %0, -13
%3 = icmp ult i32 %2, -12
br i1 %3, label %22, label %4
4: ; preds = %1
%5 = add i32 %0, 3
%6 = and i32 %5, -4
%7 = add i32 %0, -1
%8 = insertelement <4 x i32> poison, i32 %7, i64 0
%9 = shufflevector <4 x i32> %8, <4 x i32> poison, <4 x i32> zeroinitializer
switch i32 %6, label %10 [
i32 4, label %12
i32 8, label %11
]
10: ; preds = %4
br label %12
11: ; preds = %4
br label %12
12: ; preds = %4, %11, %10
%13 = phi i32 [ 0, %4 ], [ 8, %10 ], [ 4, %11 ]
%14 = phi <4 x i32> [ <i32 1, i32 1, i32 1, i32 1>, %4 ], [ <i32 5, i32 12, i32 21, i32 32>, %10 ], [ <i32 1, i32 2, i32 3, i32 4>, %11 ]
%15 = phi <4 x i32> [ <i32 1, i32 2, i32 3, i32 4>, %4 ], [ <i32 45, i32 120, i32 231, i32 384>, %10 ], [ <i32 5, i32 12, i32 21, i32 32>, %11 ]
%16 = insertelement <4 x i32> poison, i32 %13, i64 0
%17 = shufflevector <4 x i32> %16, <4 x i32> poison, <4 x i32> zeroinitializer
%18 = or <4 x i32> %17, <i32 0, i32 1, i32 2, i32 3>
%19 = icmp ugt <4 x i32> %18, %9
%20 = select <4 x i1> %19, <4 x i32> %14, <4 x i32> %15
%21 = call i32 @llvm.vector.reduce.mul.v4i32(<4 x i32> %20)
br label %22
22: ; preds = %12, %1
%23 = phi i32 [ 0, %1 ], [ %21, %12 ]
ret i32 %23
}