; LLVM IR from holzschu/ios_system :: text_cmds/look (look)

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

@dflag = external dso_local global i32, align 4
@fflag = external dso_local global i32, align 4

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

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

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

; Function Attrs: nounwind uwtable
define dso_local ptr @prepkey(ptr noundef %0, i32 noundef %1) #3 {
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  %9 = alloca i64, align 8
  %10 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i32 %1, ptr %4, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #7
  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 4, ptr %8) #7
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #7
  %11 = load ptr, ptr %3, align 8, !tbaa !5
  %12 = call i64 @strlen(ptr noundef %11) #8
  %13 = add i64 %12, 1
  %14 = mul i64 4, %13
  %15 = call noalias ptr @malloc(i64 noundef %14) #9
  store ptr %15, ptr %6, align 8, !tbaa !5
  %16 = icmp eq ptr %15, null
  br i1 %16, label %17, label %18

17:                                               ; preds = %2
  call void (i32, ptr, ...) @err(i32 noundef 2, ptr noundef null) #10
  unreachable

18:                                               ; preds = %2
  %19 = load ptr, ptr %3, align 8, !tbaa !5
  store ptr %19, ptr %5, align 8, !tbaa !5
  %20 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr %20, ptr %7, align 8, !tbaa !5
  br label %21

21:                                               ; preds = %52, %18
  %22 = load ptr, ptr %5, align 8, !tbaa !5
  %23 = call i64 @mbrtowc(ptr noundef %8, ptr noundef %22, i64 noundef 16, ptr noundef null) #7
  store i64 %23, ptr %9, align 8, !tbaa !11
  %24 = icmp ne i64 %23, 0
  br i1 %24, label %25, label %56

25:                                               ; preds = %21
  %26 = load i64, ptr %9, align 8, !tbaa !11
  %27 = icmp eq i64 %26, -1
  br i1 %27, label %31, label %28

28:                                               ; preds = %25
  %29 = load i64, ptr %9, align 8, !tbaa !11
  %30 = icmp eq i64 %29, -2
  br i1 %30, label %31, label %35

31:                                               ; preds = %28, %25
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #7
  %32 = call ptr @__errno_location() #11
  %33 = load i32, ptr %32, align 4, !tbaa !9
  store i32 %33, ptr %10, align 4, !tbaa !9
  %34 = call ptr @__errno_location() #11
  store i32 84, ptr %34, align 4, !tbaa !9
  call void (i32, ptr, ...) @err(i32 noundef 2, ptr noundef null) #10
  unreachable

35:                                               ; preds = %28
  %36 = load i32, ptr @fflag, align 4, !tbaa !9
  %37 = icmp ne i32 %36, 0
  br i1 %37, label %38, label %41

38:                                               ; preds = %35
  %39 = load i32, ptr %8, align 4, !tbaa !9
  %40 = call i32 @towlower(i32 noundef %39) #7
  store i32 %40, ptr %8, align 4, !tbaa !9
  br label %41

41:                                               ; preds = %38, %35
  %42 = load i32, ptr @dflag, align 4, !tbaa !9
  %43 = icmp ne i32 %42, 0
  br i1 %43, label %44, label %48

44:                                               ; preds = %41
  %45 = load i32, ptr %8, align 4, !tbaa !9
  %46 = call i32 @iswalnum(i32 noundef %45) #7
  %47 = icmp ne i32 %46, 0
  br i1 %47, label %48, label %52

48:                                               ; preds = %44, %41
  %49 = load i32, ptr %8, align 4, !tbaa !9
  %50 = load ptr, ptr %7, align 8, !tbaa !5
  %51 = getelementptr inbounds i32, ptr %50, i32 1
  store ptr %51, ptr %7, align 8, !tbaa !5
  store i32 %49, ptr %50, align 4, !tbaa !9
  br label %52

52:                                               ; preds = %48, %44
  %53 = load i64, ptr %9, align 8, !tbaa !11
  %54 = load ptr, ptr %5, align 8, !tbaa !5
  %55 = getelementptr inbounds i8, ptr %54, i64 %53
  store ptr %55, ptr %5, align 8, !tbaa !5
  br label %21, !llvm.loop !13

56:                                               ; preds = %21
  %57 = load ptr, ptr %7, align 8, !tbaa !5
  store i32 0, ptr %57, align 4, !tbaa !9
  %58 = load i32, ptr %4, align 4, !tbaa !9
  %59 = icmp ne i32 %58, 0
  br i1 %59, label %60, label %68

60:                                               ; preds = %56
  %61 = load ptr, ptr %6, align 8, !tbaa !5
  %62 = load i32, ptr %4, align 4, !tbaa !9
  %63 = call ptr @wcschr(ptr noundef %61, i32 noundef %62) #8
  store ptr %63, ptr %7, align 8, !tbaa !5
  %64 = icmp ne ptr %63, null
  br i1 %64, label %65, label %68

65:                                               ; preds = %60
  %66 = load ptr, ptr %7, align 8, !tbaa !5
  %67 = getelementptr inbounds i32, ptr %66, i32 1
  store ptr %67, ptr %7, align 8, !tbaa !5
  store i32 0, ptr %67, align 4, !tbaa !9
  br label %68

68:                                               ; preds = %65, %60, %56
  %69 = load ptr, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #7
  call void @llvm.lifetime.end.p0(i64 4, 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
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #7
  ret ptr %69
}

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

; Function Attrs: noreturn
declare void @err(i32 noundef, ptr noundef, ...) #5

; Function Attrs: nounwind willreturn memory(none)
declare ptr @__errno_location() #6

; Function Attrs: nounwind
declare i32 @towlower(i32 noundef) #1

