; LLVM IR from NotManyIdeasDev/double-pendulum-sim :: double-pendulum-sim/Libraries/raygui-2.8/examples/text_box_selection (GuiTextBoxCut)

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

@guiTextBoxState = external hidden global %struct.GuiTextBoxState, align 4

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

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

; Function Attrs: nounwind willreturn memory(read)
declare i64 @strlen(ptr noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local i32 @GuiTextBoxGetByteIndex(ptr noundef, i32 noundef, i32 noundef, i32 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

declare ptr @TextSubtext(ptr noundef, i32 noundef, i32 noundef) #4

declare void @SetClipboardText(ptr noundef) #4

; Function Attrs: nounwind uwtable
define dso_local void @GuiTextBoxCut(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 ptr, align 8
  %8 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  %9 = load ptr, ptr %2, align 8, !tbaa !5
  %10 = icmp ne ptr %9, null
  br i1 %10, label %11, label %73

11:                                               ; preds = %1
  %12 = load i32, ptr getelementptr inbounds (%struct.GuiTextBoxState, ptr @guiTextBoxState, i32 0, i32 3), align 4, !tbaa !9
  %13 = icmp ne i32 %12, -1
  br i1 %13, label %14, label %73

14:                                               ; preds = %11
  %15 = load i32, ptr @guiTextBoxState, align 4, !tbaa !12
  %16 = icmp ne i32 %15, -1
  br i1 %16, label %17, label %73

17:                                               ; preds = %14
  %18 = load i32, ptr getelementptr inbounds (%struct.GuiTextBoxState, ptr @guiTextBoxState, i32 0, i32 3), align 4, !tbaa !9
  %19 = load i32, ptr @guiTextBoxState, align 4, !tbaa !12
  %20 = icmp ne i32 %18, %19
  br i1 %20, label %21, label %73

21:                                               ; preds = %17
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #5
  %22 = load i32, ptr @guiTextBoxState, align 4, !tbaa !12
  store i32 %22, ptr %3, align 4, !tbaa !13
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #5
  %23 = load i32, ptr getelementptr inbounds (%struct.GuiTextBoxState, ptr @guiTextBoxState, i32 0, i32 3), align 4, !tbaa !9
  store i32 %23, ptr %4, align 4, !tbaa !13
  %24 = load i32, ptr @guiTextBoxState, align 4, !tbaa !12
  %25 = load i32, ptr getelementptr inbounds (%struct.GuiTextBoxState, ptr @guiTextBoxState, i32 0, i32 3), align 4, !tbaa !9
  %26 = icmp sgt i32 %24, %25
  br i1 %26, label %27, label %30

27:                                               ; preds = %21
  %28 = load i32, ptr getelementptr inbounds (%struct.GuiTextBoxState, ptr @guiTextBoxState, i32 0, i32 3), align 4, !tbaa !9
  store i32 %28, ptr %3, align 4, !tbaa !13
  %29 = load i32, ptr @guiTextBoxState, align 4, !tbaa !12
  store i32 %29, ptr %4, align 4, !tbaa !13
  br label %30

30:                                               ; preds = %27, %21
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #5
  %31 = load ptr, ptr %2, align 8, !tbaa !5
  %32 = load i32, ptr %3, align 4, !tbaa !13
  %33 = call i32 @GuiTextBoxGetByteIndex(ptr noundef %31, i32 noundef 0, i32 noundef 0, i32 noundef %32)
  store i32 %33, ptr %5, align 4, !tbaa !13
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #5
  %34 = load ptr, ptr %2, align 8, !tbaa !5
  %35 = load i32, ptr %4, align 4, !tbaa !13
  %36 = call i32 @GuiTextBoxGetByteIndex(ptr noundef %34, i32 noundef 0, i32 noundef 0, i32 noundef %35)
  store i32 %36, ptr %6, align 4, !tbaa !13
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #5
  %37 = load ptr, ptr %2, align 8, !tbaa !5
  %38 = load i32, ptr %5, align 4, !tbaa !13
  %39 = load i32, ptr %6, align 4, !tbaa !13
  %40 = load i32, ptr %5, align 4, !tbaa !13
  %41 = sub nsw i32 %39, %40
  %42 = call ptr @TextSubtext(ptr noundef %37, i32 noundef %38, i32 noundef %41)
  store ptr %42, ptr %7, align 8, !tbaa !5
  %43 = load ptr, ptr %7, align 8, !tbaa !5
  call void @SetClipboardText(ptr noundef %43)
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #5
  %44 = load ptr, ptr %2, align 8, !tbaa !5
  %45 = call i64 @strlen(ptr noundef %44) #6
  %46 = trunc i64 %45 to i32
  store i32 %46, ptr %8, align 4, !tbaa !13
  %47 = load ptr, ptr %2, align 8, !tbaa !5
  %48 = load i32, ptr %5, align 4, !tbaa !13
  %49 = sext i32 %48 to i64
  %50 = getelementptr inbounds i8, ptr %47, i64 %49
  %51 = load ptr, ptr %2, align 8, !tbaa !5
  %52 = load i32, ptr %6, align 4, !tbaa !13
  %53 = sext i32 %52 to i64
  %54 = getelementptr inbounds i8, ptr %51, i64 %53
  %55 = load i32, ptr %8, align 4, !tbaa !13
  %56 = load i32, ptr %6, align 4, !tbaa !13
  %57 = sub nsw i32 %55, %56
  %58 = sext i32 %57 to i64
  call void @llvm.memmove.p0.p0.i64(ptr align 1 %50, ptr align 1 %54, i64 %58, i1 false)
  %59 = load ptr, ptr %2, align 8, !tbaa !5
  %60 = load i32, ptr %8, align 4, !tbaa !13
  %61 = load i32, ptr %6, align 4, !tbaa !13
  %62 = load i32, ptr %5, align 4, !tbaa !13
  %63 = sub nsw i32 %61, %62
  %64 = sub nsw i32 %60, %63
  %65 = sext i32 %64 to i64
  %66 = getelementptr inbounds i8, ptr %59, i64 %65
  store i8 0, ptr %66, align 1, !tbaa !14
  %67 = load i32, ptr %3, align 4, !tbaa !13
  store i32 %67, ptr @guiTextBoxState, align 4, !tbaa !12
  %68 = load i32, ptr getelementptr inbounds (%struct.GuiTextBoxState, ptr @guiTextBoxState, i32 0, i32 3), align 4, !tbaa !9
  %69 = load i32, ptr getelementptr inbounds (%struct.GuiTextBoxState, ptr @guiTextBoxState, i32 0, i32 1), align 4, !tbaa !15
  %70 = icmp slt i32 %68, %69
  br i1 %70, label %71, label %72

71:                                               ; preds = %30
  store i32 -1, ptr getelementptr inbounds (%struct.GuiTextBoxState, ptr @guiTextBoxState, i32 0, i32 1), align 4, !tbaa !15
  br label %72

72:                                               ; preds = %71, %30
  store i32 -1, ptr getelementptr inbounds (%struct.GuiTextBoxState, ptr @guiTextBoxState, i32 0, i32 3), align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #5
  br label %73

73:                                               ; preds = %72, %17, %14, %11, %1
  ret void
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #4 = { "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 #5 = { nounwind }
attributes #6 = { 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 12}
!10 = !{!"GuiTextBoxState", !11, i64 0, !11, i64 4, !11, i64 8, !11, i64 12}
!11 = !{!"int", !7, i64 0}
!12 = !{!10, !11, i64 0}
!13 = !{!11, !11, i64 0}
!14 = !{!7, !7, i64 0}
!15 = !{!10, !11, i64 4}


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

@guiTextBoxState = external hidden local_unnamed_addr global %struct.GuiTextBoxState, align 4

; Function Attrs: mustprogress nofree nounwind willreturn memory(argmem: read)
declare i64 @strlen(ptr nocapture noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local i32 @GuiTextBoxGetByteIndex(ptr noundef, i32 noundef, i32 noundef, i32 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

declare ptr @TextSubtext(ptr noundef, i32 noundef, i32 noundef) local_unnamed_addr #3

declare void @SetClipboardText(ptr noundef) local_unnamed_addr #3

; Function Attrs: nounwind uwtable
define dso_local void @GuiTextBoxCut(ptr noundef %0) local_unnamed_addr #1 {
  %2 = icmp eq ptr %0, null
  %3 = load i32, ptr getelementptr inbounds (i8, ptr @guiTextBoxState, i64 12), align 4
  %4 = icmp eq i32 %3, -1
  %or.cond.not34 = select i1 %2, i1 true, i1 %4
  %5 = load i32, ptr @guiTextBoxState, align 4
  %6 = icmp eq i32 %5, -1
  %or.cond3.not31 = select i1 %or.cond.not34, i1 true, i1 %6
  %.not = icmp eq i32 %3, %5
  %or.cond28 = select i1 %or.cond3.not31, i1 true, i1 %.not
  br i1 %or.cond28, label %28, label %7

7:                                                ; preds = %1
  %spec.select = tail call i32 @llvm.smax.i32(i32 %5, i32 %3)
  %spec.select29 = tail call i32 @llvm.smin.i32(i32 %5, i32 %3)
  %8 = tail call i32 @GuiTextBoxGetByteIndex(ptr noundef nonnull %0, i32 noundef 0, i32 noundef 0, i32 noundef %spec.select29)
  %9 = tail call i32 @GuiTextBoxGetByteIndex(ptr noundef nonnull %0, i32 noundef 0, i32 noundef 0, i32 noundef %spec.select)
  %10 = sub nsw i32 %9, %8
  %11 = tail call ptr @TextSubtext(ptr noundef nonnull %0, i32 noundef %8, i32 noundef %10) #5
  tail call void @SetClipboardText(ptr noundef %11) #5
  %12 = tail call i64 @strlen(ptr noundef nonnull dereferenceable(1) %0) #6
  %13 = trunc i64 %12 to i32
  %14 = sext i32 %8 to i64
  %15 = getelementptr inbounds i8, ptr %0, i64 %14
  %16 = sext i32 %9 to i64
  %17 = getelementptr inbounds i8, ptr %0, i64 %16
  %18 = sub nsw i32 %13, %9
  %19 = sext i32 %18 to i64
  tail call void @llvm.memmove.p0.p0.i64(ptr nonnull align 1 %15, ptr nonnull align 1 %17, i64 %19, i1 false)
  %20 = sub nsw i32 %13, %10
  %21 = sext i32 %20 to i64
  %22 = getelementptr inbounds i8, ptr %0, i64 %21
  store i8 0, ptr %22, align 1, !tbaa !5
  store i32 %spec.select29, ptr @guiTextBoxState, align 4, !tbaa !8
  %23 = load i32, ptr getelementptr inbounds (i8, ptr @guiTextBoxState, i64 12), align 4, !tbaa !11
  %24 = load i32, ptr getelementptr inbounds (i8, ptr @guiTextBoxState, i64 4), align 4, !tbaa !12
  %25 = icmp slt i32 %23, %24
  br i1 %25, label %26, label %27

26:                                               ; preds = %7
  store i32 -1, ptr getelementptr inbounds (i8, ptr @guiTextBoxState, i64 4), align 4, !tbaa !12
  br label %27

27:                                               ; preds = %26, %7
  store i32 -1, ptr getelementptr inbounds (i8, ptr @guiTextBoxState, i64 12), align 4, !tbaa !11
  br label %28

28:                                               ; preds = %27, %1
  ret void
}

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

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

attributes #0 = { 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 #1 = { 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 #2 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { "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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #5 = { nounwind }
attributes #6 = { 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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = !{!9, !10, i64 0}
!9 = !{!"GuiTextBoxState", !10, i64 0, !10, i64 4, !10, i64 8, !10, i64 12}
!10 = !{!"int", !6, i64 0}
!11 = !{!9, !10, i64 12}
!12 = !{!9, !10, i64 4}


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