; LLVM IR from AlgoGenesis/C :: Miscellaneous Algorithms/DeadLock Detection (Program)

; 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.Process = type { i32, ptr, ptr, i32 }
%struct.Resource = type { i32, ptr, ptr }

@Phead = external dso_local global ptr, align 8
@Rhead = external dso_local global ptr, align 8
@.str = external hidden unnamed_addr constant [36 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [37 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [26 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [42 x i8], align 1

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

11:                                               ; preds = %23, %2
  %12 = load ptr, ptr %5, align 8, !tbaa !9
  %13 = icmp ne ptr %12, null
  br i1 %13, label %14, label %27

14:                                               ; preds = %11
  %15 = load ptr, ptr %5, align 8, !tbaa !9
  %16 = getelementptr inbounds %struct.Process, ptr %15, i32 0, i32 0
  %17 = load i32, ptr %16, align 8, !tbaa !11
  %18 = load i32, ptr %4, align 4, !tbaa !5
  %19 = icmp eq i32 %17, %18
  br i1 %19, label %20, label %23

20:                                               ; preds = %14
  %21 = load i32, ptr %4, align 4, !tbaa !5
  %22 = call i32 (ptr, ...) @printf(ptr noundef @.str, i32 noundef %21)
  store i32 1, ptr %6, align 4
  br label %117

23:                                               ; preds = %14
  %24 = load ptr, ptr %5, align 8, !tbaa !9
  %25 = getelementptr inbounds %struct.Process, ptr %24, i32 0, i32 1
  %26 = load ptr, ptr %25, align 8, !tbaa !13
  store ptr %26, ptr %5, align 8, !tbaa !9
  br label %11, !llvm.loop !14

27:                                               ; preds = %11
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #5
  %28 = load ptr, ptr @Rhead, align 8, !tbaa !9
  store ptr %28, ptr %7, align 8, !tbaa !9
  br label %29

29:                                               ; preds = %41, %27
  %30 = load ptr, ptr %7, align 8, !tbaa !9
  %31 = icmp ne ptr %30, null
  br i1 %31, label %32, label %45

32:                                               ; preds = %29
  %33 = load ptr, ptr %7, align 8, !tbaa !9
  %34 = getelementptr inbounds %struct.Resource, ptr %33, i32 0, i32 0
  %35 = load i32, ptr %34, align 8, !tbaa !16
  %36 = load i32, ptr %3, align 4, !tbaa !5
  %37 = icmp eq i32 %35, %36
  br i1 %37, label %38, label %41

38:                                               ; preds = %32
  %39 = load i32, ptr %3, align 4, !tbaa !5
  %40 = call i32 (ptr, ...) @printf(ptr noundef @.str.1, i32 noundef %39)
  store i32 1, ptr %6, align 4
  br label %116

41:                                               ; preds = %32
  %42 = load ptr, ptr %7, align 8, !tbaa !9
  %43 = getelementptr inbounds %struct.Resource, ptr %42, i32 0, i32 1
  %44 = load ptr, ptr %43, align 8, !tbaa !18
  store ptr %44, ptr %7, align 8, !tbaa !9
  br label %29, !llvm.loop !19

45:                                               ; preds = %29
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #5
  %46 = call noalias ptr @malloc(i64 noundef 32) #6
  store ptr %46, ptr %8, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #5
  %47 = call noalias ptr @malloc(i64 noundef 24) #6
  store ptr %47, ptr %9, align 8, !tbaa !9
  %48 = load ptr, ptr %8, align 8, !tbaa !9
  %49 = icmp eq ptr %48, null
  br i1 %49, label %53, label %50

50:                                               ; preds = %45
  %51 = load ptr, ptr %9, align 8, !tbaa !9
  %52 = icmp eq ptr %51, null
  br i1 %52, label %53, label %55

53:                                               ; preds = %50, %45
  %54 = call i32 (ptr, ...) @printf(ptr noundef @.str.2)
  call void @exit(i32 noundef 1) #7
  unreachable

55:                                               ; preds = %50
  %56 = load i32, ptr %4, align 4, !tbaa !5
  %57 = load ptr, ptr %8, align 8, !tbaa !9
  %58 = getelementptr inbounds %struct.Process, ptr %57, i32 0, i32 0
  store i32 %56, ptr %58, align 8, !tbaa !11
  %59 = load ptr, ptr %8, align 8, !tbaa !9
  %60 = getelementptr inbounds %struct.Process, ptr %59, i32 0, i32 2
  store ptr null, ptr %60, align 8, !tbaa !20
  %61 = load ptr, ptr %8, align 8, !tbaa !9
  %62 = getelementptr inbounds %struct.Process, ptr %61, i32 0, i32 1
  store ptr null, ptr %62, align 8, !tbaa !13
  %63 = load ptr, ptr %8, align 8, !tbaa !9
  %64 = getelementptr inbounds %struct.Process, ptr %63, i32 0, i32 3
  store i32 0, ptr %64, align 8, !tbaa !21
  %65 = load i32, ptr %3, align 4, !tbaa !5
  %66 = load ptr, ptr %9, align 8, !tbaa !9
  %67 = getelementptr inbounds %struct.Resource, ptr %66, i32 0, i32 0
  store i32 %65, ptr %67, align 8, !tbaa !16
  %68 = load ptr, ptr %8, align 8, !tbaa !9
  %69 = load ptr, ptr %9, align 8, !tbaa !9
  %70 = getelementptr inbounds %struct.Resource, ptr %69, i32 0, i32 2
  store ptr %68, ptr %70, align 8, !tbaa !22
  %71 = load ptr, ptr %9, align 8, !tbaa !9
  %72 = getelementptr inbounds %struct.Resource, ptr %71, i32 0, i32 1
  store ptr null, ptr %72, align 8, !tbaa !18
  %73 = load ptr, ptr @Phead, align 8, !tbaa !9
  %74 = icmp eq ptr %73, null
  br i1 %74, label %75, label %77

75:                                               ; preds = %55
  %76 = load ptr, ptr %8, align 8, !tbaa !9
  store ptr %76, ptr @Phead, align 8, !tbaa !9
  br label %92

77:                                               ; preds = %55
  %78 = load ptr, ptr @Phead, align 8, !tbaa !9
  store ptr %78, ptr %5, align 8, !tbaa !9
  br label %79

79:                                               ; preds = %84, %77
  %80 = load ptr, ptr %5, align 8, !tbaa !9
  %81 = getelementptr inbounds %struct.Process, ptr %80, i32 0, i32 1
  %82 = load ptr, ptr %81, align 8, !tbaa !13
  %83 = icmp ne ptr %82, null
  br i1 %83, label %84, label %88

84:                                               ; preds = %79
  %85 = load ptr, ptr %5, align 8, !tbaa !9
  %86 = getelementptr inbounds %struct.Process, ptr %85, i32 0, i32 1
  %87 = load ptr, ptr %86, align 8, !tbaa !13
  store ptr %87, ptr %5, align 8, !tbaa !9
  br label %79, !llvm.loop !23

88:                                               ; preds = %79
  %89 = load ptr, ptr %8, align 8, !tbaa !9
  %90 = load ptr, ptr %5, align 8, !tbaa !9
  %91 = getelementptr inbounds %struct.Process, ptr %90, i32 0, i32 1
  store ptr %89, ptr %91, align 8, !tbaa !13
  br label %92

92:                                               ; preds = %88, %75
  %93 = load ptr, ptr @Rhead, align 8, !tbaa !9
  %94 = icmp eq ptr %93, null
  br i1 %94, label %95, label %97

95:                                               ; preds = %92
  %96 = load ptr, ptr %9, align 8, !tbaa !9
  store ptr %96, ptr @Rhead, align 8, !tbaa !9
  br label %112

97:                                               ; preds = %92
  %98 = load ptr, ptr @Rhead, align 8, !tbaa !9
  store ptr %98, ptr %7, align 8, !tbaa !9
  br label %99

99:                                               ; preds = %104, %97
  %100 = load ptr, ptr %7, align 8, !tbaa !9
  %101 = getelementptr inbounds %struct.Resource, ptr %100, i32 0, i32 1
  %102 = load ptr, ptr %101, align 8, !tbaa !18
  %103 = icmp ne ptr %102, null
  br i1 %103, label %104, label %108

104:                                              ; preds = %99
  %105 = load ptr, ptr %7, align 8, !tbaa !9
  %106 = getelementptr inbounds %struct.Resource, ptr %105, i32 0, i32 1
  %107 = load ptr, ptr %106, align 8, !tbaa !18
  store ptr %107, ptr %7, align 8, !tbaa !9
  br label %99, !llvm.loop !24

108:                                              ; preds = %99
  %109 = load ptr, ptr %9, align 8, !tbaa !9
  %110 = load ptr, ptr %7, align 8, !tbaa !9
  %111 = getelementptr inbounds %struct.Resource, ptr %110, i32 0, i32 1
  store ptr %109, ptr %111, align 8, !tbaa !18
  br label %112

112:                                              ; preds = %108, %95
  %113 = load i32, ptr %3, align 4, !tbaa !5
  %114 = load i32, ptr %4, align 4, !tbaa !5
  %115 = call i32 (ptr, ...) @printf(ptr noundef @.str.3, i32 noundef %113, i32 noundef %114)
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #5
  store i32 0, ptr %6, align 4
  br label %116

116:                                              ; preds = %112, %38
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #5
  br label %117

117:                                              ; preds = %116, %20
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #5
  %118 = load i32, ptr %6, align 4
  switch i32 %118, label %120 [
    i32 0, label %119
    i32 1, label %119
  ]

119:                                              ; preds = %117, %117
  ret void

120:                                              ; preds = %117
  unreachable
}

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

declare i32 @printf(ptr noundef, ...) #2

; Function Attrs: nounwind allocsize(0)
declare noalias ptr @malloc(i64 noundef) #3

; Function Attrs: noreturn nounwind
declare void @exit(i32 noundef) #4

; 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 allocsize(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 = { noreturn 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 #5 = { nounwind }
attributes #6 = { nounwind allocsize(0) }
attributes #7 = { 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 0}
!12 = !{!"Process", !6, i64 0, !10, i64 8, !10, i64 16, !6, i64 24}
!13 = !{!12, !10, i64 8}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = !{!17, !6, i64 0}
!17 = !{!"Resource", !6, i64 0, !10, i64 8, !10, i64 16}
!18 = !{!17, !10, i64 8}
!19 = distinct !{!19, !15}
!20 = !{!12, !10, i64 16}
!21 = !{!12, !6, i64 24}
!22 = !{!17, !10, i64 16}
!23 = distinct !{!23, !15}
!24 = distinct !{!24, !15}


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

@Phead = external dso_local local_unnamed_addr global ptr, align 8
@Rhead = external dso_local local_unnamed_addr global ptr, align 8
@.str = external hidden unnamed_addr constant [36 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [37 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [26 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [42 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local void @Allocate(i32 noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %.050 = load ptr, ptr @Phead, align 8, !tbaa !5
  %.not51 = icmp eq ptr %.050, null
  br i1 %.not51, label %.preheader48, label %.lr.ph

3:                                                ; preds = %.lr.ph
  %4 = getelementptr inbounds i8, ptr %.052, i64 8
  %.0 = load ptr, ptr %4, align 8, !tbaa !5
  %.not = icmp eq ptr %.0, null
  br i1 %.not, label %.preheader48, label %.lr.ph, !llvm.loop !9

.preheader48:                                     ; preds = %3, %2
  %.03453 = load ptr, ptr @Rhead, align 8, !tbaa !5
  %.not4454 = icmp eq ptr %.03453, null
  br i1 %.not4454, label %._crit_edge, label %.lr.ph56

.lr.ph:                                           ; preds = %2, %3
  %.052 = phi ptr [ %.0, %3 ], [ %.050, %2 ]
  %5 = load i32, ptr %.052, align 8, !tbaa !11
  %6 = icmp eq i32 %5, %1
  br i1 %6, label %7, label %3

7:                                                ; preds = %.lr.ph
  %8 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str, i32 noundef %1)
  br label %36

9:                                                ; preds = %.lr.ph56
  %10 = getelementptr inbounds i8, ptr %.03455, i64 8
  %.034 = load ptr, ptr %10, align 8, !tbaa !5
  %.not44 = icmp eq ptr %.034, null
  br i1 %.not44, label %._crit_edge, label %.lr.ph56, !llvm.loop !14

.lr.ph56:                                         ; preds = %.preheader48, %9
  %.03455 = phi ptr [ %.034, %9 ], [ %.03453, %.preheader48 ]
  %11 = load i32, ptr %.03455, align 8, !tbaa !15
  %12 = icmp eq i32 %11, %0
  br i1 %12, label %13, label %9

13:                                               ; preds = %.lr.ph56
  %14 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1, i32 noundef %0)
  br label %36

._crit_edge:                                      ; preds = %9, %.preheader48
  %15 = tail call noalias dereferenceable_or_null(32) ptr @malloc(i64 noundef 32) #5
  %16 = tail call noalias dereferenceable_or_null(24) ptr @malloc(i64 noundef 24) #5
  %17 = icmp eq ptr %15, null
  %18 = icmp eq ptr %16, null
  %or.cond = or i1 %17, %18
  br i1 %or.cond, label %19, label %21

19:                                               ; preds = %._crit_edge
  %20 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.2)
  tail call void @exit(i32 noundef 1) #6
  unreachable

21:                                               ; preds = %._crit_edge
  store i32 %1, ptr %15, align 8, !tbaa !11
  %22 = getelementptr inbounds i8, ptr %15, i64 8
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(20) %22, i8 0, i64 20, i1 false)
  store i32 %0, ptr %16, align 8, !tbaa !15
  %23 = getelementptr inbounds i8, ptr %16, i64 16
  store ptr %15, ptr %23, align 8, !tbaa !17
  %24 = getelementptr inbounds i8, ptr %16, i64 8
  store ptr null, ptr %24, align 8, !tbaa !18
  br i1 %.not51, label %29, label %.preheader47

.preheader47:                                     ; preds = %21, %.preheader47
  %.1 = phi ptr [ %26, %.preheader47 ], [ %.050, %21 ]
  %25 = getelementptr inbounds i8, ptr %.1, i64 8
  %26 = load ptr, ptr %25, align 8, !tbaa !19
  %.not45 = icmp eq ptr %26, null
  br i1 %.not45, label %27, label %.preheader47, !llvm.loop !20

27:                                               ; preds = %.preheader47
  %28 = getelementptr inbounds i8, ptr %.1, i64 8
  br label %29

29:                                               ; preds = %21, %27
  %.sink = phi ptr [ %28, %27 ], [ @Phead, %21 ]
  store ptr %15, ptr %.sink, align 8, !tbaa !5
  br i1 %.not4454, label %34, label %.preheader

.preheader:                                       ; preds = %29, %.preheader
  %.135 = phi ptr [ %31, %.preheader ], [ %.03453, %29 ]
  %30 = getelementptr inbounds i8, ptr %.135, i64 8
  %31 = load ptr, ptr %30, align 8, !tbaa !18
  %.not46 = icmp eq ptr %31, null
  br i1 %.not46, label %32, label %.preheader, !llvm.loop !21

32:                                               ; preds = %.preheader
  %33 = getelementptr inbounds i8, ptr %.135, i64 8
  br label %34

34:                                               ; preds = %29, %32
  %.sink58 = phi ptr [ %33, %32 ], [ @Rhead, %29 ]
  store ptr %16, ptr %.sink58, align 8, !tbaa !5
  %35 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.3, i32 noundef %0, i32 noundef %1)
  br label %36

36:                                               ; preds = %13, %34, %7
  ret void
}

; Function Attrs: nofree nounwind
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) local_unnamed_addr #1

; Function Attrs: mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @malloc(i64 noundef) local_unnamed_addr #2

; Function Attrs: noreturn nounwind
declare void @exit(i32 noundef) local_unnamed_addr #3

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

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 = { 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 #2 = { mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite) "alloc-family"="malloc" "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 = { noreturn 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 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #5 = { nounwind allocsize(0) }
attributes #6 = { 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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = !{!12, !13, i64 0}
!12 = !{!"Process", !13, i64 0, !6, i64 8, !6, i64 16, !13, i64 24}
!13 = !{!"int", !7, i64 0}
!14 = distinct !{!14, !10}
!15 = !{!16, !13, i64 0}
!16 = !{!"Resource", !13, i64 0, !6, i64 8, !6, i64 16}
!17 = !{!16, !6, i64 16}
!18 = !{!16, !6, i64 8}
!19 = !{!12, !6, i64 8}
!20 = distinct !{!20, !10}
!21 = distinct !{!21, !10}


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