; Function Attrs: nounwind
declare i32 @iswalnum(i32 noundef) #1

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

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

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { 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 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 = { 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 #5 = { noreturn "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 willreturn memory(none) "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 #7 = { nounwind }
attributes #8 = { nounwind willreturn memory(read) }
attributes #9 = { nounwind allocsize(0) }
attributes #10 = { noreturn }
attributes #11 = { nounwind willreturn memory(none) }

!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 = !{!12, !12, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"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"

@dflag = external dso_local local_unnamed_addr global i32, align 4
@fflag = external dso_local local_unnamed_addr global i32, align 4

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

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

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

; Function Attrs: nounwind uwtable
define dso_local noundef ptr @prepkey(ptr noundef %0, i32 noundef %1) local_unnamed_addr #3 {
  %3 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %3) #8
  %4 = tail call i64 @strlen(ptr noundef nonnull dereferenceable(1) %0) #9
  %5 = shl i64 %4, 2
  %6 = add i64 %5, 4
  %7 = tail call noalias ptr @malloc(i64 noundef %6) #10
  %8 = icmp eq ptr %7, null
  br i1 %8, label %10, label %.preheader

.preheader:                                       ; preds = %2
  %9 = call i64 @mbrtowc(ptr noundef nonnull %3, ptr noundef %0, i64 noundef 16, ptr noundef null) #8
  %.not26 = icmp eq i64 %9, 0
  br i1 %.not26, label %._crit_edge, label %.lr.ph

10:                                               ; preds = %2
  tail call void (i32, ptr, ...) @err(i32 noundef 2, ptr noundef null) #11
  unreachable

.lr.ph:                                           ; preds = %.preheader, %25
  %11 = phi i64 [ %27, %25 ], [ %9, %.preheader ]
  %.028 = phi ptr [ %26, %25 ], [ %0, %.preheader ]
  %.01527 = phi ptr [ %.1, %25 ], [ %7, %.preheader ]
  %or.cond = icmp ugt i64 %11, -3
  br i1 %or.cond, label %12, label %14

12:                                               ; preds = %.lr.ph
  %13 = tail call ptr @__errno_location() #12
  store i32 84, ptr %13, align 4, !tbaa !5
  call void (i32, ptr, ...) @err(i32 noundef 2, ptr noundef null) #11
  unreachable

14:                                               ; preds = %.lr.ph
  %15 = load i32, ptr @fflag, align 4, !tbaa !5
  %.not22 = icmp eq i32 %15, 0
  %.pre30.pre = load i32, ptr %3, align 4, !tbaa !5
  br i1 %.not22, label %18, label %16

16:                                               ; preds = %14
  %17 = call i32 @towlower(i32 noundef %.pre30.pre) #8
  store i32 %17, ptr %3, align 4, !tbaa !5
  br label %18

18:                                               ; preds = %16, %14
  %.pre30 = phi i32 [ %17, %16 ], [ %.pre30.pre, %14 ]
  %19 = load i32, ptr @dflag, align 4, !tbaa !5
  %.not23 = icmp eq i32 %19, 0
  br i1 %.not23, label %22, label %20

20:                                               ; preds = %18
  %21 = call i32 @iswalnum(i32 noundef %.pre30) #8
  %.not24 = icmp eq i32 %21, 0
  br i1 %.not24, label %25, label %._crit_edge29

._crit_edge29:                                    ; preds = %20
  %.pre = load i32, ptr %3, align 4, !tbaa !5
  br label %22

22:                                               ; preds = %._crit_edge29, %18
  %23 = phi i32 [ %.pre, %._crit_edge29 ], [ %.pre30, %18 ]
  %24 = getelementptr inbounds i8, ptr %.01527, i64 4
  store i32 %23, ptr %.01527, align 4, !tbaa !5
  br label %25

25:                                               ; preds = %22, %20
  %.1 = phi ptr [ %24, %22 ], [ %.01527, %20 ]
  %26 = getelementptr inbounds i8, ptr %.028, i64 %11
  %27 = call i64 @mbrtowc(ptr noundef nonnull %3, ptr noundef nonnull %26, i64 noundef 16, ptr noundef null) #8
  %.not = icmp eq i64 %27, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !9

._crit_edge:                                      ; preds = %25, %.preheader
  %.015.lcssa = phi ptr [ %7, %.preheader ], [ %.1, %25 ]
  store i32 0, ptr %.015.lcssa, align 4, !tbaa !5
  %.not20 = icmp eq i32 %1, 0
  br i1 %.not20, label %32, label %28

28:                                               ; preds = %._crit_edge
  %29 = call ptr @wcschr(ptr noundef nonnull %7, i32 noundef %1) #9
  %.not21 = icmp eq ptr %29, null
  br i1 %.not21, label %32, label %30

30:                                               ; preds = %28
  %31 = getelementptr inbounds i8, ptr %29, i64 4
  store i32 0, ptr %31, align 4, !tbaa !5
  br label %32

32:                                               ; preds = %30, %28, %._crit_edge
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %3) #8
  ret ptr %7
}

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

; Function Attrs: noreturn
declare void @err(i32 noundef, ptr noundef, ...) local_unnamed_addr #5

; Function Attrs: mustprogress nofree nosync nounwind willreturn memory(none)
declare ptr @__errno_location() local_unnamed_addr #6

; Function Attrs: nounwind
declare i32 @towlower(i32 noundef) local_unnamed_addr #1

; Function Attrs: nounwind
declare i32 @iswalnum(i32 noundef) local_unnamed_addr #1

; Function Attrs: mustprogress nofree nounwind willreturn memory(read)
declare ptr @wcschr(ptr noundef, i32 noundef) local_unnamed_addr #7

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

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { 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 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 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 #5 = { noreturn "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 = { mustprogress nofree nosync nounwind willreturn memory(none) "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 #7 = { mustprogress nofree 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 #8 = { nounwind }
attributes #9 = { nounwind willreturn memory(read) }
attributes #10 = { nounwind allocsize(0) }
attributes #11 = { noreturn nounwind }
attributes #12 = { nounwind willreturn memory(none) }

!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 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}


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