; 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.list_data_s = type { i16, i16 }
%struct.list_head_s = type { ptr, ptr }

; 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
declare dso_local i32 @cmp_idx(ptr noundef, ptr noundef, ptr noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local ptr @core_list_mergesort(ptr noundef, ptr noundef, ptr noundef) #1

; Function Attrs: nounwind uwtable
define dso_local ptr @core_list_init(i32 noundef %0, ptr noundef %1, i16 noundef signext %2) #1 {
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca i16, align 2
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca ptr, align 8
  %10 = alloca ptr, align 8
  %11 = alloca ptr, align 8
  %12 = alloca i32, align 4
  %13 = alloca ptr, align 8
  %14 = alloca ptr, align 8
  %15 = alloca %struct.list_data_s, align 2
  %16 = alloca i16, align 2
  %17 = alloca i16, align 2
  %18 = alloca i16, align 2
  store i32 %0, ptr %4, align 4, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !9
  store i16 %2, ptr %6, align 2, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  store i32 20, ptr %7, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #2
  %19 = load i32, ptr %4, align 4, !tbaa !5
  %20 = load i32, ptr %7, align 4, !tbaa !5
  %21 = udiv i32 %19, %20
  %22 = sub i32 %21, 2
  store i32 %22, ptr %8, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #2
  %23 = load ptr, ptr %5, align 8, !tbaa !9
  %24 = load i32, ptr %8, align 4, !tbaa !5
  %25 = zext i32 %24 to i64
  %26 = getelementptr inbounds %struct.list_head_s, ptr %23, i64 %25
  store ptr %26, ptr %9, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #2
  %27 = load ptr, ptr %9, align 8, !tbaa !9
  store ptr %27, ptr %10, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #2
  %28 = load ptr, ptr %10, align 8, !tbaa !9
  %29 = load i32, ptr %8, align 4, !tbaa !5
  %30 = zext i32 %29 to i64
  %31 = getelementptr inbounds %struct.list_data_s, ptr %28, i64 %30
  store ptr %31, ptr %11, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %14) #2
  %32 = load ptr, ptr %5, align 8, !tbaa !9
  store ptr %32, ptr %14, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %15) #2
  %33 = load ptr, ptr %14, align 8, !tbaa !9
  %34 = getelementptr inbounds %struct.list_head_s, ptr %33, i32 0, i32 0
  store ptr null, ptr %34, align 8, !tbaa !13
  %35 = load ptr, ptr %10, align 8, !tbaa !9
  %36 = load ptr, ptr %14, align 8, !tbaa !9
  %37 = getelementptr inbounds %struct.list_head_s, ptr %36, i32 0, i32 1
  store ptr %35, ptr %37, align 8, !tbaa !15
  %38 = load ptr, ptr %14, align 8, !tbaa !9
  %39 = getelementptr inbounds %struct.list_head_s, ptr %38, i32 0, i32 1
  %40 = load ptr, ptr %39, align 8, !tbaa !15
  %41 = getelementptr inbounds %struct.list_data_s, ptr %40, i32 0, i32 1
  store i16 0, ptr %41, align 2, !tbaa !16
  %42 = load ptr, ptr %14, align 8, !tbaa !9
  %43 = getelementptr inbounds %struct.list_head_s, ptr %42, i32 0, i32 1
  %44 = load ptr, ptr %43, align 8, !tbaa !15
  %45 = getelementptr inbounds %struct.list_data_s, ptr %44, i32 0, i32 0
  store i16 -32640, ptr %45, align 2, !tbaa !18
  %46 = load ptr, ptr %5, align 8, !tbaa !9
  %47 = getelementptr inbounds %struct.list_head_s, ptr %46, i32 1
  store ptr %47, ptr %5, align 8, !tbaa !9
  %48 = load ptr, ptr %10, align 8, !tbaa !9
  %49 = getelementptr inbounds %struct.list_data_s, ptr %48, i32 1
  store ptr %49, ptr %10, align 8, !tbaa !9
  %50 = getelementptr inbounds %struct.list_data_s, ptr %15, i32 0, i32 1
  store i16 32767, ptr %50, align 2, !tbaa !16
  %51 = getelementptr inbounds %struct.list_data_s, ptr %15, i32 0, i32 0
  store i16 -1, ptr %51, align 2, !tbaa !18
  %52 = load ptr, ptr %14, align 8, !tbaa !9
  %53 = load ptr, ptr %9, align 8, !tbaa !9
  %54 = load ptr, ptr %11, align 8, !tbaa !9
  %55 = call ptr @core_list_insert_new(ptr noundef %52, ptr noundef %15, ptr noundef %5, ptr noundef %10, ptr noundef %53, ptr noundef %54)
  store i32 0, ptr %12, align 4, !tbaa !5
  br label %56

56:                                               ; preds = %88, %3
  %57 = load i32, ptr %12, align 4, !tbaa !5
  %58 = load i32, ptr %8, align 4, !tbaa !5
  %59 = icmp ult i32 %57, %58
  br i1 %59, label %60, label %91

60:                                               ; preds = %56
  call void @llvm.lifetime.start.p0(i64 2, ptr %16) #2
  %61 = load i16, ptr %6, align 2, !tbaa !11
  %62 = sext i16 %61 to i32
  %63 = load i32, ptr %12, align 4, !tbaa !5
  %64 = xor i32 %62, %63
  %65 = trunc i32 %64 to i16
  %66 = zext i16 %65 to i32
  %67 = and i32 %66, 15
  %68 = trunc i32 %67 to i16
  store i16 %68, ptr %16, align 2, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 2, ptr %17) #2
  %69 = load i16, ptr %16, align 2, !tbaa !11
  %70 = zext i16 %69 to i32
  %71 = shl i32 %70, 3
  %72 = load i32, ptr %12, align 4, !tbaa !5
  %73 = and i32 %72, 7
  %74 = or i32 %71, %73
  %75 = trunc i32 %74 to i16
  store i16 %75, ptr %17, align 2, !tbaa !11
  %76 = load i16, ptr %17, align 2, !tbaa !11
  %77 = zext i16 %76 to i32
  %78 = shl i32 %77, 8
  %79 = load i16, ptr %17, align 2, !tbaa !11
  %80 = zext i16 %79 to i32
  %81 = or i32 %78, %80
  %82 = trunc i32 %81 to i16
  %83 = getelementptr inbounds %struct.list_data_s, ptr %15, i32 0, i32 0
  store i16 %82, ptr %83, align 2, !tbaa !18
  %84 = load ptr, ptr %14, align 8, !tbaa !9
  %85 = load ptr, ptr %9, align 8, !tbaa !9
  %86 = load ptr, ptr %11, align 8, !tbaa !9
  %87 = call ptr @core_list_insert_new(ptr noundef %84, ptr noundef %15, ptr noundef %5, ptr noundef %10, ptr noundef %85, ptr noundef %86)
  call void @llvm.lifetime.end.p0(i64 2, ptr %17) #2
  call void @llvm.lifetime.end.p0(i64 2, ptr %16) #2
  br label %88

88:                                               ; preds = %60
  %89 = load i32, ptr %12, align 4, !tbaa !5
  %90 = add i32 %89, 1
  store i32 %90, ptr %12, align 4, !tbaa !5
  br label %56, !llvm.loop !19

91:                                               ; preds = %56
  %92 = load ptr, ptr %14, align 8, !tbaa !9
  %93 = getelementptr inbounds %struct.list_head_s, ptr %92, i32 0, i32 0
  %94 = load ptr, ptr %93, align 8, !tbaa !13
  store ptr %94, ptr %13, align 8, !tbaa !9
  store i32 1, ptr %12, align 4, !tbaa !5
  br label %95

95:                                               ; preds = %132, %91
  %96 = load ptr, ptr %13, align 8, !tbaa !9
  %97 = getelementptr inbounds %struct.list_head_s, ptr %96, i32 0, i32 0
  %98 = load ptr, ptr %97, align 8, !tbaa !13
  %99 = icmp ne ptr %98, null
  br i1 %99, label %100, label %136

100:                                              ; preds = %95
  %101 = load i32, ptr %12, align 4, !tbaa !5
  %102 = load i32, ptr %8, align 4, !tbaa !5
  %103 = udiv i32 %102, 5
  %104 = icmp ult i32 %101, %103
  br i1 %104, label %105, label %113

105:                                              ; preds = %100
  %106 = load i32, ptr %12, align 4, !tbaa !5
  %107 = add i32 %106, 1
  store i32 %107, ptr %12, align 4, !tbaa !5
  %108 = trunc i32 %106 to i16
  %109 = load ptr, ptr %13, align 8, !tbaa !9
  %110 = getelementptr inbounds %struct.list_head_s, ptr %109, i32 0, i32 1
  %111 = load ptr, ptr %110, align 8, !tbaa !15
  %112 = getelementptr inbounds %struct.list_data_s, ptr %111, i32 0, i32 1
  store i16 %108, ptr %112, align 2, !tbaa !16
  br label %132

113:                                              ; preds = %100
  call void @llvm.lifetime.start.p0(i64 2, ptr %18) #2
  %114 = load i32, ptr %12, align 4, !tbaa !5
  %115 = add i32 %114, 1
  store i32 %115, ptr %12, align 4, !tbaa !5
  %116 = load i16, ptr %6, align 2, !tbaa !11
  %117 = sext i16 %116 to i32
  %118 = xor i32 %114, %117
  %119 = trunc i32 %118 to i16
  store i16 %119, ptr %18, align 2, !tbaa !11
  %120 = load i32, ptr %12, align 4, !tbaa !5
  %121 = and i32 %120, 7
  %122 = shl i32 %121, 8
  %123 = load i16, ptr %18, align 2, !tbaa !11
  %124 = zext i16 %123 to i32
  %125 = or i32 %122, %124
  %126 = and i32 16383, %125
  %127 = trunc i32 %126 to i16
  %128 = load ptr, ptr %13, align 8, !tbaa !9
  %129 = getelementptr inbounds %struct.list_head_s, ptr %128, i32 0, i32 1
  %130 = load ptr, ptr %129, align 8, !tbaa !15
  %131 = getelementptr inbounds %struct.list_data_s, ptr %130, i32 0, i32 1
  store i16 %127, ptr %131, align 2, !tbaa !16
  call void @llvm.lifetime.end.p0(i64 2, ptr %18) #2
  br label %132

132:                                              ; preds = %113, %105
  %133 = load ptr, ptr %13, align 8, !tbaa !9
  %134 = getelementptr inbounds %struct.list_head_s, ptr %133, i32 0, i32 0
  %135 = load ptr, ptr %134, align 8, !tbaa !13
  store ptr %135, ptr %13, align 8, !tbaa !9
  br label %95, !llvm.loop !21

136:                                              ; preds = %95
  %137 = load ptr, ptr %14, align 8, !tbaa !9
  %138 = call ptr @core_list_mergesort(ptr noundef %137, ptr noundef @cmp_idx, ptr noundef null)
  store ptr %138, ptr %14, align 8, !tbaa !9
  %139 = load ptr, ptr %14, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 4, ptr %15) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %14) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  ret ptr %139
}

; Function Attrs: nounwind uwtable
declare dso_local ptr @core_list_insert_new(ptr noundef, ptr noundef, ptr noundef, ptr noundef, ptr noundef, ptr noundef) #1

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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"short", !7, i64 0}
!13 = !{!14, !10, i64 0}
!14 = !{!"list_head_s", !10, i64 0, !10, i64 8}
!15 = !{!14, !10, i64 8}
!16 = !{!17, !12, i64 2}
!17 = !{!"list_data_s", !12, i64 0, !12, i64 2}
!18 = !{!17, !12, i64 0}
!19 = distinct !{!19, !20}
!20 = !{!"llvm.loop.mustprogress"}
!21 = distinct !{!21, !20}


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

%struct.list_data_s = type { i16, i16 }
%struct.list_head_s = type { ptr, ptr }

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

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

; Function Attrs: nounwind uwtable
declare dso_local i32 @cmp_idx(ptr noundef, ptr noundef, ptr noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local ptr @core_list_mergesort(ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local ptr @core_list_init(i32 noundef %0, ptr noundef %1, i16 noundef signext %2) local_unnamed_addr #1 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca %struct.list_data_s, align 2
  %7 = udiv i32 %0, 20
  %8 = add nsw i32 %7, -2
  %9 = zext i32 %8 to i64
  %10 = getelementptr inbounds %struct.list_head_s, ptr %1, i64 %9
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %5) #2
  %11 = getelementptr inbounds %struct.list_data_s, ptr %10, i64 %9
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %6) #2
  store ptr null, ptr %1, align 8, !tbaa !5
  %12 = getelementptr inbounds i8, ptr %1, i64 8
  store ptr %10, ptr %12, align 8, !tbaa !10
  %13 = getelementptr inbounds i8, ptr %10, i64 2
  store i16 0, ptr %13, align 2, !tbaa !11
  store i16 -32640, ptr %10, align 2, !tbaa !14
  %14 = getelementptr inbounds i8, ptr %1, i64 16
  store ptr %14, ptr %4, align 8, !tbaa !15
  %15 = getelementptr inbounds i8, ptr %10, i64 4
  store ptr %15, ptr %5, align 8, !tbaa !15
  %16 = getelementptr inbounds i8, ptr %6, i64 2
  store i16 32767, ptr %16, align 2, !tbaa !11
  store i16 -1, ptr %6, align 2, !tbaa !14
  %17 = call ptr @core_list_insert_new(ptr noundef nonnull %1, ptr noundef nonnull %6, ptr noundef nonnull %4, ptr noundef nonnull %5, ptr noundef nonnull %10, ptr noundef nonnull %11)
  %.not45 = icmp eq i32 %8, 0
  br i1 %.not45, label %.preheader, label %.lr.ph

.preheader:                                       ; preds = %.lr.ph, %3
  %.03740 = load ptr, ptr %1, align 8, !tbaa !5
  %18 = load ptr, ptr %.03740, align 8, !tbaa !5
  %.not41 = icmp eq ptr %18, null
  br i1 %.not41, label %._crit_edge, label %.lr.ph44

.lr.ph44:                                         ; preds = %.preheader
  %19 = udiv i32 %8, 5
  br label %29

.lr.ph:                                           ; preds = %3, %.lr.ph
  %.039 = phi i32 [ %28, %.lr.ph ], [ 0, %3 ]
  %20 = trunc i32 %.039 to i16
  %21 = xor i16 %20, %2
  %22 = shl i16 %21, 3
  %23 = and i16 %22, 120
  %24 = and i16 %20, 7
  %25 = or disjoint i16 %23, %24
  %26 = mul nuw nsw i16 %25, 257
  store i16 %26, ptr %6, align 2, !tbaa !14
  %27 = call ptr @core_list_insert_new(ptr noundef nonnull %1, ptr noundef nonnull %6, ptr noundef nonnull %4, ptr noundef nonnull %5, ptr noundef nonnull %10, ptr noundef nonnull %11)
  %28 = add nuw i32 %.039, 1
  %exitcond.not = icmp eq i32 %28, %8
  br i1 %exitcond.not, label %.preheader, label %.lr.ph, !llvm.loop !16

29:                                               ; preds = %.lr.ph44, %39
  %.037 = phi ptr [ %18, %.lr.ph44 ], [ %43, %39 ]
  %.03743 = phi ptr [ %.03740, %.lr.ph44 ], [ %.037, %39 ]
  %.142 = phi i32 [ 1, %.lr.ph44 ], [ %31, %39 ]
  %30 = icmp ult i32 %.142, %19
  %31 = add i32 %.142, 1
  %32 = trunc i32 %.142 to i16
  br i1 %30, label %39, label %33

33:                                               ; preds = %29
  %34 = xor i16 %32, %2
  %35 = and i16 %34, 16383
  %.tr = trunc i32 %31 to i16
  %36 = shl i16 %.tr, 8
  %37 = and i16 %36, 1792
  %38 = or i16 %37, %35
  br label %39

39:                                               ; preds = %29, %33
  %.sink = phi i16 [ %38, %33 ], [ %32, %29 ]
  %40 = getelementptr inbounds i8, ptr %.03743, i64 8
  %41 = load ptr, ptr %40, align 8, !tbaa !10
  %42 = getelementptr inbounds i8, ptr %41, i64 2
  store i16 %.sink, ptr %42, align 2, !tbaa !11
  %43 = load ptr, ptr %.037, align 8, !tbaa !5
  %.not = icmp eq ptr %43, null
  br i1 %.not, label %._crit_edge, label %29, !llvm.loop !18

._crit_edge:                                      ; preds = %39, %.preheader
  %44 = call ptr @core_list_mergesort(ptr noundef nonnull %1, ptr noundef nonnull @cmp_idx, ptr noundef null)
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %6) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %5) #2
  ret ptr %44
}

; Function Attrs: nounwind uwtable
declare dso_local ptr @core_list_insert_new(ptr noundef, ptr noundef, ptr noundef, ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #1

attributes #0 = { mustprogress 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, !7, i64 0}
!6 = !{!"list_head_s", !7, i64 0, !7, i64 8}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!6, !7, i64 8}
!11 = !{!12, !13, i64 2}
!12 = !{!"list_data_s", !13, i64 0, !13, i64 2}
!13 = !{!"short", !8, i64 0}
!14 = !{!12, !13, i64 0}
!15 = !{!7, !7, i64 0}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}
!18 = distinct !{!18, !17}


; Effective O3 passes: DSEPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,ReassociatePass,SROAPass,SimplifyCFGPass