; LLVM IR from holzschu/ios_system :: shell_cmds/hexdump (display)

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

@length = external global i32, align 4
@stdin = external global ptr, align 8

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

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

; Function Attrs: nounwind uwtable
define dso_local i64 @peek(ptr noundef %0, i64 noundef %1) #1 {
  %3 = alloca ptr, align 8
  %4 = alloca i64, align 8
  %5 = alloca i64, align 8
  %6 = alloca i64, align 8
  %7 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i64 %1, ptr %4, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #4
  %8 = load i32, ptr @length, align 4, !tbaa !11
  %9 = icmp ne i32 %8, -1
  br i1 %9, label %10, label %18

10:                                               ; preds = %2
  %11 = load i64, ptr %4, align 8, !tbaa !9
  %12 = load i32, ptr @length, align 4, !tbaa !11
  %13 = sext i32 %12 to i64
  %14 = icmp ugt i64 %11, %13
  br i1 %14, label %15, label %18

15:                                               ; preds = %10
  %16 = load i32, ptr @length, align 4, !tbaa !11
  %17 = sext i32 %16 to i64
  store i64 %17, ptr %4, align 8, !tbaa !9
  br label %18

18:                                               ; preds = %15, %10, %2
  store i64 0, ptr %6, align 8, !tbaa !9
  br label %19

19:                                               ; preds = %28, %18
  %20 = load i64, ptr %6, align 8, !tbaa !9
  %21 = load i64, ptr %4, align 8, !tbaa !9
  %22 = icmp ult i64 %20, %21
  br i1 %22, label %23, label %26

23:                                               ; preds = %19
  %24 = call i32 @getchar()
  store i32 %24, ptr %7, align 4, !tbaa !11
  %25 = icmp ne i32 %24, -1
  br label %26

26:                                               ; preds = %23, %19
  %27 = phi i1 [ false, %19 ], [ %25, %23 ]
  br i1 %27, label %28, label %35

28:                                               ; preds = %26
  %29 = load i32, ptr %7, align 4, !tbaa !11
  %30 = trunc i32 %29 to i8
  %31 = load ptr, ptr %3, align 8, !tbaa !5
  %32 = getelementptr inbounds i8, ptr %31, i32 1
  store ptr %32, ptr %3, align 8, !tbaa !5
  store i8 %30, ptr %31, align 1, !tbaa !13
  %33 = load i64, ptr %6, align 8, !tbaa !9
  %34 = add i64 %33, 1
  store i64 %34, ptr %6, align 8, !tbaa !9
  br label %19, !llvm.loop !14

35:                                               ; preds = %26
  %36 = load i64, ptr %6, align 8, !tbaa !9
  store i64 %36, ptr %5, align 8, !tbaa !9
  br label %37

37:                                               ; preds = %41, %35
  %38 = load i64, ptr %5, align 8, !tbaa !9
  %39 = add i64 %38, -1
  store i64 %39, ptr %5, align 8, !tbaa !9
  %40 = icmp ugt i64 %38, 0
  br i1 %40, label %41, label %49

41:                                               ; preds = %37
  %42 = load ptr, ptr %3, align 8, !tbaa !5
  %43 = getelementptr inbounds i8, ptr %42, i32 -1
  store ptr %43, ptr %3, align 8, !tbaa !5
  %44 = load i8, ptr %43, align 1, !tbaa !13
  %45 = zext i8 %44 to i32
  store i32 %45, ptr %7, align 4, !tbaa !11
  %46 = load i32, ptr %7, align 4, !tbaa !11
  %47 = load ptr, ptr @stdin, align 8, !tbaa !5
  %48 = call i32 @ungetc(i32 noundef %46, ptr noundef %47)
  br label %37, !llvm.loop !16

49:                                               ; preds = %37
  %50 = load i64, ptr %6, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #4
  ret i64 %50
}

; Function Attrs: inlinehint nounwind uwtable
declare i32 @getchar() #2

declare i32 @ungetc(i32 noundef, ptr noundef) #3

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #2 = { inlinehint 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 #3 = { "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 }

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


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

