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

@gamemap = external dso_local global i32, align 4
@playeringame = external dso_local global [4 x i32], align 16
@gameepisode = external dso_local global i32, align 4
@gameskill = external dso_local global i32, align 4
@gameaction = external dso_local global i32, align 4
@leveltime = external global i32, align 4
@savename = external dso_local global [256 x i8], align 16
@savebuffer = external dso_local global ptr, align 8
@save_p = external global ptr, align 8
@.str.9 = external hidden unnamed_addr constant [11 x i8], align 1
@.str.10 = external hidden unnamed_addr constant [13 x i8], align 1
@setsizeneeded = external global i32, 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: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #1

; Function Attrs: nounwind uwtable
define dso_local void @G_DoLoadGame() #2 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca [16 x i8], align 16
  %7 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #7
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #7
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #7
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #7
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #7
  call void @llvm.lifetime.start.p0(i64 16, ptr %6) #7
  store i32 0, ptr @gameaction, align 4, !tbaa !5
  %8 = call i32 @M_ReadFile(ptr noundef @savename, ptr noundef @savebuffer)
  store i32 %8, ptr %1, align 4, !tbaa !5
  %9 = load i32, ptr %1, align 4, !tbaa !5
  %10 = load ptr, ptr @savebuffer, align 8, !tbaa !9
  %11 = getelementptr inbounds i8, ptr %10, i64 24
  store ptr %11, ptr @save_p, align 8, !tbaa !9
  %12 = getelementptr inbounds [16 x i8], ptr %6, i64 0, i64 0
  call void @llvm.memset.p0.i64(ptr align 16 %12, i8 0, i64 16, i1 false)
  %13 = getelementptr inbounds [16 x i8], ptr %6, i64 0, i64 0
  %14 = call i32 (ptr, ptr, ...) @sprintf(ptr noundef %13, ptr noundef @.str.9, i32 noundef 110) #7
  %15 = load ptr, ptr @save_p, align 8, !tbaa !9
  %16 = getelementptr inbounds [16 x i8], ptr %6, i64 0, i64 0
  %17 = call i32 @strcmp(ptr noundef %15, ptr noundef %16) #8
  %18 = icmp ne i32 %17, 0
  br i1 %18, label %19, label %20

19:                                               ; preds = %0
  store i32 1, ptr %7, align 4
  br label %83

20:                                               ; preds = %0
  %21 = load ptr, ptr @save_p, align 8, !tbaa !9
  %22 = getelementptr inbounds i8, ptr %21, i64 16
  store ptr %22, ptr @save_p, align 8, !tbaa !9
  %23 = load ptr, ptr @save_p, align 8, !tbaa !9
  %24 = getelementptr inbounds i8, ptr %23, i32 1
  store ptr %24, ptr @save_p, align 8, !tbaa !9
  %25 = load i8, ptr %23, align 1, !tbaa !11
  %26 = zext i8 %25 to i32
  store i32 %26, ptr @gameskill, align 4, !tbaa !5
  %27 = load ptr, ptr @save_p, align 8, !tbaa !9
  %28 = getelementptr inbounds i8, ptr %27, i32 1
  store ptr %28, ptr @save_p, align 8, !tbaa !9
  %29 = load i8, ptr %27, align 1, !tbaa !11
  %30 = zext i8 %29 to i32
  store i32 %30, ptr @gameepisode, align 4, !tbaa !5
  %31 = load ptr, ptr @save_p, align 8, !tbaa !9
  %32 = getelementptr inbounds i8, ptr %31, i32 1
  store ptr %32, ptr @save_p, align 8, !tbaa !9
  %33 = load i8, ptr %31, align 1, !tbaa !11
  %34 = zext i8 %33 to i32
  store i32 %34, ptr @gamemap, align 4, !tbaa !5
  store i32 0, ptr %2, align 4, !tbaa !5
  br label %35

35:                                               ; preds = %46, %20
  %36 = load i32, ptr %2, align 4, !tbaa !5
  %37 = icmp slt i32 %36, 4
  br i1 %37, label %38, label %49

38:                                               ; preds = %35
  %39 = load ptr, ptr @save_p, align 8, !tbaa !9
  %40 = getelementptr inbounds i8, ptr %39, i32 1
  store ptr %40, ptr @save_p, align 8, !tbaa !9
  %41 = load i8, ptr %39, align 1, !tbaa !11
  %42 = zext i8 %41 to i32
  %43 = load i32, ptr %2, align 4, !tbaa !5
  %44 = sext i32 %43 to i64
  %45 = getelementptr inbounds [4 x i32], ptr @playeringame, i64 0, i64 %44
  store i32 %42, ptr %45, align 4, !tbaa !5
  br label %46

46:                                               ; preds = %38
  %47 = load i32, ptr %2, align 4, !tbaa !5
  %48 = add nsw i32 %47, 1
  store i32 %48, ptr %2, align 4, !tbaa !5
  br label %35, !llvm.loop !12

49:                                               ; preds = %35
  %50 = load i32, ptr @gameskill, align 4, !tbaa !5
  %51 = load i32, ptr @gameepisode, align 4, !tbaa !5
  %52 = load i32, ptr @gamemap, align 4, !tbaa !5
  call void @G_InitNew(i32 noundef %50, i32 noundef %51, i32 noundef %52)
  %53 = load ptr, ptr @save_p, align 8, !tbaa !9
  %54 = getelementptr inbounds i8, ptr %53, i32 1
  store ptr %54, ptr @save_p, align 8, !tbaa !9
  %55 = load i8, ptr %53, align 1, !tbaa !11
  %56 = zext i8 %55 to i32
  store i32 %56, ptr %3, align 4, !tbaa !5
  %57 = load ptr, ptr @save_p, align 8, !tbaa !9
  %58 = getelementptr inbounds i8, ptr %57, i32 1
  store ptr %58, ptr @save_p, align 8, !tbaa !9
  %59 = load i8, ptr %57, align 1, !tbaa !11
  %60 = zext i8 %59 to i32
  store i32 %60, ptr %4, align 4, !tbaa !5
  %61 = load ptr, ptr @save_p, align 8, !tbaa !9
  %62 = getelementptr inbounds i8, ptr %61, i32 1
  store ptr %62, ptr @save_p, align 8, !tbaa !9
  %63 = load i8, ptr %61, align 1, !tbaa !11
  %64 = zext i8 %63 to i32
  store i32 %64, ptr %5, align 4, !tbaa !5
  %65 = load i32, ptr %3, align 4, !tbaa !5
  %66 = shl i32 %65, 16
  %67 = load i32, ptr %4, align 4, !tbaa !5
  %68 = shl i32 %67, 8
  %69 = add nsw i32 %66, %68
  %70 = load i32, ptr %5, align 4, !tbaa !5
  %71 = add nsw i32 %69, %70
  store i32 %71, ptr @leveltime, align 4, !tbaa !5
  call void @P_UnArchivePlayers()
  call void @P_UnArchiveWorld()
  call void @P_UnArchiveThinkers()
  call void @P_UnArchiveSpecials()
  %72 = load ptr, ptr @save_p, align 8, !tbaa !9
  %73 = load i8, ptr %72, align 1, !tbaa !11
  %74 = zext i8 %73 to i32
  %75 = icmp ne i32 %74, 29
  br i1 %75, label %76, label %77

76:                                               ; preds = %49
  call void (ptr, ...) @I_Error(ptr noundef @.str.10) #9
  unreachable

77:                                               ; preds = %49
  %78 = load ptr, ptr @savebuffer, align 8, !tbaa !9
  call void @Z_Free(ptr noundef %78)
  %79 = load i32, ptr @setsizeneeded, align 4, !tbaa !5
  %80 = icmp ne i32 %79, 0
  br i1 %80, label %81, label %82

81:                                               ; preds = %77
  call void @R_ExecuteSetViewSize()
  br label %82

82:                                               ; preds = %81, %77
  call void @R_FillBackScreen()
  store i32 0, ptr %7, align 4
  br label %83

83:                                               ; preds = %82, %19
  call void @llvm.lifetime.end.p0(i64 16, ptr %6) #7
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #7
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #7
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #7
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #7
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #7
  %84 = load i32, ptr %7, align 4
  switch i32 %84, label %86 [
    i32 0, label %85
    i32 1, label %85
  ]

85:                                               ; preds = %83, %83
  ret void

86:                                               ; preds = %83
  unreachable
}

; Function Attrs: nounwind
declare i32 @sprintf(ptr noundef, ptr noundef, ...) #3

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

declare i32 @M_ReadFile(ptr noundef, ptr noundef) #5

; Function Attrs: nounwind willreturn memory(read)
declare i32 @strcmp(ptr noundef, ptr noundef) #6

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

declare void @P_UnArchivePlayers() #5

declare void @P_UnArchiveWorld() #5

declare void @P_UnArchiveThinkers() #5

declare void @P_UnArchiveSpecials() #5

declare void @Z_Free(ptr noundef) #5

declare void @R_ExecuteSetViewSize() #5

declare void @R_FillBackScreen() #5

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nounwind willreturn memory(argmem: write) }
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 "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 = { 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 #5 = { "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 #6 = { 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 #7 = { nounwind }
attributes #8 = { nounwind willreturn memory(read) }
attributes #9 = { 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 = !{!7, !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"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"

@gamemap = external dso_local local_unnamed_addr global i32, align 4
@playeringame = external dso_local local_unnamed_addr global [4 x i32], align 16
@gameepisode = external dso_local local_unnamed_addr global i32, align 4
@gameskill = external dso_local local_unnamed_addr global i32, align 4
@gameaction = external dso_local local_unnamed_addr global i32, align 4
@leveltime = external local_unnamed_addr global i32, align 4
@savename = external dso_local global [256 x i8], align 16
@savebuffer = external dso_local global ptr, align 8
@save_p = external local_unnamed_addr global ptr, align 8
@.str.9 = external hidden unnamed_addr constant [11 x i8], align 1
@.str.10 = external hidden unnamed_addr constant [13 x i8], align 1
@setsizeneeded = external local_unnamed_addr global i32, align 4

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

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

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #1

; Function Attrs: nounwind uwtable
define dso_local void @G_DoLoadGame() local_unnamed_addr #2 {
  %1 = alloca [16 x i8], align 16
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %1) #7
  store i32 0, ptr @gameaction, align 4, !tbaa !5
  %2 = tail call i32 @M_ReadFile(ptr noundef nonnull @savename, ptr noundef nonnull @savebuffer) #7
  %3 = load ptr, ptr @savebuffer, align 8, !tbaa !9
  %4 = getelementptr inbounds i8, ptr %3, i64 24
  store ptr %4, ptr @save_p, align 8, !tbaa !9
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(16) %1, i8 0, i64 16, i1 false)
  %5 = call i32 (ptr, ptr, ...) @sprintf(ptr noundef nonnull dereferenceable(1) %1, ptr noundef nonnull dereferenceable(1) @.str.9, i32 noundef 110) #7
  %6 = load ptr, ptr @save_p, align 8, !tbaa !9
  %7 = call i32 @strcmp(ptr noundef nonnull dereferenceable(1) %6, ptr noundef nonnull dereferenceable(1) %1) #8
  %.not = icmp eq i32 %7, 0
  br i1 %.not, label %8, label %44

8:                                                ; preds = %0
  %9 = getelementptr inbounds i8, ptr %6, i64 16
  %10 = getelementptr inbounds i8, ptr %6, i64 17
  %11 = load i8, ptr %9, align 1, !tbaa !11
  %12 = zext i8 %11 to i32
  store i32 %12, ptr @gameskill, align 4, !tbaa !5
  %13 = getelementptr inbounds i8, ptr %6, i64 18
  %14 = load i8, ptr %10, align 1, !tbaa !11
  %15 = zext i8 %14 to i32
  store i32 %15, ptr @gameepisode, align 4, !tbaa !5
  %16 = getelementptr inbounds i8, ptr %6, i64 19
  %17 = load i8, ptr %13, align 1, !tbaa !11
  %18 = zext i8 %17 to i32
  store i32 %18, ptr @gamemap, align 4, !tbaa !5
  %19 = getelementptr inbounds i8, ptr %6, i64 23
  store ptr %19, ptr @save_p, align 8, !tbaa !9
  %20 = load <4 x i8>, ptr %16, align 1, !tbaa !11
  %21 = zext <4 x i8> %20 to <4 x i32>
  store <4 x i32> %21, ptr @playeringame, align 16, !tbaa !5
  tail call void @G_InitNew(i32 noundef %12, i32 noundef %15, i32 noundef %18)
  %22 = load ptr, ptr @save_p, align 8, !tbaa !9
  %23 = getelementptr inbounds i8, ptr %22, i64 1
  store ptr %23, ptr @save_p, align 8, !tbaa !9
  %24 = load i8, ptr %22, align 1, !tbaa !11
  %25 = zext i8 %24 to i32
  %26 = getelementptr inbounds i8, ptr %22, i64 2
  store ptr %26, ptr @save_p, align 8, !tbaa !9
  %27 = load i8, ptr %23, align 1, !tbaa !11
  %28 = zext i8 %27 to i32
  %29 = getelementptr inbounds i8, ptr %22, i64 3
  store ptr %29, ptr @save_p, align 8, !tbaa !9
  %30 = load i8, ptr %26, align 1, !tbaa !11
  %31 = zext i8 %30 to i32
  %32 = shl nuw nsw i32 %25, 16
  %33 = shl nuw nsw i32 %28, 8
  %34 = or disjoint i32 %33, %32
  %35 = or disjoint i32 %34, %31
  store i32 %35, ptr @leveltime, align 4, !tbaa !5
  tail call void @P_UnArchivePlayers() #7
  tail call void @P_UnArchiveWorld() #7
  tail call void @P_UnArchiveThinkers() #7
  tail call void @P_UnArchiveSpecials() #7
  %36 = load ptr, ptr @save_p, align 8, !tbaa !9
  %37 = load i8, ptr %36, align 1, !tbaa !11
  %.not7 = icmp eq i8 %37, 29
  br i1 %.not7, label %39, label %38

38:                                               ; preds = %8
  tail call void (ptr, ...) @I_Error(ptr noundef nonnull @.str.10) #9
  unreachable

39:                                               ; preds = %8
  %40 = load ptr, ptr @savebuffer, align 8, !tbaa !9
  tail call void @Z_Free(ptr noundef %40) #7
  %41 = load i32, ptr @setsizeneeded, align 4, !tbaa !5
  %.not8 = icmp eq i32 %41, 0
  br i1 %.not8, label %43, label %42

42:                                               ; preds = %39
  tail call void @R_ExecuteSetViewSize() #7
  br label %43

43:                                               ; preds = %42, %39
  tail call void @R_FillBackScreen() #7
  br label %44

44:                                               ; preds = %0, %43
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %1) #7
  ret void
}

; Function Attrs: nofree nounwind
declare noundef i32 @sprintf(ptr noalias nocapture noundef writeonly, ptr nocapture noundef readonly, ...) local_unnamed_addr #3

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

declare i32 @M_ReadFile(ptr noundef, ptr noundef) local_unnamed_addr #5

; Function Attrs: mustprogress nofree nounwind willreturn memory(argmem: read)
declare i32 @strcmp(ptr nocapture noundef, ptr nocapture noundef) local_unnamed_addr #6

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

declare void @P_UnArchivePlayers() local_unnamed_addr #5

declare void @P_UnArchiveWorld() local_unnamed_addr #5

declare void @P_UnArchiveThinkers() local_unnamed_addr #5

declare void @P_UnArchiveSpecials() local_unnamed_addr #5

declare void @Z_Free(ptr noundef) local_unnamed_addr #5

declare void @R_ExecuteSetViewSize() local_unnamed_addr #5

declare void @R_FillBackScreen() local_unnamed_addr #5

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
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 = { nofree nounwind "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 = { 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 #5 = { "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 #6 = { 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 #7 = { nounwind }
attributes #8 = { nounwind willreturn memory(read) }
attributes #9 = { 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 = !{!7, !7, i64 0}


; Effective O3 passes: DSEPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,LICMPass,LoopRotatePass,ReassociatePass,SLPVectorizerPass,SROAPass,SimplifyCFGPass,TailCallElimPass