; 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"

%union.U_INT = type { i32 }

@key_length = external dso_local global i8, align 1
@master_key = external dso_local global <{ i8, i8, i8, i8, i8, i8, [64 x i8] }>, align 16
@__const.initialize_key_table.prime = external hidden unnamed_addr constant [35 x i32], align 16
@key_table = external dso_local global [2560 x i8], align 16

; Function Attrs: nounwind uwtable
define dso_local zeroext i8 @initialize_key_table() #0 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca %union.U_INT, align 4
  %5 = alloca %union.U_INT, align 4
  %6 = alloca [35 x i32], align 16
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #4
  call void @llvm.lifetime.start.p0(i64 140, ptr %6) #4
  call void @llvm.memcpy.p0.p0.i64(ptr align 16 %6, ptr align 16 @__const.initialize_key_table.prime, i64 140, i1 false)
  store i32 1, ptr %2, align 4, !tbaa !5
  br label %7

7:                                                ; preds = %80, %0
  %8 = load i32, ptr %2, align 4, !tbaa !5
  %9 = load i8, ptr @key_length, align 1, !tbaa !9
  %10 = zext i8 %9 to i32
  %11 = icmp sle i32 %8, %10
  br i1 %11, label %12, label %83

12:                                               ; preds = %7
  %13 = load i32, ptr %2, align 4, !tbaa !5
  %14 = load i8, ptr @key_length, align 1, !tbaa !9
  %15 = zext i8 %14 to i32
  %16 = icmp ne i32 %13, %15
  br i1 %16, label %17, label %29

17:                                               ; preds = %12
  %18 = load i32, ptr %2, align 4, !tbaa !5
  %19 = sub nsw i32 %18, 1
  %20 = sext i32 %19 to i64
  %21 = getelementptr inbounds [70 x i8], ptr @master_key, i64 0, i64 %20
  %22 = load i8, ptr %21, align 1, !tbaa !9
  %23 = getelementptr inbounds [2 x i8], ptr %4, i64 0, i64 0
  store i8 %22, ptr %23, align 4, !tbaa !9
  %24 = load i32, ptr %2, align 4, !tbaa !5
  %25 = sext i32 %24 to i64
  %26 = getelementptr inbounds [70 x i8], ptr @master_key, i64 0, i64 %25
  %27 = load i8, ptr %26, align 1, !tbaa !9
  %28 = getelementptr inbounds [2 x i8], ptr %4, i64 0, i64 1
  store i8 %27, ptr %28, align 1, !tbaa !9
  br label %38

29:                                               ; preds = %12
  %30 = load i32, ptr %2, align 4, !tbaa !5
  %31 = sub nsw i32 %30, 1
  %32 = sext i32 %31 to i64
  %33 = getelementptr inbounds [70 x i8], ptr @master_key, i64 0, i64 %32
  %34 = load i8, ptr %33, align 1, !tbaa !9
  %35 = getelementptr inbounds [2 x i8], ptr %4, i64 0, i64 0
  store i8 %34, ptr %35, align 4, !tbaa !9
  %36 = load i8, ptr @master_key, align 16, !tbaa !9
  %37 = getelementptr inbounds [2 x i8], ptr %4, i64 0, i64 1
  store i8 %36, ptr %37, align 1, !tbaa !9
  br label %38

38:                                               ; preds = %29, %17
  %39 = load i32, ptr %4, align 4, !tbaa !9
  call void @srand(i32 noundef %39) #4
  store i32 0, ptr %1, align 4, !tbaa !5
  store i32 0, ptr %3, align 4, !tbaa !5
  br label %40

40:                                               ; preds = %76, %38
  %41 = load i32, ptr %3, align 4, !tbaa !5
  %42 = icmp slt i32 %41, 2560
  br i1 %42, label %43, label %79

43:                                               ; preds = %40
  %44 = load i32, ptr %2, align 4, !tbaa !5
  %45 = sext i32 %44 to i64
  %46 = getelementptr inbounds [35 x i32], ptr %6, i64 0, i64 %45
  %47 = load i32, ptr %46, align 4, !tbaa !5
  %48 = load i32, ptr %1, align 4, !tbaa !5
  %49 = add i32 %48, %47
  store i32 %49, ptr %1, align 4, !tbaa !5
  %50 = load i32, ptr %1, align 4, !tbaa !5
  %51 = icmp sgt i32 %50, 2559
  br i1 %51, label %52, label %55

52:                                               ; preds = %43
  %53 = load i32, ptr %1, align 4, !tbaa !5
  %54 = sub nsw i32 %53, 2560
  store i32 %54, ptr %1, align 4, !tbaa !5
  br label %55

55:                                               ; preds = %52, %43
  %56 = call i32 @rand() #4
  store i32 %56, ptr %5, align 4, !tbaa !9
  %57 = getelementptr inbounds [2 x i8], ptr %5, i64 0, i64 0
  %58 = load i8, ptr %57, align 4, !tbaa !9
  %59 = load i32, ptr %1, align 4, !tbaa !5
  %60 = sext i32 %59 to i64
  %61 = getelementptr inbounds [2560 x i8], ptr @key_table, i64 0, i64 %60
  store i8 %58, ptr %61, align 1, !tbaa !9
  %62 = load i32, ptr %1, align 4, !tbaa !5
  %63 = add nsw i32 %62, 1
  %64 = icmp ne i32 %63, 2559
  br i1 %64, label %65, label %72

65:                                               ; preds = %55
  %66 = getelementptr inbounds [2 x i8], ptr %5, i64 0, i64 1
  %67 = load i8, ptr %66, align 1, !tbaa !9
  %68 = load i32, ptr %1, align 4, !tbaa !5
  %69 = add nsw i32 %68, 1
  %70 = sext i32 %69 to i64
  %71 = getelementptr inbounds [2560 x i8], ptr @key_table, i64 0, i64 %70
  store i8 %67, ptr %71, align 1, !tbaa !9
  br label %75

72:                                               ; preds = %55
  %73 = getelementptr inbounds [2 x i8], ptr %5, i64 0, i64 1
  %74 = load i8, ptr %73, align 1, !tbaa !9
  store i8 %74, ptr @key_table, align 16, !tbaa !9
  br label %75

75:                                               ; preds = %72, %65
  br label %76

76:                                               ; preds = %75
  %77 = load i32, ptr %3, align 4, !tbaa !5
  %78 = add nsw i32 %77, 1
  store i32 %78, ptr %3, align 4, !tbaa !5
  br label %40, !llvm.loop !10

79:                                               ; preds = %40
  br label %80

80:                                               ; preds = %79
  %81 = load i32, ptr %2, align 4, !tbaa !5
  %82 = add nsw i32 %81, 1
  store i32 %82, ptr %2, align 4, !tbaa !5
  br label %7, !llvm.loop !12

83:                                               ; preds = %7
  call void @llvm.lifetime.end.p0(i64 140, ptr %6) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #4
  ret i8 0
}

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

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #2

; Function Attrs: nounwind
declare void @srand(i32 noundef) #3

; Function Attrs: nounwind
declare i32 @rand() #3

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

attributes #0 = { 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 #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { nounwind "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 #4 = { 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!7, !7, i64 0}
!10 = distinct !{!10, !11}
!11 = !{!"llvm.loop.mustprogress"}
!12 = distinct !{!12, !11}


; 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_length = external dso_local local_unnamed_addr global i8, align 1
@master_key = external dso_local local_unnamed_addr global <{ i8, i8, i8, i8, i8, i8, [64 x i8] }>, align 16
@__const.initialize_key_table.prime = external hidden unnamed_addr constant [35 x i32], align 16
@key_table = external dso_local local_unnamed_addr global [2560 x i8], align 16

; Function Attrs: nounwind uwtable
define dso_local noundef zeroext i8 @initialize_key_table() local_unnamed_addr #0 {
  %1 = load i8, ptr @key_length, align 1, !tbaa !5
  %.not27 = icmp eq i8 %1, 0
  br i1 %.not27, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %0, %21
  %indvars.iv = phi i64 [ %indvars.iv.next, %21 ], [ 1, %0 ]
  %.in = phi i8 [ %22, %21 ], [ %1, %0 ]
  %2 = zext i8 %.in to i64
  %.not23 = icmp eq i64 %indvars.iv, %2
  %3 = getelementptr inbounds [70 x i8], ptr @master_key, i64 0, i64 %indvars.iv
  %.sroa.5.0.in = select i1 %.not23, ptr @master_key, ptr %3
  %4 = add nsw i64 %indvars.iv, -1
  %.sroa.02.0.in = getelementptr inbounds [70 x i8], ptr @master_key, i64 0, i64 %4
  %.sroa.02.0 = load i8, ptr %.sroa.02.0.in, align 1, !tbaa !5
  %.sroa.5.0 = load i8, ptr %.sroa.5.0.in, align 1, !tbaa !5
  %.sroa.5.0.insert.ext = zext i8 %.sroa.5.0 to i32
  %.sroa.5.0.insert.shift = shl nuw nsw i32 %.sroa.5.0.insert.ext, 8
  %.sroa.02.0.insert.ext = zext i8 %.sroa.02.0 to i32
  %.sroa.02.0.insert.insert = or disjoint i32 %.sroa.5.0.insert.shift, %.sroa.02.0.insert.ext
  tail call void @srand(i32 noundef %.sroa.02.0.insert.insert) #2
  %5 = getelementptr inbounds [35 x i32], ptr @__const.initialize_key_table.prime, i64 0, i64 %indvars.iv
  %6 = load i32, ptr %5, align 4, !tbaa !8
  br label %7

7:                                                ; preds = %.lr.ph, %19
  %.026 = phi i32 [ 0, %.lr.ph ], [ %spec.select, %19 ]
  %.01825 = phi i32 [ 0, %.lr.ph ], [ %20, %19 ]
  %8 = add i32 %6, %.026
  %9 = icmp sgt i32 %8, 2559
  %10 = add nsw i32 %8, -2560
  %spec.select = select i1 %9, i32 %10, i32 %8
  %11 = tail call i32 @rand() #2
  %.sroa.0.0.extract.trunc = trunc i32 %11 to i8
  %.sroa.4.0.extract.shift = lshr i32 %11, 8
  %.sroa.4.0.extract.trunc = trunc i32 %.sroa.4.0.extract.shift to i8
  %12 = sext i32 %spec.select to i64
  %13 = getelementptr inbounds [2560 x i8], ptr @key_table, i64 0, i64 %12
  store i8 %.sroa.0.0.extract.trunc, ptr %13, align 1, !tbaa !5
  %14 = add nsw i32 %spec.select, 1
  %.not24 = icmp eq i32 %14, 2559
  br i1 %.not24, label %18, label %15

15:                                               ; preds = %7
  %16 = sext i32 %14 to i64
  %17 = getelementptr inbounds [2560 x i8], ptr @key_table, i64 0, i64 %16
  store i8 %.sroa.4.0.extract.trunc, ptr %17, align 1, !tbaa !5
  br label %19

18:                                               ; preds = %7
  store i8 %.sroa.4.0.extract.trunc, ptr @key_table, align 16, !tbaa !5
  br label %19

19:                                               ; preds = %15, %18
  %20 = add nuw nsw i32 %.01825, 1
  %exitcond.not = icmp eq i32 %20, 2560
  br i1 %exitcond.not, label %21, label %7, !llvm.loop !10

21:                                               ; preds = %19
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %22 = load i8, ptr @key_length, align 1, !tbaa !5
  %23 = zext i8 %22 to i64
  %.not.not = icmp ult i64 %indvars.iv, %23
  br i1 %.not.not, label %.lr.ph, label %._crit_edge, !llvm.loop !12

._crit_edge:                                      ; preds = %21, %0
  ret i8 0
}

; Function Attrs: nounwind
declare void @srand(i32 noundef) local_unnamed_addr #1

; Function Attrs: nounwind
declare i32 @rand() local_unnamed_addr #1

attributes #0 = { 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 #1 = { nounwind "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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = !{!9, !9, i64 0}
!9 = !{!"int", !6, i64 0}
!10 = distinct !{!10, !11}
!11 = !{!"llvm.loop.mustprogress"}
!12 = distinct !{!12, !11}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SCCPPass,SROAPass,SimplifyCFGPass,TailCallElimPass