; LLVM IR from cvxgrp/scs :: linsys/csparse.ll (scs_cs_spalloc)

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

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

17:                                               ; preds = %5
  store ptr null, ptr %6, align 8
  store i32 1, ptr %13, align 4
  br label %78

18:                                               ; preds = %5
  %19 = load i32, ptr %7, align 4, !tbaa !5
  %20 = load ptr, ptr %12, align 8, !tbaa !9
  %21 = getelementptr inbounds %struct.ScsMatrix, ptr %20, i32 0, i32 3
  store i32 %19, ptr %21, align 8, !tbaa !11
  %22 = load i32, ptr %8, align 4, !tbaa !5
  %23 = load ptr, ptr %12, align 8, !tbaa !9
  %24 = getelementptr inbounds %struct.ScsMatrix, ptr %23, i32 0, i32 4
  store i32 %22, ptr %24, align 4, !tbaa !13
  %25 = load i32, ptr %11, align 4, !tbaa !5
  %26 = icmp ne i32 %25, 0
  br i1 %26, label %27, label %29

27:                                               ; preds = %18
  %28 = load i32, ptr %9, align 4, !tbaa !5
  br label %32

29:                                               ; preds = %18
  %30 = load i32, ptr %8, align 4, !tbaa !5
  %31 = add nsw i32 %30, 1
  br label %32

32:                                               ; preds = %29, %27
  %33 = phi i32 [ %28, %27 ], [ %31, %29 ]
  %34 = sext i32 %33 to i64
  %35 = call noalias ptr @calloc(i64 noundef %34, i64 noundef 4) #4
  %36 = load ptr, ptr %12, align 8, !tbaa !9
  %37 = getelementptr inbounds %struct.ScsMatrix, ptr %36, i32 0, i32 2
  store ptr %35, ptr %37, align 8, !tbaa !14
  %38 = load i32, ptr %9, align 4, !tbaa !5
  %39 = sext i32 %38 to i64
  %40 = call noalias ptr @calloc(i64 noundef %39, i64 noundef 4) #4
  %41 = load ptr, ptr %12, align 8, !tbaa !9
  %42 = getelementptr inbounds %struct.ScsMatrix, ptr %41, i32 0, i32 1
  store ptr %40, ptr %42, align 8, !tbaa !15
  %43 = load i32, ptr %10, align 4, !tbaa !5
  %44 = icmp ne i32 %43, 0
  br i1 %44, label %45, label %49

45:                                               ; preds = %32
  %46 = load i32, ptr %9, align 4, !tbaa !5
  %47 = sext i32 %46 to i64
  %48 = call noalias ptr @calloc(i64 noundef %47, i64 noundef 8) #4
  br label %50

49:                                               ; preds = %32
  br label %50

50:                                               ; preds = %49, %45
  %51 = phi ptr [ %48, %45 ], [ null, %49 ]
  %52 = load ptr, ptr %12, align 8, !tbaa !9
  %53 = getelementptr inbounds %struct.ScsMatrix, ptr %52, i32 0, i32 0
  store ptr %51, ptr %53, align 8, !tbaa !16
  %54 = load ptr, ptr %12, align 8, !tbaa !9
  %55 = getelementptr inbounds %struct.ScsMatrix, ptr %54, i32 0, i32 2
  %56 = load ptr, ptr %55, align 8, !tbaa !14
  %57 = icmp ne ptr %56, null
  br i1 %57, label %58, label %71

58:                                               ; preds = %50
  %59 = load ptr, ptr %12, align 8, !tbaa !9
  %60 = getelementptr inbounds %struct.ScsMatrix, ptr %59, i32 0, i32 1
  %61 = load ptr, ptr %60, align 8, !tbaa !15
  %62 = icmp ne ptr %61, null
  br i1 %62, label %63, label %71

63:                                               ; preds = %58
  %64 = load i32, ptr %10, align 4, !tbaa !5
  %65 = icmp ne i32 %64, 0
  br i1 %65, label %66, label %74

66:                                               ; preds = %63
  %67 = load ptr, ptr %12, align 8, !tbaa !9
  %68 = getelementptr inbounds %struct.ScsMatrix, ptr %67, i32 0, i32 0
  %69 = load ptr, ptr %68, align 8, !tbaa !16
  %70 = icmp ne ptr %69, null
  br i1 %70, label %74, label %71

71:                                               ; preds = %66, %58, %50
  %72 = load ptr, ptr %12, align 8, !tbaa !9
  %73 = call ptr @_scs_cs_spfree(ptr noundef %72)
  br label %76

74:                                               ; preds = %66, %63
  %75 = load ptr, ptr %12, align 8, !tbaa !9
  br label %76

76:                                               ; preds = %74, %71
  %77 = phi ptr [ %73, %71 ], [ %75, %74 ]
  store ptr %77, ptr %6, align 8
  store i32 1, ptr %13, align 4
  br label %78

78:                                               ; preds = %76, %17
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #3
  %79 = load ptr, ptr %6, align 8
  ret ptr %79
}

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

; Function Attrs: nounwind allocsize(0,1)
declare noalias ptr @calloc(i64 noundef, i64 noundef) #2

; Function Attrs: nounwind uwtable
declare dso_local ptr @_scs_cs_spfree(ptr noundef) #0

; 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 = { nounwind allocsize(0,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 #3 = { nounwind }
attributes #4 = { nounwind allocsize(0,1) }

!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 24}
!12 = !{!"", !10, i64 0, !10, i64 8, !10, i64 16, !6, i64 24, !6, i64 28}
!13 = !{!12, !6, i64 28}
!14 = !{!12, !10, i64 16}
!15 = !{!12, !10, i64 8}
!16 = !{!12, !10, 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"

; Function Attrs: nounwind uwtable
define dso_local ptr @_scs_cs_spalloc(i32 noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3, i32 noundef %4) local_unnamed_addr #0 {
  %6 = tail call noalias dereferenceable_or_null(32) ptr @calloc(i64 noundef 1, i64 noundef 32) #2
  %.not = icmp eq ptr %6, null
  br i1 %.not, label %24, label %7

7:                                                ; preds = %5
  %8 = getelementptr inbounds i8, ptr %6, i64 24
  store i32 %0, ptr %8, align 8, !tbaa !5
  %9 = getelementptr inbounds i8, ptr %6, i64 28
  store i32 %1, ptr %9, align 4, !tbaa !11
  %.not21 = icmp eq i32 %4, 0
  %10 = add nsw i32 %1, 1
  %11 = select i1 %.not21, i32 %10, i32 %2
  %12 = sext i32 %11 to i64
  %13 = tail call noalias ptr @calloc(i64 noundef %12, i64 noundef 4) #2
  %14 = getelementptr inbounds i8, ptr %6, i64 16
  store ptr %13, ptr %14, align 8, !tbaa !12
  %15 = sext i32 %2 to i64
  %16 = tail call noalias ptr @calloc(i64 noundef %15, i64 noundef 4) #2
  %17 = getelementptr inbounds i8, ptr %6, i64 8
  store ptr %16, ptr %17, align 8, !tbaa !13
  %.not22 = icmp ne i32 %3, 0
  br i1 %.not22, label %18, label %20

18:                                               ; preds = %7
  %19 = tail call noalias ptr @calloc(i64 noundef %15, i64 noundef 8) #2
  br label %20

20:                                               ; preds = %7, %18
  %21 = phi ptr [ %19, %18 ], [ null, %7 ]
  store ptr %21, ptr %6, align 8, !tbaa !14
  %.not23 = icmp eq ptr %13, null
  %.not24 = icmp eq ptr %16, null
  %or.cond26 = or i1 %.not23, %.not24
  %.not25 = icmp eq ptr %21, null
  %or.cond = and i1 %.not22, %.not25
  %or.cond27 = or i1 %or.cond26, %or.cond
  br i1 %or.cond27, label %22, label %24

22:                                               ; preds = %20
  %23 = tail call ptr @_scs_cs_spfree(ptr noundef nonnull %6)
  br label %24

24:                                               ; preds = %20, %22, %5
  %.0 = phi ptr [ null, %5 ], [ %23, %22 ], [ %6, %20 ]
  ret ptr %.0
}

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

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

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 = { mustprogress nofree nounwind willreturn allockind("alloc,zeroed") allocsize(0,1) 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 #2 = { nounwind allocsize(0,1) }

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


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