; LLVM IR from wojciech-graj/TermGL :: termgl (tgl_init)

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

; 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 nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #1

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

; Function Attrs: nounwind uwtable
declare dso_local void @tgl_clear(ptr noundef, i8 noundef zeroext) #2

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

12:                                               ; preds = %2
  store ptr null, ptr %3, align 8
  store i32 1, ptr %7, align 4
  br label %51

13:                                               ; preds = %2
  %14 = load ptr, ptr %6, align 8, !tbaa !9
  %15 = getelementptr inbounds %struct.TGL, ptr %8, i32 0, i32 0
  %16 = load i32, ptr %4, align 4, !tbaa !5
  store i32 %16, ptr %15, align 8, !tbaa !11
  %17 = getelementptr inbounds %struct.TGL, ptr %8, i32 0, i32 1
  %18 = load i32, ptr %5, align 4, !tbaa !5
  store i32 %18, ptr %17, align 4, !tbaa !14
  %19 = getelementptr inbounds %struct.TGL, ptr %8, i32 0, i32 2
  %20 = load i32, ptr %4, align 4, !tbaa !5
  %21 = sub i32 %20, 1
  store i32 %21, ptr %19, align 8, !tbaa !15
  %22 = getelementptr inbounds %struct.TGL, ptr %8, i32 0, i32 3
  %23 = load i32, ptr %5, align 4, !tbaa !5
  %24 = sub i32 %23, 1
  store i32 %24, ptr %22, align 4, !tbaa !16
  %25 = getelementptr inbounds %struct.TGL, ptr %8, i32 0, i32 4
  %26 = load i32, ptr %4, align 4, !tbaa !5
  %27 = load i32, ptr %5, align 4, !tbaa !5
  %28 = mul i32 %26, %27
  store i32 %28, ptr %25, align 8, !tbaa !17
  %29 = getelementptr inbounds %struct.TGL, ptr %8, i32 0, i32 5
  %30 = load i32, ptr %4, align 4, !tbaa !5
  %31 = zext i32 %30 to i64
  %32 = mul i64 9, %31
  %33 = load i32, ptr %5, align 4, !tbaa !5
  %34 = zext i32 %33 to i64
  %35 = mul i64 %32, %34
  %36 = call noalias ptr @malloc(i64 noundef %35) #6
  store ptr %36, ptr %29, align 8, !tbaa !18
  %37 = getelementptr inbounds %struct.TGL, ptr %8, i32 0, i32 6
  store ptr null, ptr %37, align 8, !tbaa !19
  %38 = getelementptr inbounds %struct.TGL, ptr %8, i32 0, i32 7
  store ptr null, ptr %38, align 8, !tbaa !20
  %39 = getelementptr inbounds %struct.TGL, ptr %8, i32 0, i32 8
  store i32 0, ptr %39, align 8, !tbaa !21
  %40 = getelementptr inbounds %struct.TGL, ptr %8, i32 0, i32 9
  store i8 0, ptr %40, align 4, !tbaa !22
  %41 = getelementptr inbounds %struct.TGL, ptr %8, i32 0, i32 10
  store i8 0, ptr %41, align 1, !tbaa !23
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %14, ptr align 8 %8, i64 56, i1 false), !tbaa.struct !24
  %42 = load ptr, ptr %6, align 8, !tbaa !9
  %43 = getelementptr inbounds %struct.TGL, ptr %42, i32 0, i32 5
  %44 = load ptr, ptr %43, align 8, !tbaa !18
  %45 = icmp ne ptr %44, null
  br i1 %45, label %48, label %46

46:                                               ; preds = %13
  %47 = load ptr, ptr %6, align 8, !tbaa !9
  call void @free(ptr noundef %47) #5
  store ptr null, ptr %3, align 8
  store i32 1, ptr %7, align 4
  br label %51

48:                                               ; preds = %13
  %49 = load ptr, ptr %6, align 8, !tbaa !9
  call void @tgl_clear(ptr noundef %49, i8 noundef zeroext 1)
  %50 = load ptr, ptr %6, align 8, !tbaa !9
  store ptr %50, ptr %3, align 8
  store i32 1, ptr %7, align 4
  br label %51

