; LLVM IR from stamparm/cryptospecs :: symmetrical/sources (redoc3)

; Unoptimized IR (clang -disable-llvm-passes):
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

@key_table = external dso_local global [2560 x i8], align 16
@mask_table = external dso_local global [16 x i8], align 16

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: nounwind uwtable
define dso_local zeroext i8 @create_mask_table() #1 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca ptr, align 8
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #2
  store ptr @mask_table, ptr %3, align 8, !tbaa !5
  store i32 0, ptr %2, align 4, !tbaa !9
  store i32 0, ptr %1, align 4, !tbaa !9
  br label %4

4:                                                ; preds = %11, %0
  %5 = load i32, ptr %1, align 4, !tbaa !9
  %6 = icmp slt i32 %5, 16
  br i1 %6, label %7, label %14

7:                                                ; preds = %4
  %8 = load i32, ptr %1, align 4, !tbaa !9
  %9 = sext i32 %8 to i64
  %10 = getelementptr inbounds [16 x i8], ptr @mask_table, i64 0, i64 %9
  store i8 0, ptr %10, align 1, !tbaa !11
  br label %11

11:                                               ; preds = %7
  %12 = load i32, ptr %1, align 4, !tbaa !9
  %13 = add nsw i32 %12, 1
  store i32 %13, ptr %1, align 4, !tbaa !9
  br label %4, !llvm.loop !12

14:                                               ; preds = %4
  store i32 0, ptr %1, align 4, !tbaa !9
  br label %15

15:                                               ; preds = %37, %14
  %16 = load i32, ptr %1, align 4, !tbaa !9
  %17 = icmp slt i32 %16, 2560
  br i1 %17, label %18, label %40

18:                                               ; preds = %15
  %19 = load i32, ptr %1, align 4, !tbaa !9
  %20 = sext i32 %19 to i64
  %21 = getelementptr inbounds [2560 x i8], ptr @key_table, i64 0, i64 %20
  %22 = load i8, ptr %21, align 1, !tbaa !11
  %23 = zext i8 %22 to i32
  %24 = load ptr, ptr %3, align 8, !tbaa !5
  %25 = load i8, ptr %24, align 1, !tbaa !11
  %26 = zext i8 %25 to i32
  %27 = xor i32 %26, %23
  %28 = trunc i32 %27 to i8
  store i8 %28, ptr %24, align 1, !tbaa !11
  %29 = load ptr, ptr %3, align 8, !tbaa !5
  %30 = getelementptr inbounds i8, ptr %29, i32 1
  store ptr %30, ptr %3, align 8, !tbaa !5
  %31 = load i32, ptr %2, align 4, !tbaa !9
  %32 = add nsw i32 %31, 1
  store i32 %32, ptr %2, align 4, !tbaa !9
  %33 = load i32, ptr %2, align 4, !tbaa !9
  %34 = icmp eq i32 %33, 16
  br i1 %34, label %35, label %36

35:                                               ; preds = %18
  store i32 0, ptr %2, align 4, !tbaa !9
  store ptr @mask_table, ptr %3, align 8, !tbaa !5
  br label %36

36:                                               ; preds = %35, %18
  br label %37

37:                                               ; preds = %36
  %38 = load i32, ptr %1, align 4, !tbaa !9
  %39 = add nsw i32 %38, 1
  store i32 %39, ptr %1, align 4, !tbaa !9
  br label %15, !llvm.loop !14

40:                                               ; preds = %15
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #2
  ret i8 0
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nounwind uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #2 = { nounwind }

!llvm.module.flags = !{!0, !1, !2, !3}
!llvm.ident = !{!4}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 8, !"PIC Level", i32 2}
!2 = !{i32 7, !"PIE Level", i32 2}
!3 = !{i32 7, !"uwtable", i32 2}
!4 = !{!"clang version 19.1.0 (git@github.com:llvm/llvm-project.git a4bf6cd7cfb1a1421ba92bca9d017b49936c55e4)"}
!5 = !{!6, !6, i64 0}
!6 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!7, !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}
!14 = distinct !{!14, !13}


; Optimized IR (opt -passes='default<O3>'):
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

@key_table = external dso_local local_unnamed_addr global [2560 x i8], align 16
@mask_table = external dso_local local_unnamed_addr global [16 x i8], align 16

; Function Attrs: nofree norecurse nosync nounwind memory(readwrite, inaccessiblemem: none) uwtable
define dso_local noundef zeroext i8 @create_mask_table() local_unnamed_addr #0 {
.preheader.preheader:
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(16) @mask_table, i8 0, i64 16, i1 false), !tbaa !5
  br label %.preheader

.preheader:                                       ; preds = %.preheader, %.preheader.preheader
  %indvars.iv = phi i64 [ 0, %.preheader.preheader ], [ %indvars.iv.next.1, %.preheader ]
  %.018 = phi ptr [ @mask_table, %.preheader.preheader ], [ %spec.select14.1, %.preheader ]
  %.0917 = phi i32 [ 0, %.preheader.preheader ], [ %spec.select.1, %.preheader ]
  %0 = getelementptr inbounds [2560 x i8], ptr @key_table, i64 0, i64 %indvars.iv
  %1 = load i8, ptr %0, align 2, !tbaa !5
  %2 = load i8, ptr %.018, align 1, !tbaa !5
  %3 = xor i8 %2, %1
  store i8 %3, ptr %.018, align 1, !tbaa !5
  %4 = getelementptr inbounds i8, ptr %.018, i64 1
  %5 = icmp eq i32 %.0917, 15
  %6 = add nsw i32 %.0917, 2
  %spec.select14 = select i1 %5, ptr @mask_table, ptr %4
  %indvars.iv.next = or disjoint i64 %indvars.iv, 1
  %7 = getelementptr inbounds [2560 x i8], ptr @key_table, i64 0, i64 %indvars.iv.next
  %8 = load i8, ptr %7, align 1, !tbaa !5
  %9 = load i8, ptr %spec.select14, align 1, !tbaa !5
  %10 = xor i8 %9, %8
  store i8 %10, ptr %spec.select14, align 1, !tbaa !5
  %11 = getelementptr inbounds i8, ptr %spec.select14, i64 1
  %12 = select i1 %5, i32 1, i32 %6
  %13 = icmp eq i32 %12, 16
  %spec.select.1 = select i1 %13, i32 0, i32 %12
  %spec.select14.1 = select i1 %13, ptr @mask_table, ptr %11
  %indvars.iv.next.1 = add nuw nsw i64 %indvars.iv, 2
  %exitcond.not.1 = icmp eq i64 %indvars.iv.next.1, 2560
  br i1 %exitcond.not.1, label %14, label %.preheader, !llvm.loop !8

14:                                               ; preds = %.preheader
  ret i8 0
}

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #1

attributes #0 = { nofree norecurse nosync nounwind memory(readwrite, inaccessiblemem: none) uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #1 = { nocallback nofree nounwind willreturn memory(argmem: write) }

!llvm.module.flags = !{!0, !1, !2, !3}
!llvm.ident = !{!4}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 8, !"PIC Level", i32 2}
!2 = !{i32 7, !"PIE Level", i32 2}
!3 = !{i32 7, !"uwtable", i32 2}
!4 = !{!"clang version 19.1.0 (git@github.com:llvm/llvm-project.git a4bf6cd7cfb1a1421ba92bca9d017b49936c55e4)"}
!5 = !{!6, !6, i64 0}
!6 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = distinct !{!8, !9}
!9 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,LoopIdiomRecognizePass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass