; LLVM IR from lolosssss/leetcode :: c (6_zigzag_conversion)

; 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: nounwind uwtable
define dso_local ptr @convert(ptr noundef %0, i32 noundef %1) #0 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %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 ptr, align 8
  %14 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  store i32 0, ptr %6, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  store i32 0, ptr %7, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  store i32 0, ptr %8, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  store i32 0, ptr %9, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #3
  %15 = load i32, ptr %5, align 4, !tbaa !9
  %16 = mul nsw i32 %15, 2
  %17 = sub nsw i32 %16, 2
  store i32 %17, ptr %10, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #3
  %18 = load i32, ptr %10, align 4, !tbaa !9
  store i32 %18, ptr %11, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #3
  %19 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %19, ptr %12, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #3
  store ptr null, ptr %13, align 8, !tbaa !5
  %20 = load i32, ptr %5, align 4, !tbaa !9
  %21 = icmp eq i32 %20, 1
  br i1 %21, label %22, label %24

22:                                               ; preds = %2
  %23 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %23, ptr %3, align 8
  store i32 1, ptr %14, align 4
  br label %92

24:                                               ; preds = %2
  br label %25

25:                                               ; preds = %31, %24
  %26 = load ptr, ptr %12, align 8, !tbaa !5
  %27 = getelementptr inbounds i8, ptr %26, i32 1
  store ptr %27, ptr %12, align 8, !tbaa !5
  %28 = load i8, ptr %26, align 1, !tbaa !11
  %29 = sext i8 %28 to i32
  %30 = icmp ne i32 %29, 0
  br i1 %30, label %31, label %34

31:                                               ; preds = %25
  %32 = load i32, ptr %6, align 4, !tbaa !9
  %33 = add nsw i32 %32, 1
  store i32 %33, ptr %6, align 4, !tbaa !9
  br label %25, !llvm.loop !12

34:                                               ; preds = %25
  %35 = load i32, ptr %6, align 4, !tbaa !9
  %36 = load i32, ptr %5, align 4, !tbaa !9
  %37 = icmp slt i32 %35, %36
  br i1 %37, label %38, label %40

38:                                               ; preds = %34
  %39 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %39, ptr %3, align 8
  store i32 1, ptr %14, align 4
  br label %92

40:                                               ; preds = %34
  %41 = load i32, ptr %6, align 4, !tbaa !9
  %42 = add nsw i32 %41, 1
  %43 = sext i32 %42 to i64
  %44 = mul i64 %43, 1
  %45 = call noalias ptr @malloc(i64 noundef %44) #4
  store ptr %45, ptr %13, align 8, !tbaa !5
  store i32 0, ptr %7, align 4, !tbaa !9
  br label %46

46:                                               ; preds = %83, %40
  %47 = load i32, ptr %7, align 4, !tbaa !9
  %48 = load i32, ptr %5, align 4, !tbaa !9
  %49 = icmp slt i32 %47, %48
  br i1 %49, label %50, label %86

50:                                               ; preds = %46
  %51 = load i32, ptr %7, align 4, !tbaa !9
  store i32 %51, ptr %9, align 4, !tbaa !9
  %52 = load i32, ptr %10, align 4, !tbaa !9
  %53 = load i32, ptr %7, align 4, !tbaa !9
  %54 = mul nsw i32 2, %53
  %55 = sub nsw i32 %52, %54
  store i32 %55, ptr %11, align 4, !tbaa !9
  br label %56

56:                                               ; preds = %78, %50
  %57 = load i32, ptr %9, align 4, !tbaa !9
  %58 = load i32, ptr %6, align 4, !tbaa !9
  %59 = icmp slt i32 %57, %58
  br i1 %59, label %60, label %82

60:                                               ; preds = %56
  %61 = load ptr, ptr %4, align 8, !tbaa !5
  %62 = load i32, ptr %9, align 4, !tbaa !9
  %63 = sext i32 %62 to i64
  %64 = getelementptr inbounds i8, ptr %61, i64 %63
  %65 = load i8, ptr %64, align 1, !tbaa !11
  %66 = load ptr, ptr %13, align 8, !tbaa !5
  %67 = load i32, ptr %8, align 4, !tbaa !9
  %68 = sext i32 %67 to i64
  %69 = getelementptr inbounds i8, ptr %66, i64 %68
  store i8 %65, ptr %69, align 1, !tbaa !11
  %70 = load i32, ptr %11, align 4, !tbaa !9
  %71 = load i32, ptr %9, align 4, !tbaa !9
  %72 = add nsw i32 %71, %70
  store i32 %72, ptr %9, align 4, !tbaa !9
  %73 = load i32, ptr %11, align 4, !tbaa !9
  %74 = icmp ne i32 %73, 0
  br i1 %74, label %75, label %78

75:                                               ; preds = %60
  %76 = load i32, ptr %8, align 4, !tbaa !9
  %77 = add nsw i32 %76, 1
  store i32 %77, ptr %8, align 4, !tbaa !9
  br label %78

78:                                               ; preds = %75, %60
  %79 = load i32, ptr %10, align 4, !tbaa !9
  %80 = load i32, ptr %11, align 4, !tbaa !9
  %81 = sub nsw i32 %79, %80
  store i32 %81, ptr %11, align 4, !tbaa !9
  br label %56, !llvm.loop !14

82:                                               ; preds = %56
  br label %83

83:                                               ; preds = %82
  %84 = load i32, ptr %7, align 4, !tbaa !9
  %85 = add nsw i32 %84, 1
  store i32 %85, ptr %7, align 4, !tbaa !9
  br label %46, !llvm.loop !15

