; LLVM IR from dynexcoin/Dynex :: src/crypto (oaes_lib)

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

%struct._oaes_ctx = type { ptr, i16, [16 x i8] }
%struct._oaes_key = type { i64, ptr, i64, ptr, i64, i64 }

@oaes_header = external hidden global <{ i8, i8, i8, i8, i8, i8, [10 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: 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) #1

; Function Attrs: nounwind uwtable
define dso_local i32 @oaes_key_import(ptr noundef %0, ptr noundef %1, i64 noundef %2) #2 {
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store i64 %2, ptr %7, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #5
  %12 = load ptr, ptr %5, align 8, !tbaa !5
  store ptr %12, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #5
  store i32 0, ptr %9, align 4, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #5
  %13 = load ptr, ptr %8, align 8, !tbaa !5
  %14 = icmp eq ptr null, %13
  br i1 %14, label %15, label %16

15:                                               ; preds = %3
  store i32 2, ptr %4, align 4
  store i32 1, ptr %11, align 4
  br label %125

16:                                               ; preds = %3
  %17 = load ptr, ptr %6, align 8, !tbaa !5
  %18 = icmp eq ptr null, %17
  br i1 %18, label %19, label %20

19:                                               ; preds = %16
  store i32 3, ptr %4, align 4
  store i32 1, ptr %11, align 4
  br label %125

20:                                               ; preds = %16
  %21 = load i64, ptr %7, align 8, !tbaa !9
  switch i64 %21, label %23 [
    i64 32, label %22
    i64 40, label %22
    i64 48, label %22
  ]

22:                                               ; preds = %20, %20, %20
  br label %24

23:                                               ; preds = %20
  store i32 4, ptr %4, align 4
  store i32 1, ptr %11, align 4
  br label %125

24:                                               ; preds = %22
  %25 = load ptr, ptr %6, align 8, !tbaa !5
  %26 = call i32 @memcmp(ptr noundef %25, ptr noundef @oaes_header, i64 noundef 4) #6
  %27 = icmp ne i32 0, %26
  br i1 %27, label %28, label %29

28:                                               ; preds = %24
  store i32 10, ptr %4, align 4
  store i32 1, ptr %11, align 4
  br label %125

29:                                               ; preds = %24
  %30 = load ptr, ptr %6, align 8, !tbaa !5
  %31 = getelementptr inbounds i8, ptr %30, i64 4
  %32 = load i8, ptr %31, align 1, !tbaa !13
  %33 = zext i8 %32 to i32
  switch i32 %33, label %34 [
    i32 1, label %35
  ]

34:                                               ; preds = %29
  store i32 10, ptr %4, align 4
  store i32 1, ptr %11, align 4
  br label %125

35:                                               ; preds = %29
  %36 = load ptr, ptr %6, align 8, !tbaa !5
  %37 = getelementptr inbounds i8, ptr %36, i64 5
  %38 = load i8, ptr %37, align 1, !tbaa !13
  %39 = zext i8 %38 to i32
  switch i32 %39, label %40 [
    i32 1, label %41
  ]

40:                                               ; preds = %35
  store i32 10, ptr %4, align 4
  store i32 1, ptr %11, align 4
  br label %125

41:                                               ; preds = %35
  %42 = load ptr, ptr %6, align 8, !tbaa !5
  %43 = getelementptr inbounds i8, ptr %42, i64 7
  %44 = load i8, ptr %43, align 1, !tbaa !13
  %45 = zext i8 %44 to i32
  store i32 %45, ptr %10, align 4, !tbaa !11
  %46 = load i32, ptr %10, align 4, !tbaa !11
  switch i32 %46, label %48 [
    i32 16, label %47
    i32 24, label %47
    i32 32, label %47
  ]

47:                                               ; preds = %41, %41, %41
  br label %49

48:                                               ; preds = %41
  store i32 10, ptr %4, align 4
  store i32 1, ptr %11, align 4
  br label %125

49:                                               ; preds = %47
  %50 = load i64, ptr %7, align 8, !tbaa !9
  %51 = trunc i64 %50 to i32
  %52 = load i32, ptr %10, align 4, !tbaa !11
  %53 = add nsw i32 %52, 16
  %54 = icmp ne i32 %51, %53
  br i1 %54, label %55, label %56

55:                                               ; preds = %49
  store i32 4, ptr %4, align 4
  store i32 1, ptr %11, align 4
  br label %125

56:                                               ; preds = %49
  %57 = load ptr, ptr %8, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct._oaes_ctx, ptr %57, i32 0, i32 0
  %59 = load ptr, ptr %58, align 8, !tbaa !14
  %60 = icmp ne ptr %59, null
  br i1 %60, label %61, label %65

61:                                               ; preds = %56
  %62 = load ptr, ptr %8, align 8, !tbaa !5
  %63 = getelementptr inbounds %struct._oaes_ctx, ptr %62, i32 0, i32 0
  %64 = call i32 @oaes_key_destroy(ptr noundef %63)
  br label %65

65:                                               ; preds = %61, %56
  %66 = call noalias ptr @calloc(i64 noundef 48, i64 noundef 1) #7
  %67 = load ptr, ptr %8, align 8, !tbaa !5
  %68 = getelementptr inbounds %struct._oaes_ctx, ptr %67, i32 0, i32 0
  store ptr %66, ptr %68, align 8, !tbaa !14
  %69 = load ptr, ptr %8, align 8, !tbaa !5
  %70 = getelementptr inbounds %struct._oaes_ctx, ptr %69, i32 0, i32 0
  %71 = load ptr, ptr %70, align 8, !tbaa !14
  %72 = icmp eq ptr null, %71
  br i1 %72, label %73, label %74

73:                                               ; preds = %65
  store i32 8, ptr %4, align 4
  store i32 1, ptr %11, align 4
  br label %125

74:                                               ; preds = %65
  %75 = load i32, ptr %10, align 4, !tbaa !11
  %76 = sext i32 %75 to i64
  %77 = load ptr, ptr %8, align 8, !tbaa !5
  %78 = getelementptr inbounds %struct._oaes_ctx, ptr %77, i32 0, i32 0
  %79 = load ptr, ptr %78, align 8, !tbaa !14
  %80 = getelementptr inbounds %struct._oaes_key, ptr %79, i32 0, i32 0
  store i64 %76, ptr %80, align 8, !tbaa !17
  %81 = load i32, ptr %10, align 4, !tbaa !11
  %82 = sext i32 %81 to i64
  %83 = call noalias ptr @calloc(i64 noundef %82, i64 noundef 1) #7
  %84 = load ptr, ptr %8, align 8, !tbaa !5
  %85 = getelementptr inbounds %struct._oaes_ctx, ptr %84, i32 0, i32 0
  %86 = load ptr, ptr %85, align 8, !tbaa !14
  %87 = getelementptr inbounds %struct._oaes_key, ptr %86, i32 0, i32 1
  store ptr %83, ptr %87, align 8, !tbaa !19
  %88 = load ptr, ptr %8, align 8, !tbaa !5
  %89 = getelementptr inbounds %struct._oaes_ctx, ptr %88, i32 0, i32 0
  %90 = load ptr, ptr %89, align 8, !tbaa !14
  %91 = getelementptr inbounds %struct._oaes_key, ptr %90, i32 0, i32 1
  %92 = load ptr, ptr %91, align 8, !tbaa !19
  %93 = icmp eq ptr null, %92
  br i1 %93, label %94, label %98

94:                                               ; preds = %74
  %95 = load ptr, ptr %8, align 8, !tbaa !5
  %96 = getelementptr inbounds %struct._oaes_ctx, ptr %95, i32 0, i32 0
  %97 = call i32 @oaes_key_destroy(ptr noundef %96)
  store i32 8, ptr %4, align 4
  store i32 1, ptr %11, align 4
  br label %125

98:                                               ; preds = %74
  %99 = load ptr, ptr %8, align 8, !tbaa !5
  %100 = getelementptr inbounds %struct._oaes_ctx, ptr %99, i32 0, i32 0
  %101 = load ptr, ptr %100, align 8, !tbaa !14
  %102 = getelementptr inbounds %struct._oaes_key, ptr %101, i32 0, i32 1
  %103 = load ptr, ptr %102, align 8, !tbaa !19
  %104 = load ptr, ptr %6, align 8, !tbaa !5
  %105 = getelementptr inbounds i8, ptr %104, i64 16
  %106 = load i32, ptr %10, align 4, !tbaa !11
  %107 = sext i32 %106 to i64
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %103, ptr align 1 %105, i64 %107, i1 false)
  %108 = load i32, ptr %9, align 4, !tbaa !11
  %109 = icmp ne i32 %108, 0
  br i1 %109, label %114, label %110