51:                                               ; preds = %48, %46, %12
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #5
  %52 = load ptr, ptr %3, align 8
  ret ptr %52
}

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

; Function Attrs: nounwind
declare void @free(ptr noundef) #4

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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 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 = { 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) }

!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 = !{!"TGL", !6, i64 0, !6, i64 4, !6, i64 8, !6, i64 12, !6, i64 16, !10, i64 24, !10, i64 32, !10, i64 40, !6, i64 48, !13, i64 52, !7, i64 53}
!13 = !{!"_Bool", !7, i64 0}
!14 = !{!12, !6, i64 4}
!15 = !{!12, !6, i64 8}
!16 = !{!12, !6, i64 12}
!17 = !{!12, !6, i64 16}
!18 = !{!12, !10, i64 24}
!19 = !{!12, !10, i64 32}
!20 = !{!12, !10, i64 40}
!21 = !{!12, !6, i64 48}
!22 = !{!12, !13, i64 52}
!23 = !{!12, !7, i64 53}
!24 = !{i64 0, i64 4, !5, i64 4, i64 4, !5, i64 8, i64 4, !5, i64 12, i64 4, !5, i64 16, i64 4, !5, i64 24, i64 8, !9, i64 32, i64 8, !9, i64 40, i64 8, !9, i64 48, i64 4, !5, i64 52, i64 1, !25, i64 53, i64 1, !26}
!25 = !{!13, !13, i64 0}
!26 = !{!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"

; Function Attrs: nounwind uwtable
declare dso_local void @tgl_clear(ptr noundef, i8 noundef zeroext) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local noundef ptr @tgl_init(i32 noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = tail call noalias dereferenceable_or_null(56) ptr @malloc(i64 noundef 56) #4
  %.not = icmp eq ptr %3, null
  br i1 %.not, label %15, label %4

4:                                                ; preds = %2
  %5 = add i32 %0, -1
  %6 = add i32 %1, -1
  %7 = mul i32 %1, %0
  %8 = zext i32 %0 to i64
  %9 = mul nuw nsw i64 %8, 9
  %10 = zext i32 %1 to i64
  %11 = mul i64 %9, %10
  %12 = tail call noalias ptr @malloc(i64 noundef %11) #4
  store i32 %0, ptr %3, align 8, !tbaa !5
  %.sroa.2.0..sroa_idx = getelementptr inbounds i8, ptr %3, i64 4
  store i32 %1, ptr %.sroa.2.0..sroa_idx, align 4, !tbaa !5
  %.sroa.3.0..sroa_idx = getelementptr inbounds i8, ptr %3, i64 8
  store i32 %5, ptr %.sroa.3.0..sroa_idx, align 8, !tbaa !5
  %.sroa.4.0..sroa_idx = getelementptr inbounds i8, ptr %3, i64 12
  store i32 %6, ptr %.sroa.4.0..sroa_idx, align 4, !tbaa !5
  %.sroa.5.0..sroa_idx = getelementptr inbounds i8, ptr %3, i64 16
  store i32 %7, ptr %.sroa.5.0..sroa_idx, align 8, !tbaa !5
  %.sroa.61.0..sroa_idx = getelementptr inbounds i8, ptr %3, i64 24
  store ptr %12, ptr %.sroa.61.0..sroa_idx, align 8, !tbaa !9
  %.sroa.7.0..sroa_idx = getelementptr inbounds i8, ptr %3, i64 32
  %.not16 = icmp eq ptr %12, null
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(22) %.sroa.7.0..sroa_idx, i8 0, i64 22, i1 false)
  br i1 %.not16, label %13, label %14

13:                                               ; preds = %4
  tail call void @free(ptr noundef nonnull %3) #5
  br label %15

14:                                               ; preds = %4
  tail call void @tgl_clear(ptr noundef nonnull %3, i8 noundef zeroext 1)
  br label %15

15:                                               ; preds = %2, %14, %13
  %.0 = phi ptr [ %3, %14 ], [ null, %13 ], [ null, %2 ]
  ret ptr %.0
}

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

; Function Attrs: mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite)
declare void @free(ptr allocptr nocapture noundef) local_unnamed_addr #2

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

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,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 #2 = { mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, 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 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #4 = { nounwind allocsize(0) }
attributes #5 = { 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}


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