; LLVM IR from AlgoGenesis/C :: Sorting Algorithms/Zigzag_Conversions (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 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 ptr, align 8
  %12 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  %13 = load i32, ptr %5, align 4, !tbaa !9
  %14 = icmp sle i32 %13, 1
  br i1 %14, label %21, label %15

15:                                               ; preds = %2
  %16 = load i32, ptr %5, align 4, !tbaa !9
  %17 = sext i32 %16 to i64
  %18 = load ptr, ptr %4, align 8, !tbaa !5
  %19 = call i64 @strlen(ptr noundef %18) #5
  %20 = icmp uge i64 %17, %19
  br i1 %20, label %21, label %23

21:                                               ; preds = %15, %2
  %22 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %22, ptr %3, align 8
  br label %123

23:                                               ; preds = %15
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #6
  %24 = load i32, ptr %5, align 4, !tbaa !9
  %25 = sext i32 %24 to i64
  %26 = mul i64 1, %25
  %27 = call noalias ptr @malloc(i64 noundef %26) #7
  store ptr %27, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #6
  store i32 0, ptr %7, align 4, !tbaa !9
  br label %28

28:                                               ; preds = %49, %23
  %29 = load i32, ptr %7, align 4, !tbaa !9
  %30 = load i32, ptr %5, align 4, !tbaa !9
  %31 = icmp slt i32 %29, %30
  br i1 %31, label %33, label %32

32:                                               ; preds = %28
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #6
  br label %52

33:                                               ; preds = %28
  %34 = load ptr, ptr %4, align 8, !tbaa !5
  %35 = call i64 @strlen(ptr noundef %34) #5
  %36 = add i64 %35, 1
  %37 = mul i64 1, %36
  %38 = call noalias ptr @malloc(i64 noundef %37) #7
  %39 = load ptr, ptr %6, align 8, !tbaa !5
  %40 = load i32, ptr %7, align 4, !tbaa !9
  %41 = sext i32 %40 to i64
  %42 = getelementptr inbounds ptr, ptr %39, i64 %41
  store ptr %38, ptr %42, align 8, !tbaa !5
  %43 = load ptr, ptr %6, align 8, !tbaa !5
  %44 = load i32, ptr %7, align 4, !tbaa !9
  %45 = sext i32 %44 to i64
  %46 = getelementptr inbounds ptr, ptr %43, i64 %45
  %47 = load ptr, ptr %46, align 8, !tbaa !5
  %48 = getelementptr inbounds i8, ptr %47, i64 0
  store i8 0, ptr %48, align 1, !tbaa !11
  br label %49

49:                                               ; preds = %33
  %50 = load i32, ptr %7, align 4, !tbaa !9
  %51 = add nsw i32 %50, 1
  store i32 %51, ptr %7, align 4, !tbaa !9
  br label %28, !llvm.loop !12

52:                                               ; preds = %32
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #6
  store i32 0, ptr %8, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #6
  store i32 0, ptr %9, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #6
  store i32 0, ptr %10, align 4, !tbaa !9
  br label %53

53:                                               ; preds = %88, %52
  %54 = load i32, ptr %10, align 4, !tbaa !9
  %55 = sext i32 %54 to i64
  %56 = load ptr, ptr %4, align 8, !tbaa !5
  %57 = call i64 @strlen(ptr noundef %56) #5
  %58 = icmp ult i64 %55, %57
  br i1 %58, label %60, label %59

59:                                               ; preds = %53
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #6
  br label %91

60:                                               ; preds = %53
  %61 = load ptr, ptr %6, align 8, !tbaa !5
  %62 = load i32, ptr %8, align 4, !tbaa !9
  %63 = sext i32 %62 to i64
  %64 = getelementptr inbounds ptr, ptr %61, i64 %63
  %65 = load ptr, ptr %64, align 8, !tbaa !5
  %66 = load ptr, ptr %4, align 8, !tbaa !5
  %67 = load i32, ptr %10, align 4, !tbaa !9
  %68 = sext i32 %67 to i64
  %69 = getelementptr inbounds i8, ptr %66, i64 %68
  %70 = call ptr @strncat(ptr noundef %65, ptr noundef %69, i64 noundef 1) #6
  %71 = load i32, ptr %8, align 4, !tbaa !9
  %72 = icmp eq i32 %71, 0
  br i1 %72, label %73, label %74

73:                                               ; preds = %60
  store i32 1, ptr %9, align 4, !tbaa !9
  br label %81

74:                                               ; preds = %60
  %75 = load i32, ptr %8, align 4, !tbaa !9
  %76 = load i32, ptr %5, align 4, !tbaa !9
  %77 = sub nsw i32 %76, 1
  %78 = icmp eq i32 %75, %77
  br i1 %78, label %79, label %80

79:                                               ; preds = %74
  store i32 0, ptr %9, align 4, !tbaa !9
  br label %80

80:                                               ; preds = %79, %74
  br label %81

81:                                               ; preds = %80, %73
  %82 = load i32, ptr %9, align 4, !tbaa !9
  %83 = icmp ne i32 %82, 0
  %84 = zext i1 %83 to i64
  %85 = select i1 %83, i32 1, i32 -1
  %86 = load i32, ptr %8, align 4, !tbaa !9
  %87 = add nsw i32 %86, %85
  store i32 %87, ptr %8, align 4, !tbaa !9
  br label %88

88:                                               ; preds = %81
  %89 = load i32, ptr %10, align 4, !tbaa !9
  %90 = add nsw i32 %89, 1
  store i32 %90, ptr %10, align 4, !tbaa !9
  br label %53, !llvm.loop !14

91:                                               ; preds = %59
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #6
  %92 = load ptr, ptr %4, align 8, !tbaa !5
  %93 = call i64 @strlen(ptr noundef %92) #5
  %94 = add i64 %93, 1
  %95 = mul i64 1, %94
  %96 = call noalias ptr @malloc(i64 noundef %95) #7
  store ptr %96, ptr %11, align 8, !tbaa !5
  %97 = load ptr, ptr %11, align 8, !tbaa !5
  %98 = getelementptr inbounds i8, ptr %97, i64 0
  store i8 0, ptr %98, align 1, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #6
  store i32 0, ptr %12, align 4, !tbaa !9
  br label %99

99:                                               ; preds = %117, %91
  %100 = load i32, ptr %12, align 4, !tbaa !9
  %101 = load i32, ptr %5, align 4, !tbaa !9
  %102 = icmp slt i32 %100, %101
  br i1 %102, label %104, label %103

103:                                              ; preds = %99
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #6
  br label %120

104:                                              ; preds = %99
  %105 = load ptr, ptr %11, align 8, !tbaa !5
  %106 = load ptr, ptr %6, align 8, !tbaa !5
  %107 = load i32, ptr %12, align 4, !tbaa !9
  %108 = sext i32 %107 to i64
  %109 = getelementptr inbounds ptr, ptr %106, i64 %108
  %110 = load ptr, ptr %109, align 8, !tbaa !5
  %111 = call ptr @strcat(ptr noundef %105, ptr noundef %110) #6
  %112 = load ptr, ptr %6, align 8, !tbaa !5
  %113 = load i32, ptr %12, align 4, !tbaa !9
  %114 = sext i32 %113 to i64
  %115 = getelementptr inbounds ptr, ptr %112, i64 %114
  %116 = load ptr, ptr %115, align 8, !tbaa !5
  call void @free(ptr noundef %116) #6
  br label %117

117:                                              ; preds = %104
  %118 = load i32, ptr %12, align 4, !tbaa !9
  %119 = add nsw i32 %118, 1
  store i32 %119, ptr %12, align 4, !tbaa !9
  br label %99, !llvm.loop !15

120:                                              ; preds = %103
  %121 = load ptr, ptr %6, align 8, !tbaa !5
  call void @free(ptr noundef %121) #6
  %122 = load ptr, ptr %11, align 8, !tbaa !5
  store ptr %122, ptr %3, align 8
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #6
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #6
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #6
  br label %123

123:                                              ; preds = %120, %21
  %124 = load ptr, ptr %3, align 8
  ret ptr %124
}

