; LLVM IR from BrunoLevy/learn-fpga :: FemtoRV/TUTORIALS/FROM_BLINKER_TO_RISCV/FIRMWARE/COREMARK (core_list_join)

; 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.RESULTS_S = type { i16, i16, i16, [4 x ptr], i32, i32, i32, ptr, %struct.MAT_PARAMS_S, i16, i16, i16, i16, i16, %struct.CORE_PORTABLE_S }
%struct.MAT_PARAMS_S = type { i32, ptr, ptr, ptr }
%struct.CORE_PORTABLE_S = type { i8 }

; Function Attrs: nounwind uwtable
define dso_local signext i16 @calc_func(ptr noundef %0, ptr noundef %1) #0 {
  %3 = alloca i16, align 2
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i16, align 2
  %7 = alloca i16, align 2
  %8 = alloca i8, align 1
  %9 = alloca i32, align 4
  %10 = alloca i16, align 2
  %11 = alloca i16, align 2
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 2, ptr %6) #3
  %12 = load ptr, ptr %4, align 8, !tbaa !5
  %13 = load i16, ptr %12, align 2, !tbaa !9
  store i16 %13, ptr %6, align 2, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 2, ptr %7) #3
  call void @llvm.lifetime.start.p0(i64 1, ptr %8) #3
  %14 = load i16, ptr %6, align 2, !tbaa !9
  %15 = sext i16 %14 to i32
  %16 = ashr i32 %15, 7
  %17 = and i32 %16, 1
  %18 = trunc i32 %17 to i8
  store i8 %18, ptr %8, align 1, !tbaa !11
  %19 = load i8, ptr %8, align 1, !tbaa !11
  %20 = icmp ne i8 %19, 0
  br i1 %20, label %21, label %26

21:                                               ; preds = %2
  %22 = load i16, ptr %6, align 2, !tbaa !9
  %23 = sext i16 %22 to i32
  %24 = and i32 %23, 127
  %25 = trunc i32 %24 to i16
  store i16 %25, ptr %3, align 2
  store i32 1, ptr %9, align 4
  br label %121

26:                                               ; preds = %2
  call void @llvm.lifetime.start.p0(i64 2, ptr %10) #3
  %27 = load i16, ptr %6, align 2, !tbaa !9
  %28 = sext i16 %27 to i32
  %29 = and i32 %28, 7
  %30 = trunc i32 %29 to i16
  store i16 %30, ptr %10, align 2, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 2, ptr %11) #3
  %31 = load i16, ptr %6, align 2, !tbaa !9
  %32 = sext i16 %31 to i32
  %33 = ashr i32 %32, 3
  %34 = and i32 %33, 15
  %35 = trunc i32 %34 to i16
  store i16 %35, ptr %11, align 2, !tbaa !9
  %36 = load i16, ptr %11, align 2, !tbaa !9
  %37 = sext i16 %36 to i32
  %38 = shl i32 %37, 4
  %39 = load i16, ptr %11, align 2, !tbaa !9
  %40 = sext i16 %39 to i32
  %41 = or i32 %40, %38
  %42 = trunc i32 %41 to i16
  store i16 %42, ptr %11, align 2, !tbaa !9
  %43 = load i16, ptr %10, align 2, !tbaa !9
  %44 = sext i16 %43 to i32
  switch i32 %44, label %97 [
    i32 0, label %45
    i32 1, label %79
  ]

45:                                               ; preds = %26
  %46 = load i16, ptr %11, align 2, !tbaa !9
  %47 = sext i16 %46 to i32
  %48 = icmp slt i32 %47, 34
  br i1 %48, label %49, label %50

49:                                               ; preds = %45
  store i16 34, ptr %11, align 2, !tbaa !9
  br label %50

50:                                               ; preds = %49, %45
  %51 = load ptr, ptr %5, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct.RESULTS_S, ptr %51, i32 0, i32 4
  %53 = load i32, ptr %52, align 8, !tbaa !12
  %54 = load ptr, ptr %5, align 8, !tbaa !5
  %55 = getelementptr inbounds %struct.RESULTS_S, ptr %54, i32 0, i32 3
  %56 = getelementptr inbounds [4 x ptr], ptr %55, i64 0, i64 3
  %57 = load ptr, ptr %56, align 8, !tbaa !5
  %58 = load ptr, ptr %5, align 8, !tbaa !5
  %59 = getelementptr inbounds %struct.RESULTS_S, ptr %58, i32 0, i32 0
  %60 = load i16, ptr %59, align 8, !tbaa !17
  %61 = load ptr, ptr %5, align 8, !tbaa !5
  %62 = getelementptr inbounds %struct.RESULTS_S, ptr %61, i32 0, i32 1
  %63 = load i16, ptr %62, align 2, !tbaa !18
  %64 = load i16, ptr %11, align 2, !tbaa !9
  %65 = load ptr, ptr %5, align 8, !tbaa !5
  %66 = getelementptr inbounds %struct.RESULTS_S, ptr %65, i32 0, i32 9
  %67 = load i16, ptr %66, align 8, !tbaa !19
  %68 = call zeroext i16 @core_bench_state(i32 noundef %53, ptr noundef %57, i16 noundef signext %60, i16 noundef signext %63, i16 noundef signext %64, i16 noundef zeroext %67)
  store i16 %68, ptr %7, align 2, !tbaa !9
  %69 = load ptr, ptr %5, align 8, !tbaa !5
  %70 = getelementptr inbounds %struct.RESULTS_S, ptr %69, i32 0, i32 12
  %71 = load i16, ptr %70, align 2, !tbaa !20
  %72 = zext i16 %71 to i32
  %73 = icmp eq i32 %72, 0
  br i1 %73, label %74, label %78

74:                                               ; preds = %50
  %75 = load i16, ptr %7, align 2, !tbaa !9
  %76 = load ptr, ptr %5, align 8, !tbaa !5
  %77 = getelementptr inbounds %struct.RESULTS_S, ptr %76, i32 0, i32 12
  store i16 %75, ptr %77, align 2, !tbaa !20
  br label %78

78:                                               ; preds = %74, %50
  br label %99

79:                                               ; preds = %26
  %80 = load ptr, ptr %5, align 8, !tbaa !5
  %81 = getelementptr inbounds %struct.RESULTS_S, ptr %80, i32 0, i32 8
  %82 = load i16, ptr %11, align 2, !tbaa !9
  %83 = load ptr, ptr %5, align 8, !tbaa !5
  %84 = getelementptr inbounds %struct.RESULTS_S, ptr %83, i32 0, i32 9
  %85 = load i16, ptr %84, align 8, !tbaa !19
  %86 = call zeroext i16 @core_bench_matrix(ptr noundef %81, i16 noundef signext %82, i16 noundef zeroext %85)
  store i16 %86, ptr %7, align 2, !tbaa !9
  %87 = load ptr, ptr %5, align 8, !tbaa !5
  %88 = getelementptr inbounds %struct.RESULTS_S, ptr %87, i32 0, i32 11
  %89 = load i16, ptr %88, align 4, !tbaa !21
  %90 = zext i16 %89 to i32
  %91 = icmp eq i32 %90, 0
  br i1 %91, label %92, label %96

92:                                               ; preds = %79
  %93 = load i16, ptr %7, align 2, !tbaa !9
  %94 = load ptr, ptr %5, align 8, !tbaa !5
  %95 = getelementptr inbounds %struct.RESULTS_S, ptr %94, i32 0, i32 11
  store i16 %93, ptr %95, align 4, !tbaa !21
  br label %96

96:                                               ; preds = %92, %79
  br label %99

97:                                               ; preds = %26
  %98 = load i16, ptr %6, align 2, !tbaa !9
  store i16 %98, ptr %7, align 2, !tbaa !9
  br label %99

