; LLVM IR from IOsonata/IOsonata :: src/sensors (temp_ntc)

; 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.__Ntc_Temp_Res = type { i32, i32 }

; Function Attrs: mustprogress nounwind uwtable
define dso_local i32 @LookupNtcTemp(i32 noundef %0, ptr noundef %1, i64 noundef %2) #0 {
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  store i32 %0, ptr %4, align 4, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !9
  store i64 %2, ptr %6, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  %11 = load i64, ptr %6, align 8, !tbaa !11
  %12 = lshr i64 %11, 1
  %13 = trunc i64 %12 to i32
  store i32 %13, ptr %7, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #2
  %14 = load i64, ptr %6, align 8, !tbaa !11
  %15 = trunc i64 %14 to i32
  store i32 %15, ptr %8, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #2
  store i32 0, ptr %9, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #2
  store i32 20, ptr %10, align 4, !tbaa !5
  br label %16

16:                                               ; preds = %42, %3
  %17 = load ptr, ptr %5, align 8, !tbaa !9
  %18 = load i32, ptr %7, align 4, !tbaa !5
  %19 = sext i32 %18 to i64
  %20 = getelementptr inbounds %struct.__Ntc_Temp_Res, ptr %17, i64 %19
  %21 = getelementptr inbounds %struct.__Ntc_Temp_Res, ptr %20, i32 0, i32 1
  %22 = load i32, ptr %21, align 4, !tbaa !13
  %23 = load i32, ptr %4, align 4, !tbaa !5
  %24 = icmp uge i32 %22, %23
  br i1 %24, label %25, label %27

25:                                               ; preds = %16
  %26 = load i32, ptr %7, align 4, !tbaa !5
  store i32 %26, ptr %9, align 4, !tbaa !5
  br label %29

27:                                               ; preds = %16
  %28 = load i32, ptr %7, align 4, !tbaa !5
  store i32 %28, ptr %8, align 4, !tbaa !5
  br label %29

29:                                               ; preds = %27, %25
  %30 = load i32, ptr %8, align 4, !tbaa !5
  %31 = load i32, ptr %9, align 4, !tbaa !5
  %32 = add nsw i32 %30, %31
  %33 = ashr i32 %32, 1
  store i32 %33, ptr %7, align 4, !tbaa !5
  br label %34

34:                                               ; preds = %29
  %35 = load i32, ptr %7, align 4, !tbaa !5
  %36 = load i32, ptr %9, align 4, !tbaa !5
  %37 = icmp ne i32 %35, %36
  br i1 %37, label %38, label %42

38:                                               ; preds = %34
  %39 = load i32, ptr %7, align 4, !tbaa !5
  %40 = load i32, ptr %8, align 4, !tbaa !5
  %41 = icmp ne i32 %39, %40
  br label %42

42:                                               ; preds = %38, %34
  %43 = phi i1 [ false, %34 ], [ %41, %38 ]
  br i1 %43, label %16, label %44, !llvm.loop !15

44:                                               ; preds = %42
  %45 = load ptr, ptr %5, align 8, !tbaa !9
  %46 = load i32, ptr %9, align 4, !tbaa !5
  %47 = sext i32 %46 to i64
  %48 = getelementptr inbounds %struct.__Ntc_Temp_Res, ptr %45, i64 %47
  %49 = getelementptr inbounds %struct.__Ntc_Temp_Res, ptr %48, i32 0, i32 0
  %50 = load i32, ptr %49, align 4, !tbaa !17
  %51 = load ptr, ptr %5, align 8, !tbaa !9
  %52 = load i32, ptr %8, align 4, !tbaa !5
  %53 = sext i32 %52 to i64
  %54 = getelementptr inbounds %struct.__Ntc_Temp_Res, ptr %51, i64 %53
  %55 = getelementptr inbounds %struct.__Ntc_Temp_Res, ptr %54, i32 0, i32 0
  %56 = load i32, ptr %55, align 4, !tbaa !17
  %57 = icmp sgt i32 %50, %56
  br i1 %57, label %58, label %65

58:                                               ; preds = %44
  %59 = load ptr, ptr %5, align 8, !tbaa !9
  %60 = load i32, ptr %9, align 4, !tbaa !5
  %61 = sext i32 %60 to i64
  %62 = getelementptr inbounds %struct.__Ntc_Temp_Res, ptr %59, i64 %61
  %63 = getelementptr inbounds %struct.__Ntc_Temp_Res, ptr %62, i32 0, i32 0
  %64 = load i32, ptr %63, align 4, !tbaa !17
  br label %72

65:                                               ; preds = %44
  %66 = load ptr, ptr %5, align 8, !tbaa !9
  %67 = load i32, ptr %8, align 4, !tbaa !5
  %68 = sext i32 %67 to i64
  %69 = getelementptr inbounds %struct.__Ntc_Temp_Res, ptr %66, i64 %68
  %70 = getelementptr inbounds %struct.__Ntc_Temp_Res, ptr %69, i32 0, i32 0
  %71 = load i32, ptr %70, align 4, !tbaa !17
  br label %72

72:                                               ; preds = %65, %58
  %73 = phi i32 [ %64, %58 ], [ %71, %65 ]
  store i32 %73, ptr %10, align 4, !tbaa !5
  %74 = load i32, ptr %10, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #2
  call void @llvm.lifetime.end.p0(i64 4, 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 i32 %74
}

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

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

attributes #0 = { mustprogress 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 }

!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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = !{!14, !6, i64 4}
!14 = !{!"_ZTS14__Ntc_Temp_Res", !6, i64 0, !6, i64 4}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}
!17 = !{!14, !6, i64 0}


; 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.__Ntc_Temp_Res = type { i32, i32 }

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(argmem: read) uwtable
define dso_local i32 @LookupNtcTemp(i32 noundef %0, ptr nocapture noundef readonly %1, i64 noundef %2) local_unnamed_addr #0 {
  %4 = lshr i64 %2, 1
  %5 = trunc i64 %4 to i32
  %6 = trunc i64 %2 to i32
  br label %7

7:                                                ; preds = %7, %3
  %.023 = phi i32 [ %5, %3 ], [ %12, %7 ]
  %.021 = phi i32 [ %6, %3 ], [ %.023..021, %7 ]
  %.0 = phi i32 [ 0, %3 ], [ %.0..023, %7 ]
  %8 = sext i32 %.023 to i64
  %9 = getelementptr inbounds %struct.__Ntc_Temp_Res, ptr %1, i64 %8, i32 1
  %10 = load i32, ptr %9, align 4, !tbaa !5
  %.not = icmp ult i32 %10, %0
  %.023..021 = select i1 %.not, i32 %.023, i32 %.021
  %.0..023 = select i1 %.not, i32 %.0, i32 %.023
  %11 = add nsw i32 %.023..021, %.0..023
  %12 = ashr i32 %11, 1
  %13 = icmp ne i32 %12, %.0..023
  %14 = icmp ne i32 %12, %.023..021
  %15 = and i1 %13, %14
  br i1 %15, label %7, label %16, !llvm.loop !10

16:                                               ; preds = %7
  %17 = sext i32 %.0..023 to i64
  %18 = getelementptr inbounds %struct.__Ntc_Temp_Res, ptr %1, i64 %17
  %19 = load i32, ptr %18, align 4, !tbaa !12
  %20 = sext i32 %.023..021 to i64
  %21 = getelementptr inbounds %struct.__Ntc_Temp_Res, ptr %1, i64 %20
  %22 = load i32, ptr %21, align 4, !tbaa !12
  %. = tail call i32 @llvm.smax.i32(i32 %19, i32 %22)
  ret i32 %.
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.smax.i32(i32, i32) #1

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(argmem: read) 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, !7, i64 4}
!6 = !{!"_ZTS14__Ntc_Temp_Res", !7, i64 0, !7, i64 4}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = distinct !{!10, !11}
!11 = !{!"llvm.loop.mustprogress"}
!12 = !{!6, !7, i64 0}


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