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

; 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 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) #1

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

; Function Attrs: nounwind uwtable
define dso_local i32 @tgl_enable(ptr noundef %0, i8 noundef zeroext %1) #1 {
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca i8, align 1
  %6 = alloca i8, align 1
  %7 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i8 %1, ptr %5, align 1, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 1, ptr %6) #3
  %8 = load i8, ptr %5, align 1, !tbaa !9
  %9 = zext i8 %8 to i32
  %10 = load ptr, ptr %4, align 8, !tbaa !5
  %11 = getelementptr inbounds %struct.TGL, ptr %10, i32 0, i32 10
  %12 = load i8, ptr %11, align 1, !tbaa !10
  %13 = zext i8 %12 to i32
  %14 = xor i32 %13, -1
  %15 = and i32 %9, %14
  %16 = trunc i32 %15 to i8
  store i8 %16, ptr %6, align 1, !tbaa !9
  %17 = load i8, ptr %5, align 1, !tbaa !9
  %18 = zext i8 %17 to i32
  %19 = load ptr, ptr %4, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.TGL, ptr %19, i32 0, i32 10
  %21 = load i8, ptr %20, align 1, !tbaa !10
  %22 = zext i8 %21 to i32
  %23 = or i32 %22, %18
  %24 = trunc i32 %23 to i8
  store i8 %24, ptr %20, align 1, !tbaa !10
  %25 = load i8, ptr %6, align 1, !tbaa !9
  %26 = zext i8 %25 to i32
  %27 = and i32 %26, 4
  %28 = icmp ne i32 %27, 0
  br i1 %28, label %29, label %47

29:                                               ; preds = %2
  %30 = load ptr, ptr %4, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.TGL, ptr %30, i32 0, i32 9
  store i8 1, ptr %31, align 4, !tbaa !14
  %32 = load ptr, ptr %4, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.TGL, ptr %32, i32 0, i32 4
  %34 = load i32, ptr %33, align 8, !tbaa !15
  %35 = zext i32 %34 to i64
  %36 = mul i64 4, %35
  %37 = call noalias ptr @malloc(i64 noundef %36) #4
  %38 = load ptr, ptr %4, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.TGL, ptr %38, i32 0, i32 6
  store ptr %37, ptr %39, align 8, !tbaa !16
  %40 = load ptr, ptr %4, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.TGL, ptr %40, i32 0, i32 6
  %42 = load ptr, ptr %41, align 8, !tbaa !16
  %43 = icmp ne ptr %42, null
  br i1 %43, label %45, label %44

44:                                               ; preds = %29
  store i32 -1, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %81

45:                                               ; preds = %29
  %46 = load ptr, ptr %4, align 8, !tbaa !5
  call void @tgl_clear(ptr noundef %46, i8 noundef zeroext 4)
  br label %47

47:                                               ; preds = %45, %2
  %48 = load i8, ptr %6, align 1, !tbaa !9
  %49 = zext i8 %48 to i32
  %50 = and i32 %49, 2
  %51 = icmp ne i32 %50, 0
  br i1 %51, label %52, label %80

52:                                               ; preds = %47
  %53 = load ptr, ptr %4, align 8, !tbaa !5
  %54 = getelementptr inbounds %struct.TGL, ptr %53, i32 0, i32 4
  %55 = load i32, ptr %54, align 8, !tbaa !15
  %56 = mul i32 44, %55
  %57 = load ptr, ptr %4, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct.TGL, ptr %57, i32 0, i32 1
  %59 = load i32, ptr %58, align 4, !tbaa !17
  %60 = mul i32 %59, 4
  %61 = add i32 %56, %60
  %62 = add i32 %61, 4
  %63 = add i32 %62, 1
  %64 = load ptr, ptr %4, align 8, !tbaa !5
  %65 = getelementptr inbounds %struct.TGL, ptr %64, i32 0, i32 8
  store i32 %63, ptr %65, align 8, !tbaa !18
  %66 = load ptr, ptr %4, align 8, !tbaa !5
  %67 = getelementptr inbounds %struct.TGL, ptr %66, i32 0, i32 8
  %68 = load i32, ptr %67, align 8, !tbaa !18
  %69 = zext i32 %68 to i64
  %70 = call noalias ptr @malloc(i64 noundef %69) #4
  %71 = load ptr, ptr %4, align 8, !tbaa !5
  %72 = getelementptr inbounds %struct.TGL, ptr %71, i32 0, i32 7
  store ptr %70, ptr %72, align 8, !tbaa !19
  %73 = load ptr, ptr %4, align 8, !tbaa !5
  %74 = getelementptr inbounds %struct.TGL, ptr %73, i32 0, i32 7
  %75 = load ptr, ptr %74, align 8, !tbaa !19
  %76 = icmp ne ptr %75, null
  br i1 %76, label %78, label %77

77:                                               ; preds = %52
  store i32 -1, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %81

78:                                               ; preds = %52
  %79 = load ptr, ptr %4, align 8, !tbaa !5
  call void @tgl_clear(ptr noundef %79, i8 noundef zeroext 2)
  br label %80

80:                                               ; preds = %78, %47
  store i32 0, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %81

81:                                               ; preds = %80, %77, %44
  call void @llvm.lifetime.end.p0(i64 1, ptr %6) #3
  %82 = load i32, ptr %3, align 4
  ret i32 %82
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #2 = { 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 #3 = { nounwind }
attributes #4 = { 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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!7, !7, i64 0}
!10 = !{!11, !7, i64 53}
!11 = !{!"TGL", !12, i64 0, !12, i64 4, !12, i64 8, !12, i64 12, !12, i64 16, !6, i64 24, !6, i64 32, !6, i64 40, !12, i64 48, !13, i64 52, !7, i64 53}
!12 = !{!"int", !7, i64 0}
!13 = !{!"_Bool", !7, i64 0}
!14 = !{!11, !13, i64 52}
!15 = !{!11, !12, i64 16}
!16 = !{!11, !6, i64 32}
!17 = !{!11, !12, i64 4}
!18 = !{!11, !12, i64 48}
!19 = !{!11, !6, i64 40}


; 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: 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: nounwind uwtable
define dso_local range(i32 -1, 1) i32 @tgl_enable(ptr noundef %0, i8 noundef zeroext %1) local_unnamed_addr #0 {
  %3 = getelementptr inbounds i8, ptr %0, i64 53
  %4 = load i8, ptr %3, align 1, !tbaa !5
  %5 = xor i8 %4, -1
  %6 = and i8 %5, %1
  %7 = or i8 %4, %1
  store i8 %7, ptr %3, align 1, !tbaa !5
  %8 = zext i8 %6 to i32
  %9 = and i32 %8, 4
  %.not = icmp eq i32 %9, 0
  br i1 %.not, label %19, label %10

10:                                               ; preds = %2
  %11 = getelementptr inbounds i8, ptr %0, i64 52
  store i8 1, ptr %11, align 4, !tbaa !12
  %12 = getelementptr inbounds i8, ptr %0, i64 16
  %13 = load i32, ptr %12, align 8, !tbaa !13
  %14 = zext i32 %13 to i64
  %15 = shl nuw nsw i64 %14, 2
  %16 = tail call noalias ptr @malloc(i64 noundef %15) #2
  %17 = getelementptr inbounds i8, ptr %0, i64 32
  store ptr %16, ptr %17, align 8, !tbaa !14
  %.not18 = icmp eq ptr %16, null
  br i1 %.not18, label %35, label %18

18:                                               ; preds = %10
  tail call void @tgl_clear(ptr noundef nonnull %0, i8 noundef zeroext 4)
  br label %19

19:                                               ; preds = %18, %2
  %20 = and i32 %8, 2
  %.not19 = icmp eq i32 %20, 0
  br i1 %.not19, label %35, label %21

21:                                               ; preds = %19
  %22 = getelementptr inbounds i8, ptr %0, i64 16
  %23 = load i32, ptr %22, align 8, !tbaa !13
  %24 = mul i32 %23, 44
  %25 = getelementptr inbounds i8, ptr %0, i64 4
  %26 = load i32, ptr %25, align 4, !tbaa !15
  %27 = shl i32 %26, 2
  %28 = add i32 %24, 5
  %29 = add i32 %28, %27
  %30 = getelementptr inbounds i8, ptr %0, i64 48
  store i32 %29, ptr %30, align 8, !tbaa !16
  %31 = zext i32 %29 to i64
  %32 = tail call noalias ptr @malloc(i64 noundef %31) #2
  %33 = getelementptr inbounds i8, ptr %0, i64 40
  store ptr %32, ptr %33, align 8, !tbaa !17
  %.not20 = icmp eq ptr %32, null
  br i1 %.not20, label %35, label %34

34:                                               ; preds = %21
  tail call void @tgl_clear(ptr noundef nonnull %0, i8 noundef zeroext 2)
  br label %35

35:                                               ; preds = %19, %34, %21, %10
  %.0 = phi i32 [ -1, %10 ], [ -1, %21 ], [ 0, %34 ], [ 0, %19 ]
  ret i32 %.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,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 = { 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, !8, i64 53}
!6 = !{!"TGL", !7, i64 0, !7, i64 4, !7, i64 8, !7, i64 12, !7, i64 16, !10, i64 24, !10, i64 32, !10, i64 40, !7, i64 48, !11, i64 52, !8, i64 53}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!"_Bool", !8, i64 0}
!12 = !{!6, !11, i64 52}
!13 = !{!6, !7, i64 16}
!14 = !{!6, !10, i64 32}
!15 = !{!6, !7, i64 4}
!16 = !{!6, !7, i64 48}
!17 = !{!6, !10, i64 40}


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