; LLVM IR from cvra/robot-software :: master-firmware/src/can (bus_enumerator)

; 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.bus_enumerator_t = type { ptr, ptr, i16, i16, i16 }
%struct.bus_enumerator_entry_t = type { ptr, i8, 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
define dso_local zeroext i16 @bus_enumerator_discovered_nodes_count(ptr noundef %0) #1 {
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #2
  store i32 0, ptr %4, align 4, !tbaa !9
  store i32 0, ptr %3, align 4, !tbaa !9
  br label %5

5:                                                ; preds = %27, %1
  %6 = load i32, ptr %3, align 4, !tbaa !9
  %7 = load ptr, ptr %2, align 8, !tbaa !5
  %8 = getelementptr inbounds %struct.bus_enumerator_t, ptr %7, i32 0, i32 3
  %9 = load i16, ptr %8, align 2, !tbaa !11
  %10 = zext i16 %9 to i32
  %11 = icmp ult i32 %6, %10
  br i1 %11, label %12, label %30

12:                                               ; preds = %5
  %13 = load ptr, ptr %2, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct.bus_enumerator_t, ptr %13, i32 0, i32 0
  %15 = load ptr, ptr %14, align 8, !tbaa !14
  %16 = load i32, ptr %3, align 4, !tbaa !9
  %17 = zext i32 %16 to i64
  %18 = getelementptr inbounds %struct.bus_enumerator_entry_t, ptr %15, i64 %17
  %19 = getelementptr inbounds %struct.bus_enumerator_entry_t, ptr %18, i32 0, i32 1
  %20 = load i8, ptr %19, align 8, !tbaa !15
  %21 = zext i8 %20 to i32
  %22 = icmp ne i32 %21, 255
  br i1 %22, label %23, label %26

23:                                               ; preds = %12
  %24 = load i32, ptr %4, align 4, !tbaa !9
  %25 = add i32 %24, 1
  store i32 %25, ptr %4, align 4, !tbaa !9
  br label %26

26:                                               ; preds = %23, %12
  br label %27

27:                                               ; preds = %26
  %28 = load i32, ptr %3, align 4, !tbaa !9
  %29 = add i32 %28, 1
  store i32 %29, ptr %3, align 4, !tbaa !9
  br label %5, !llvm.loop !17

30:                                               ; preds = %5
  %31 = load i32, ptr %4, align 4, !tbaa !9
  %32 = trunc i32 %31 to i16
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #2
  ret i16 %32
}

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

%struct.bus_enumerator_entry_t = type { ptr, i8, ptr }

; Function Attrs: nofree norecurse nosync nounwind memory(read, inaccessiblemem: none) uwtable
define dso_local zeroext i16 @bus_enumerator_discovered_nodes_count(ptr nocapture noundef readonly %0) local_unnamed_addr #0 {
  %2 = getelementptr inbounds i8, ptr %0, i64 18
  %3 = load i16, ptr %2, align 2, !tbaa !5
  %.not9 = icmp eq i16 %3, 0
  br i1 %.not9, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %1
  %4 = load ptr, ptr %0, align 8, !tbaa !11
  %wide.trip.count = zext i16 %3 to i64
  %min.iters.check = icmp ult i16 %3, 16
  br i1 %min.iters.check, label %scalar.ph.preheader, label %vector.ph

vector.ph:                                        ; preds = %.lr.ph
  %n.vec = and i64 %wide.trip.count, 65520
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %vec.phi = phi <8 x i16> [ zeroinitializer, %vector.ph ], [ %72, %vector.body ]
  %vec.phi10 = phi <8 x i16> [ zeroinitializer, %vector.ph ], [ %73, %vector.body ]
  %5 = or disjoint i64 %index, 1
  %6 = or disjoint i64 %index, 2
  %7 = or disjoint i64 %index, 3
  %8 = or disjoint i64 %index, 4
  %9 = or disjoint i64 %index, 5
  %10 = or disjoint i64 %index, 6
  %11 = or disjoint i64 %index, 7
  %12 = or disjoint i64 %index, 8
  %13 = or disjoint i64 %index, 9
  %14 = or disjoint i64 %index, 10
  %15 = or disjoint i64 %index, 11
  %16 = or disjoint i64 %index, 12
  %17 = or disjoint i64 %index, 13
  %18 = or disjoint i64 %index, 14
  %19 = or disjoint i64 %index, 15
  %20 = getelementptr inbounds %struct.bus_enumerator_entry_t, ptr %4, i64 %index, i32 1
  %21 = getelementptr inbounds %struct.bus_enumerator_entry_t, ptr %4, i64 %5, i32 1
  %22 = getelementptr inbounds %struct.bus_enumerator_entry_t, ptr %4, i64 %6, i32 1
  %23 = getelementptr inbounds %struct.bus_enumerator_entry_t, ptr %4, i64 %7, i32 1
  %24 = getelementptr inbounds %struct.bus_enumerator_entry_t, ptr %4, i64 %8, i32 1
  %25 = getelementptr inbounds %struct.bus_enumerator_entry_t, ptr %4, i64 %9, i32 1
  %26 = getelementptr inbounds %struct.bus_enumerator_entry_t, ptr %4, i64 %10, i32 1
  %27 = getelementptr inbounds %struct.bus_enumerator_entry_t, ptr %4, i64 %11, i32 1
  %28 = getelementptr inbounds %struct.bus_enumerator_entry_t, ptr %4, i64 %12, i32 1
  %29 = getelementptr inbounds %struct.bus_enumerator_entry_t, ptr %4, i64 %13, i32 1
  %30 = getelementptr inbounds %struct.bus_enumerator_entry_t, ptr %4, i64 %14, i32 1
  %31 = getelementptr inbounds %struct.bus_enumerator_entry_t, ptr %4, i64 %15, i32 1
  %32 = getelementptr inbounds %struct.bus_enumerator_entry_t, ptr %4, i64 %16, i32 1
  %33 = getelementptr inbounds %struct.bus_enumerator_entry_t, ptr %4, i64 %17, i32 1
  %34 = getelementptr inbounds %struct.bus_enumerator_entry_t, ptr %4, i64 %18, i32 1
  %35 = getelementptr inbounds %struct.bus_enumerator_entry_t, ptr %4, i64 %19, i32 1
  %36 = load i8, ptr %20, align 8, !tbaa !12
  %37 = load i8, ptr %21, align 8, !tbaa !12
  %38 = load i8, ptr %22, align 8, !tbaa !12
  %39 = load i8, ptr %23, align 8, !tbaa !12
  %40 = load i8, ptr %24, align 8, !tbaa !12
  %41 = load i8, ptr %25, align 8, !tbaa !12
  %42 = load i8, ptr %26, align 8, !tbaa !12
  %43 = load i8, ptr %27, align 8, !tbaa !12
  %44 = insertelement <8 x i8> poison, i8 %36, i64 0
  %45 = insertelement <8 x i8> %44, i8 %37, i64 1
  %46 = insertelement <8 x i8> %45, i8 %38, i64 2
  %47 = insertelement <8 x i8> %46, i8 %39, i64 3
  %48 = insertelement <8 x i8> %47, i8 %40, i64 4
  %49 = insertelement <8 x i8> %48, i8 %41, i64 5
  %50 = insertelement <8 x i8> %49, i8 %42, i64 6
  %51 = insertelement <8 x i8> %50, i8 %43, i64 7
  %52 = load i8, ptr %28, align 8, !tbaa !12
  %53 = load i8, ptr %29, align 8, !tbaa !12
  %54 = load i8, ptr %30, align 8, !tbaa !12
  %55 = load i8, ptr %31, align 8, !tbaa !12
  %56 = load i8, ptr %32, align 8, !tbaa !12
  %57 = load i8, ptr %33, align 8, !tbaa !12
  %58 = load i8, ptr %34, align 8, !tbaa !12
  %59 = load i8, ptr %35, align 8, !tbaa !12
  %60 = insertelement <8 x i8> poison, i8 %52, i64 0
  %61 = insertelement <8 x i8> %60, i8 %53, i64 1
  %62 = insertelement <8 x i8> %61, i8 %54, i64 2
  %63 = insertelement <8 x i8> %62, i8 %55, i64 3
  %64 = insertelement <8 x i8> %63, i8 %56, i64 4
  %65 = insertelement <8 x i8> %64, i8 %57, i64 5
  %66 = insertelement <8 x i8> %65, i8 %58, i64 6
  %67 = insertelement <8 x i8> %66, i8 %59, i64 7
  %68 = icmp ne <8 x i8> %51, <i8 -1, i8 -1, i8 -1, i8 -1, i8 -1, i8 -1, i8 -1, i8 -1>
  %69 = icmp ne <8 x i8> %67, <i8 -1, i8 -1, i8 -1, i8 -1, i8 -1, i8 -1, i8 -1, i8 -1>
  %70 = zext <8 x i1> %68 to <8 x i16>
  %71 = zext <8 x i1> %69 to <8 x i16>
  %72 = add <8 x i16> %vec.phi, %70
  %73 = add <8 x i16> %vec.phi10, %71
  %index.next = add nuw i64 %index, 16
  %74 = icmp eq i64 %index.next, %n.vec
  br i1 %74, label %middle.block, label %vector.body, !llvm.loop !14

middle.block:                                     ; preds = %vector.body
  %bin.rdx = add <8 x i16> %73, %72
  %75 = tail call i16 @llvm.vector.reduce.add.v8i16(<8 x i16> %bin.rdx)
  %cmp.n = icmp eq i64 %n.vec, %wide.trip.count
  br i1 %cmp.n, label %._crit_edge, label %scalar.ph.preheader

scalar.ph.preheader:                              ; preds = %middle.block, %.lr.ph
  %indvars.iv.ph = phi i64 [ 0, %.lr.ph ], [ %n.vec, %middle.block ]
  %.08.ph = phi i16 [ 0, %.lr.ph ], [ %75, %middle.block ]
  br label %scalar.ph

scalar.ph:                                        ; preds = %scalar.ph.preheader, %scalar.ph
  %indvars.iv = phi i64 [ %indvars.iv.next, %scalar.ph ], [ %indvars.iv.ph, %scalar.ph.preheader ]
  %.08 = phi i16 [ %spec.select, %scalar.ph ], [ %.08.ph, %scalar.ph.preheader ]
  %76 = getelementptr inbounds %struct.bus_enumerator_entry_t, ptr %4, i64 %indvars.iv, i32 1
  %77 = load i8, ptr %76, align 8, !tbaa !12
  %.not = icmp ne i8 %77, -1
  %78 = zext i1 %.not to i16
  %spec.select = add i16 %.08, %78
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count
  br i1 %exitcond.not, label %._crit_edge, label %scalar.ph, !llvm.loop !18

._crit_edge:                                      ; preds = %scalar.ph, %middle.block, %1
  %.0.lcssa = phi i16 [ 0, %1 ], [ %75, %middle.block ], [ %spec.select, %scalar.ph ]
  ret i16 %.0.lcssa
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i16 @llvm.vector.reduce.add.v8i16(<8 x i16>) #1

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" }
attributes #1 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }

!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 18}
!6 = !{!"", !7, i64 0, !7, i64 8, !10, i64 16, !10, i64 18, !10, i64 20}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"short", !8, i64 0}
!11 = !{!6, !7, i64 0}
!12 = !{!13, !8, i64 8}
!13 = !{!"", !7, i64 0, !8, i64 8, !7, i64 16}
!14 = distinct !{!14, !15, !16, !17}
!15 = !{!"llvm.loop.mustprogress"}
!16 = !{!"llvm.loop.isvectorized", i32 1}
!17 = !{!"llvm.loop.unroll.runtime.disable"}
!18 = distinct !{!18, !15, !17, !16}


; Effective O3 passes: AggressiveInstCombinePass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopLoadEliminationPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,LoopVectorizePass,PostOrderFunctionAttrsPass,ReassociatePass,SCCPPass,SROAPass,SimplifyCFGPass,TailCallElimPass