; LLVM IR from McSimp/linoodle :: windows_api.ll (Z19WideCharToMultiBytejjPKwiPciPKcPi)

; 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.__mbstate_t = type { i32, %union.anon.5 }
%union.anon.5 = type { i32 }

; Function Attrs: nounwind allocsize(0,1)
declare noalias ptr @calloc(i64 noundef, i64 noundef) #0

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

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

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

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

; Function Attrs: mustprogress nounwind uwtable
declare dso_local win64cc void @_Z12SetLastErrorj(i32 noundef) #4

; Function Attrs: mustprogress nounwind uwtable
define dso_local win64cc noundef i32 @_Z19WideCharToMultiBytejjPKwiPciPKcPi(i32 noundef %0, i32 noundef %1, ptr noundef %2, i32 noundef %3, ptr noundef %4, i32 noundef %5, ptr noundef %6, ptr noundef %7) #4 {
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  %12 = alloca ptr, align 8
  %13 = alloca i32, align 4
  %14 = alloca ptr, align 8
  %15 = alloca i32, align 4
  %16 = alloca ptr, align 8
  %17 = alloca ptr, align 8
  %18 = alloca ptr, align 8
  %19 = alloca %struct.__mbstate_t, align 4
  %20 = alloca ptr, align 8
  %21 = alloca i64, align 8
  %22 = alloca i32, align 4
  %23 = alloca i64, align 8
  %24 = alloca %struct.__mbstate_t, align 4
  %25 = alloca i64, align 8
  store i32 %0, ptr %10, align 4, !tbaa !5
  store i32 %1, ptr %11, align 4, !tbaa !5
  store ptr %2, ptr %12, align 8, !tbaa !9
  store i32 %3, ptr %13, align 4, !tbaa !5
  store ptr %4, ptr %14, align 8, !tbaa !9
  store i32 %5, ptr %15, align 4, !tbaa !5
  store ptr %6, ptr %16, align 8, !tbaa !9
  store ptr %7, ptr %17, align 8, !tbaa !9
  %26 = load i32, ptr %10, align 4, !tbaa !5
  %27 = icmp ne i32 %26, 0
  br i1 %27, label %34, label %28

28:                                               ; preds = %8
  %29 = load i32, ptr %11, align 4, !tbaa !5
  %30 = icmp ne i32 %29, 0
  br i1 %30, label %34, label %31

31:                                               ; preds = %28
  %32 = load i32, ptr %13, align 4, !tbaa !5
  %33 = icmp eq i32 %32, 0
  br i1 %33, label %34, label %35

34:                                               ; preds = %31, %28, %8
  call win64cc void @_Z12SetLastErrorj(i32 noundef 87)
  store i32 0, ptr %9, align 4
  br label %93

35:                                               ; preds = %31
  %36 = load i32, ptr %13, align 4, !tbaa !5
  %37 = icmp ne i32 %36, -1
  br i1 %37, label %38, label %72

38:                                               ; preds = %35
  call void @llvm.lifetime.start.p0(i64 8, ptr %18) #6
  %39 = load i32, ptr %13, align 4, !tbaa !5
  %40 = add nsw i32 %39, 1
  %41 = sext i32 %40 to i64
  %42 = call noalias ptr @calloc(i64 noundef %41, i64 noundef 4) #7
  store ptr %42, ptr %18, align 8, !tbaa !9
  %43 = load ptr, ptr %18, align 8, !tbaa !9
  %44 = load ptr, ptr %12, align 8, !tbaa !9
  %45 = load i32, ptr %13, align 4, !tbaa !5
  %46 = mul nsw i32 %45, 2
  %47 = sext i32 %46 to i64
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %43, ptr align 4 %44, i64 %47, i1 false)
  call void @llvm.lifetime.start.p0(i64 8, ptr %19) #6
  call void @llvm.memset.p0.i64(ptr align 4 %19, i8 0, i64 8, i1 false)
  call void @llvm.lifetime.start.p0(i64 8, ptr %20) #6
  %48 = load ptr, ptr %18, align 8, !tbaa !9
  store ptr %48, ptr %20, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %21) #6
  %49 = call i64 @wcsrtombs(ptr noundef null, ptr noundef %20, i64 noundef 0, ptr noundef %19) #6
  store i64 %49, ptr %21, align 8, !tbaa !11
  %50 = load i32, ptr %15, align 4, !tbaa !5
  %51 = icmp eq i32 %50, 0
  br i1 %51, label %52, label %56

52:                                               ; preds = %38
  %53 = load ptr, ptr %18, align 8, !tbaa !9
  call void @free(ptr noundef %53) #6
  %54 = load i64, ptr %21, align 8, !tbaa !11
  %55 = trunc i64 %54 to i32
  store i32 %55, ptr %9, align 4
  store i32 1, ptr %22, align 4
  br label %71

56:                                               ; preds = %38
  %57 = load i32, ptr %15, align 4, !tbaa !5
  %58 = sext i32 %57 to i64
  %59 = load i64, ptr %21, align 8, !tbaa !11
  %60 = icmp ult i64 %58, %59
  br i1 %60, label %61, label %63

61:                                               ; preds = %56
  call win64cc void @_Z12SetLastErrorj(i32 noundef 122)
  %62 = load ptr, ptr %18, align 8, !tbaa !9
  call void @free(ptr noundef %62) #6
  store i32 0, ptr %9, align 4
  store i32 1, ptr %22, align 4
  br label %71

63:                                               ; preds = %56
  call void @llvm.lifetime.start.p0(i64 8, ptr %23) #6
  %64 = load ptr, ptr %14, align 8, !tbaa !9
  %65 = load i32, ptr %15, align 4, !tbaa !5
  %66 = sext i32 %65 to i64
  %67 = call i64 @wcsrtombs(ptr noundef %64, ptr noundef %20, i64 noundef %66, ptr noundef %19) #6
  store i64 %67, ptr %23, align 8, !tbaa !11
  %68 = load ptr, ptr %18, align 8, !tbaa !9
  call void @free(ptr noundef %68) #6
  %69 = load i64, ptr %23, align 8, !tbaa !11
  %70 = trunc i64 %69 to i32
  store i32 %70, ptr %9, align 4
  store i32 1, ptr %22, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %23) #6
  br label %71

71:                                               ; preds = %63, %61, %52
  call void @llvm.lifetime.end.p0(i64 8, ptr %21) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %20) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %19) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %18) #6
  br label %93

72:                                               ; preds = %35
  call void @llvm.lifetime.start.p0(i64 8, ptr %24) #6
  call void @llvm.memset.p0.i64(ptr align 4 %24, i8 0, i64 8, i1 false)
  call void @llvm.lifetime.start.p0(i64 8, ptr %25) #6
  %73 = call i64 @wcsrtombs(ptr noundef null, ptr noundef %12, i64 noundef 0, ptr noundef %24) #6
  %74 = add i64 1, %73
  store i64 %74, ptr %25, align 8, !tbaa !11
  %75 = load i32, ptr %15, align 4, !tbaa !5
  %76 = icmp eq i32 %75, 0
  br i1 %76, label %77, label %80

77:                                               ; preds = %72
  %78 = load i64, ptr %25, align 8, !tbaa !11
  %79 = trunc i64 %78 to i32
  store i32 %79, ptr %9, align 4
  store i32 1, ptr %22, align 4
  br label %92

80:                                               ; preds = %72
  %81 = load i32, ptr %15, align 4, !tbaa !5
  %82 = sext i32 %81 to i64
  %83 = load i64, ptr %25, align 8, !tbaa !11
  %84 = icmp ult i64 %82, %83
  br i1 %84, label %85, label %86

85:                                               ; preds = %80
  call win64cc void @_Z12SetLastErrorj(i32 noundef 122)
  store i32 0, ptr %9, align 4
  store i32 1, ptr %22, align 4
  br label %92

86:                                               ; preds = %80
  %87 = load ptr, ptr %14, align 8, !tbaa !9
  %88 = load i32, ptr %15, align 4, !tbaa !5
  %89 = sext i32 %88 to i64
  %90 = call i64 @wcsrtombs(ptr noundef %87, ptr noundef %12, i64 noundef %89, ptr noundef %24) #6
  %91 = trunc i64 %90 to i32
  store i32 %91, ptr %9, align 4
  store i32 1, ptr %22, align 4
  br label %92

92:                                               ; preds = %86, %85, %77
  call void @llvm.lifetime.end.p0(i64 8, ptr %25) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %24) #6
  br label %93

93:                                               ; preds = %92, %71, %34
  %94 = load i32, ptr %9, align 4
  ret i32 %94
}

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

; Function Attrs: nounwind
declare i64 @wcsrtombs(ptr noundef, ptr noundef, i64 noundef, ptr noundef) #1

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

!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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"long", !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"

%struct.__mbstate_t = type { i32, %union.anon.5 }
%union.anon.5 = type { i32 }

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

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

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

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

; Function Attrs: mustprogress nounwind uwtable
declare dso_local win64cc void @_Z12SetLastErrorj(i32 noundef) local_unnamed_addr #3

; Function Attrs: mustprogress nounwind uwtable
define dso_local win64cc noundef i32 @_Z19WideCharToMultiBytejjPKwiPciPKcPi(i32 noundef %0, i32 noundef %1, ptr noundef %2, i32 noundef %3, ptr noundef %4, i32 noundef %5, ptr nocapture noundef readnone %6, ptr nocapture noundef readnone %7) local_unnamed_addr #3 {
  %9 = alloca ptr, align 8
  %10 = alloca %struct.__mbstate_t, align 8
  %11 = alloca ptr, align 8
  %12 = alloca %struct.__mbstate_t, align 8
  store ptr %2, ptr %9, align 8, !tbaa !5
  %13 = or i32 %1, %0
  %or.cond = icmp ne i32 %13, 0
  %14 = icmp eq i32 %3, 0
  %or.cond3 = or i1 %or.cond, %14
  br i1 %or.cond3, label %15, label %16

15:                                               ; preds = %8
  tail call win64cc void @_Z12SetLastErrorj(i32 noundef 87)
  br label %49

16:                                               ; preds = %8
  %.not = icmp eq i32 %3, -1
  br i1 %.not, label %35, label %17

17:                                               ; preds = %16
  %18 = add nuw nsw i32 %3, 1
  %19 = sext i32 %18 to i64
  %20 = tail call noalias ptr @calloc(i64 noundef %19, i64 noundef 4) #6
  %21 = shl nsw i32 %3, 1
  %22 = sext i32 %21 to i64
  tail call void @llvm.memcpy.p0.p0.i64(ptr align 4 %20, ptr align 4 %2, i64 %22, i1 false)
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %10) #7
  store i64 0, ptr %10, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %11) #7
  store ptr %20, ptr %11, align 8, !tbaa !5
  %23 = call i64 @wcsrtombs(ptr noundef null, ptr noundef nonnull %11, i64 noundef 0, ptr noundef nonnull %10) #7
  %24 = icmp eq i32 %5, 0
  br i1 %24, label %25, label %27

25:                                               ; preds = %17
  call void @free(ptr noundef %20) #7
  %26 = trunc i64 %23 to i32
  br label %34

27:                                               ; preds = %17
  %28 = sext i32 %5 to i64
  %29 = icmp ugt i64 %23, %28
  br i1 %29, label %30, label %31

30:                                               ; preds = %27
  call win64cc void @_Z12SetLastErrorj(i32 noundef 122)
  call void @free(ptr noundef %20) #7
  br label %34

31:                                               ; preds = %27
  %32 = call i64 @wcsrtombs(ptr noundef %4, ptr noundef nonnull %11, i64 noundef %28, ptr noundef nonnull %10) #7
  call void @free(ptr noundef %20) #7
  %33 = trunc i64 %32 to i32
  br label %34

34:                                               ; preds = %31, %30, %25
  %.0 = phi i32 [ %26, %25 ], [ 0, %30 ], [ %33, %31 ]
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %11) #7
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %10) #7
  br label %49

35:                                               ; preds = %16
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %12) #7
  store i64 0, ptr %12, align 8
  %36 = call i64 @wcsrtombs(ptr noundef null, ptr noundef nonnull %9, i64 noundef 0, ptr noundef nonnull %12) #7
  %37 = add i64 %36, 1
  %38 = icmp eq i32 %5, 0
  br i1 %38, label %39, label %41

39:                                               ; preds = %35
  %40 = trunc i64 %37 to i32
  br label %48

41:                                               ; preds = %35
  %42 = sext i32 %5 to i64
  %43 = icmp ugt i64 %37, %42
  br i1 %43, label %44, label %45

44:                                               ; preds = %41
  call win64cc void @_Z12SetLastErrorj(i32 noundef 122)
  br label %48

45:                                               ; preds = %41
  %46 = call i64 @wcsrtombs(ptr noundef %4, ptr noundef nonnull %9, i64 noundef %42, ptr noundef nonnull %12) #7
  %47 = trunc i64 %46 to i32
  br label %48

48:                                               ; preds = %45, %44, %39
  %.1 = phi i32 [ %40, %39 ], [ 0, %44 ], [ %47, %45 ]
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %12) #7
  br label %49

49:                                               ; preds = %48, %34, %15
  %.2 = phi i32 [ 0, %15 ], [ %.0, %34 ], [ %.1, %48 ]
  ret i32 %.2
}

; Function Attrs: mustprogress 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) #4

; Function Attrs: nounwind
declare i64 @wcsrtombs(ptr noundef, ptr noundef, i64 noundef, ptr noundef) local_unnamed_addr #5

attributes #0 = { mustprogress nofree nounwind willreturn allockind("alloc,zeroed") allocsize(0,1) 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 #1 = { 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 #2 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { mustprogress 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 #4 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #5 = { 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 #6 = { nounwind allocsize(0,1) }
attributes #7 = { 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++ TBAA"}


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