; LLVM IR from PetoiCamp/OpenCatEsp32 :: src/icm42670 (inv_imu_driver)

; 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.inv_imu_device = type { %struct.inv_imu_transport, ptr, [4128 x i8], i8, i8, i8, i32, i64, i64 }
%struct.inv_imu_transport = type { %struct.inv_imu_serif, %struct.register_cache, i8 }
%struct.inv_imu_serif = type { ptr, ptr, ptr, i32, i32, i32 }
%struct.register_cache = type { i8, i8, i8, i8 }

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

declare void @inv_imu_sleep_us(i32 noundef) #1

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

declare i32 @inv_imu_write_reg(ptr noundef, i32 noundef, i32 noundef, ptr noundef) #1

declare i32 @inv_imu_read_reg(ptr noundef, i32 noundef, i32 noundef, ptr noundef) #1

; Function Attrs: nounwind uwtable
define dso_local i32 @inv_imu_enable_accel_low_power_mode(ptr noundef %0) #2 {
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i8, align 1
  %9 = alloca i8, align 1
  %10 = alloca i8, align 1
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #3
  store i32 0, ptr %3, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  store i32 0, ptr %7, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 1, ptr %8) #3
  call void @llvm.lifetime.start.p0(i64 1, ptr %9) #3
  call void @llvm.lifetime.start.p0(i64 1, ptr %10) #3
  %11 = load ptr, ptr %2, align 8, !tbaa !5
  %12 = call i32 @inv_imu_read_reg(ptr noundef %11, i32 noundef 65567, i32 noundef 1, ptr noundef %8)
  %13 = load i32, ptr %3, align 4, !tbaa !9
  %14 = or i32 %13, %12
  store i32 %14, ptr %3, align 4, !tbaa !9
  %15 = load i8, ptr %8, align 1, !tbaa !11
  %16 = zext i8 %15 to i32
  %17 = and i32 %16, 3
  store i32 %17, ptr %4, align 4, !tbaa !9
  %18 = load i8, ptr %8, align 1, !tbaa !11
  %19 = zext i8 %18 to i32
  %20 = and i32 %19, 12
  store i32 %20, ptr %5, align 4, !tbaa !9
  %21 = load i32, ptr %4, align 4, !tbaa !9
  %22 = icmp ne i32 %21, 2
  br i1 %22, label %23, label %43

23:                                               ; preds = %1
  %24 = load i32, ptr %5, align 4, !tbaa !9
  %25 = icmp eq i32 %24, 0
  br i1 %25, label %29, label %26

26:                                               ; preds = %23
  %27 = load i32, ptr %5, align 4, !tbaa !9
  %28 = icmp eq i32 %27, 4
  br i1 %28, label %29, label %43

29:                                               ; preds = %26, %23
  %30 = load ptr, ptr %2, align 8, !tbaa !5
  %31 = call i32 @inv_imu_read_reg(ptr noundef %30, i32 noundef 65569, i32 noundef 1, ptr noundef %9)
  %32 = load i32, ptr %3, align 4, !tbaa !9
  %33 = or i32 %32, %31
  store i32 %33, ptr %3, align 4, !tbaa !9
  %34 = load i8, ptr %9, align 1, !tbaa !11
  %35 = zext i8 %34 to i32
  %36 = and i32 %35, 15
  store i32 %36, ptr %6, align 4, !tbaa !9
  %37 = load i32, ptr %6, align 4, !tbaa !9
  %38 = call i32 @inv_imu_convert_odr_bitfield_to_us(i32 noundef %37)
  store i32 %38, ptr %7, align 4, !tbaa !9
  %39 = load ptr, ptr %2, align 8, !tbaa !5
  %40 = call i32 @select_rcosc(ptr noundef %39)
  %41 = load i32, ptr %3, align 4, !tbaa !9
  %42 = or i32 %41, %40
  store i32 %42, ptr %3, align 4, !tbaa !9
  br label %43

43:                                               ; preds = %29, %26, %1
  %44 = load ptr, ptr %2, align 8, !tbaa !5
  %45 = call i32 @inv_imu_read_reg(ptr noundef %44, i32 noundef 65567, i32 noundef 1, ptr noundef %8)
  %46 = load i32, ptr %3, align 4, !tbaa !9
  %47 = or i32 %46, %45
  store i32 %47, ptr %3, align 4, !tbaa !9
  %48 = load i8, ptr %8, align 1, !tbaa !11
  %49 = zext i8 %48 to i32
  %50 = and i32 %49, -4
  %51 = trunc i32 %50 to i8
  store i8 %51, ptr %8, align 1, !tbaa !11
  %52 = load i8, ptr %8, align 1, !tbaa !11
  %53 = zext i8 %52 to i32
  %54 = or i32 %53, 2
  %55 = trunc i32 %54 to i8
  store i8 %55, ptr %8, align 1, !tbaa !11
  %56 = load ptr, ptr %2, align 8, !tbaa !5
  %57 = call i32 @inv_imu_write_reg(ptr noundef %56, i32 noundef 65567, i32 noundef 1, ptr noundef %8)
  %58 = load i32, ptr %3, align 4, !tbaa !9
  %59 = or i32 %58, %57
  store i32 %59, ptr %3, align 4, !tbaa !9
  call void @inv_imu_sleep_us(i32 noundef 200)
  %60 = load i32, ptr %4, align 4, !tbaa !9
  %61 = icmp ne i32 %60, 2
  br i1 %61, label %62, label %79

62:                                               ; preds = %43
  %63 = load i32, ptr %5, align 4, !tbaa !9
  %64 = icmp eq i32 %63, 0
  br i1 %64, label %68, label %65

65:                                               ; preds = %62
  %66 = load i32, ptr %5, align 4, !tbaa !9
  %67 = icmp eq i32 %66, 4
  br i1 %67, label %68, label %79

68:                                               ; preds = %65, %62
  %69 = load i32, ptr %7, align 4, !tbaa !9
  %70 = icmp ugt i32 %69, 200
  br i1 %70, label %71, label %74

71:                                               ; preds = %68
  %72 = load i32, ptr %7, align 4, !tbaa !9
  %73 = sub i32 %72, 200
  call void @inv_imu_sleep_us(i32 noundef %73)
  br label %74

74:                                               ; preds = %71, %68
  %75 = load ptr, ptr %2, align 8, !tbaa !5
  %76 = call i32 @select_wuosc(ptr noundef %75)
  %77 = load i32, ptr %3, align 4, !tbaa !9
  %78 = or i32 %77, %76
  store i32 %78, ptr %3, align 4, !tbaa !9
  br label %79

79:                                               ; preds = %74, %65, %43
  %80 = load i32, ptr %4, align 4, !tbaa !9
  %81 = icmp eq i32 %80, 0
  br i1 %81, label %82, label %91

82:                                               ; preds = %79
  %83 = load ptr, ptr %2, align 8, !tbaa !5
  %84 = getelementptr inbounds %struct.inv_imu_device, ptr %83, i32 0, i32 6
  %85 = load i32, ptr %84, align 4, !tbaa !12
  %86 = icmp ne i32 %85, 0
  br i1 %86, label %87, label %91

87:                                               ; preds = %82
  %88 = call i64 @inv_imu_get_time_us()
  %89 = load ptr, ptr %2, align 8, !tbaa !5
  %90 = getelementptr inbounds %struct.inv_imu_device, ptr %89, i32 0, i32 8
  store i64 %88, ptr %90, align 8, !tbaa !18
  br label %91

91:                                               ; preds = %87, %82, %79
  %92 = load ptr, ptr %2, align 8, !tbaa !5
  %93 = call i32 @inv_imu_read_reg(ptr noundef %92, i32 noundef 2, i32 noundef 1, ptr noundef %10)
  %94 = load i32, ptr %3, align 4, !tbaa !9
  %95 = or i32 %94, %93
  store i32 %95, ptr %3, align 4, !tbaa !9
  %96 = load i8, ptr %10, align 1, !tbaa !11
  %97 = zext i8 %96 to i32
  %98 = and i32 %97, -2
  %99 = trunc i32 %98 to i8
  store i8 %99, ptr %10, align 1, !tbaa !11
  %100 = load ptr, ptr %2, align 8, !tbaa !5
  %101 = call i32 @inv_imu_write_reg(ptr noundef %100, i32 noundef 2, i32 noundef 1, ptr noundef %10)
  %102 = load i32, ptr %3, align 4, !tbaa !9
  %103 = or i32 %102, %101
  store i32 %103, ptr %3, align 4, !tbaa !9
  %104 = load i32, ptr %3, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 1, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 1, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 1, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #3
  ret i32 %104
}

; Function Attrs: nounwind uwtable
declare dso_local i32 @inv_imu_convert_odr_bitfield_to_us(i32 noundef) #2

; Function Attrs: nounwind uwtable
declare hidden i32 @select_rcosc(ptr noundef) #2

; Function Attrs: nounwind uwtable
declare hidden i32 @select_wuosc(ptr noundef) #2

declare i64 @inv_imu_get_time_us() #1

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { "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 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 #3 = { 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 = !{!7, !7, i64 0}
!12 = !{!13, !10, i64 4188}
!13 = !{!"inv_imu_device", !14, i64 0, !6, i64 48, !7, i64 56, !7, i64 4184, !7, i64 4185, !7, i64 4186, !10, i64 4188, !17, i64 4192, !17, i64 4200}
!14 = !{!"inv_imu_transport", !15, i64 0, !16, i64 40, !7, i64 44}
!15 = !{!"inv_imu_serif", !6, i64 0, !6, i64 8, !6, i64 16, !10, i64 24, !10, i64 28, !10, i64 32}
!16 = !{!"register_cache", !7, i64 0, !7, i64 1, !7, i64 2, !7, i64 3}
!17 = !{!"long", !7, i64 0}
!18 = !{!13, !17, i64 4200}


; 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: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #0

declare void @inv_imu_sleep_us(i32 noundef) local_unnamed_addr #1

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

declare i32 @inv_imu_write_reg(ptr noundef, i32 noundef, i32 noundef, ptr noundef) local_unnamed_addr #1

declare i32 @inv_imu_read_reg(ptr noundef, i32 noundef, i32 noundef, ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local i32 @inv_imu_enable_accel_low_power_mode(ptr noundef %0) local_unnamed_addr #2 {
  %2 = alloca i8, align 1
  %3 = alloca i8, align 1
  %4 = alloca i8, align 1
  call void @llvm.lifetime.start.p0(i64 1, ptr nonnull %2) #3
  call void @llvm.lifetime.start.p0(i64 1, ptr nonnull %3) #3
  call void @llvm.lifetime.start.p0(i64 1, ptr nonnull %4) #3
  %5 = call i32 @inv_imu_read_reg(ptr noundef %0, i32 noundef 65567, i32 noundef 1, ptr noundef nonnull %2) #3
  %6 = load i8, ptr %2, align 1, !tbaa !5
  %7 = zext i8 %6 to i32
  %8 = and i32 %7, 3
  %.not = icmp ne i32 %8, 2
  %9 = and i32 %7, 8
  %or.cond = icmp eq i32 %9, 0
  %or.cond33 = and i1 %.not, %or.cond
  br i1 %or.cond33, label %10, label %19

10:                                               ; preds = %1
  %11 = call i32 @inv_imu_read_reg(ptr noundef %0, i32 noundef 65569, i32 noundef 1, ptr noundef nonnull %3) #3
  %12 = load i8, ptr %3, align 1, !tbaa !5
  %13 = and i8 %12, 15
  %14 = zext nneg i8 %13 to i32
  %15 = call i32 @inv_imu_convert_odr_bitfield_to_us(i32 noundef %14)
  %16 = call i32 @select_rcosc(ptr noundef %0)
  %17 = or i32 %11, %16
  %18 = or i32 %17, %5
  br label %19

19:                                               ; preds = %10, %1
  %.031 = phi i32 [ %18, %10 ], [ %5, %1 ]
  %.0 = phi i32 [ %15, %10 ], [ 0, %1 ]
  %20 = call i32 @inv_imu_read_reg(ptr noundef %0, i32 noundef 65567, i32 noundef 1, ptr noundef nonnull %2) #3
  %21 = or i32 %20, %.031
  %22 = load i8, ptr %2, align 1, !tbaa !5
  %23 = and i8 %22, -4
  %24 = or disjoint i8 %23, 2
  store i8 %24, ptr %2, align 1, !tbaa !5
  %25 = call i32 @inv_imu_write_reg(ptr noundef %0, i32 noundef 65567, i32 noundef 1, ptr noundef nonnull %2) #3
  %26 = or i32 %21, %25
  call void @inv_imu_sleep_us(i32 noundef 200) #3
  br i1 %or.cond33, label %27, label %34

27:                                               ; preds = %19
  %28 = icmp ugt i32 %.0, 200
  br i1 %28, label %29, label %31

29:                                               ; preds = %27
  %30 = add i32 %.0, -200
  call void @inv_imu_sleep_us(i32 noundef %30) #3
  br label %31

31:                                               ; preds = %29, %27
  %32 = call i32 @select_wuosc(ptr noundef %0)
  %33 = or i32 %32, %26
  br label %34

34:                                               ; preds = %31, %19
  %.1 = phi i32 [ %33, %31 ], [ %26, %19 ]
  %35 = icmp eq i32 %8, 0
  br i1 %35, label %36, label %42

36:                                               ; preds = %34
  %37 = getelementptr inbounds i8, ptr %0, i64 4188
  %38 = load i32, ptr %37, align 4, !tbaa !8
  %.not32 = icmp eq i32 %38, 0
  br i1 %.not32, label %42, label %39

39:                                               ; preds = %36
  %40 = call i64 @inv_imu_get_time_us() #3
  %41 = getelementptr inbounds i8, ptr %0, i64 4200
  store i64 %40, ptr %41, align 8, !tbaa !16
  br label %42

42:                                               ; preds = %39, %36, %34
  %43 = call i32 @inv_imu_read_reg(ptr noundef %0, i32 noundef 2, i32 noundef 1, ptr noundef nonnull %4) #3
  %44 = or i32 %43, %.1
  %45 = load i8, ptr %4, align 1, !tbaa !5
  %46 = and i8 %45, -2
  store i8 %46, ptr %4, align 1, !tbaa !5
  %47 = call i32 @inv_imu_write_reg(ptr noundef %0, i32 noundef 2, i32 noundef 1, ptr noundef nonnull %4) #3
  %48 = or i32 %44, %47
  call void @llvm.lifetime.end.p0(i64 1, ptr nonnull %4) #3
  call void @llvm.lifetime.end.p0(i64 1, ptr nonnull %3) #3
  call void @llvm.lifetime.end.p0(i64 1, ptr nonnull %2) #3
  ret i32 %48
}

; Function Attrs: nounwind uwtable
declare dso_local i32 @inv_imu_convert_odr_bitfield_to_us(i32 noundef) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
declare hidden i32 @select_rcosc(ptr noundef) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
declare hidden i32 @select_wuosc(ptr noundef) local_unnamed_addr #2

declare i64 @inv_imu_get_time_us() local_unnamed_addr #1

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { "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 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 #3 = { 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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = !{!9, !13, i64 4188}
!9 = !{!"inv_imu_device", !10, i64 0, !12, i64 48, !6, i64 56, !6, i64 4184, !6, i64 4185, !6, i64 4186, !13, i64 4188, !15, i64 4192, !15, i64 4200}
!10 = !{!"inv_imu_transport", !11, i64 0, !14, i64 40, !6, i64 44}
!11 = !{!"inv_imu_serif", !12, i64 0, !12, i64 8, !12, i64 16, !13, i64 24, !13, i64 28, !13, i64 32}
!12 = !{!"any pointer", !6, i64 0}
!13 = !{!"int", !6, i64 0}
!14 = !{!"register_cache", !6, i64 0, !6, i64 1, !6, i64 2, !6, i64 3}
!15 = !{!"long", !6, i64 0}
!16 = !{!9, !15, i64 4200}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,ReassociatePass,SROAPass,SimplifyCFGPass