86:                                               ; preds = %46
  %87 = load ptr, ptr %13, align 8, !tbaa !5
  %88 = load i32, ptr %8, align 4, !tbaa !9
  %89 = sext i32 %88 to i64
  %90 = getelementptr inbounds i8, ptr %87, i64 %89
  store i8 0, ptr %90, align 1, !tbaa !11
  %91 = load ptr, ptr %13, align 8, !tbaa !5
  store ptr %91, ptr %3, align 8
  store i32 1, ptr %14, align 4
  br label %92

92:                                               ; preds = %86, %38, %22
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  %93 = load ptr, ptr %3, align 8
  ret ptr %93
}

; 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)
declare noalias ptr @malloc(i64 noundef) #2

; 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) "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 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!7, !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}
!14 = distinct !{!14, !13}
!15 = distinct !{!15, !13}


; 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 nounwind memory(readwrite, argmem: read) uwtable
define dso_local noundef ptr @convert(ptr noundef readonly %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = shl nsw i32 %1, 1
  %4 = add nsw i32 %3, -2
  %5 = icmp eq i32 %1, 1
  br i1 %5, label %29, label %.preheader

.preheader:                                       ; preds = %2
  %6 = load i8, ptr %0, align 1, !tbaa !5
  %.not40 = icmp eq i8 %6, 0
  br i1 %.not40, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %.preheader, %.lr.ph
  %.042 = phi ptr [ %7, %.lr.ph ], [ %0, %.preheader ]
  %.03741 = phi i32 [ %8, %.lr.ph ], [ 0, %.preheader ]
  %7 = getelementptr inbounds i8, ptr %.042, i64 1
  %8 = add nuw nsw i32 %.03741, 1
  %9 = load i8, ptr %7, align 1, !tbaa !5
  %.not = icmp eq i8 %9, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !8

._crit_edge:                                      ; preds = %.lr.ph, %.preheader
  %.037.lcssa = phi i32 [ 0, %.preheader ], [ %8, %.lr.ph ]
  %10 = icmp slt i32 %.037.lcssa, %1
  br i1 %10, label %29, label %11

11:                                               ; preds = %._crit_edge
  %12 = add nuw nsw i32 %.037.lcssa, 1
  %13 = zext nneg i32 %12 to i64
  %14 = tail call noalias ptr @malloc(i64 noundef %13) #2
  %15 = icmp sgt i32 %1, 0
  br i1 %15, label %.lr.ph53, label %._crit_edge54

.lr.ph53:                                         ; preds = %11, %._crit_edge48
  %.03551 = phi i32 [ %.1.lcssa, %._crit_edge48 ], [ 0, %11 ]
  %.03650 = phi i32 [ %26, %._crit_edge48 ], [ 0, %11 ]
  %16 = icmp slt i32 %.03650, %.037.lcssa
  br i1 %16, label %.lr.ph47.preheader, label %._crit_edge48

.lr.ph47.preheader:                               ; preds = %.lr.ph53
  %17 = shl nuw nsw i32 %.03650, 1
  br label %.lr.ph47

.lr.ph47:                                         ; preds = %.lr.ph47.preheader, %.lr.ph47
  %.pn45 = phi i32 [ %.032, %.lr.ph47 ], [ %17, %.lr.ph47.preheader ]
  %.03444 = phi i32 [ %23, %.lr.ph47 ], [ %.03650, %.lr.ph47.preheader ]
  %.143 = phi i32 [ %spec.select, %.lr.ph47 ], [ %.03551, %.lr.ph47.preheader ]
  %.032 = sub i32 %4, %.pn45
  %18 = sext i32 %.03444 to i64
  %19 = getelementptr inbounds i8, ptr %0, i64 %18
  %20 = load i8, ptr %19, align 1, !tbaa !5
  %21 = sext i32 %.143 to i64
  %22 = getelementptr inbounds i8, ptr %14, i64 %21
  store i8 %20, ptr %22, align 1, !tbaa !5
  %23 = add nsw i32 %.032, %.03444
  %.not39 = icmp ne i32 %.032, 0
  %24 = zext i1 %.not39 to i32
  %spec.select = add nsw i32 %.143, %24
  %25 = icmp slt i32 %23, %.037.lcssa
  br i1 %25, label %.lr.ph47, label %._crit_edge48, !llvm.loop !10

._crit_edge48:                                    ; preds = %.lr.ph47, %.lr.ph53
  %.1.lcssa = phi i32 [ %.03551, %.lr.ph53 ], [ %spec.select, %.lr.ph47 ]
  %26 = add nuw nsw i32 %.03650, 1
  %exitcond.not = icmp eq i32 %26, %1
  br i1 %exitcond.not, label %._crit_edge54.loopexit, label %.lr.ph53, !llvm.loop !11

._crit_edge54.loopexit:                           ; preds = %._crit_edge48
  %27 = sext i32 %.1.lcssa to i64
  br label %._crit_edge54

._crit_edge54:                                    ; preds = %._crit_edge54.loopexit, %11
  %.035.lcssa = phi i64 [ 0, %11 ], [ %27, %._crit_edge54.loopexit ]
  %28 = getelementptr inbounds i8, ptr %14, i64 %.035.lcssa
  store i8 0, ptr %28, align 1, !tbaa !5
  br label %29

29:                                               ; preds = %._crit_edge, %2, %._crit_edge54
  %.033 = phi ptr [ %14, %._crit_edge54 ], [ %0, %2 ], [ %0, %._crit_edge ]
  ret ptr %.033
}

; 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

attributes #0 = { nofree nounwind memory(readwrite, argmem: read) 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, !6, i64 0}
!6 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = distinct !{!8, !9}
!9 = !{!"llvm.loop.mustprogress"}
!10 = distinct !{!10, !9}
!11 = distinct !{!11, !9}


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