110:                                              ; preds = %98
  %111 = load ptr, ptr %5, align 8, !tbaa !5
  %112 = call i32 @oaes_key_expand(ptr noundef %111)
  %113 = icmp ne i32 %112, 0
  br label %114

114:                                              ; preds = %110, %98
  %115 = phi i1 [ true, %98 ], [ %113, %110 ]
  %116 = zext i1 %115 to i32
  store i32 %116, ptr %9, align 4, !tbaa !11
  %117 = load i32, ptr %9, align 4, !tbaa !11
  %118 = icmp ne i32 %117, 0
  br i1 %118, label %119, label %124

119:                                              ; preds = %114
  %120 = load ptr, ptr %8, align 8, !tbaa !5
  %121 = getelementptr inbounds %struct._oaes_ctx, ptr %120, i32 0, i32 0
  %122 = call i32 @oaes_key_destroy(ptr noundef %121)
  %123 = load i32, ptr %9, align 4, !tbaa !11
  store i32 %123, ptr %4, align 4
  store i32 1, ptr %11, align 4
  br label %125

124:                                              ; preds = %114
  store i32 0, ptr %4, align 4
  store i32 1, ptr %11, align 4
  br label %125

125:                                              ; preds = %124, %119, %94, %73, %55, %48, %40, %34, %28, %23, %19, %15
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #5
  %126 = load i32, ptr %4, align 4
  ret i32 %126
}

; Function Attrs: nounwind willreturn memory(read)
declare i32 @memcmp(ptr noundef, ptr noundef, i64 noundef) #3

; Function Attrs: nounwind uwtable
declare hidden i32 @oaes_key_destroy(ptr noundef) #2

; Function Attrs: nounwind allocsize(0,1)
declare noalias ptr @calloc(i64 noundef, i64 noundef) #4

; Function Attrs: nounwind uwtable
declare hidden i32 @oaes_key_expand(ptr noundef) #2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { 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 #3 = { nounwind willreturn memory(read) "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 allocsize(0,1) "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 #5 = { nounwind }
attributes #6 = { nounwind willreturn memory(read) }
attributes #7 = { nounwind allocsize(0,1) }

!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 = !{!"long", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!7, !7, i64 0}
!14 = !{!15, !6, i64 0}
!15 = !{!"_oaes_ctx", !6, i64 0, !16, i64 8, !7, i64 10}
!16 = !{!"short", !7, i64 0}
!17 = !{!18, !10, i64 0}
!18 = !{!"_oaes_key", !10, i64 0, !6, i64 8, !10, i64 16, !6, i64 24, !10, i64 32, !10, i64 40}
!19 = !{!18, !6, i64 8}


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

@oaes_header = external hidden global <{ i8, i8, i8, i8, i8, i8, [10 x i8] }>, align 16

; Function Attrs: mustprogress 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) #0

; Function Attrs: nounwind uwtable
define dso_local range(i32 0, 11) i32 @oaes_key_import(ptr noundef %0, ptr noundef readonly %1, i64 noundef %2) local_unnamed_addr #1 {
  %4 = icmp eq ptr %0, null
  br i1 %4, label %38, label %5

5:                                                ; preds = %3
  %6 = icmp eq ptr %1, null
  br i1 %6, label %38, label %7

7:                                                ; preds = %5
  switch i64 %2, label %38 [
    i64 32, label %8
    i64 40, label %8
    i64 48, label %8
  ]

8:                                                ; preds = %7, %7, %7
  %bcmp = tail call i32 @bcmp(ptr noundef nonnull dereferenceable(4) %1, ptr noundef nonnull dereferenceable(4) @oaes_header, i64 4)
  %.not = icmp eq i32 %bcmp, 0
  br i1 %.not, label %9, label %38

9:                                                ; preds = %8
  %10 = getelementptr inbounds i8, ptr %1, i64 4
  %11 = load i8, ptr %10, align 1, !tbaa !5
  %cond = icmp eq i8 %11, 1
  br i1 %cond, label %12, label %38

12:                                               ; preds = %9
  %13 = getelementptr inbounds i8, ptr %1, i64 5
  %14 = load i8, ptr %13, align 1, !tbaa !5
  %cond1 = icmp eq i8 %14, 1
  br i1 %cond1, label %15, label %38

