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

; 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.patch_t = type { i16, i16, i16, i16, [8 x i32] }

@colon = external hidden global ptr, align 8
@sucks = external hidden global ptr, align 8

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

declare void @V_DrawPatch(i32 noundef, i32 noundef, i32 noundef, ptr noundef) #1

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

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

; Function Attrs: nounwind uwtable
define dso_local void @WI_drawTime(i32 noundef %0, i32 noundef %1, i32 noundef %2) #2 {
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  store i32 %0, ptr %4, align 4, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !5
  store i32 %2, ptr %6, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  %10 = load i32, ptr %6, align 4, !tbaa !5
  %11 = icmp slt i32 %10, 0
  br i1 %11, label %12, label %13

12:                                               ; preds = %3
  store i32 1, ptr %9, align 4
  br label %61

13:                                               ; preds = %3
  %14 = load i32, ptr %6, align 4, !tbaa !5
  %15 = icmp sle i32 %14, 3599
  br i1 %15, label %16, label %51

16:                                               ; preds = %13
  store i32 1, ptr %7, align 4, !tbaa !5
  br label %17

17:                                               ; preds = %45, %16
  %18 = load i32, ptr %6, align 4, !tbaa !5
  %19 = load i32, ptr %7, align 4, !tbaa !5
  %20 = sdiv i32 %18, %19
  %21 = srem i32 %20, 60
  store i32 %21, ptr %8, align 4, !tbaa !5
  %22 = load i32, ptr %4, align 4, !tbaa !5
  %23 = load i32, ptr %5, align 4, !tbaa !5
  %24 = load i32, ptr %8, align 4, !tbaa !5
  %25 = call i32 @WI_drawNum(i32 noundef %22, i32 noundef %23, i32 noundef %24, i32 noundef 2)
  %26 = load ptr, ptr @colon, align 8, !tbaa !9
  %27 = getelementptr inbounds %struct.patch_t, ptr %26, i32 0, i32 0
  %28 = load i16, ptr %27, align 4, !tbaa !11
  %29 = sext i16 %28 to i32
  %30 = sub nsw i32 %25, %29
  store i32 %30, ptr %4, align 4, !tbaa !5
  %31 = load i32, ptr %7, align 4, !tbaa !5
  %32 = mul nsw i32 %31, 60
  store i32 %32, ptr %7, align 4, !tbaa !5
  %33 = load i32, ptr %7, align 4, !tbaa !5
  %34 = icmp eq i32 %33, 60
  br i1 %34, label %40, label %35

35:                                               ; preds = %17
  %36 = load i32, ptr %6, align 4, !tbaa !5
  %37 = load i32, ptr %7, align 4, !tbaa !5
  %38 = sdiv i32 %36, %37
  %39 = icmp ne i32 %38, 0
  br i1 %39, label %40, label %44

40:                                               ; preds = %35, %17
  %41 = load i32, ptr %4, align 4, !tbaa !5
  %42 = load i32, ptr %5, align 4, !tbaa !5
  %43 = load ptr, ptr @colon, align 8, !tbaa !9
  call void @V_DrawPatch(i32 noundef %41, i32 noundef %42, i32 noundef 0, ptr noundef %43)
  br label %44

44:                                               ; preds = %40, %35
  br label %45

45:                                               ; preds = %44
  %46 = load i32, ptr %6, align 4, !tbaa !5
  %47 = load i32, ptr %7, align 4, !tbaa !5
  %48 = sdiv i32 %46, %47
  %49 = icmp ne i32 %48, 0
  br i1 %49, label %17, label %50, !llvm.loop !14

50:                                               ; preds = %45
  br label %60

51:                                               ; preds = %13
  %52 = load i32, ptr %4, align 4, !tbaa !5
  %53 = load ptr, ptr @sucks, align 8, !tbaa !9
  %54 = getelementptr inbounds %struct.patch_t, ptr %53, i32 0, i32 0
  %55 = load i16, ptr %54, align 4, !tbaa !11
  %56 = sext i16 %55 to i32
  %57 = sub nsw i32 %52, %56
  %58 = load i32, ptr %5, align 4, !tbaa !5
  %59 = load ptr, ptr @sucks, align 8, !tbaa !9
  call void @V_DrawPatch(i32 noundef %57, i32 noundef %58, i32 noundef 0, ptr noundef %59)
  br label %60

60:                                               ; preds = %51, %50
  store i32 0, ptr %9, align 4
  br label %61

61:                                               ; preds = %60, %12
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  %62 = load i32, ptr %9, align 4
  switch i32 %62, label %64 [
    i32 0, label %63
    i32 1, label %63
  ]

63:                                               ; preds = %61, %61
  ret void

64:                                               ; preds = %61
  unreachable
}

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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !13, i64 0}
!12 = !{!"", !13, i64 0, !13, i64 2, !13, i64 4, !13, i64 6, !7, i64 8}
!13 = !{!"short", !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"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"

@colon = external hidden local_unnamed_addr global ptr, align 8
@sucks = external hidden local_unnamed_addr global ptr, align 8

declare void @V_DrawPatch(i32 noundef, i32 noundef, i32 noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local i32 @WI_drawNum(i32 noundef, i32 noundef, i32 noundef, i32 noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local void @WI_drawTime(i32 noundef %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #1 {
  %4 = icmp slt i32 %2, 0
  br i1 %4, label %.loopexit, label %5

5:                                                ; preds = %3
  %6 = icmp ult i32 %2, 3600
  br i1 %6, label %.preheader, label %18

.preheader:                                       ; preds = %5, %17
  %.017 = phi i32 [ %14, %17 ], [ 1, %5 ]
  %.0 = phi i32 [ %13, %17 ], [ %0, %5 ]
  %7 = udiv i32 %2, %.017
  %8 = urem i32 %7, 60
  %9 = tail call i32 @WI_drawNum(i32 noundef %.0, i32 noundef %1, i32 noundef %8, i32 noundef 2)
  %10 = load ptr, ptr @colon, align 8, !tbaa !5
  %11 = load i16, ptr %10, align 4, !tbaa !9
  %12 = sext i16 %11 to i32
  %13 = sub nsw i32 %9, %12
  %14 = mul nuw nsw i32 %.017, 60
  %15 = icmp ne i32 %.017, 1
  %.not = icmp ugt i32 %14, %2
  %or.cond = select i1 %15, i1 %.not, i1 false
  br i1 %or.cond, label %17, label %16

16:                                               ; preds = %.preheader
  tail call void @V_DrawPatch(i32 noundef %13, i32 noundef %1, i32 noundef 0, ptr noundef nonnull %10) #2
  br label %17

17:                                               ; preds = %.preheader, %16
  br i1 %.not, label %.loopexit, label %.preheader, !llvm.loop !12

18:                                               ; preds = %5
  %19 = load ptr, ptr @sucks, align 8, !tbaa !5
  %20 = load i16, ptr %19, align 4, !tbaa !9
  %21 = sext i16 %20 to i32
  %22 = sub nsw i32 %0, %21
  tail call void @V_DrawPatch(i32 noundef %22, i32 noundef %1, i32 noundef 0, ptr noundef nonnull %19) #2
  br label %.loopexit

.loopexit:                                        ; preds = %17, %18, %3
  ret void
}

attributes #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 = { 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 = { 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, !11, i64 0}
!10 = !{!"", !11, i64 0, !11, i64 2, !11, i64 4, !11, i64 6, !7, i64 8}
!11 = !{!"short", !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}


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