99:                                               ; preds = %97, %96, %78
  %100 = load i16, ptr %7, align 2, !tbaa !9
  %101 = load ptr, ptr %5, align 8, !tbaa !5
  %102 = getelementptr inbounds %struct.RESULTS_S, ptr %101, i32 0, i32 9
  %103 = load i16, ptr %102, align 8, !tbaa !19
  %104 = call zeroext i16 @crcu16(i16 noundef zeroext %100, i16 noundef zeroext %103)
  %105 = load ptr, ptr %5, align 8, !tbaa !5
  %106 = getelementptr inbounds %struct.RESULTS_S, ptr %105, i32 0, i32 9
  store i16 %104, ptr %106, align 8, !tbaa !19
  %107 = load i16, ptr %7, align 2, !tbaa !9
  %108 = sext i16 %107 to i32
  %109 = and i32 %108, 127
  %110 = trunc i32 %109 to i16
  store i16 %110, ptr %7, align 2, !tbaa !9
  %111 = load i16, ptr %6, align 2, !tbaa !9
  %112 = sext i16 %111 to i32
  %113 = and i32 %112, 65280
  %114 = or i32 %113, 128
  %115 = load i16, ptr %7, align 2, !tbaa !9
  %116 = sext i16 %115 to i32
  %117 = or i32 %114, %116
  %118 = trunc i32 %117 to i16
  %119 = load ptr, ptr %4, align 8, !tbaa !5
  store i16 %118, ptr %119, align 2, !tbaa !9
  %120 = load i16, ptr %7, align 2, !tbaa !9
  store i16 %120, ptr %3, align 2
  store i32 1, ptr %9, align 4
  call void @llvm.lifetime.end.p0(i64 2, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 2, ptr %10) #3
  br label %121

121:                                              ; preds = %99, %21
  call void @llvm.lifetime.end.p0(i64 1, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 2, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 2, ptr %6) #3
  %122 = load i16, ptr %3, align 2
  ret i16 %122
}

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

declare zeroext i16 @core_bench_state(i32 noundef, ptr noundef, i16 noundef signext, i16 noundef signext, i16 noundef signext, i16 noundef zeroext) #2

declare zeroext i16 @core_bench_matrix(ptr noundef, i16 noundef signext, i16 noundef zeroext) #2

declare zeroext i16 @crcu16(i16 noundef zeroext, i16 noundef zeroext) #2

; 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 = { "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 }

!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 = !{!7, !7, i64 0}
!12 = !{!13, !14, i64 40}
!13 = !{!"RESULTS_S", !10, i64 0, !10, i64 2, !10, i64 4, !7, i64 8, !14, i64 40, !14, i64 44, !14, i64 48, !6, i64 56, !15, i64 64, !10, i64 96, !10, i64 98, !10, i64 100, !10, i64 102, !10, i64 104, !16, i64 106}
!14 = !{!"int", !7, i64 0}
!15 = !{!"MAT_PARAMS_S", !14, i64 0, !6, i64 8, !6, i64 16, !6, i64 24}
!16 = !{!"CORE_PORTABLE_S", !7, i64 0}
!17 = !{!13, !10, i64 0}
!18 = !{!13, !10, i64 2}
!19 = !{!13, !10, i64 96}
!20 = !{!13, !10, i64 102}
!21 = !{!13, !10, i64 100}


; 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: nounwind uwtable
define dso_local signext range(i16 0, 128) i16 @calc_func(ptr nocapture noundef %0, ptr noundef %1) local_unnamed_addr #0 {
  %3 = load i16, ptr %0, align 2, !tbaa !5
  %4 = and i16 %3, 128
  %.not = icmp eq i16 %4, 0
  br i1 %.not, label %7, label %5

5:                                                ; preds = %2
  %6 = and i16 %3, 127
  br label %44

7:                                                ; preds = %2
  %8 = and i16 %3, 7
  %9 = lshr i16 %3, 3
  %10 = and i16 %9, 15
  %11 = mul nuw nsw i16 %10, 17
  switch i16 %8, label %36 [
    i16 0, label %12
    i16 1, label %27
  ]

12:                                               ; preds = %7
  %spec.store.select = tail call i16 @llvm.umax.i16(i16 %11, i16 34)
  %13 = getelementptr inbounds i8, ptr %1, i64 40
  %14 = load i32, ptr %13, align 8, !tbaa !9
  %15 = getelementptr inbounds i8, ptr %1, i64 32
  %16 = load ptr, ptr %15, align 8, !tbaa !15
  %17 = load i16, ptr %1, align 8, !tbaa !16
  %18 = getelementptr inbounds i8, ptr %1, i64 2
  %19 = load i16, ptr %18, align 2, !tbaa !17
  %20 = getelementptr inbounds i8, ptr %1, i64 96
  %21 = load i16, ptr %20, align 8, !tbaa !18
  %22 = tail call zeroext i16 @core_bench_state(i32 noundef %14, ptr noundef %16, i16 noundef signext %17, i16 noundef signext %19, i16 noundef signext %spec.store.select, i16 noundef zeroext %21) #3
  %23 = getelementptr inbounds i8, ptr %1, i64 102
  %24 = load i16, ptr %23, align 2, !tbaa !19
  %25 = icmp eq i16 %24, 0
  br i1 %25, label %26, label %36

26:                                               ; preds = %12
  store i16 %22, ptr %23, align 2, !tbaa !19
  br label %36

27:                                               ; preds = %7
  %28 = getelementptr inbounds i8, ptr %1, i64 64
  %29 = getelementptr inbounds i8, ptr %1, i64 96
  %30 = load i16, ptr %29, align 8, !tbaa !18
  %31 = tail call zeroext i16 @core_bench_matrix(ptr noundef nonnull %28, i16 noundef signext %11, i16 noundef zeroext %30) #3
  %32 = getelementptr inbounds i8, ptr %1, i64 100
  %33 = load i16, ptr %32, align 4, !tbaa !20
  %34 = icmp eq i16 %33, 0
  br i1 %34, label %35, label %36

35:                                               ; preds = %27
  store i16 %31, ptr %32, align 4, !tbaa !20
  br label %36

36:                                               ; preds = %7, %27, %35, %12, %26
  %.034 = phi i16 [ %31, %35 ], [ %31, %27 ], [ %22, %26 ], [ %22, %12 ], [ %3, %7 ]
  %37 = getelementptr inbounds i8, ptr %1, i64 96
  %38 = load i16, ptr %37, align 8, !tbaa !18
  %39 = tail call zeroext i16 @crcu16(i16 noundef zeroext %.034, i16 noundef zeroext %38) #3
  store i16 %39, ptr %37, align 8, !tbaa !18
  %40 = and i16 %.034, 127
  %41 = and i16 %3, -256
  %42 = or disjoint i16 %41, %40
  %43 = or disjoint i16 %42, 128
  store i16 %43, ptr %0, align 2, !tbaa !5
  br label %44

44:                                               ; preds = %36, %5
  %.0 = phi i16 [ %6, %5 ], [ %40, %36 ]
  ret i16 %.0
}

declare zeroext i16 @core_bench_state(i32 noundef, ptr noundef, i16 noundef signext, i16 noundef signext, i16 noundef signext, i16 noundef zeroext) local_unnamed_addr #1

declare zeroext i16 @core_bench_matrix(ptr noundef, i16 noundef signext, i16 noundef zeroext) local_unnamed_addr #1

declare zeroext i16 @crcu16(i16 noundef zeroext, i16 noundef zeroext) local_unnamed_addr #1

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i16 @llvm.umax.i16(i16, i16) #2

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 = { "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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #3 = { 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 = !{!"short", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !11, i64 40}
!10 = !{!"RESULTS_S", !6, i64 0, !6, i64 2, !6, i64 4, !7, i64 8, !11, i64 40, !11, i64 44, !11, i64 48, !12, i64 56, !13, i64 64, !6, i64 96, !6, i64 98, !6, i64 100, !6, i64 102, !6, i64 104, !14, i64 106}
!11 = !{!"int", !7, i64 0}
!12 = !{!"any pointer", !7, i64 0}
!13 = !{!"MAT_PARAMS_S", !11, i64 0, !12, i64 8, !12, i64 16, !12, i64 24}
!14 = !{!"CORE_PORTABLE_S", !7, i64 0}
!15 = !{!12, !12, i64 0}
!16 = !{!10, !6, i64 0}
!17 = !{!10, !6, i64 2}
!18 = !{!10, !6, i64 96}
!19 = !{!10, !6, i64 102}
!20 = !{!10, !6, i64 100}


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