15:                                               ; preds = %12
  %16 = getelementptr inbounds i8, ptr %1, i64 7
  %17 = load i8, ptr %16, align 1, !tbaa !5
  switch i8 %17, label %38 [
    i8 16, label %18
    i8 24, label %18
    i8 32, label %18
  ]

18:                                               ; preds = %15, %15, %15
  %19 = zext nneg i8 %17 to i32
  %20 = trunc i64 %2 to i32
  %21 = add nuw nsw i32 %19, 16
  %.not34 = icmp eq i32 %21, %20
  br i1 %.not34, label %22, label %38

22:                                               ; preds = %18
  %23 = load ptr, ptr %0, align 8, !tbaa !8
  %.not35 = icmp eq ptr %23, null
  br i1 %.not35, label %26, label %24

24:                                               ; preds = %22
  %25 = tail call i32 @oaes_key_destroy(ptr noundef nonnull %0)
  br label %26

26:                                               ; preds = %24, %22
  %27 = tail call noalias dereferenceable_or_null(48) ptr @calloc(i64 noundef 48, i64 noundef 1) #4
  store ptr %27, ptr %0, align 8, !tbaa !8
  %28 = icmp eq ptr %27, null
  br i1 %28, label %38, label %29

29:                                               ; preds = %26
  %30 = zext nneg i8 %17 to i64
  store i64 %30, ptr %27, align 8, !tbaa !12
  %31 = tail call noalias ptr @calloc(i64 noundef %30, i64 noundef 1) #4
  %32 = getelementptr inbounds i8, ptr %27, i64 8
  store ptr %31, ptr %32, align 8, !tbaa !15
  %33 = icmp eq ptr %31, null
  br i1 %33, label %.sink.split, label %34

34:                                               ; preds = %29
  %35 = getelementptr inbounds i8, ptr %1, i64 16
  tail call void @llvm.memcpy.p0.p0.i64(ptr nonnull align 1 %31, ptr nonnull align 1 %35, i64 %30, i1 false)
  %36 = tail call i32 @oaes_key_expand(ptr noundef nonnull %0)
  %.not36 = icmp eq i32 %36, 0
  br i1 %.not36, label %38, label %.sink.split

.sink.split:                                      ; preds = %34, %29
  %.0.ph = phi i32 [ 8, %29 ], [ 1, %34 ]
  %37 = tail call i32 @oaes_key_destroy(ptr noundef nonnull %0)
  br label %38

38:                                               ; preds = %.sink.split, %34, %26, %18, %15, %12, %9, %8, %7, %5, %3
  %.0 = phi i32 [ 2, %3 ], [ 3, %5 ], [ 4, %7 ], [ 10, %8 ], [ 10, %9 ], [ 10, %12 ], [ 10, %15 ], [ 4, %18 ], [ 8, %26 ], [ 0, %34 ], [ %.0.ph, %.sink.split ]
  ret i32 %.0
}

; Function Attrs: nounwind uwtable
declare hidden i32 @oaes_key_destroy(ptr noundef) local_unnamed_addr #1

; Function Attrs: mustprogress nofree nounwind willreturn allockind("alloc,zeroed") allocsize(0,1) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @calloc(i64 noundef, i64 noundef) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
declare hidden i32 @oaes_key_expand(ptr noundef) local_unnamed_addr #1

; Function Attrs: nofree nounwind willreturn memory(argmem: read)
declare i32 @bcmp(ptr nocapture, ptr nocapture, i64) local_unnamed_addr #3

attributes #0 = { mustprogress nocallback nofree 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 = { mustprogress nofree nounwind willreturn allockind("alloc,zeroed") allocsize(0,1) memory(inaccessiblemem: readwrite) "alloc-family"="malloc" "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 #3 = { nofree nounwind willreturn memory(argmem: read) }
attributes #4 = { nounwind allocsize(0,1) }

!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, !10, i64 0}
!9 = !{!"_oaes_ctx", !10, i64 0, !11, i64 8, !6, i64 10}
!10 = !{!"any pointer", !6, i64 0}
!11 = !{!"short", !6, i64 0}
!12 = !{!13, !14, i64 0}
!13 = !{!"_oaes_key", !14, i64 0, !10, i64 8, !14, i64 16, !10, i64 24, !14, i64 32, !14, i64 40}
!14 = !{!"long", !6, i64 0}
!15 = !{!13, !10, i64 8}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass