; 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] }

; 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_set_option(ptr noundef %0, i16 noundef zeroext %1, ptr noundef %2) #2 {
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca i16, align 2
  %7 = alloca ptr, align 8
  %8 = alloca i64, align 8
  %9 = alloca ptr, align 8
  %10 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store i16 %1, ptr %6, align 2, !tbaa !9
  store ptr %2, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #5
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #5
  %11 = load ptr, ptr %5, align 8, !tbaa !5
  store ptr %11, ptr %9, align 8, !tbaa !5
  %12 = load ptr, ptr %9, align 8, !tbaa !5
  %13 = icmp eq ptr null, %12
  br i1 %13, label %14, label %15

14:                                               ; preds = %3
  store i32 2, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %68

15:                                               ; preds = %3
  %16 = load i16, ptr %6, align 2, !tbaa !9
  %17 = zext i16 %16 to i32
  switch i32 %17, label %58 [
    i32 1, label %18
    i32 2, label %28
  ]

18:                                               ; preds = %15
  %19 = load ptr, ptr %9, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct._oaes_ctx, ptr %19, i32 0, i32 1
  %21 = load i16, ptr %20, align 8, !tbaa !11
  %22 = zext i16 %21 to i32
  %23 = and i32 %22, -3
  %24 = trunc i32 %23 to i16
  store i16 %24, ptr %20, align 8, !tbaa !11
  %25 = load ptr, ptr %9, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct._oaes_ctx, ptr %25, i32 0, i32 2
  %27 = getelementptr inbounds [16 x i8], ptr %26, i64 0, i64 0
  call void @llvm.memset.p0.i64(ptr align 2 %27, i8 0, i64 16, i1 false)
  br label %59

28:                                               ; preds = %15
  %29 = load ptr, ptr %9, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct._oaes_ctx, ptr %29, i32 0, i32 1
  %31 = load i16, ptr %30, align 8, !tbaa !11
  %32 = zext i16 %31 to i32
  %33 = and i32 %32, -2
  %34 = trunc i32 %33 to i16
  store i16 %34, ptr %30, align 8, !tbaa !11
  %35 = load ptr, ptr %7, align 8, !tbaa !5
  %36 = icmp ne ptr %35, null
  br i1 %36, label %37, label %42

37:                                               ; preds = %28
  %38 = load ptr, ptr %9, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct._oaes_ctx, ptr %38, i32 0, i32 2
  %40 = getelementptr inbounds [16 x i8], ptr %39, i64 0, i64 0
  %41 = load ptr, ptr %7, align 8, !tbaa !5
  call void @llvm.memcpy.p0.p0.i64(ptr align 2 %40, ptr align 1 %41, i64 16, i1 false)
  br label %57

42:                                               ; preds = %28
  store i64 0, ptr %8, align 8, !tbaa !13
  br label %43

43:                                               ; preds = %53, %42
  %44 = load i64, ptr %8, align 8, !tbaa !13
  %45 = icmp ult i64 %44, 16
  br i1 %45, label %46, label %56

46:                                               ; preds = %43
  %47 = call i32 @rand() #5
  %48 = trunc i32 %47 to i8
  %49 = load ptr, ptr %9, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct._oaes_ctx, ptr %49, i32 0, i32 2
  %51 = load i64, ptr %8, align 8, !tbaa !13
  %52 = getelementptr inbounds [16 x i8], ptr %50, i64 0, i64 %51
  store i8 %48, ptr %52, align 1, !tbaa !15
  br label %53

53:                                               ; preds = %46
  %54 = load i64, ptr %8, align 8, !tbaa !13
  %55 = add i64 %54, 1
  store i64 %55, ptr %8, align 8, !tbaa !13
  br label %43, !llvm.loop !16

56:                                               ; preds = %43
  br label %57

57:                                               ; preds = %56, %37
  br label %59

58:                                               ; preds = %15
  store i32 3, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %68

59:                                               ; preds = %57, %18
  %60 = load i16, ptr %6, align 2, !tbaa !9
  %61 = zext i16 %60 to i32
  %62 = load ptr, ptr %9, align 8, !tbaa !5
  %63 = getelementptr inbounds %struct._oaes_ctx, ptr %62, i32 0, i32 1
  %64 = load i16, ptr %63, align 8, !tbaa !11
  %65 = zext i16 %64 to i32
  %66 = or i32 %65, %61
  %67 = trunc i32 %66 to i16
  store i16 %67, ptr %63, align 8, !tbaa !11
  store i32 0, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %68

68:                                               ; preds = %59, %58, %14
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #5
  %69 = load i32, ptr %4, align 4
  ret i32 %69
}

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

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

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 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #4 = { 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 #5 = { 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 = !{!"short", !7, i64 0}
!11 = !{!12, !10, i64 8}
!12 = !{!"_oaes_ctx", !6, i64 0, !10, i64 8, !7, i64 10}
!13 = !{!14, !14, i64 0}
!14 = !{!"long", !7, i64 0}
!15 = !{!7, !7, i64 0}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}


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

; 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, 4) i32 @oaes_set_option(ptr noundef %0, i16 noundef zeroext %1, ptr noundef readonly %2) local_unnamed_addr #1 {
  %4 = icmp eq ptr %0, null
  br i1 %4, label %67, label %5

5:                                                ; preds = %3
  switch i16 %1, label %67 [
    i16 1, label %6
    i16 2, label %11
  ]