; Function Attrs: nounwind willreturn memory(read)
declare i64 @strlen(ptr noundef) #1

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

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

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

; Function Attrs: nounwind
declare ptr @strncat(ptr noundef, ptr noundef, i64 noundef) #4

; Function Attrs: nounwind
declare ptr @strcat(ptr noundef, ptr noundef) #4

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

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 = { nounwind willreturn memory(read) "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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 willreturn memory(read) }
attributes #6 = { nounwind }
attributes #7 = { 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: nounwind uwtable
define dso_local noundef ptr @convert(ptr noundef readonly %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = icmp slt i32 %1, 2
  br i1 %3, label %30, label %4

4:                                                ; preds = %2
  %5 = zext nneg i32 %1 to i64
  %6 = tail call i64 @strlen(ptr noundef nonnull dereferenceable(1) %0) #6
  %.not = icmp ugt i64 %6, %5
  br i1 %.not, label %.lr.ph, label %30

.lr.ph:                                           ; preds = %4
  %7 = tail call noalias ptr @malloc(i64 noundef %5) #7
  %8 = add i64 %6, 1
  br label %10

.lr.ph51:                                         ; preds = %10
  %9 = add nsw i32 %1, -1
  br label %15

10:                                               ; preds = %.lr.ph, %10
  %indvars.iv = phi i64 [ 0, %.lr.ph ], [ %indvars.iv.next, %10 ]
  %11 = tail call noalias ptr @malloc(i64 noundef %8) #7
  %12 = getelementptr inbounds ptr, ptr %7, i64 %indvars.iv
  store ptr %11, ptr %12, align 8, !tbaa !5
  store i8 0, ptr %11, align 1, !tbaa !9
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %5
  br i1 %exitcond.not, label %.lr.ph51, label %10, !llvm.loop !10

.lr.ph54.preheader:                               ; preds = %15
  %13 = add nuw nsw i64 %25, 1
  %14 = tail call noalias ptr @malloc(i64 noundef %13) #7
  store i8 0, ptr %14, align 1, !tbaa !9
  %smax = tail call i32 @llvm.smax.i32(i32 %1, i32 1)
  %wide.trip.count65 = zext nneg i32 %smax to i64
  br label %.lr.ph54

15:                                               ; preds = %.lr.ph51, %15
  %indvars.iv59 = phi i64 [ 0, %.lr.ph51 ], [ %indvars.iv.next60, %15 ]
  %.03949 = phi i32 [ 0, %.lr.ph51 ], [ %.1, %15 ]
  %.04048 = phi i32 [ 0, %.lr.ph51 ], [ %24, %15 ]
  %16 = sext i32 %.04048 to i64
  %17 = getelementptr inbounds ptr, ptr %7, i64 %16
  %18 = load ptr, ptr %17, align 8, !tbaa !5
  %19 = getelementptr inbounds i8, ptr %0, i64 %indvars.iv59
  %20 = tail call ptr @strncat(ptr noundef nonnull dereferenceable(1) %18, ptr noundef nonnull dereferenceable(1) %19, i64 noundef 1) #8
  %21 = icmp eq i32 %.04048, 0
  %22 = icmp eq i32 %.04048, %9
  %spec.select = select i1 %22, i32 0, i32 %.03949
  %.1 = select i1 %21, i32 1, i32 %spec.select
  %.not46 = icmp eq i32 %.1, 0
  %23 = select i1 %.not46, i32 -1, i32 1
  %24 = add nsw i32 %23, %.04048
  %indvars.iv.next60 = add nuw nsw i64 %indvars.iv59, 1
  %25 = tail call i64 @strlen(ptr noundef nonnull dereferenceable(1) %0) #6
  %26 = icmp ugt i64 %25, %indvars.iv.next60
  br i1 %26, label %15, label %.lr.ph54.preheader, !llvm.loop !12

._crit_edge55:                                    ; preds = %.lr.ph54
  tail call void @free(ptr noundef nonnull %7) #8
  br label %30

.lr.ph54:                                         ; preds = %.lr.ph54.preheader, %.lr.ph54
  %indvars.iv62 = phi i64 [ 0, %.lr.ph54.preheader ], [ %indvars.iv.next63, %.lr.ph54 ]
  %27 = getelementptr inbounds ptr, ptr %7, i64 %indvars.iv62
  %28 = load ptr, ptr %27, align 8, !tbaa !5
  %29 = tail call ptr @strcat(ptr noundef nonnull dereferenceable(1) %14, ptr noundef nonnull dereferenceable(1) %28) #8
  tail call void @free(ptr noundef %28) #8
  %indvars.iv.next63 = add nuw nsw i64 %indvars.iv62, 1
  %exitcond66.not = icmp eq i64 %indvars.iv.next63, %wide.trip.count65
  br i1 %exitcond66.not, label %._crit_edge55, label %.lr.ph54, !llvm.loop !13

30:                                               ; preds = %2, %4, %._crit_edge55
  %.037 = phi ptr [ %14, %._crit_edge55 ], [ %0, %4 ], [ %0, %2 ]
  ret ptr %.037
}

; Function Attrs: mustprogress nofree nounwind willreturn memory(argmem: read)
declare i64 @strlen(ptr nocapture noundef) local_unnamed_addr #1

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

; Function Attrs: mustprogress nofree nounwind willreturn memory(argmem: readwrite)
declare ptr @strncat(ptr noalias noundef returned, ptr noalias nocapture noundef readonly, i64 noundef) local_unnamed_addr #3

; Function Attrs: mustprogress nofree nounwind willreturn memory(argmem: readwrite)
declare ptr @strcat(ptr noalias noundef returned, ptr noalias nocapture noundef readonly) local_unnamed_addr #3

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

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.smax.i32(i32, i32) #5

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 memory(argmem: read) "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 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 #3 = { mustprogress nofree nounwind willreturn memory(argmem: readwrite) "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 = { 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 #5 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #6 = { nounwind willreturn memory(read) }
attributes #7 = { nounwind allocsize(0) }
attributes #8 = { 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 = !{!7, !7, i64 0}
!10 = distinct !{!10, !11}
!11 = !{!"llvm.loop.mustprogress"}
!12 = distinct !{!12, !11}
!13 = distinct !{!13, !11}


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