; 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: nounwind uwtable
define dso_local void @bus_enumerator_add_node(ptr noundef %0, ptr noundef %1, ptr noundef %2) #0 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i16, align 2
  %8 = alloca i16, align 2
  %9 = alloca i16, align 2
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  store ptr %2, ptr %6, align 8, !tbaa !5
  %10 = load ptr, ptr %4, align 8, !tbaa !5
  %11 = getelementptr inbounds %struct.bus_enumerator_t, ptr %10, i32 0, i32 3
  %12 = load i16, ptr %11, align 2, !tbaa !9
  %13 = zext i16 %12 to i32
  %14 = load ptr, ptr %4, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct.bus_enumerator_t, ptr %14, i32 0, i32 2
  %16 = load i16, ptr %15, align 8, !tbaa !12
  %17 = zext i16 %16 to i32
  %18 = icmp slt i32 %13, %17
  br i1 %18, label %19, label %103

19:                                               ; preds = %3
  call void @llvm.lifetime.start.p0(i64 2, ptr %7) #4
  store i16 0, ptr %7, align 2, !tbaa !13
  call void @llvm.lifetime.start.p0(i64 2, ptr %8) #4
  %20 = load ptr, ptr %4, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.bus_enumerator_t, ptr %20, i32 0, i32 3
  %22 = load i16, ptr %21, align 2, !tbaa !9
  store i16 %22, ptr %8, align 2, !tbaa !13
  call void @llvm.lifetime.start.p0(i64 2, ptr %9) #4
  br label %23

23:                                               ; preds = %51, %19
  %24 = load i16, ptr %7, align 2, !tbaa !13
  %25 = zext i16 %24 to i32
  %26 = load i16, ptr %8, align 2, !tbaa !13
  %27 = zext i16 %26 to i32
  %28 = icmp ne i32 %25, %27
  br i1 %28, label %29, label %52

29:                                               ; preds = %23
  %30 = load i16, ptr %7, align 2, !tbaa !13
  %31 = load i16, ptr %8, align 2, !tbaa !13
  %32 = call zeroext i16 @midpoint(i16 noundef zeroext %30, i16 noundef zeroext %31)
  store i16 %32, ptr %9, align 2, !tbaa !13
  %33 = load ptr, ptr %5, align 8, !tbaa !5
  %34 = load ptr, ptr %4, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.bus_enumerator_t, ptr %34, i32 0, i32 0
  %36 = load ptr, ptr %35, align 8, !tbaa !14
  %37 = load i16, ptr %9, align 2, !tbaa !13
  %38 = zext i16 %37 to i64
  %39 = getelementptr inbounds %struct.bus_enumerator_entry_t, ptr %36, i64 %38
  %40 = getelementptr inbounds %struct.bus_enumerator_entry_t, ptr %39, i32 0, i32 0
  %41 = load ptr, ptr %40, align 8, !tbaa !15
  %42 = call i32 @strcmp(ptr noundef %33, ptr noundef %41) #5
  %43 = icmp sgt i32 %42, 0
  br i1 %43, label %44, label %49

44:                                               ; preds = %29
  %45 = load i16, ptr %9, align 2, !tbaa !13
  %46 = zext i16 %45 to i32
  %47 = add nsw i32 %46, 1
  %48 = trunc i32 %47 to i16
  store i16 %48, ptr %7, align 2, !tbaa !13
  br label %51

49:                                               ; preds = %29
  %50 = load i16, ptr %9, align 2, !tbaa !13
  store i16 %50, ptr %8, align 2, !tbaa !13
  br label %51

51:                                               ; preds = %49, %44
  br label %23, !llvm.loop !17

52:                                               ; preds = %23
  %53 = load ptr, ptr %4, align 8, !tbaa !5
  %54 = getelementptr inbounds %struct.bus_enumerator_t, ptr %53, i32 0, i32 0
  %55 = load ptr, ptr %54, align 8, !tbaa !14
  %56 = load i16, ptr %7, align 2, !tbaa !13
  %57 = zext i16 %56 to i32
  %58 = add nsw i32 %57, 1
  %59 = sext i32 %58 to i64
  %60 = getelementptr inbounds %struct.bus_enumerator_entry_t, ptr %55, i64 %59
  %61 = load ptr, ptr %4, align 8, !tbaa !5
  %62 = getelementptr inbounds %struct.bus_enumerator_t, ptr %61, i32 0, i32 0
  %63 = load ptr, ptr %62, align 8, !tbaa !14
  %64 = load i16, ptr %7, align 2, !tbaa !13
  %65 = zext i16 %64 to i64
  %66 = getelementptr inbounds %struct.bus_enumerator_entry_t, ptr %63, i64 %65
  %67 = load ptr, ptr %4, align 8, !tbaa !5
  %68 = getelementptr inbounds %struct.bus_enumerator_t, ptr %67, i32 0, i32 3
  %69 = load i16, ptr %68, align 2, !tbaa !9
  %70 = zext i16 %69 to i32
  %71 = load i16, ptr %7, align 2, !tbaa !13
  %72 = zext i16 %71 to i32
  %73 = sub nsw i32 %70, %72
  %74 = sext i32 %73 to i64
  %75 = mul i64 %74, 24
  call void @llvm.memmove.p0.p0.i64(ptr align 8 %60, ptr align 8 %66, i64 %75, i1 false)
  %76 = load ptr, ptr %5, align 8, !tbaa !5
  %77 = load ptr, ptr %4, align 8, !tbaa !5
  %78 = getelementptr inbounds %struct.bus_enumerator_t, ptr %77, i32 0, i32 0
  %79 = load ptr, ptr %78, align 8, !tbaa !14
  %80 = load i16, ptr %7, align 2, !tbaa !13
  %81 = zext i16 %80 to i64
  %82 = getelementptr inbounds %struct.bus_enumerator_entry_t, ptr %79, i64 %81
  %83 = getelementptr inbounds %struct.bus_enumerator_entry_t, ptr %82, i32 0, i32 0
  store ptr %76, ptr %83, align 8, !tbaa !15
  %84 = load ptr, ptr %6, align 8, !tbaa !5
  %85 = load ptr, ptr %4, align 8, !tbaa !5
  %86 = getelementptr inbounds %struct.bus_enumerator_t, ptr %85, i32 0, i32 0
  %87 = load ptr, ptr %86, align 8, !tbaa !14
  %88 = load i16, ptr %7, align 2, !tbaa !13
  %89 = zext i16 %88 to i64
  %90 = getelementptr inbounds %struct.bus_enumerator_entry_t, ptr %87, i64 %89
  %91 = getelementptr inbounds %struct.bus_enumerator_entry_t, ptr %90, i32 0, i32 2
  store ptr %84, ptr %91, align 8, !tbaa !19
  %92 = load ptr, ptr %4, align 8, !tbaa !5
  %93 = getelementptr inbounds %struct.bus_enumerator_t, ptr %92, i32 0, i32 0
  %94 = load ptr, ptr %93, align 8, !tbaa !14
  %95 = load i16, ptr %7, align 2, !tbaa !13
  %96 = zext i16 %95 to i64
  %97 = getelementptr inbounds %struct.bus_enumerator_entry_t, ptr %94, i64 %96
  %98 = getelementptr inbounds %struct.bus_enumerator_entry_t, ptr %97, i32 0, i32 1
  store i8 -1, ptr %98, align 8, !tbaa !20
  %99 = load ptr, ptr %4, align 8, !tbaa !5
  %100 = getelementptr inbounds %struct.bus_enumerator_t, ptr %99, i32 0, i32 3
  %101 = load i16, ptr %100, align 2, !tbaa !9
  %102 = add i16 %101, 1
  store i16 %102, ptr %100, align 2, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 2, ptr %9) #4
  call void @llvm.lifetime.end.p0(i64 2, ptr %8) #4
  call void @llvm.lifetime.end.p0(i64 2, ptr %7) #4
  br label %103

103:                                              ; preds = %52, %3
  ret void
}

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

; Function Attrs: nounwind uwtable
declare hidden zeroext i16 @midpoint(i16 noundef zeroext, i16 noundef zeroext) #0

; Function Attrs: nounwind willreturn memory(read)
declare i32 @strcmp(ptr noundef, ptr noundef) #2

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

; 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 = { nounwind willreturn memory(read) "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: readwrite) }
attributes #4 = { nounwind }
attributes #5 = { nounwind willreturn memory(read) }

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


; 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: nounwind uwtable
define dso_local void @bus_enumerator_add_node(ptr nocapture noundef %0, ptr noundef %1, ptr noundef %2) local_unnamed_addr #0 {
  %4 = getelementptr inbounds i8, ptr %0, i64 18
  %5 = load i16, ptr %4, align 2, !tbaa !5
  %6 = getelementptr inbounds i8, ptr %0, i64 16
  %7 = load i16, ptr %6, align 8, !tbaa !11
  %8 = icmp ult i16 %5, %7
  br i1 %8, label %.preheader, label %31

.preheader:                                       ; preds = %3
  %.not29 = icmp eq i16 %5, 0
  br i1 %.not29, label %.preheader.._crit_edge_crit_edge, label %.lr.ph

.preheader.._crit_edge_crit_edge:                 ; preds = %.preheader
  %.pre = load ptr, ptr %0, align 8, !tbaa !12
  br label %._crit_edge

.lr.ph:                                           ; preds = %.preheader, %.lr.ph
  %.031 = phi i16 [ %.1, %.lr.ph ], [ 0, %.preheader ]
  %.02630 = phi i16 [ %.127, %.lr.ph ], [ %5, %.preheader ]
  %9 = tail call zeroext i16 @midpoint(i16 noundef zeroext %.031, i16 noundef zeroext %.02630)
  %10 = load ptr, ptr %0, align 8, !tbaa !12
  %11 = zext i16 %9 to i64
  %12 = getelementptr inbounds %struct.bus_enumerator_entry_t, ptr %10, i64 %11
  %13 = load ptr, ptr %12, align 8, !tbaa !13
  %14 = tail call i32 @strcmp(ptr noundef nonnull dereferenceable(1) %1, ptr noundef nonnull dereferenceable(1) %13) #3
  %15 = icmp sgt i32 %14, 0
  %16 = add i16 %9, 1
  %.127 = select i1 %15, i16 %.02630, i16 %9
  %.1 = select i1 %15, i16 %16, i16 %.031
  %.not = icmp eq i16 %.1, %.127
  br i1 %.not, label %._crit_edge.loopexit, label %.lr.ph, !llvm.loop !15

._crit_edge.loopexit:                             ; preds = %.lr.ph
  %.pre32 = load i16, ptr %4, align 2, !tbaa !5
  %17 = zext i16 %.127 to i64
  %18 = zext i16 %.pre32 to i64
  br label %._crit_edge

._crit_edge:                                      ; preds = %.preheader.._crit_edge_crit_edge, %._crit_edge.loopexit
  %19 = phi i64 [ 0, %.preheader.._crit_edge_crit_edge ], [ %18, %._crit_edge.loopexit ]
  %20 = phi ptr [ %.pre, %.preheader.._crit_edge_crit_edge ], [ %10, %._crit_edge.loopexit ]
  %.0.lcssa = phi i64 [ 0, %.preheader.._crit_edge_crit_edge ], [ %17, %._crit_edge.loopexit ]
  %21 = getelementptr inbounds %struct.bus_enumerator_entry_t, ptr %20, i64 %.0.lcssa
  %22 = getelementptr inbounds i8, ptr %21, i64 24
  %23 = sub nsw i64 %19, %.0.lcssa
  %24 = mul nsw i64 %23, 24
  tail call void @llvm.memmove.p0.p0.i64(ptr nonnull align 8 %22, ptr align 8 %21, i64 %24, i1 false)
  %25 = load ptr, ptr %0, align 8, !tbaa !12
  %26 = getelementptr inbounds %struct.bus_enumerator_entry_t, ptr %25, i64 %.0.lcssa
  store ptr %1, ptr %26, align 8, !tbaa !13
  %27 = getelementptr inbounds %struct.bus_enumerator_entry_t, ptr %25, i64 %.0.lcssa, i32 2
  store ptr %2, ptr %27, align 8, !tbaa !17
  %28 = getelementptr inbounds %struct.bus_enumerator_entry_t, ptr %25, i64 %.0.lcssa, i32 1
  store i8 -1, ptr %28, align 8, !tbaa !18
  %29 = load i16, ptr %4, align 2, !tbaa !5
  %30 = add i16 %29, 1
  store i16 %30, ptr %4, align 2, !tbaa !5
  br label %31

31:                                               ; preds = %._crit_edge, %3
  ret void
}

; Function Attrs: nounwind uwtable
declare hidden zeroext i16 @midpoint(i16 noundef zeroext, i16 noundef zeroext) local_unnamed_addr #0

; Function Attrs: mustprogress nofree nounwind willreturn memory(argmem: read)
declare i32 @strcmp(ptr nocapture noundef, ptr nocapture noundef) local_unnamed_addr #1

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memmove.p0.p0.i64(ptr nocapture writeonly, ptr nocapture readonly, i64, i1 immarg) #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 = { mustprogress nofree nounwind willreturn memory(argmem: read) "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: readwrite) }
attributes #3 = { nounwind willreturn memory(read) }

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


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass