; 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: nounwind uwtable
define dso_local ptr @core_list_find(ptr noundef %0, ptr noundef %1) #0 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  %6 = load ptr, ptr %5, align 8, !tbaa !5
  %7 = getelementptr inbounds %struct.list_data_s, ptr %6, i32 0, i32 1
  %8 = load i16, ptr %7, align 2, !tbaa !9
  %9 = sext i16 %8 to i32
  %10 = icmp sge i32 %9, 0
  br i1 %10, label %11, label %35

11:                                               ; preds = %2
  br label %12

12:                                               ; preds = %29, %11
  %13 = load ptr, ptr %4, align 8, !tbaa !5
  %14 = icmp ne ptr %13, null
  br i1 %14, label %15, label %27

15:                                               ; preds = %12
  %16 = load ptr, ptr %4, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.list_head_s, ptr %16, i32 0, i32 1
  %18 = load ptr, ptr %17, align 8, !tbaa !12
  %19 = getelementptr inbounds %struct.list_data_s, ptr %18, i32 0, i32 1
  %20 = load i16, ptr %19, align 2, !tbaa !9
  %21 = sext i16 %20 to i32
  %22 = load ptr, ptr %5, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct.list_data_s, ptr %22, i32 0, i32 1
  %24 = load i16, ptr %23, align 2, !tbaa !9
  %25 = sext i16 %24 to i32
  %26 = icmp ne i32 %21, %25
  br label %27

27:                                               ; preds = %15, %12
  %28 = phi i1 [ false, %12 ], [ %26, %15 ]
  br i1 %28, label %29, label %33

29:                                               ; preds = %27
  %30 = load ptr, ptr %4, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.list_head_s, ptr %30, i32 0, i32 0
  %32 = load ptr, ptr %31, align 8, !tbaa !14
  store ptr %32, ptr %4, align 8, !tbaa !5
  br label %12, !llvm.loop !15

33:                                               ; preds = %27
  %34 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %34, ptr %3, align 8
  br label %60

35:                                               ; preds = %2
  br label %36

36:                                               ; preds = %54, %35
  %37 = load ptr, ptr %4, align 8, !tbaa !5
  %38 = icmp ne ptr %37, null
  br i1 %38, label %39, label %52

39:                                               ; preds = %36
  %40 = load ptr, ptr %4, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.list_head_s, ptr %40, i32 0, i32 1
  %42 = load ptr, ptr %41, align 8, !tbaa !12
  %43 = getelementptr inbounds %struct.list_data_s, ptr %42, i32 0, i32 0
  %44 = load i16, ptr %43, align 2, !tbaa !17
  %45 = sext i16 %44 to i32
  %46 = and i32 %45, 255
  %47 = load ptr, ptr %5, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.list_data_s, ptr %47, i32 0, i32 0
  %49 = load i16, ptr %48, align 2, !tbaa !17
  %50 = sext i16 %49 to i32
  %51 = icmp ne i32 %46, %50
  br label %52

52:                                               ; preds = %39, %36
  %53 = phi i1 [ false, %36 ], [ %51, %39 ]
  br i1 %53, label %54, label %58

54:                                               ; preds = %52
  %55 = load ptr, ptr %4, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.list_head_s, ptr %55, i32 0, i32 0
  %57 = load ptr, ptr %56, align 8, !tbaa !14
  store ptr %57, ptr %4, align 8, !tbaa !5
  br label %36, !llvm.loop !18

58:                                               ; preds = %52
  %59 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %59, ptr %3, align 8
  br label %60

60:                                               ; preds = %58, %33
  %61 = load ptr, ptr %3, align 8
  ret ptr %61
}

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

!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, !11, i64 2}
!10 = !{!"list_data_s", !11, i64 0, !11, i64 2}
!11 = !{!"short", !7, i64 0}
!12 = !{!13, !6, i64 8}
!13 = !{!"list_head_s", !6, i64 0, !6, i64 8}
!14 = !{!13, !6, i64 0}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}
!17 = !{!10, !11, i64 0}
!18 = distinct !{!18, !16}


; 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: nofree norecurse nosync nounwind memory(read, inaccessiblemem: none) uwtable
define dso_local ptr @core_list_find(ptr noundef readonly %0, ptr nocapture noundef readonly %1) local_unnamed_addr #0 {
  %3 = getelementptr inbounds i8, ptr %1, i64 2
  %4 = load i16, ptr %3, align 2, !tbaa !5
  %5 = icmp sgt i16 %4, -1
  %.not1625 = icmp eq ptr %0, null
  br i1 %5, label %.preheader, label %.preheader18

.preheader18:                                     ; preds = %2
  br i1 %.not1625, label %.critedge, label %.lr.ph

.lr.ph:                                           ; preds = %.preheader18
  %6 = load i16, ptr %1, align 2, !tbaa !10
  br label %13

.preheader:                                       ; preds = %2
  br i1 %.not1625, label %.critedge, label %.lr.ph27

.lr.ph27:                                         ; preds = %.preheader, %11
  %.01426 = phi ptr [ %12, %11 ], [ %0, %.preheader ]
  %7 = getelementptr inbounds i8, ptr %.01426, i64 8
  %8 = load ptr, ptr %7, align 8, !tbaa !11
  %9 = getelementptr inbounds i8, ptr %8, i64 2
  %10 = load i16, ptr %9, align 2, !tbaa !5
  %.not17 = icmp eq i16 %10, %4
  br i1 %.not17, label %.critedge, label %11

11:                                               ; preds = %.lr.ph27
  %12 = load ptr, ptr %.01426, align 8, !tbaa !14
  %.not16 = icmp eq ptr %12, null
  br i1 %.not16, label %.critedge, label %.lr.ph27, !llvm.loop !15

13:                                               ; preds = %.lr.ph, %18
  %.122 = phi ptr [ %0, %.lr.ph ], [ %19, %18 ]
  %14 = getelementptr inbounds i8, ptr %.122, i64 8
  %15 = load ptr, ptr %14, align 8, !tbaa !11
  %16 = load i16, ptr %15, align 2, !tbaa !10
  %17 = and i16 %16, 255
  %.not15 = icmp eq i16 %17, %6
  br i1 %.not15, label %.critedge, label %18

18:                                               ; preds = %13
  %19 = load ptr, ptr %.122, align 8, !tbaa !14
  %.not = icmp eq ptr %19, null
  br i1 %.not, label %.critedge, label %13, !llvm.loop !17

.critedge:                                        ; preds = %18, %13, %11, %.lr.ph27, %.preheader18, %.preheader
  %.0 = phi ptr [ null, %.preheader ], [ null, %.preheader18 ], [ null, %11 ], [ %.01426, %.lr.ph27 ], [ null, %18 ], [ %.122, %13 ]
  ret ptr %.0
}

attributes #0 = { nofree norecurse nosync nounwind memory(read, inaccessiblemem: none) 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" }

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


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GlobalOptPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass