; LLVM IR from BrunoLevy/learn-fpga :: LiteX/software/Doom/mc1-doom/src (p_tick)

; 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.thinker_s = type { ptr, ptr, %union.actionf_t }
%union.actionf_t = type { ptr }

@thinkercap = external dso_local global %struct.thinker_s, align 8

; Function Attrs: nounwind uwtable
define dso_local void @P_RunThinkers() #0 {
  %1 = alloca ptr, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr %1) #3
  %2 = load ptr, ptr getelementptr inbounds (%struct.thinker_s, ptr @thinkercap, i32 0, i32 1), align 8, !tbaa !5
  store ptr %2, ptr %1, align 8, !tbaa !10
  br label %3

3:                                                ; preds = %38, %0
  %4 = load ptr, ptr %1, align 8, !tbaa !10
  %5 = icmp ne ptr %4, @thinkercap
  br i1 %5, label %6, label %42

6:                                                ; preds = %3
  %7 = load ptr, ptr %1, align 8, !tbaa !10
  %8 = getelementptr inbounds %struct.thinker_s, ptr %7, i32 0, i32 2
  %9 = load ptr, ptr %8, align 8, !tbaa !11
  %10 = icmp eq ptr %9, inttoptr (i64 -1 to ptr)
  br i1 %10, label %11, label %27

11:                                               ; preds = %6
  %12 = load ptr, ptr %1, align 8, !tbaa !10
  %13 = getelementptr inbounds %struct.thinker_s, ptr %12, i32 0, i32 0
  %14 = load ptr, ptr %13, align 8, !tbaa !12
  %15 = load ptr, ptr %1, align 8, !tbaa !10
  %16 = getelementptr inbounds %struct.thinker_s, ptr %15, i32 0, i32 1
  %17 = load ptr, ptr %16, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.thinker_s, ptr %17, i32 0, i32 0
  store ptr %14, ptr %18, align 8, !tbaa !12
  %19 = load ptr, ptr %1, align 8, !tbaa !10
  %20 = getelementptr inbounds %struct.thinker_s, ptr %19, i32 0, i32 1
  %21 = load ptr, ptr %20, align 8, !tbaa !5
  %22 = load ptr, ptr %1, align 8, !tbaa !10
  %23 = getelementptr inbounds %struct.thinker_s, ptr %22, i32 0, i32 0
  %24 = load ptr, ptr %23, align 8, !tbaa !12
  %25 = getelementptr inbounds %struct.thinker_s, ptr %24, i32 0, i32 1
  store ptr %21, ptr %25, align 8, !tbaa !5
  %26 = load ptr, ptr %1, align 8, !tbaa !10
  call void @Z_Free(ptr noundef %26)
  br label %38

27:                                               ; preds = %6
  %28 = load ptr, ptr %1, align 8, !tbaa !10
  %29 = getelementptr inbounds %struct.thinker_s, ptr %28, i32 0, i32 2
  %30 = load ptr, ptr %29, align 8, !tbaa !11
  %31 = icmp ne ptr %30, null
  br i1 %31, label %32, label %37

32:                                               ; preds = %27
  %33 = load ptr, ptr %1, align 8, !tbaa !10
  %34 = getelementptr inbounds %struct.thinker_s, ptr %33, i32 0, i32 2
  %35 = load ptr, ptr %34, align 8, !tbaa !11
  %36 = load ptr, ptr %1, align 8, !tbaa !10
  call void %35(ptr noundef %36)
  br label %37

37:                                               ; preds = %32, %27
  br label %38

38:                                               ; preds = %37, %11
  %39 = load ptr, ptr %1, align 8, !tbaa !10
  %40 = getelementptr inbounds %struct.thinker_s, ptr %39, i32 0, i32 1
  %41 = load ptr, ptr %40, align 8, !tbaa !5
  store ptr %41, ptr %1, align 8, !tbaa !10
  br label %3, !llvm.loop !13

42:                                               ; preds = %3
  call void @llvm.lifetime.end.p0(i64 8, ptr %1) #3
  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 @Z_Free(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 = { 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 = { "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, !7, i64 8}
!6 = !{!"thinker_s", !7, i64 0, !7, i64 8, !8, i64 16}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!7, !7, i64 0}
!11 = !{!8, !8, i64 0}
!12 = !{!6, !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}


; 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.thinker_s = type { ptr, ptr, %union.actionf_t }
%union.actionf_t = type { ptr }

@thinkercap = external dso_local global %struct.thinker_s, align 8

; Function Attrs: nounwind uwtable
define dso_local void @P_RunThinkers() local_unnamed_addr #0 {
  %.013 = load ptr, ptr getelementptr inbounds (i8, ptr @thinkercap, i64 8), align 8, !tbaa !5
  %.not14 = icmp eq ptr %.013, @thinkercap
  br i1 %.not14, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %0, %9
  %.015 = phi ptr [ %.0, %9 ], [ %.013, %0 ]
  %1 = getelementptr inbounds i8, ptr %.015, i64 16
  %2 = load ptr, ptr %1, align 8, !tbaa !10
  %magicptr = ptrtoint ptr %2 to i64
  switch i64 %magicptr, label %8 [
    i64 -1, label %3
    i64 0, label %9
  ]

3:                                                ; preds = %.lr.ph
  %4 = load ptr, ptr %.015, align 8, !tbaa !11
  %5 = getelementptr inbounds i8, ptr %.015, i64 8
  %6 = load ptr, ptr %5, align 8, !tbaa !5
  store ptr %4, ptr %6, align 8, !tbaa !11
  %7 = getelementptr inbounds i8, ptr %4, i64 8
  store ptr %6, ptr %7, align 8, !tbaa !5
  tail call void @Z_Free(ptr noundef nonnull %.015) #2
  br label %9

8:                                                ; preds = %.lr.ph
  tail call void %2(ptr noundef %.015) #2
  br label %9

9:                                                ; preds = %.lr.ph, %8, %3
  %10 = getelementptr inbounds i8, ptr %.015, i64 8
  %.0 = load ptr, ptr %10, align 8, !tbaa !5
  %.not = icmp eq ptr %.0, @thinkercap
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !12

._crit_edge:                                      ; preds = %9, %0
  ret void
}

declare void @Z_Free(ptr noundef) local_unnamed_addr #1

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

!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 8}
!6 = !{!"thinker_s", !7, i64 0, !7, i64 8, !8, i64 16}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!8, !8, i64 0}
!11 = !{!6, !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass