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

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

@consoleplayer = external dso_local global i32, align 4
@gametic = external dso_local global i32, align 4
@playeringame = external dso_local global [4 x i32], align 16
@starttime = external dso_local global i32, align 4
@singledemo = external dso_local global i32, align 4
@deathmatch = external dso_local global i32, align 4
@demoplayback = external dso_local global i32, align 4
@demorecording = external dso_local global i32, align 4
@netgame = external dso_local global i32, align 4
@netdemo = external dso_local global i32, align 4
@respawnparm = external global i32, align 4
@fastparm = external global i32, align 4
@nomonsters = external global i32, align 4
@demo_p = external dso_local global ptr, align 8
@demoname = external dso_local global [32 x i8], align 16
@demobuffer = external dso_local global ptr, align 8
@timingdemo = external dso_local global i32, align 4
@.str.22 = external hidden unnamed_addr constant [33 x i8], align 1
@.str.23 = external hidden unnamed_addr constant [107 x i8], align 1
@.str.24 = external hidden unnamed_addr constant [17 x i8], align 1

; 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

declare i32 @I_GetTime() #1

; Function Attrs: noreturn
declare void @I_Error(ptr noundef, ...) #2

declare void @Z_Free(ptr noundef) #1

declare i32 @M_WriteFile(ptr noundef, ptr noundef, i32 noundef) #1

; Function Attrs: nounwind uwtable
define dso_local i32 @G_CheckDemoStatus() #3 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #4
  %4 = load i32, ptr @timingdemo, align 4, !tbaa !5
  %5 = icmp ne i32 %4, 0
  br i1 %5, label %6, label %12

6:                                                ; preds = %0
  %7 = call i32 @I_GetTime()
  store i32 %7, ptr %2, align 4, !tbaa !5
  %8 = load i32, ptr @gametic, align 4, !tbaa !5
  %9 = load i32, ptr %2, align 4, !tbaa !5
  %10 = load i32, ptr @starttime, align 4, !tbaa !5
  %11 = sub nsw i32 %9, %10
  call void (ptr, ...) @I_Error(ptr noundef @.str.22, i32 noundef %8, i32 noundef %11) #5
  unreachable

12:                                               ; preds = %0
  %13 = load i32, ptr @demoplayback, align 4, !tbaa !5
  %14 = icmp ne i32 %13, 0
  br i1 %14, label %15, label %28

15:                                               ; preds = %12
  %16 = load i32, ptr @singledemo, align 4, !tbaa !5
  %17 = icmp ne i32 %16, 0
  br i1 %17, label %18, label %19

18:                                               ; preds = %15
  call void @I_Quit()
  br label %19

19:                                               ; preds = %18, %15
  %20 = load ptr, ptr @demobuffer, align 8, !tbaa !9
  %21 = getelementptr inbounds i8, ptr %20, i64 -48
  %22 = getelementptr inbounds %struct.memblock_s, ptr %21, i32 0, i32 4
  %23 = load i32, ptr %22, align 4, !tbaa !11
  %24 = icmp ne i32 %23, 1919505
  br i1 %24, label %25, label %26

25:                                               ; preds = %19
  call void (ptr, ...) @I_Error(ptr noundef @.str.23, i32 noundef 1612) #5
  unreachable

26:                                               ; preds = %19
  %27 = load ptr, ptr @demobuffer, align 8, !tbaa !9
  call void @Z_ChangeTag2(ptr noundef %27, i32 noundef 101)
  store i32 0, ptr @demoplayback, align 4, !tbaa !5
  store i32 0, ptr @netdemo, align 4, !tbaa !5
  store i32 0, ptr @netgame, align 4, !tbaa !5
  store i32 0, ptr @deathmatch, align 4, !tbaa !5
  store i32 0, ptr getelementptr inbounds ([4 x i32], ptr @playeringame, i64 0, i64 3), align 4, !tbaa !5
  store i32 0, ptr getelementptr inbounds ([4 x i32], ptr @playeringame, i64 0, i64 2), align 8, !tbaa !5
  store i32 0, ptr getelementptr inbounds ([4 x i32], ptr @playeringame, i64 0, i64 1), align 4, !tbaa !5
  store i32 0, ptr @respawnparm, align 4, !tbaa !5
  store i32 0, ptr @fastparm, align 4, !tbaa !5
  store i32 0, ptr @nomonsters, align 4, !tbaa !5
  store i32 0, ptr @consoleplayer, align 4, !tbaa !5
  call void @D_AdvanceDemo()
  store i32 1, ptr %1, align 4
  store i32 1, ptr %3, align 4
  br label %44

28:                                               ; preds = %12
  %29 = load i32, ptr @demorecording, align 4, !tbaa !5
  %30 = icmp ne i32 %29, 0
  br i1 %30, label %31, label %43

31:                                               ; preds = %28
  %32 = load ptr, ptr @demo_p, align 8, !tbaa !9
  %33 = getelementptr inbounds i8, ptr %32, i32 1
  store ptr %33, ptr @demo_p, align 8, !tbaa !9
  store i8 -128, ptr %32, align 1, !tbaa !13
  %34 = load ptr, ptr @demobuffer, align 8, !tbaa !9
  %35 = load ptr, ptr @demo_p, align 8, !tbaa !9
  %36 = load ptr, ptr @demobuffer, align 8, !tbaa !9
  %37 = ptrtoint ptr %35 to i64
  %38 = ptrtoint ptr %36 to i64
  %39 = sub i64 %37, %38
  %40 = trunc i64 %39 to i32
  %41 = call i32 @M_WriteFile(ptr noundef @demoname, ptr noundef %34, i32 noundef %40)
  %42 = load ptr, ptr @demobuffer, align 8, !tbaa !9
  call void @Z_Free(ptr noundef %42)
  store i32 0, ptr @demorecording, align 4, !tbaa !5
  call void (ptr, ...) @I_Error(ptr noundef @.str.24, ptr noundef @demoname) #5
  unreachable

43:                                               ; preds = %28
  store i32 0, ptr %1, align 4
  store i32 1, ptr %3, align 4
  br label %44

44:                                               ; preds = %43, %26
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #4
  %45 = load i32, ptr %1, align 4
  ret i32 %45
}

declare void @I_Quit() #1

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

declare void @D_AdvanceDemo() #1

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

!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, !6, i64 20}
!12 = !{!"memblock_s", !6, i64 0, !6, i64 4, !10, i64 8, !6, i64 16, !6, i64 20, !10, i64 24, !10, i64 32, !6, i64 40}
!13 = !{!7, !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"

@consoleplayer = external dso_local local_unnamed_addr global i32, align 4
@gametic = external dso_local local_unnamed_addr global i32, align 4
@playeringame = external dso_local local_unnamed_addr global [4 x i32], align 16
@starttime = external dso_local local_unnamed_addr global i32, align 4
@singledemo = external dso_local local_unnamed_addr global i32, align 4
@deathmatch = external dso_local local_unnamed_addr global i32, align 4
@demoplayback = external dso_local local_unnamed_addr global i32, align 4
@demorecording = external dso_local local_unnamed_addr global i32, align 4
@netgame = external dso_local local_unnamed_addr global i32, align 4
@netdemo = external dso_local local_unnamed_addr global i32, align 4
@respawnparm = external local_unnamed_addr global i32, align 4
@fastparm = external local_unnamed_addr global i32, align 4
@nomonsters = external local_unnamed_addr global i32, align 4
@demo_p = external dso_local local_unnamed_addr global ptr, align 8
@demoname = external dso_local global [32 x i8], align 16
@demobuffer = external dso_local local_unnamed_addr global ptr, align 8
@timingdemo = external dso_local local_unnamed_addr global i32, align 4
@.str.22 = external hidden unnamed_addr constant [33 x i8], align 1
@.str.23 = external hidden unnamed_addr constant [107 x i8], align 1
@.str.24 = external hidden unnamed_addr constant [17 x i8], align 1

declare i32 @I_GetTime() local_unnamed_addr #0

; Function Attrs: noreturn
declare void @I_Error(ptr noundef, ...) local_unnamed_addr #1

declare void @Z_Free(ptr noundef) local_unnamed_addr #0

declare i32 @M_WriteFile(ptr noundef, ptr noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local range(i32 0, 2) i32 @G_CheckDemoStatus() local_unnamed_addr #2 {
  %1 = load i32, ptr @timingdemo, align 4, !tbaa !5
  %.not = icmp eq i32 %1, 0
  br i1 %.not, label %7, label %2

2:                                                ; preds = %0
  %3 = tail call i32 @I_GetTime() #3
  %4 = load i32, ptr @gametic, align 4, !tbaa !5
  %5 = load i32, ptr @starttime, align 4, !tbaa !5
  %6 = sub nsw i32 %3, %5
  tail call void (ptr, ...) @I_Error(ptr noundef nonnull @.str.22, i32 noundef %4, i32 noundef %6) #4
  unreachable

7:                                                ; preds = %0
  %8 = load i32, ptr @demoplayback, align 4, !tbaa !5
  %.not2 = icmp eq i32 %8, 0
  br i1 %.not2, label %18, label %9

9:                                                ; preds = %7
  %10 = load i32, ptr @singledemo, align 4, !tbaa !5
  %.not4 = icmp eq i32 %10, 0
  br i1 %.not4, label %12, label %11

11:                                               ; preds = %9
  tail call void @I_Quit() #3
  br label %12

12:                                               ; preds = %11, %9
  %13 = load ptr, ptr @demobuffer, align 8, !tbaa !9
  %14 = getelementptr inbounds i8, ptr %13, i64 -28
  %15 = load i32, ptr %14, align 4, !tbaa !11
  %.not5 = icmp eq i32 %15, 1919505
  br i1 %.not5, label %17, label %16

16:                                               ; preds = %12
  tail call void (ptr, ...) @I_Error(ptr noundef nonnull @.str.23, i32 noundef 1612) #4
  unreachable

17:                                               ; preds = %12
  tail call void @Z_ChangeTag2(ptr noundef nonnull %13, i32 noundef 101) #3
  store i32 0, ptr @demoplayback, align 4, !tbaa !5
  store i32 0, ptr @netdemo, align 4, !tbaa !5
  store i32 0, ptr @netgame, align 4, !tbaa !5
  store i32 0, ptr @deathmatch, align 4, !tbaa !5
  store i32 0, ptr getelementptr inbounds (i8, ptr @playeringame, i64 12), align 4, !tbaa !5
  store i32 0, ptr getelementptr inbounds (i8, ptr @playeringame, i64 8), align 8, !tbaa !5
  store i32 0, ptr getelementptr inbounds (i8, ptr @playeringame, i64 4), align 4, !tbaa !5
  store i32 0, ptr @respawnparm, align 4, !tbaa !5
  store i32 0, ptr @fastparm, align 4, !tbaa !5
  store i32 0, ptr @nomonsters, align 4, !tbaa !5
  store i32 0, ptr @consoleplayer, align 4, !tbaa !5
  tail call void @D_AdvanceDemo() #3
  br label %31

18:                                               ; preds = %7
  %19 = load i32, ptr @demorecording, align 4, !tbaa !5
  %.not3 = icmp eq i32 %19, 0
  br i1 %.not3, label %31, label %20

20:                                               ; preds = %18
  %21 = load ptr, ptr @demo_p, align 8, !tbaa !9
  %22 = getelementptr inbounds i8, ptr %21, i64 1
  store ptr %22, ptr @demo_p, align 8, !tbaa !9
  store i8 -128, ptr %21, align 1, !tbaa !13
  %23 = load ptr, ptr @demobuffer, align 8, !tbaa !9
  %24 = load ptr, ptr @demo_p, align 8, !tbaa !9
  %25 = ptrtoint ptr %24 to i64
  %26 = ptrtoint ptr %23 to i64
  %27 = sub i64 %25, %26
  %28 = trunc i64 %27 to i32
  %29 = tail call i32 @M_WriteFile(ptr noundef nonnull @demoname, ptr noundef %23, i32 noundef %28) #3
  %30 = load ptr, ptr @demobuffer, align 8, !tbaa !9
  tail call void @Z_Free(ptr noundef %30) #3
  store i32 0, ptr @demorecording, align 4, !tbaa !5
  tail call void (ptr, ...) @I_Error(ptr noundef nonnull @.str.24, ptr noundef nonnull @demoname) #4
  unreachable

31:                                               ; preds = %18, %17
  %.0 = phi i32 [ 1, %17 ], [ 0, %18 ]
  ret i32 %.0
}

declare void @I_Quit() local_unnamed_addr #0

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

declare void @D_AdvanceDemo() local_unnamed_addr #0

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 = { noreturn "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 }
attributes #4 = { noreturn 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, !6, i64 20}
!12 = !{!"memblock_s", !6, i64 0, !6, i64 4, !10, i64 8, !6, i64 16, !6, i64 20, !10, i64 24, !10, i64 32, !6, i64 40}
!13 = !{!7, !7, i64 0}


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