; LLVM IR from 0intro/wmii :: lib/libstuff/fmt (localelen)

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

@.str = external hidden unnamed_addr constant [6 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local i32 @localelen(ptr noundef %0, ptr noundef %1) #0 {
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca %struct.__mbstate_t, align 4
  %7 = alloca i64, align 8
  %8 = alloca i64, align 8
  %9 = alloca i32, align 4
  %10 = alloca %struct.__mbstate_t, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #7
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #7
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #7
  %11 = call ptr @nl_langinfo(i32 noundef 14) #7
  %12 = call i32 @strcmp(ptr noundef %11, ptr noundef @.str) #8
  %13 = icmp ne i32 %12, 0
  br i1 %13, label %28, label %14

14:                                               ; preds = %2
  %15 = load ptr, ptr %5, align 8, !tbaa !5
  %16 = icmp ne ptr %15, null
  br i1 %16, label %17, label %25

17:                                               ; preds = %14
  %18 = load ptr, ptr %4, align 8, !tbaa !5
  %19 = load ptr, ptr %5, align 8, !tbaa !5
  %20 = load ptr, ptr %4, align 8, !tbaa !5
  %21 = ptrtoint ptr %19 to i64
  %22 = ptrtoint ptr %20 to i64
  %23 = sub i64 %21, %22
  %24 = call i32 @utfnlen(ptr noundef %18, i64 noundef %23)
  store i32 %24, ptr %3, align 4
  store i32 1, ptr %9, align 4
  br label %56

25:                                               ; preds = %14
  %26 = load ptr, ptr %4, align 8, !tbaa !5
  %27 = call i32 @utflen(ptr noundef %26)
  store i32 %27, ptr %3, align 4
  store i32 1, ptr %9, align 4
  br label %56

28:                                               ; preds = %2
  %29 = getelementptr inbounds %struct.__mbstate_t, ptr %10, i32 0, i32 0
  store i32 0, ptr %29, align 4, !tbaa !9
  %30 = getelementptr inbounds %struct.__mbstate_t, ptr %10, i32 0, i32 1
  call void @llvm.memset.p0.i64(ptr align 4 %30, i8 0, i64 4, i1 false)
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %6, ptr align 4 %10, i64 8, i1 false), !tbaa.struct !12
  store i64 0, ptr %7, align 8, !tbaa !15
  store i64 0, ptr %7, align 8, !tbaa !15
  br label %31

31:                                               ; preds = %55, %28
  %32 = load ptr, ptr %4, align 8, !tbaa !5
  %33 = load ptr, ptr %5, align 8, !tbaa !5
  %34 = icmp ne ptr %33, null
  br i1 %34, label %35, label %41

35:                                               ; preds = %31
  %36 = load ptr, ptr %5, align 8, !tbaa !5
  %37 = load ptr, ptr %4, align 8, !tbaa !5
  %38 = ptrtoint ptr %36 to i64
  %39 = ptrtoint ptr %37 to i64
  %40 = sub i64 %38, %39
  br label %42

41:                                               ; preds = %31
  br label %42

42:                                               ; preds = %41, %35
  %43 = phi i64 [ %40, %35 ], [ 16, %41 ]
  %44 = call i64 @mbrtowc(ptr noundef null, ptr noundef %32, i64 noundef %43, ptr noundef %6) #7
  store i64 %44, ptr %8, align 8, !tbaa !15
  switch i64 %44, label %49 [
    i64 -1, label %45
    i64 0, label %46
    i64 -2, label %46
  ]

45:                                               ; preds = %42
  store i32 -1, ptr %3, align 4
  store i32 1, ptr %9, align 4
  br label %56

46:                                               ; preds = %42, %42
  %47 = load i64, ptr %7, align 8, !tbaa !15
  %48 = trunc i64 %47 to i32
  store i32 %48, ptr %3, align 4
  store i32 1, ptr %9, align 4
  br label %56

49:                                               ; preds = %42
  %50 = load i64, ptr %7, align 8, !tbaa !15
  %51 = add i64 %50, 1
  store i64 %51, ptr %7, align 8, !tbaa !15
  %52 = load i64, ptr %8, align 8, !tbaa !15
  %53 = load ptr, ptr %4, align 8, !tbaa !5
  %54 = getelementptr inbounds i8, ptr %53, i64 %52
  store ptr %54, ptr %4, align 8, !tbaa !5
  br label %55

55:                                               ; preds = %49
  br label %31

56:                                               ; preds = %46, %45, %25, %17
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #7
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #7
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #7
  %57 = load i32, ptr %3, align 4
  ret i32 %57
}

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

; Function Attrs: nounwind willreturn memory(read)
declare i32 @strcmp(ptr noundef, ptr noundef) #2

; Function Attrs: nounwind
declare ptr @nl_langinfo(i32 noundef) #3

declare i32 @utfnlen(ptr noundef, i64 noundef) #4

declare i32 @utflen(ptr noundef) #4

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

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

; Function Attrs: nounwind
declare i64 @mbrtowc(ptr noundef, ptr noundef, i64 noundef, ptr noundef) #3

; 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 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 #3 = { 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 #4 = { "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: write) }
attributes #6 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #7 = { nounwind }
attributes #8 = { nounwind willreturn memory(read) }

!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, !11, i64 0}
!10 = !{!"", !11, i64 0, !7, i64 4}
!11 = !{!"int", !7, i64 0}
!12 = !{i64 0, i64 4, !13, i64 4, i64 4, !14}
!13 = !{!11, !11, i64 0}
!14 = !{!7, !7, i64 0}
!15 = !{!16, !16, i64 0}
!16 = !{!"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 }
%union.anon = type { i32 }

@.str = external hidden unnamed_addr constant [6 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local i32 @localelen(ptr noundef %0, ptr noundef %1) local_unnamed_addr #0 {
  %3 = alloca %struct.__mbstate_t, align 4
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %3) #5
  %4 = tail call ptr @nl_langinfo(i32 noundef 14) #5
  %5 = tail call i32 @strcmp(ptr noundef nonnull dereferenceable(1) %4, ptr noundef nonnull dereferenceable(1) @.str) #6
  %.not = icmp eq i32 %5, 0
  br i1 %.not, label %6, label %14

6:                                                ; preds = %2
  %.not16 = icmp eq ptr %1, null
  br i1 %.not16, label %12, label %7

7:                                                ; preds = %6
  %8 = ptrtoint ptr %1 to i64
  %9 = ptrtoint ptr %0 to i64
  %10 = sub i64 %8, %9
  %11 = tail call i32 @utfnlen(ptr noundef %0, i64 noundef %10) #5
  br label %.split20.us

12:                                               ; preds = %6
  %13 = tail call i32 @utflen(ptr noundef %0) #5
  br label %.split20.us

14:                                               ; preds = %2
  store i32 0, ptr %3, align 4, !tbaa !5
  %.sroa.2.0..sroa_idx = getelementptr inbounds i8, ptr %3, i64 4
  store i32 0, ptr %.sroa.2.0..sroa_idx, align 4, !tbaa !9
  %.not17 = icmp eq ptr %1, null
  %15 = ptrtoint ptr %1 to i64
  br i1 %.not17, label %.split.us, label %.split

.split.us:                                        ; preds = %14, %17
  %.014.us = phi i32 [ %18, %17 ], [ 0, %14 ]
  %.013.us = phi ptr [ %19, %17 ], [ %0, %14 ]
  %16 = call i64 @mbrtowc(ptr noundef null, ptr noundef %.013.us, i64 noundef 16, ptr noundef nonnull %3) #5
  switch i64 %16, label %17 [
    i64 -1, label %.split20.us.loopexit32
    i64 0, label %.split20.us
    i64 -2, label %.split20.us
  ]

17:                                               ; preds = %.split.us
  %18 = add i32 %.014.us, 1
  %19 = getelementptr inbounds i8, ptr %.013.us, i64 %16
  br label %.split.us

.split:                                           ; preds = %14, %23
  %.014 = phi i32 [ %24, %23 ], [ 0, %14 ]
  %.013 = phi ptr [ %25, %23 ], [ %0, %14 ]
  %20 = ptrtoint ptr %.013 to i64
  %21 = sub i64 %15, %20
  %22 = call i64 @mbrtowc(ptr noundef null, ptr noundef %.013, i64 noundef %21, ptr noundef nonnull %3) #5
  switch i64 %22, label %23 [
    i64 -1, label %.split20.us.loopexit34
    i64 0, label %.split20.us
    i64 -2, label %.split20.us
  ]

23:                                               ; preds = %.split
  %24 = add i32 %.014, 1
  %25 = getelementptr inbounds i8, ptr %.013, i64 %22
  br label %.split

.split20.us.loopexit32:                           ; preds = %.split.us
  br label %.split20.us

.split20.us.loopexit34:                           ; preds = %.split
  br label %.split20.us

.split20.us:                                      ; preds = %.split, %.split, %.split.us, %.split.us, %.split20.us.loopexit34, %.split20.us.loopexit32, %12, %7
  %.0 = phi i32 [ %11, %7 ], [ %13, %12 ], [ -1, %.split20.us.loopexit32 ], [ -1, %.split20.us.loopexit34 ], [ %.014.us, %.split.us ], [ %.014.us, %.split.us ], [ %.014, %.split ], [ %.014, %.split ]
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %3) #5
  ret i32 %.0
}

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

; Function Attrs: mustprogress nofree nounwind willreturn memory(argmem: read)
declare i32 @strcmp(ptr nocapture noundef, ptr nocapture noundef) local_unnamed_addr #2

; Function Attrs: nounwind
declare ptr @nl_langinfo(i32 noundef) local_unnamed_addr #3

declare i32 @utfnlen(ptr noundef, i64 noundef) local_unnamed_addr #4

declare i32 @utflen(ptr noundef) local_unnamed_addr #4

; Function Attrs: nounwind
declare i64 @mbrtowc(ptr noundef, ptr noundef, i64 noundef, ptr noundef) local_unnamed_addr #3

; Function Attrs: mustprogress 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 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { 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 #3 = { 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 #4 = { "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 }
attributes #6 = { nounwind willreturn memory(read) }

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


; Effective O3 passes: AggressiveInstCombinePass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopInstSimplifyPass,LoopSimplifyCFGPass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass