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

; 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.weaponinfo_t = type { i32, i32, i32, i32, i32, i32 }
%struct.player_s = type { ptr, i32, %struct.ticcmd_t, i32, i32, i32, i32, i32, i32, i32, [6 x i32], [6 x i32], i32, [4 x i32], i32, i32, [9 x i32], [4 x i32], [4 x i32], i32, i32, i32, i32, i32, i32, i32, ptr, i32, i32, ptr, i32, i32, i32, [2 x %struct.pspdef_t], i32 }
%struct.ticcmd_t = type { i8, i8, i16, i16, i8, i8 }
%struct.pspdef_t = type { ptr, i32, i32, i32 }

@netgame = external global i32, align 4
@deathmatch = external global i32, align 4
@weaponinfo = external global [9 x %struct.weaponinfo_t], align 16
@players = external global [4 x %struct.player_s], align 16
@consoleplayer = external global i32, align 4

; Function Attrs: nounwind uwtable
declare dso_local i32 @P_GiveAmmo(ptr noundef, i32 noundef, i32 noundef) #0

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

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

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

13:                                               ; preds = %3
  %14 = load i32, ptr @deathmatch, align 4, !tbaa !9
  %15 = icmp ne i32 %14, 2
  br i1 %15, label %16, label %67

16:                                               ; preds = %13
  %17 = load i32, ptr %7, align 4, !tbaa !9
  %18 = icmp ne i32 %17, 0
  br i1 %18, label %67, label %19

19:                                               ; preds = %16
  %20 = load ptr, ptr %5, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.player_s, ptr %20, i32 0, i32 16
  %22 = load i32, ptr %6, align 4, !tbaa !9
  %23 = zext i32 %22 to i64
  %24 = getelementptr inbounds [9 x i32], ptr %21, i64 0, i64 %23
  %25 = load i32, ptr %24, align 4, !tbaa !9
  %26 = icmp ne i32 %25, 0
  br i1 %26, label %27, label %28

27:                                               ; preds = %19
  store i32 0, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %122

28:                                               ; preds = %19
  %29 = load ptr, ptr %5, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.player_s, ptr %29, i32 0, i32 28
  %31 = load i32, ptr %30, align 4, !tbaa !11
  %32 = add nsw i32 %31, 6
  store i32 %32, ptr %30, align 4, !tbaa !11
  %33 = load ptr, ptr %5, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.player_s, ptr %33, i32 0, i32 16
  %35 = load i32, ptr %6, align 4, !tbaa !9
  %36 = zext i32 %35 to i64
  %37 = getelementptr inbounds [9 x i32], ptr %34, i64 0, i64 %36
  store i32 1, ptr %37, align 4, !tbaa !9
  %38 = load i32, ptr @deathmatch, align 4, !tbaa !9
  %39 = icmp ne i32 %38, 0
  br i1 %39, label %40, label %48

40:                                               ; preds = %28
  %41 = load ptr, ptr %5, align 8, !tbaa !5
  %42 = load i32, ptr %6, align 4, !tbaa !9
  %43 = zext i32 %42 to i64
  %44 = getelementptr inbounds [9 x %struct.weaponinfo_t], ptr @weaponinfo, i64 0, i64 %43
  %45 = getelementptr inbounds %struct.weaponinfo_t, ptr %44, i32 0, i32 0
  %46 = load i32, ptr %45, align 8, !tbaa !15
  %47 = call i32 @P_GiveAmmo(ptr noundef %41, i32 noundef %46, i32 noundef 5)
  br label %56

48:                                               ; preds = %28
  %49 = load ptr, ptr %5, align 8, !tbaa !5
  %50 = load i32, ptr %6, align 4, !tbaa !9
  %51 = zext i32 %50 to i64
  %52 = getelementptr inbounds [9 x %struct.weaponinfo_t], ptr @weaponinfo, i64 0, i64 %51
  %53 = getelementptr inbounds %struct.weaponinfo_t, ptr %52, i32 0, i32 0
  %54 = load i32, ptr %53, align 8, !tbaa !15
  %55 = call i32 @P_GiveAmmo(ptr noundef %49, i32 noundef %54, i32 noundef 2)
  br label %56

56:                                               ; preds = %48, %40
  %57 = load i32, ptr %6, align 4, !tbaa !9
  %58 = load ptr, ptr %5, align 8, !tbaa !5
  %59 = getelementptr inbounds %struct.player_s, ptr %58, i32 0, i32 15
  store i32 %57, ptr %59, align 8, !tbaa !17
  %60 = load ptr, ptr %5, align 8, !tbaa !5
  %61 = load i32, ptr @consoleplayer, align 4, !tbaa !9
  %62 = sext i32 %61 to i64
  %63 = getelementptr inbounds [4 x %struct.player_s], ptr @players, i64 0, i64 %62
  %64 = icmp eq ptr %60, %63
  br i1 %64, label %65, label %66

65:                                               ; preds = %56
  call void @S_StartSound(ptr noundef null, i32 noundef 33)
  br label %66

66:                                               ; preds = %65, %56
  store i32 0, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %122

67:                                               ; preds = %16, %13, %3
  %68 = load i32, ptr %6, align 4, !tbaa !9
  %69 = zext i32 %68 to i64
  %70 = getelementptr inbounds [9 x %struct.weaponinfo_t], ptr @weaponinfo, i64 0, i64 %69
  %71 = getelementptr inbounds %struct.weaponinfo_t, ptr %70, i32 0, i32 0
  %72 = load i32, ptr %71, align 8, !tbaa !15
  %73 = icmp ne i32 %72, 5
  br i1 %73, label %74, label %94

74:                                               ; preds = %67
  %75 = load i32, ptr %7, align 4, !tbaa !9
  %76 = icmp ne i32 %75, 0
  br i1 %76, label %77, label %85

77:                                               ; preds = %74
  %78 = load ptr, ptr %5, align 8, !tbaa !5
  %79 = load i32, ptr %6, align 4, !tbaa !9
  %80 = zext i32 %79 to i64
  %81 = getelementptr inbounds [9 x %struct.weaponinfo_t], ptr @weaponinfo, i64 0, i64 %80
  %82 = getelementptr inbounds %struct.weaponinfo_t, ptr %81, i32 0, i32 0
  %83 = load i32, ptr %82, align 8, !tbaa !15
  %84 = call i32 @P_GiveAmmo(ptr noundef %78, i32 noundef %83, i32 noundef 1)
  store i32 %84, ptr %8, align 4, !tbaa !9
  br label %93

85:                                               ; preds = %74
  %86 = load ptr, ptr %5, align 8, !tbaa !5
  %87 = load i32, ptr %6, align 4, !tbaa !9
  %88 = zext i32 %87 to i64
  %89 = getelementptr inbounds [9 x %struct.weaponinfo_t], ptr @weaponinfo, i64 0, i64 %88
  %90 = getelementptr inbounds %struct.weaponinfo_t, ptr %89, i32 0, i32 0
  %91 = load i32, ptr %90, align 8, !tbaa !15
  %92 = call i32 @P_GiveAmmo(ptr noundef %86, i32 noundef %91, i32 noundef 2)
  store i32 %92, ptr %8, align 4, !tbaa !9
  br label %93

93:                                               ; preds = %85, %77
  br label %95

94:                                               ; preds = %67
  store i32 0, ptr %8, align 4, !tbaa !9
  br label %95

95:                                               ; preds = %94, %93
  %96 = load ptr, ptr %5, align 8, !tbaa !5
  %97 = getelementptr inbounds %struct.player_s, ptr %96, i32 0, i32 16
  %98 = load i32, ptr %6, align 4, !tbaa !9
  %99 = zext i32 %98 to i64
  %100 = getelementptr inbounds [9 x i32], ptr %97, i64 0, i64 %99
  %101 = load i32, ptr %100, align 4, !tbaa !9
  %102 = icmp ne i32 %101, 0
  br i1 %102, label %103, label %104

103:                                              ; preds = %95
  store i32 0, ptr %9, align 4, !tbaa !9
  br label %113

104:                                              ; preds = %95
  store i32 1, ptr %9, align 4, !tbaa !9
  %105 = load ptr, ptr %5, align 8, !tbaa !5
  %106 = getelementptr inbounds %struct.player_s, ptr %105, i32 0, i32 16
  %107 = load i32, ptr %6, align 4, !tbaa !9
  %108 = zext i32 %107 to i64
  %109 = getelementptr inbounds [9 x i32], ptr %106, i64 0, i64 %108
  store i32 1, ptr %109, align 4, !tbaa !9
  %110 = load i32, ptr %6, align 4, !tbaa !9
  %111 = load ptr, ptr %5, align 8, !tbaa !5
  %112 = getelementptr inbounds %struct.player_s, ptr %111, i32 0, i32 15
  store i32 %110, ptr %112, align 8, !tbaa !17
  br label %113

113:                                              ; preds = %104, %103
  %114 = load i32, ptr %9, align 4, !tbaa !9
  %115 = icmp ne i32 %114, 0
  br i1 %115, label %119, label %116

116:                                              ; preds = %113
  %117 = load i32, ptr %8, align 4, !tbaa !9
  %118 = icmp ne i32 %117, 0
  br label %119

119:                                              ; preds = %116, %113
  %120 = phi i1 [ true, %113 ], [ %118, %116 ]
  %121 = zext i1 %120 to i32
  store i32 %121, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %122

122:                                              ; preds = %119, %66, %27
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  %123 = load i32, ptr %4, align 4
  ret i32 %123
}

declare void @S_StartSound(ptr noundef, i32 noundef) #2

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, !6, i64 0}
!6 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !10, i64 236}
!12 = !{!"player_s", !6, i64 0, !10, i64 8, !13, i64 12, !10, i64 20, !10, i64 24, !10, i64 28, !10, i64 32, !10, i64 36, !10, i64 40, !10, i64 44, !7, i64 48, !7, i64 72, !10, i64 96, !7, i64 100, !10, i64 116, !10, i64 120, !7, i64 124, !7, i64 160, !7, i64 176, !10, i64 192, !10, i64 196, !10, i64 200, !10, i64 204, !10, i64 208, !10, i64 212, !10, i64 216, !6, i64 224, !10, i64 232, !10, i64 236, !6, i64 240, !10, i64 248, !10, i64 252, !10, i64 256, !7, i64 264, !10, i64 312}
!13 = !{!"", !7, i64 0, !7, i64 1, !14, i64 2, !14, i64 4, !7, i64 6, !7, i64 7}
!14 = !{!"short", !7, i64 0}
!15 = !{!16, !10, i64 0}
!16 = !{!"", !10, i64 0, !10, i64 4, !10, i64 8, !10, i64 12, !10, i64 16, !10, i64 20}
!17 = !{!12, !10, i64 120}


; 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.weaponinfo_t = type { i32, i32, i32, i32, i32, i32 }
%struct.player_s = type { ptr, i32, %struct.ticcmd_t, i32, i32, i32, i32, i32, i32, i32, [6 x i32], [6 x i32], i32, [4 x i32], i32, i32, [9 x i32], [4 x i32], [4 x i32], i32, i32, i32, i32, i32, i32, i32, ptr, i32, i32, ptr, i32, i32, i32, [2 x %struct.pspdef_t], i32 }
%struct.ticcmd_t = type { i8, i8, i16, i16, i8, i8 }
%struct.pspdef_t = type { ptr, i32, i32, i32 }

@netgame = external local_unnamed_addr global i32, align 4
@deathmatch = external local_unnamed_addr global i32, align 4
@weaponinfo = external local_unnamed_addr global [9 x %struct.weaponinfo_t], align 16
@players = external global [4 x %struct.player_s], align 16
@consoleplayer = external local_unnamed_addr global i32, align 4

; Function Attrs: nounwind uwtable
declare dso_local i32 @P_GiveAmmo(ptr noundef, i32 noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local range(i32 0, 2) i32 @P_GiveWeapon(ptr noundef %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #0 {
  %4 = load i32, ptr @netgame, align 4, !tbaa !5
  %5 = icmp eq i32 %4, 0
  %6 = load i32, ptr @deathmatch, align 4
  %7 = icmp eq i32 %6, 2
  %or.cond.not34 = select i1 %5, i1 true, i1 %7
  %8 = icmp ne i32 %2, 0
  %or.cond3 = or i1 %8, %or.cond.not34
  br i1 %or.cond3, label %28, label %9

9:                                                ; preds = %3
  %10 = getelementptr inbounds i8, ptr %0, i64 124
  %11 = zext i32 %1 to i64
  %12 = getelementptr inbounds [9 x i32], ptr %10, i64 0, i64 %11
  %13 = load i32, ptr %12, align 4, !tbaa !5
  %.not = icmp eq i32 %13, 0
  br i1 %.not, label %14, label %43

14:                                               ; preds = %9
  %15 = getelementptr inbounds i8, ptr %0, i64 236
  %16 = load i32, ptr %15, align 4, !tbaa !9
  %17 = add nsw i32 %16, 6
  store i32 %17, ptr %15, align 4, !tbaa !9
  store i32 1, ptr %12, align 4, !tbaa !5
  %18 = load i32, ptr @deathmatch, align 4, !tbaa !5
  %.not35 = icmp eq i32 %18, 0
  %19 = getelementptr inbounds [9 x %struct.weaponinfo_t], ptr @weaponinfo, i64 0, i64 %11
  %20 = load i32, ptr %19, align 8, !tbaa !14
  %. = select i1 %.not35, i32 2, i32 5
  %21 = tail call i32 @P_GiveAmmo(ptr noundef nonnull %0, i32 noundef %20, i32 noundef %.)
  %22 = getelementptr inbounds i8, ptr %0, i64 120
  store i32 %1, ptr %22, align 8, !tbaa !16
  %23 = load i32, ptr @consoleplayer, align 4, !tbaa !5
  %24 = sext i32 %23 to i64
  %25 = getelementptr inbounds [4 x %struct.player_s], ptr @players, i64 0, i64 %24
  %26 = icmp eq ptr %25, %0
  br i1 %26, label %27, label %43

27:                                               ; preds = %14
  tail call void @S_StartSound(ptr noundef null, i32 noundef 33) #2
  br label %43

28:                                               ; preds = %3
  %29 = zext i32 %1 to i64
  %30 = getelementptr inbounds [9 x %struct.weaponinfo_t], ptr @weaponinfo, i64 0, i64 %29
  %31 = load i32, ptr %30, align 8, !tbaa !14
  %.not36 = icmp eq i32 %31, 5
  br i1 %.not36, label %34, label %.sink.split

.sink.split:                                      ; preds = %28
  %.39 = select i1 %8, i32 1, i32 2
  %32 = tail call i32 @P_GiveAmmo(ptr noundef %0, i32 noundef %31, i32 noundef %.39)
  %33 = icmp ne i32 %32, 0
  br label %34

34:                                               ; preds = %.sink.split, %28
  %.030 = phi i1 [ false, %28 ], [ %33, %.sink.split ]
  %35 = getelementptr inbounds i8, ptr %0, i64 124
  %36 = getelementptr inbounds [9 x i32], ptr %35, i64 0, i64 %29
  %37 = load i32, ptr %36, align 4, !tbaa !5
  %.not37 = icmp eq i32 %37, 0
  br i1 %.not37, label %38, label %40

38:                                               ; preds = %34
  store i32 1, ptr %36, align 4, !tbaa !5
  %39 = getelementptr inbounds i8, ptr %0, i64 120
  store i32 %1, ptr %39, align 8, !tbaa !16
  br label %40

40:                                               ; preds = %34, %38
  %41 = phi i1 [ %.030, %34 ], [ true, %38 ]
  %42 = zext i1 %41 to i32
  br label %43

43:                                               ; preds = %14, %27, %9, %40
  %.031 = phi i32 [ %42, %40 ], [ 0, %9 ], [ 0, %27 ], [ 0, %14 ]
  ret i32 %.031
}

declare void @S_StartSound(ptr noundef, i32 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, !6, i64 0}
!6 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !6, i64 236}
!10 = !{!"player_s", !11, i64 0, !6, i64 8, !12, i64 12, !6, i64 20, !6, i64 24, !6, i64 28, !6, i64 32, !6, i64 36, !6, i64 40, !6, i64 44, !7, i64 48, !7, i64 72, !6, i64 96, !7, i64 100, !6, i64 116, !6, i64 120, !7, i64 124, !7, i64 160, !7, i64 176, !6, i64 192, !6, i64 196, !6, i64 200, !6, i64 204, !6, i64 208, !6, i64 212, !6, i64 216, !11, i64 224, !6, i64 232, !6, i64 236, !11, i64 240, !6, i64 248, !6, i64 252, !6, i64 256, !7, i64 264, !6, i64 312}
!11 = !{!"any pointer", !7, i64 0}
!12 = !{!"", !7, i64 0, !7, i64 1, !13, i64 2, !13, i64 4, !7, i64 6, !7, i64 7}
!13 = !{!"short", !7, i64 0}
!14 = !{!15, !6, i64 0}
!15 = !{!"", !6, i64 0, !6, i64 4, !6, i64 8, !6, i64 12, !6, i64 16, !6, i64 20}
!16 = !{!10, !6, i64 120}


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