; LLVM IR from JessonYue/LeetCodeLearning :: Array/huangxinyu/homework/readelf.ll (Z11readElfShdriP10Elf64_ShdrP10Elf64_Ehdr)

; 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.Elf64_Ehdr = type { [16 x i8], i16, i16, i32, i64, i64, i64, i32, i16, i16, i16, i16, i16, i16 }
%struct.Elf64_Shdr = type { i32, i32, i64, i64, i64, i64, i32, i32, i64, i64 }

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

declare i64 @read(i32 noundef, ptr noundef, i64 noundef) #1

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

; Function Attrs: nounwind
declare i64 @lseek(i32 noundef, i64 noundef, i32 noundef) #2

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_Z11readElfShdriP10Elf64_ShdrP10Elf64_Ehdr(i32 noundef %0, ptr noundef %1, ptr noundef %2) #3 {
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i64, align 8
  %9 = alloca i32, align 4
  %10 = alloca i64, align 8
  store i32 %0, ptr %5, align 4, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !9
  store ptr %2, ptr %7, align 8, !tbaa !9
  %11 = load i32, ptr %5, align 4, !tbaa !5
  %12 = load ptr, ptr %7, align 8, !tbaa !9
  %13 = getelementptr inbounds %struct.Elf64_Ehdr, ptr %12, i32 0, i32 6
  %14 = load i64, ptr %13, align 8, !tbaa !11
  %15 = call i64 @lseek(i32 noundef %11, i64 noundef %14, i32 noundef 0) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #4
  store i64 0, ptr %8, align 8, !tbaa !15
  br label %16

16:                                               ; preds = %41, %3
  %17 = load i64, ptr %8, align 8, !tbaa !15
  %18 = load ptr, ptr %7, align 8, !tbaa !9
  %19 = getelementptr inbounds %struct.Elf64_Ehdr, ptr %18, i32 0, i32 12
  %20 = load i16, ptr %19, align 4, !tbaa !16
  %21 = zext i16 %20 to i64
  %22 = icmp ult i64 %17, %21
  br i1 %22, label %24, label %23

23:                                               ; preds = %16
  store i32 2, ptr %9, align 4
  br label %44

24:                                               ; preds = %16
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #4
  %25 = load i32, ptr %5, align 4, !tbaa !5
  %26 = load ptr, ptr %6, align 8, !tbaa !9
  %27 = load i64, ptr %8, align 8, !tbaa !15
  %28 = getelementptr inbounds %struct.Elf64_Shdr, ptr %26, i64 %27
  %29 = call i64 @read(i32 noundef %25, ptr noundef %28, i64 noundef 64)
  store i64 %29, ptr %10, align 8, !tbaa !15
  %30 = load i64, ptr %10, align 8, !tbaa !15
  %31 = load ptr, ptr %7, align 8, !tbaa !9
  %32 = getelementptr inbounds %struct.Elf64_Ehdr, ptr %31, i32 0, i32 11
  %33 = load i16, ptr %32, align 2, !tbaa !17
  %34 = zext i16 %33 to i64
  %35 = icmp ne i64 %30, %34
  br i1 %35, label %36, label %37

36:                                               ; preds = %24
  store i32 -1, ptr %4, align 4
  store i32 1, ptr %9, align 4
  br label %38

37:                                               ; preds = %24
  store i32 0, ptr %9, align 4
  br label %38

38:                                               ; preds = %37, %36
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #4
  %39 = load i32, ptr %9, align 4
  switch i32 %39, label %44 [
    i32 0, label %40
  ]

40:                                               ; preds = %38
  br label %41

41:                                               ; preds = %40
  %42 = load i64, ptr %8, align 8, !tbaa !15
  %43 = add i64 %42, 1
  store i64 %43, ptr %8, align 8, !tbaa !15
  br label %16, !llvm.loop !18

44:                                               ; preds = %38, %23
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #4
  %45 = load i32, ptr %9, align 4
  switch i32 %45, label %49 [
    i32 2, label %46
    i32 1, label %47
  ]

46:                                               ; preds = %44
  store i32 0, ptr %4, align 4
  br label %47

47:                                               ; preds = %46, %44
  %48 = load i32, ptr %4, align 4
  ret i32 %48

49:                                               ; preds = %44
  unreachable
}

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

!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, !14, i64 40}
!12 = !{!"_ZTS10Elf64_Ehdr", !7, i64 0, !13, i64 16, !13, i64 18, !6, i64 20, !14, i64 24, !14, i64 32, !14, i64 40, !6, i64 48, !13, i64 52, !13, i64 54, !13, i64 56, !13, i64 58, !13, i64 60, !13, i64 62}
!13 = !{!"short", !7, i64 0}
!14 = !{!"long", !7, i64 0}
!15 = !{!14, !14, i64 0}
!16 = !{!12, !13, i64 60}
!17 = !{!12, !13, i64 58}
!18 = distinct !{!18, !19}
!19 = !{!"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"

%struct.Elf64_Shdr = type { i32, i32, i64, i64, i64, i64, i32, i32, i64, i64 }

; Function Attrs: nofree
declare noundef i64 @read(i32 noundef, ptr nocapture noundef, i64 noundef) local_unnamed_addr #0

; Function Attrs: nounwind
declare i64 @lseek(i32 noundef, i64 noundef, i32 noundef) local_unnamed_addr #1

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_Z11readElfShdriP10Elf64_ShdrP10Elf64_Ehdr(i32 noundef %0, ptr nocapture noundef %1, ptr nocapture noundef readonly %2) local_unnamed_addr #2 {
  %4 = getelementptr inbounds i8, ptr %2, i64 40
  %5 = load i64, ptr %4, align 8, !tbaa !5
  %6 = tail call i64 @lseek(i32 noundef %0, i64 noundef %5, i32 noundef 0) #3
  %7 = getelementptr inbounds i8, ptr %2, i64 60
  %8 = load i16, ptr %7, align 4, !tbaa !12
  %.not1516.not = icmp eq i16 %8, 0
  br i1 %.not1516.not, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %3
  %9 = getelementptr inbounds i8, ptr %2, i64 58
  br label %14

10:                                               ; preds = %14
  %11 = add nuw nsw i64 %.01417, 1
  %12 = load i16, ptr %7, align 4, !tbaa !12
  %13 = zext i16 %12 to i64
  %.not15 = icmp ult i64 %11, %13
  br i1 %.not15, label %14, label %._crit_edge.loopexit, !llvm.loop !13

14:                                               ; preds = %.lr.ph, %10
  %.01417 = phi i64 [ 0, %.lr.ph ], [ %11, %10 ]
  %15 = getelementptr inbounds %struct.Elf64_Shdr, ptr %1, i64 %.01417
  %16 = tail call i64 @read(i32 noundef %0, ptr noundef %15, i64 noundef 64)
  %17 = load i16, ptr %9, align 2, !tbaa !15
  %18 = zext i16 %17 to i64
  %.not = icmp ne i64 %16, %18
  br i1 %.not, label %._crit_edge.loopexit, label %10

._crit_edge.loopexit:                             ; preds = %10, %14
  %19 = sext i1 %.not to i32
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit, %3
  %.not15.lcssa = phi i32 [ 0, %3 ], [ %19, %._crit_edge.loopexit ]
  ret i32 %.not15.lcssa
}

attributes #0 = { nofree "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 = { mustprogress 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 = { 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, !11, i64 40}
!6 = !{!"_ZTS10Elf64_Ehdr", !7, i64 0, !9, i64 16, !9, i64 18, !10, i64 20, !11, i64 24, !11, i64 32, !11, i64 40, !10, i64 48, !9, i64 52, !9, i64 54, !9, i64 56, !9, i64 58, !9, i64 60, !9, i64 62}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!"short", !7, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!"long", !7, i64 0}
!12 = !{!6, !9, i64 60}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = !{!6, !9, i64 58}


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