@length = external local_unnamed_addr global i32, align 4
@stdin = external local_unnamed_addr global ptr, align 8

; Function Attrs: nofree nounwind uwtable
define dso_local i64 @peek(ptr nocapture noundef %0, i64 noundef %1) local_unnamed_addr #0 {
  %3 = load i32, ptr @length, align 4, !tbaa !5
  %.not = icmp eq i32 %3, -1
  %4 = sext i32 %3 to i64
  %spec.select = tail call i64 @llvm.umin.i64(i64 %4, i64 %1)
  %.011 = select i1 %.not, i64 %1, i64 %spec.select
  %.not29 = icmp eq i64 %.011, 0
  br i1 %.not29, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %2, %6
  %.020 = phi i64 [ %9, %6 ], [ 0, %2 ]
  %.01219 = phi ptr [ %8, %6 ], [ %0, %2 ]
  %5 = tail call i32 @getchar()
  %.not17 = icmp eq i32 %5, -1
  br i1 %.not17, label %.critedge, label %6

6:                                                ; preds = %.lr.ph
  %7 = trunc i32 %5 to i8
  %8 = getelementptr inbounds i8, ptr %.01219, i64 1
  store i8 %7, ptr %.01219, align 1, !tbaa !9
  %9 = add nuw i64 %.020, 1
  %exitcond.not = icmp eq i64 %9, %.011
  br i1 %exitcond.not, label %.lr.ph28.preheader, label %.lr.ph, !llvm.loop !10

.critedge:                                        ; preds = %.lr.ph
  %.not1825 = icmp eq i64 %.020, 0
  br i1 %.not1825, label %._crit_edge, label %.lr.ph28.preheader

.lr.ph28.preheader:                               ; preds = %6, %.critedge
  %.0.lcssa39 = phi i64 [ %.020, %.critedge ], [ %.011, %6 ]
  %.012.lcssa38 = phi ptr [ %.01219, %.critedge ], [ %8, %6 ]
  br label %.lr.ph28

.lr.ph28:                                         ; preds = %.lr.ph28.preheader, %.lr.ph28
  %.01027 = phi i64 [ %10, %.lr.ph28 ], [ %.0.lcssa39, %.lr.ph28.preheader ]
  %.126 = phi ptr [ %11, %.lr.ph28 ], [ %.012.lcssa38, %.lr.ph28.preheader ]
  %10 = add i64 %.01027, -1
  %11 = getelementptr inbounds i8, ptr %.126, i64 -1
  %12 = load i8, ptr %11, align 1, !tbaa !9
  %13 = zext i8 %12 to i32
  %14 = load ptr, ptr @stdin, align 8, !tbaa !12
  %15 = tail call i32 @ungetc(i32 noundef %13, ptr noundef %14)
  %.not18 = icmp eq i64 %10, 0
  br i1 %.not18, label %._crit_edge, label %.lr.ph28, !llvm.loop !14

._crit_edge:                                      ; preds = %.lr.ph28, %2, %.critedge
  %.0.lcssa33 = phi i64 [ 0, %.critedge ], [ 0, %2 ], [ %.0.lcssa39, %.lr.ph28 ]
  ret i64 %.0.lcssa33
}

; Function Attrs: inlinehint nofree nounwind uwtable
declare noundef i32 @getchar() local_unnamed_addr #1

; Function Attrs: nofree nounwind
declare noundef i32 @ungetc(i32 noundef, ptr nocapture noundef) local_unnamed_addr #2

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.umin.i64(i64, i64) #3

attributes #0 = { nofree 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 = { inlinehint nofree 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 #2 = { nofree 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 #3 = { nocallback nofree nosync nounwind speculatable 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 = !{!7, !7, i64 0}
!10 = distinct !{!10, !11}
!11 = !{!"llvm.loop.mustprogress"}
!12 = !{!13, !13, i64 0}
!13 = !{!"any pointer", !7, i64 0}
!14 = distinct !{!14, !11}


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