; LLVM IR from atabakd/MuJoCo-Tutorials :: src/0_preliminaries/pd.ll (Z10mouse_moveP10GLFWwindowdd)

; 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._mjvCamera = type { i32, i32, i32, [3 x double], double, double, double }
%struct._mjvScene = type { i32, i32, ptr, ptr, i32, ptr, ptr, ptr, ptr, ptr, i32, [8 x %struct._mjvLight], [2 x %struct._mjvGLCamera], i8, [3 x float], [4 x float], float, i32, [9 x i8] }
%struct._mjvLight = type { [3 x float], [3 x float], [3 x float], float, float, [3 x float], [3 x float], [3 x float], i8, i8, i8 }
%struct._mjvGLCamera = type { [3 x float], [3 x float], [3 x float], float, float, float, float, float }

@m = external dso_local global ptr, align 8
@cam = external dso_local global %struct._mjvCamera, align 8
@scn = external dso_local global %struct._mjvScene, align 8
@button_left = external dso_local global i8, align 1
@button_middle = external dso_local global i8, align 1
@button_right = external dso_local global i8, align 1
@lastx = external dso_local global double, align 8
@lasty = external dso_local global double, align 8

; Function Attrs: mustprogress uwtable
define dso_local void @_Z10mouse_moveP10GLFWwindowdd(ptr noundef %0, double noundef %1, double noundef %2) #0 {
  %4 = alloca ptr, align 8
  %5 = alloca double, align 8
  %6 = alloca double, align 8
  %7 = alloca double, align 8
  %8 = alloca double, align 8
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i8, align 1
  %12 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store double %1, ptr %5, align 8, !tbaa !9
  store double %2, ptr %6, align 8, !tbaa !9
  %13 = load i8, ptr @button_left, align 1, !tbaa !11, !range !13, !noundef !14
  %14 = trunc i8 %13 to i1
  br i1 %14, label %22, label %15

15:                                               ; preds = %3
  %16 = load i8, ptr @button_middle, align 1, !tbaa !11, !range !13, !noundef !14
  %17 = trunc i8 %16 to i1
  br i1 %17, label %22, label %18

18:                                               ; preds = %15
  %19 = load i8, ptr @button_right, align 1, !tbaa !11, !range !13, !noundef !14
  %20 = trunc i8 %19 to i1
  br i1 %20, label %22, label %21

21:                                               ; preds = %18
  br label %70

22:                                               ; preds = %18, %15, %3
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  %23 = load double, ptr %5, align 8, !tbaa !9
  %24 = load double, ptr @lastx, align 8, !tbaa !9
  %25 = fsub double %23, %24
  store double %25, ptr %7, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  %26 = load double, ptr %6, align 8, !tbaa !9
  %27 = load double, ptr @lasty, align 8, !tbaa !9
  %28 = fsub double %26, %27
  store double %28, ptr %8, align 8, !tbaa !9
  %29 = load double, ptr %5, align 8, !tbaa !9
  store double %29, ptr @lastx, align 8, !tbaa !9
  %30 = load double, ptr %6, align 8, !tbaa !9
  store double %30, ptr @lasty, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #3
  %31 = load ptr, ptr %4, align 8, !tbaa !5
  call void @glfwGetWindowSize(ptr noundef %31, ptr noundef %9, ptr noundef %10)
  call void @llvm.lifetime.start.p0(i64 1, ptr %11) #3
  %32 = load ptr, ptr %4, align 8, !tbaa !5
  %33 = call i32 @glfwGetKey(ptr noundef %32, i32 noundef 340)
  %34 = icmp eq i32 %33, 1
  br i1 %34, label %39, label %35

35:                                               ; preds = %22
  %36 = load ptr, ptr %4, align 8, !tbaa !5
  %37 = call i32 @glfwGetKey(ptr noundef %36, i32 noundef 344)
  %38 = icmp eq i32 %37, 1
  br label %39

39:                                               ; preds = %35, %22
  %40 = phi i1 [ true, %22 ], [ %38, %35 ]
  %41 = zext i1 %40 to i8
  store i8 %41, ptr %11, align 1, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #3
  %42 = load i8, ptr @button_right, align 1, !tbaa !11, !range !13, !noundef !14
  %43 = trunc i8 %42 to i1
  br i1 %43, label %44, label %49

44:                                               ; preds = %39
  %45 = load i8, ptr %11, align 1, !tbaa !11, !range !13, !noundef !14
  %46 = trunc i8 %45 to i1
  %47 = zext i1 %46 to i64
  %48 = select i1 %46, i32 4, i32 3
  store i32 %48, ptr %12, align 4, !tbaa !15
  br label %59

49:                                               ; preds = %39
  %50 = load i8, ptr @button_left, align 1, !tbaa !11, !range !13, !noundef !14
  %51 = trunc i8 %50 to i1
  br i1 %51, label %52, label %57

52:                                               ; preds = %49
  %53 = load i8, ptr %11, align 1, !tbaa !11, !range !13, !noundef !14
  %54 = trunc i8 %53 to i1
  %55 = zext i1 %54 to i64
  %56 = select i1 %54, i32 2, i32 1
  store i32 %56, ptr %12, align 4, !tbaa !15
  br label %58

57:                                               ; preds = %49
  store i32 5, ptr %12, align 4, !tbaa !15
  br label %58

58:                                               ; preds = %57, %52
  br label %59

59:                                               ; preds = %58, %44
  %60 = load ptr, ptr @m, align 8, !tbaa !5
  %61 = load i32, ptr %12, align 4, !tbaa !15
  %62 = load double, ptr %7, align 8, !tbaa !9
  %63 = load i32, ptr %10, align 4, !tbaa !17
  %64 = sitofp i32 %63 to double
  %65 = fdiv double %62, %64
  %66 = load double, ptr %8, align 8, !tbaa !9
  %67 = load i32, ptr %10, align 4, !tbaa !17
  %68 = sitofp i32 %67 to double
  %69 = fdiv double %66, %68
  call void @mjv_moveCamera(ptr noundef %60, i32 noundef %61, double noundef %65, double noundef %69, ptr noundef @scn, ptr noundef @cam)
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #3
  call void @llvm.lifetime.end.p0(i64 1, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  br label %70

70:                                               ; preds = %59, %21
  ret void
}

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

declare void @glfwGetWindowSize(ptr noundef, ptr noundef, ptr noundef) #2

declare i32 @glfwGetKey(ptr noundef, i32 noundef) #2

declare void @mjv_moveCamera(ptr noundef, i32 noundef, double noundef, double noundef, ptr noundef, ptr noundef) #2

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

attributes #0 = { mustprogress 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 = { "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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"double", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"bool", !7, i64 0}
!13 = !{i8 0, i8 2}
!14 = !{}
!15 = !{!16, !16, i64 0}
!16 = !{!"_ZTS9_mjtMouse", !7, i64 0}
!17 = !{!18, !18, i64 0}
!18 = !{!"int", !7, 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._mjvCamera = type { i32, i32, i32, [3 x double], double, double, double }
%struct._mjvScene = type { i32, i32, ptr, ptr, i32, ptr, ptr, ptr, ptr, ptr, i32, [8 x %struct._mjvLight], [2 x %struct._mjvGLCamera], i8, [3 x float], [4 x float], float, i32, [9 x i8] }
%struct._mjvLight = type { [3 x float], [3 x float], [3 x float], float, float, [3 x float], [3 x float], [3 x float], i8, i8, i8 }
%struct._mjvGLCamera = type { [3 x float], [3 x float], [3 x float], float, float, float, float, float }

@m = external dso_local local_unnamed_addr global ptr, align 8
@cam = external dso_local global %struct._mjvCamera, align 8
@scn = external dso_local global %struct._mjvScene, align 8
@button_left = external dso_local local_unnamed_addr global i8, align 1
@button_middle = external dso_local local_unnamed_addr global i8, align 1
@button_right = external dso_local local_unnamed_addr global i8, align 1
@lastx = external dso_local local_unnamed_addr global double, align 8
@lasty = external dso_local local_unnamed_addr global double, align 8

; Function Attrs: mustprogress uwtable
define dso_local void @_Z10mouse_moveP10GLFWwindowdd(ptr noundef %0, double noundef %1, double noundef %2) local_unnamed_addr #0 {
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = load i8, ptr @button_left, align 1, !tbaa !5, !range !9, !noundef !10
  %7 = trunc nuw i8 %6 to i1
  br i1 %7, label %14, label %8

8:                                                ; preds = %3
  %9 = load i8, ptr @button_middle, align 1, !tbaa !5, !range !9, !noundef !10
  %10 = trunc nuw i8 %9 to i1
  br i1 %10, label %14, label %11

11:                                               ; preds = %8
  %12 = load i8, ptr @button_right, align 1, !tbaa !5, !range !9, !noundef !10
  %13 = trunc nuw i8 %12 to i1
  br i1 %13, label %14, label %33

14:                                               ; preds = %11, %8, %3
  %15 = load double, ptr @lastx, align 8, !tbaa !11
  %16 = fsub double %1, %15
  %17 = load double, ptr @lasty, align 8, !tbaa !11
  %18 = fsub double %2, %17
  store double %1, ptr @lastx, align 8, !tbaa !11
  store double %2, ptr @lasty, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %4) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %5) #3
  call void @glfwGetWindowSize(ptr noundef %0, ptr noundef nonnull %4, ptr noundef nonnull %5)
  %19 = call i32 @glfwGetKey(ptr noundef %0, i32 noundef 340)
  %20 = icmp eq i32 %19, 1
  br i1 %20, label %.thread13, label %21

21:                                               ; preds = %14
  %22 = call i32 @glfwGetKey(ptr noundef %0, i32 noundef 344)
  %.fr = freeze i32 %22
  %23 = icmp eq i32 %.fr, 1
  %. = select i1 %23, i32 4, i32 3
  %.18 = select i1 %23, i32 2, i32 1
  br label %.thread13

.thread13:                                        ; preds = %14, %21
  %24 = phi i32 [ %., %21 ], [ 4, %14 ]
  %25 = phi i32 [ %.18, %21 ], [ 2, %14 ]
  %.in17 = load i8, ptr @button_right, align 1, !tbaa !5, !range !9, !noundef !10
  %26 = trunc nuw i8 %.in17 to i1
  %.in = load i8, ptr @button_left, align 1, !range !9
  %27 = trunc nuw i8 %.in to i1
  %spec.select = select i1 %27, i32 %25, i32 5
  %.0 = select i1 %26, i32 %24, i32 %spec.select
  %28 = load ptr, ptr @m, align 8, !tbaa !13
  %29 = load i32, ptr %5, align 4, !tbaa !15
  %30 = sitofp i32 %29 to double
  %31 = fdiv double %16, %30
  %32 = fdiv double %18, %30
  call void @mjv_moveCamera(ptr noundef %28, i32 noundef %.0, double noundef %31, double noundef %32, ptr noundef nonnull @scn, ptr noundef nonnull @cam)
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %5) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %4) #3
  br label %33

33:                                               ; preds = %11, %.thread13
  ret void
}

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

declare void @glfwGetWindowSize(ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #2

declare i32 @glfwGetKey(ptr noundef, i32 noundef) local_unnamed_addr #2

declare void @mjv_moveCamera(ptr noundef, i32 noundef, double noundef, double noundef, ptr noundef, ptr noundef) local_unnamed_addr #2

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

attributes #0 = { mustprogress 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 nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { "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 = !{!"bool", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{i8 0, i8 2}
!10 = !{}
!11 = !{!12, !12, i64 0}
!12 = !{!"double", !7, i64 0}
!13 = !{!14, !14, i64 0}
!14 = !{!"any pointer", !7, i64 0}
!15 = !{!16, !16, i64 0}
!16 = !{!"int", !7, i64 0}


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