; LLVM IR from Nonoum/ECL :: ECL_common (ECL_Helper_WriteE7)

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

; 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
define dso_local ptr @ECL_Helper_WriteE7(ptr noundef %0, i32 noundef %1, i32 noundef %2) #1 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i8, align 1
  %9 = 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
  %10 = load ptr, ptr %5, align 8, !tbaa !5
  %11 = icmp ne ptr %10, null
  br i1 %11, label %12, label %45

12:                                               ; preds = %3
  br label %13

13:                                               ; preds = %43, %12
  %14 = load i32, ptr %6, align 4, !tbaa !9
  %15 = icmp ne i32 %14, 0
  br i1 %15, label %16, label %44

16:                                               ; preds = %13
  call void @llvm.lifetime.start.p0(i64 1, ptr %8) #2
  %17 = load i32, ptr %7, align 4, !tbaa !9
  %18 = icmp ugt i32 %17, 127
  %19 = zext i1 %18 to i64
  %20 = select i1 %18, i32 128, i32 0
  %21 = trunc i32 %20 to i8
  store i8 %21, ptr %8, align 1, !tbaa !11
  %22 = load i32, ptr %7, align 4, !tbaa !9
  %23 = trunc i32 %22 to i8
  %24 = zext i8 %23 to i32
  %25 = load i8, ptr %8, align 1, !tbaa !11
  %26 = zext i8 %25 to i32
  %27 = or i32 %24, %26
  %28 = trunc i32 %27 to i8
  %29 = load ptr, ptr %5, align 8, !tbaa !5
  store i8 %28, ptr %29, align 1, !tbaa !11
  %30 = load i32, ptr %7, align 4, !tbaa !9
  %31 = lshr i32 %30, 7
  store i32 %31, ptr %7, align 4, !tbaa !9
  %32 = load ptr, ptr %5, align 8, !tbaa !5
  %33 = getelementptr inbounds i8, ptr %32, i32 1
  store ptr %33, ptr %5, align 8, !tbaa !5
  %34 = load i8, ptr %8, align 1, !tbaa !11
  %35 = icmp ne i8 %34, 0
  br i1 %35, label %38, label %36

36:                                               ; preds = %16
  %37 = load ptr, ptr %5, align 8, !tbaa !5
  store ptr %37, ptr %4, align 8
  store i32 1, ptr %9, align 4
  br label %41

38:                                               ; preds = %16
  %39 = load i32, ptr %6, align 4, !tbaa !9
  %40 = add i32 %39, -1
  store i32 %40, ptr %6, align 4, !tbaa !9
  store i32 0, ptr %9, align 4
  br label %41

41:                                               ; preds = %38, %36
  call void @llvm.lifetime.end.p0(i64 1, ptr %8) #2
  %42 = load i32, ptr %9, align 4
  switch i32 %42, label %48 [
    i32 0, label %43
    i32 1, label %46
  ]

43:                                               ; preds = %41
  br label %13, !llvm.loop !12

44:                                               ; preds = %13
  br label %45

45:                                               ; preds = %44, %3
  store ptr null, ptr %4, align 8
  br label %46

46:                                               ; preds = %45, %41
  %47 = load ptr, ptr %4, align 8
  ret ptr %47

48:                                               ; preds = %41
  unreachable
}

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 }

!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 = !{!7, !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}


; 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: nofree norecurse nosync nounwind memory(write, inaccessiblemem: none) uwtable
define dso_local ptr @ECL_Helper_WriteE7(ptr noundef writeonly %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #0 {
  %4 = icmp ne ptr %0, null
  %5 = icmp ne i32 %1, 0
  %or.cond = and i1 %4, %5
  br i1 %or.cond, label %.preheader, label %.loopexit

6:                                                ; preds = %.preheader
  %7 = add i32 %.016, -1
  %8 = lshr i32 %.015, 7
  %.old1.not = icmp eq i32 %7, 0
  br i1 %.old1.not, label %.loopexit, label %.preheader

.preheader:                                       ; preds = %3, %6
  %.018 = phi ptr [ %12, %6 ], [ %0, %3 ]
  %.016 = phi i32 [ %7, %6 ], [ %1, %3 ]
  %.015 = phi i32 [ %8, %6 ], [ %2, %3 ]
  %.inv = icmp ugt i32 %.015, 127
  %9 = select i1 %.inv, i8 -128, i8 0
  %10 = trunc i32 %.015 to i8
  %11 = or i8 %9, %10
  store i8 %11, ptr %.018, align 1, !tbaa !5
  %12 = getelementptr inbounds i8, ptr %.018, i64 1
  br i1 %.inv, label %6, label %.loopexit

.loopexit:                                        ; preds = %.preheader, %6, %3
  %.2 = phi ptr [ null, %3 ], [ null, %6 ], [ %12, %.preheader ]
  ret ptr %.2
}

attributes #0 = { nofree norecurse nosync nounwind memory(write, inaccessiblemem: none) 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" }

!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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass