; LLVM IR from achilles288/robot-monitor-graphics :: src/window/window.ll (ZN3rmg19window_key_callbackEP10GLFWwindowiiii)

; 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"

%"class.rmg::MouseEvent" = type <{ %"class.rmg::KeyboardEvent.base", i8, i16, i16, i16, i16, i16, i16, i32, i8, i8, [2 x i8] }>
%"class.rmg::KeyboardEvent.base" = type <{ i16, i8 }>
%"class.rmg::KeyboardEvent" = type <{ i16, i8, i8 }>

@_ZN3rmg10mouseEventE = external global %"class.rmg::MouseEvent", align 4

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN3rmg19window_key_callbackEP10GLFWwindowiiii(ptr noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3, i32 noundef %4) #0 {
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i8, align 1
  %12 = alloca i16, align 2
  %13 = alloca ptr, align 8
  store ptr %0, ptr %6, align 8, !tbaa !5
  store i32 %1, ptr %7, align 4, !tbaa !9
  store i32 %2, ptr %8, align 4, !tbaa !9
  store i32 %3, ptr %9, align 4, !tbaa !9
  store i32 %4, ptr %10, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 1, ptr %11) #4
  store i8 0, ptr %11, align 1, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 2, ptr %12) #4
  %14 = load i32, ptr %7, align 4, !tbaa !9
  %15 = trunc i32 %14 to i16
  %16 = call noundef signext i16 @_ZN3rmgL23GLFWKeycodeToRMGKeycodeEs(i16 noundef signext %15)
  store i16 %16, ptr %12, align 2, !tbaa !12
  %17 = load i16, ptr %12, align 2, !tbaa !12
  %18 = sext i16 %17 to i32
  %19 = icmp eq i32 %18, 17
  br i1 %19, label %20, label %21

20:                                               ; preds = %5
  store i8 1, ptr %11, align 1, !tbaa !11
  br label %33

21:                                               ; preds = %5
  %22 = load i16, ptr %12, align 2, !tbaa !12
  %23 = sext i16 %22 to i32
  %24 = icmp eq i32 %23, 18
  br i1 %24, label %25, label %26

25:                                               ; preds = %21
  store i8 2, ptr %11, align 1, !tbaa !11
  br label %32

26:                                               ; preds = %21
  %27 = load i16, ptr %12, align 2, !tbaa !12
  %28 = sext i16 %27 to i32
  %29 = icmp eq i32 %28, 16
  br i1 %29, label %30, label %31

30:                                               ; preds = %26
  store i8 4, ptr %11, align 1, !tbaa !11
  br label %31

31:                                               ; preds = %30, %26
  br label %32

32:                                               ; preds = %31, %25
  br label %33

33:                                               ; preds = %32, %20
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #4
  %34 = load ptr, ptr %6, align 8, !tbaa !5
  %35 = call ptr @glfwGetWindowUserPointer(ptr noundef %34)
  store ptr %35, ptr %13, align 8, !tbaa !5
  %36 = load i16, ptr %12, align 2, !tbaa !12
  store i16 %36, ptr @_ZN3rmg10mouseEventE, align 4, !tbaa !14
  %37 = load i32, ptr %9, align 4, !tbaa !9
  %38 = icmp eq i32 %37, 1
  br i1 %38, label %39, label %50

39:                                               ; preds = %33
  %40 = load i8, ptr %11, align 1, !tbaa !11
  %41 = zext i8 %40 to i32
  %42 = load i8, ptr getelementptr inbounds (%"class.rmg::KeyboardEvent", ptr @_ZN3rmg10mouseEventE, i32 0, i32 1), align 2, !tbaa !16
  %43 = zext i8 %42 to i32
  %44 = or i32 %43, %41
  %45 = trunc i32 %44 to i8
  store i8 %45, ptr getelementptr inbounds (%"class.rmg::KeyboardEvent", ptr @_ZN3rmg10mouseEventE, i32 0, i32 1), align 2, !tbaa !16
  %46 = load ptr, ptr %13, align 8, !tbaa !5
  %47 = load ptr, ptr %46, align 8, !tbaa !17
  %48 = getelementptr inbounds ptr, ptr %47, i64 11
  %49 = load ptr, ptr %48, align 8
  call void %49(ptr noundef nonnull align 8 dereferenceable(1440) %46, ptr noundef nonnull align 2 dereferenceable(3) @_ZN3rmg10mouseEventE)
  br label %68

50:                                               ; preds = %33
  %51 = load i32, ptr %9, align 4, !tbaa !9
  %52 = icmp eq i32 %51, 0
  br i1 %52, label %53, label %67

53:                                               ; preds = %50
  %54 = load i8, ptr getelementptr inbounds (%"class.rmg::KeyboardEvent", ptr @_ZN3rmg10mouseEventE, i32 0, i32 1), align 2, !tbaa !16
  %55 = zext i8 %54 to i32
  %56 = load i8, ptr %11, align 1, !tbaa !11
  %57 = zext i8 %56 to i32
  %58 = and i32 %55, %57
  %59 = load i8, ptr getelementptr inbounds (%"class.rmg::KeyboardEvent", ptr @_ZN3rmg10mouseEventE, i32 0, i32 1), align 2, !tbaa !16
  %60 = zext i8 %59 to i32
  %61 = xor i32 %60, %58
  %62 = trunc i32 %61 to i8
  store i8 %62, ptr getelementptr inbounds (%"class.rmg::KeyboardEvent", ptr @_ZN3rmg10mouseEventE, i32 0, i32 1), align 2, !tbaa !16
  %63 = load ptr, ptr %13, align 8, !tbaa !5
  %64 = load ptr, ptr %63, align 8, !tbaa !17
  %65 = getelementptr inbounds ptr, ptr %64, i64 12
  %66 = load ptr, ptr %65, align 8
  call void %66(ptr noundef nonnull align 8 dereferenceable(1440) %63, ptr noundef nonnull align 2 dereferenceable(3) @_ZN3rmg10mouseEventE)
  br label %67

67:                                               ; preds = %53, %50
  br label %68

68:                                               ; preds = %67, %39
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #4
  call void @llvm.lifetime.end.p0(i64 2, ptr %12) #4
  call void @llvm.lifetime.end.p0(i64 1, ptr %11) #4
  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: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

declare ptr @glfwGetWindowUserPointer(ptr noundef) #2

; Function Attrs: mustprogress nounwind uwtable
declare hidden noundef signext i16 @_ZN3rmgL23GLFWKeycodeToRMGKeycodeEs(i16 noundef signext) #3

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 = { 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 #4 = { 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 = !{!"int", !7, i64 0}
!11 = !{!7, !7, i64 0}
!12 = !{!13, !13, i64 0}
!13 = !{!"short", !7, i64 0}
!14 = !{!15, !13, i64 0}
!15 = !{!"_ZTSN3rmg13KeyboardEventE", !13, i64 0, !7, i64 2}
!16 = !{!15, !7, i64 2}
!17 = !{!18, !18, i64 0}
!18 = !{!"vtable pointer", !8, 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"

%"class.rmg::MouseEvent" = type <{ %"class.rmg::KeyboardEvent.base", i8, i16, i16, i16, i16, i16, i16, i32, i8, i8, [2 x i8] }>
%"class.rmg::KeyboardEvent.base" = type <{ i16, i8 }>

@_ZN3rmg10mouseEventE = external global %"class.rmg::MouseEvent", align 4

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN3rmg19window_key_callbackEP10GLFWwindowiiii(ptr noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3, i32 noundef %4) local_unnamed_addr #0 {
.fold.split:
  %5 = trunc i32 %1 to i16
  %6 = tail call noundef signext i16 @_ZN3rmgL23GLFWKeycodeToRMGKeycodeEs(i16 noundef signext %5)
  %switch.tableidx = add i16 %6, -16
  %7 = icmp ult i16 %switch.tableidx, 3
  %switch.cast = zext i16 %switch.tableidx to i24
  %switch.shiftamt = shl nuw nsw i24 %switch.cast, 3
  %switch.downshift = lshr i24 131332, %switch.shiftamt
  %switch.masked = trunc i24 %switch.downshift to i8
  %.0 = select i1 %7, i8 %switch.masked, i8 0
  %8 = tail call ptr @glfwGetWindowUserPointer(ptr noundef %0)
  store i16 %6, ptr @_ZN3rmg10mouseEventE, align 4, !tbaa !5
  switch i32 %3, label %19 [
    i32 1, label %9
    i32 0, label %12
  ]

9:                                                ; preds = %.fold.split
  %10 = load i8, ptr getelementptr inbounds (i8, ptr @_ZN3rmg10mouseEventE, i64 2), align 2, !tbaa !10
  %11 = or i8 %10, %.0
  br label %.sink.split

12:                                               ; preds = %.fold.split
  %13 = load i8, ptr getelementptr inbounds (i8, ptr @_ZN3rmg10mouseEventE, i64 2), align 2, !tbaa !10
  %14 = xor i8 %.0, -1
  %15 = and i8 %13, %14
  br label %.sink.split

.sink.split:                                      ; preds = %9, %12
  %.sink = phi i8 [ %15, %12 ], [ %11, %9 ]
  %.sink14 = phi i64 [ 96, %12 ], [ 88, %9 ]
  store i8 %.sink, ptr getelementptr inbounds (i8, ptr @_ZN3rmg10mouseEventE, i64 2), align 2, !tbaa !10
  %16 = load ptr, ptr %8, align 8, !tbaa !11
  %17 = getelementptr inbounds i8, ptr %16, i64 %.sink14
  %18 = load ptr, ptr %17, align 8
  tail call void %18(ptr noundef nonnull align 8 dereferenceable(1440) %8, ptr noundef nonnull align 2 dereferenceable(3) @_ZN3rmg10mouseEventE)
  br label %19

19:                                               ; preds = %.sink.split, %.fold.split
  ret void
}

declare ptr @glfwGetWindowUserPointer(ptr noundef) local_unnamed_addr #1

; Function Attrs: mustprogress nounwind uwtable
declare hidden noundef signext i16 @_ZN3rmgL23GLFWKeycodeToRMGKeycodeEs(i16 noundef signext) local_unnamed_addr #2

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 = { "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 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" }

!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 0}
!6 = !{!"_ZTSN3rmg13KeyboardEventE", !7, i64 0, !8, i64 2}
!7 = !{!"short", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!6, !8, i64 2}
!11 = !{!12, !12, i64 0}
!12 = !{!"vtable pointer", !9, i64 0}


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