6:                                                ; preds = %5
  %7 = getelementptr inbounds i8, ptr %0, i64 8
  %8 = load i16, ptr %7, align 8, !tbaa !5
  %9 = and i16 %8, -3
  %10 = getelementptr inbounds i8, ptr %0, i64 10
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 2 dereferenceable(16) %10, i8 0, i64 16, i1 false)
  br label %.loopexit

11:                                               ; preds = %5
  %12 = getelementptr inbounds i8, ptr %0, i64 8
  %13 = load i16, ptr %12, align 8, !tbaa !5
  %14 = and i16 %13, -2
  store i16 %14, ptr %12, align 8, !tbaa !5
  %.not = icmp eq ptr %2, null
  %15 = getelementptr inbounds i8, ptr %0, i64 10
  br i1 %.not, label %.preheader, label %63

.preheader:                                       ; preds = %11
  %16 = tail call i32 @rand() #4
  %17 = trunc i32 %16 to i8
  store i8 %17, ptr %15, align 1, !tbaa !11
  %18 = tail call i32 @rand() #4
  %19 = trunc i32 %18 to i8
  %20 = getelementptr inbounds i8, ptr %0, i64 11
  store i8 %19, ptr %20, align 1, !tbaa !11
  %21 = tail call i32 @rand() #4
  %22 = trunc i32 %21 to i8
  %23 = getelementptr inbounds i8, ptr %0, i64 12
  store i8 %22, ptr %23, align 1, !tbaa !11
  %24 = tail call i32 @rand() #4
  %25 = trunc i32 %24 to i8
  %26 = getelementptr inbounds i8, ptr %0, i64 13
  store i8 %25, ptr %26, align 1, !tbaa !11
  %27 = tail call i32 @rand() #4
  %28 = trunc i32 %27 to i8
  %29 = getelementptr inbounds i8, ptr %0, i64 14
  store i8 %28, ptr %29, align 1, !tbaa !11
  %30 = tail call i32 @rand() #4
  %31 = trunc i32 %30 to i8
  %32 = getelementptr inbounds i8, ptr %0, i64 15
  store i8 %31, ptr %32, align 1, !tbaa !11
  %33 = tail call i32 @rand() #4
  %34 = trunc i32 %33 to i8
  %35 = getelementptr inbounds i8, ptr %0, i64 16
  store i8 %34, ptr %35, align 1, !tbaa !11
  %36 = tail call i32 @rand() #4
  %37 = trunc i32 %36 to i8
  %38 = getelementptr inbounds i8, ptr %0, i64 17
  store i8 %37, ptr %38, align 1, !tbaa !11
  %39 = tail call i32 @rand() #4
  %40 = trunc i32 %39 to i8
  %41 = getelementptr inbounds i8, ptr %0, i64 18
  store i8 %40, ptr %41, align 1, !tbaa !11
  %42 = tail call i32 @rand() #4
  %43 = trunc i32 %42 to i8
  %44 = getelementptr inbounds i8, ptr %0, i64 19
  store i8 %43, ptr %44, align 1, !tbaa !11
  %45 = tail call i32 @rand() #4
  %46 = trunc i32 %45 to i8
  %47 = getelementptr inbounds i8, ptr %0, i64 20
  store i8 %46, ptr %47, align 1, !tbaa !11
  %48 = tail call i32 @rand() #4
  %49 = trunc i32 %48 to i8
  %50 = getelementptr inbounds i8, ptr %0, i64 21
  store i8 %49, ptr %50, align 1, !tbaa !11
  %51 = tail call i32 @rand() #4
  %52 = trunc i32 %51 to i8
  %53 = getelementptr inbounds i8, ptr %0, i64 22
  store i8 %52, ptr %53, align 1, !tbaa !11
  %54 = tail call i32 @rand() #4
  %55 = trunc i32 %54 to i8
  %56 = getelementptr inbounds i8, ptr %0, i64 23
  store i8 %55, ptr %56, align 1, !tbaa !11
  %57 = tail call i32 @rand() #4
  %58 = trunc i32 %57 to i8
  %59 = getelementptr inbounds i8, ptr %0, i64 24
  store i8 %58, ptr %59, align 1, !tbaa !11
  %60 = tail call i32 @rand() #4
  %61 = trunc i32 %60 to i8
  %62 = getelementptr inbounds i8, ptr %0, i64 25
  store i8 %61, ptr %62, align 1, !tbaa !11
  %.pre = load i16, ptr %12, align 8, !tbaa !5
  br label %.loopexit

63:                                               ; preds = %11
  tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 2 dereferenceable(16) %15, ptr noundef nonnull align 1 dereferenceable(16) %2, i64 16, i1 false)
  br label %.loopexit

.loopexit:                                        ; preds = %.preheader, %63, %6
  %64 = phi i16 [ %.pre, %.preheader ], [ %14, %63 ], [ %9, %6 ]
  %65 = getelementptr inbounds i8, ptr %0, i64 8
  %66 = or i16 %64, %1
  store i16 %66, ptr %65, align 8, !tbaa !5
  br label %67

67:                                               ; preds = %5, %3, %.loopexit
  %.0 = phi i32 [ 0, %.loopexit ], [ 2, %3 ], [ 3, %5 ]
  ret i32 %.0
}

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

; Function Attrs: nounwind
declare i32 @rand() 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 nocallback nofree nounwind willreturn memory(argmem: write) }
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, !10, i64 8}
!6 = !{!"_oaes_ctx", !7, i64 0, !10, i64 8, !8, i64 10}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"short", !8, i64 0}
!11 = !{!8, !8, i64 0}


; Effective O3 passes: DSEPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,LICMPass,LoopRotatePass,LoopSimplifyPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass