; LLVM IR from gem5/gem5 :: ext/libfdt (fdt_sw)

; 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.fdt_header = type { i32, i32, i32, i32, i32, i32, i32, i32, i32, i32 }
%struct.fdt_property = type { i32, i32, i32, [0 x i8] }

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

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @fdt_set_magic(ptr noundef, i32 noundef) #1

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @fdt_set_totalsize(ptr noundef, i32 noundef) #1

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @fdt32_to_cpu(i32 noundef) #1

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @fdt_set_off_dt_strings(ptr noundef, i32 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 uwtable
declare hidden i32 @_fdt_sw_check_header(ptr noundef) #2

; Function Attrs: nounwind uwtable
declare hidden ptr @_fdt_grab_space(ptr noundef, i64 noundef) #2

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @cpu_to_fdt32(i32 noundef) #1

; Function Attrs: nounwind uwtable
define dso_local i32 @fdt_finish(ptr noundef %0) #2 {
  %2 = alloca i32, align 4
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  %13 = alloca ptr, align 8
  %14 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #5
  %15 = load ptr, ptr %3, align 8, !tbaa !5
  store ptr %15, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #5
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #5
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #5
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #5
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #5
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #5
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #5
  %16 = load ptr, ptr %3, align 8, !tbaa !5
  %17 = call i32 @_fdt_sw_check_header(ptr noundef %16)
  store i32 %17, ptr %11, align 4, !tbaa !9
  %18 = icmp ne i32 %17, 0
  br i1 %18, label %19, label %21

19:                                               ; preds = %1
  %20 = load i32, ptr %11, align 4, !tbaa !9
  store i32 %20, ptr %2, align 4
  store i32 1, ptr %12, align 4
  br label %22

21:                                               ; preds = %1
  store i32 0, ptr %12, align 4
  br label %22

22:                                               ; preds = %21, %19
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #5
  %23 = load i32, ptr %12, align 4
  switch i32 %23, label %108 [
    i32 0, label %24
  ]

24:                                               ; preds = %22
  %25 = load ptr, ptr %3, align 8, !tbaa !5
  %26 = call ptr @_fdt_grab_space(ptr noundef %25, i64 noundef 4)
  store ptr %26, ptr %5, align 8, !tbaa !5
  %27 = load ptr, ptr %5, align 8, !tbaa !5
  %28 = icmp ne ptr %27, null
  br i1 %28, label %30, label %29

29:                                               ; preds = %24
  store i32 -3, ptr %2, align 4
  store i32 1, ptr %12, align 4
  br label %108

30:                                               ; preds = %24
  %31 = call i32 @cpu_to_fdt32(i32 noundef 9)
  %32 = load ptr, ptr %5, align 8, !tbaa !5
  store i32 %31, ptr %32, align 4, !tbaa !9
  %33 = load ptr, ptr %3, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.fdt_header, ptr %33, i32 0, i32 1
  %35 = load i32, ptr %34, align 4, !tbaa !11
  %36 = call i32 @fdt32_to_cpu(i32 noundef %35)
  %37 = load ptr, ptr %3, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.fdt_header, ptr %37, i32 0, i32 8
  %39 = load i32, ptr %38, align 4, !tbaa !13
  %40 = call i32 @fdt32_to_cpu(i32 noundef %39)
  %41 = sub i32 %36, %40
  store i32 %41, ptr %6, align 4, !tbaa !9
  %42 = load ptr, ptr %3, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct.fdt_header, ptr %42, i32 0, i32 2
  %44 = load i32, ptr %43, align 4, !tbaa !14
  %45 = call i32 @fdt32_to_cpu(i32 noundef %44)
  %46 = load ptr, ptr %3, align 8, !tbaa !5
  %47 = getelementptr inbounds %struct.fdt_header, ptr %46, i32 0, i32 9
  %48 = load i32, ptr %47, align 4, !tbaa !15
  %49 = call i32 @fdt32_to_cpu(i32 noundef %48)
  %50 = add i32 %45, %49
  store i32 %50, ptr %7, align 4, !tbaa !9
  %51 = load ptr, ptr %4, align 8, !tbaa !5
  %52 = load i32, ptr %7, align 4, !tbaa !9
  %53 = sext i32 %52 to i64
  %54 = getelementptr inbounds i8, ptr %51, i64 %53
  %55 = load ptr, ptr %4, align 8, !tbaa !5
  %56 = load i32, ptr %6, align 4, !tbaa !9
  %57 = sext i32 %56 to i64
  %58 = getelementptr inbounds i8, ptr %55, i64 %57
  %59 = load ptr, ptr %3, align 8, !tbaa !5
  %60 = getelementptr inbounds %struct.fdt_header, ptr %59, i32 0, i32 8
  %61 = load i32, ptr %60, align 4, !tbaa !13
  %62 = call i32 @fdt32_to_cpu(i32 noundef %61)
  %63 = zext i32 %62 to i64
  call void @llvm.memmove.p0.p0.i64(ptr align 1 %54, ptr align 1 %58, i64 %63, i1 false)
  %64 = load ptr, ptr %3, align 8, !tbaa !5
  %65 = load i32, ptr %7, align 4, !tbaa !9
  call void @fdt_set_off_dt_strings(ptr noundef %64, i32 noundef %65)
  store i32 0, ptr %9, align 4, !tbaa !9
  br label %66

66:                                               ; preds = %92, %30
  %67 = load ptr, ptr %3, align 8, !tbaa !5
  %68 = load i32, ptr %9, align 4, !tbaa !9
  %69 = call i32 @fdt_next_tag(ptr noundef %67, i32 noundef %68, ptr noundef %10)
  store i32 %69, ptr %8, align 4, !tbaa !9
  %70 = icmp ne i32 %69, 9
  br i1 %70, label %71, label %94

71:                                               ; preds = %66
  %72 = load i32, ptr %8, align 4, !tbaa !9
  %73 = icmp eq i32 %72, 3
  br i1 %73, label %74, label %92

74:                                               ; preds = %71
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #5
  %75 = load ptr, ptr %3, align 8, !tbaa !5
  %76 = load i32, ptr %9, align 4, !tbaa !9
  %77 = call ptr @_fdt_offset_ptr_w(ptr noundef %75, i32 noundef %76)
  store ptr %77, ptr %13, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %14) #5
  %78 = load ptr, ptr %13, align 8, !tbaa !5
  %79 = getelementptr inbounds %struct.fdt_property, ptr %78, i32 0, i32 2
  %80 = load i32, ptr %79, align 4, !tbaa !16
  %81 = call i32 @fdt32_to_cpu(i32 noundef %80)
  store i32 %81, ptr %14, align 4, !tbaa !9
  %82 = load ptr, ptr %3, align 8, !tbaa !5
  %83 = getelementptr inbounds %struct.fdt_header, ptr %82, i32 0, i32 8
  %84 = load i32, ptr %83, align 4, !tbaa !13
  %85 = call i32 @fdt32_to_cpu(i32 noundef %84)
  %86 = load i32, ptr %14, align 4, !tbaa !9
  %87 = add i32 %86, %85
  store i32 %87, ptr %14, align 4, !tbaa !9
  %88 = load i32, ptr %14, align 4, !tbaa !9
  %89 = call i32 @cpu_to_fdt32(i32 noundef %88)
  %90 = load ptr, ptr %13, align 8, !tbaa !5
  %91 = getelementptr inbounds %struct.fdt_property, ptr %90, i32 0, i32 2
  store i32 %89, ptr %91, align 4, !tbaa !16
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #5
  br label %92

92:                                               ; preds = %74, %71
  %93 = load i32, ptr %10, align 4, !tbaa !9
  store i32 %93, ptr %9, align 4, !tbaa !9
  br label %66, !llvm.loop !18

94:                                               ; preds = %66
  %95 = load i32, ptr %10, align 4, !tbaa !9
  %96 = icmp slt i32 %95, 0
  br i1 %96, label %97, label %99

97:                                               ; preds = %94
  %98 = load i32, ptr %10, align 4, !tbaa !9
  store i32 %98, ptr %2, align 4
  store i32 1, ptr %12, align 4
  br label %108

99:                                               ; preds = %94
  %100 = load ptr, ptr %3, align 8, !tbaa !5
  %101 = load i32, ptr %7, align 4, !tbaa !9
  %102 = load ptr, ptr %3, align 8, !tbaa !5
  %103 = getelementptr inbounds %struct.fdt_header, ptr %102, i32 0, i32 8
  %104 = load i32, ptr %103, align 4, !tbaa !13
  %105 = call i32 @fdt32_to_cpu(i32 noundef %104)
  %106 = add i32 %101, %105
  call void @fdt_set_totalsize(ptr noundef %100, i32 noundef %106)
  %107 = load ptr, ptr %3, align 8, !tbaa !5
  call void @fdt_set_magic(ptr noundef %107, i32 noundef -804389139)
  store i32 0, ptr %2, align 4
  store i32 1, ptr %12, align 4
  br label %108

108:                                              ; preds = %99, %97, %29, %22
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #5
  %109 = load i32, ptr %2, align 4
  ret i32 %109
}

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memmove.p0.p0.i64(ptr nocapture writeonly, ptr nocapture readonly, i64, i1 immarg) #3

declare i32 @fdt_next_tag(ptr noundef, i32 noundef, ptr noundef) #4

; Function Attrs: inlinehint nounwind uwtable
declare hidden ptr @_fdt_offset_ptr_w(ptr noundef, i32 noundef) #1

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

!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, !10, i64 4}
!12 = !{!"fdt_header", !10, i64 0, !10, i64 4, !10, i64 8, !10, i64 12, !10, i64 16, !10, i64 20, !10, i64 24, !10, i64 28, !10, i64 32, !10, i64 36}
!13 = !{!12, !10, i64 32}
!14 = !{!12, !10, i64 8}
!15 = !{!12, !10, i64 36}
!16 = !{!17, !10, i64 8}
!17 = !{!"fdt_property", !10, i64 0, !10, i64 4, !10, i64 8, !7, i64 12}
!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"

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

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @fdt_set_magic(ptr noundef, i32 noundef) local_unnamed_addr #1

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @fdt_set_totalsize(ptr noundef, i32 noundef) local_unnamed_addr #1

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

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @fdt_set_off_dt_strings(ptr noundef, i32 noundef) local_unnamed_addr #1

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

; Function Attrs: nounwind uwtable
declare hidden i32 @_fdt_sw_check_header(ptr noundef) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
declare hidden ptr @_fdt_grab_space(ptr noundef, i64 noundef) local_unnamed_addr #2

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

; Function Attrs: nounwind uwtable
define dso_local i32 @fdt_finish(ptr noundef %0) local_unnamed_addr #2 {
  %2 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %2) #5
  %3 = tail call i32 @_fdt_sw_check_header(ptr noundef %0)
  %.not = icmp eq i32 %3, 0
  br i1 %.not, label %4, label %49

4:                                                ; preds = %1
  %5 = tail call ptr @_fdt_grab_space(ptr noundef %0, i64 noundef 4)
  %.not36 = icmp eq ptr %5, null
  br i1 %.not36, label %49, label %6

6:                                                ; preds = %4
  %7 = tail call i32 @cpu_to_fdt32(i32 noundef 9)
  store i32 %7, ptr %5, align 4, !tbaa !5
  %8 = getelementptr inbounds i8, ptr %0, i64 4
  %9 = load i32, ptr %8, align 4, !tbaa !9
  %10 = tail call i32 @fdt32_to_cpu(i32 noundef %9)
  %11 = getelementptr inbounds i8, ptr %0, i64 32
  %12 = load i32, ptr %11, align 4, !tbaa !11
  %13 = tail call i32 @fdt32_to_cpu(i32 noundef %12)
  %14 = sub i32 %10, %13
  %15 = getelementptr inbounds i8, ptr %0, i64 8
  %16 = load i32, ptr %15, align 4, !tbaa !12
  %17 = tail call i32 @fdt32_to_cpu(i32 noundef %16)
  %18 = getelementptr inbounds i8, ptr %0, i64 36
  %19 = load i32, ptr %18, align 4, !tbaa !13
  %20 = tail call i32 @fdt32_to_cpu(i32 noundef %19)
  %21 = add i32 %20, %17
  %22 = sext i32 %21 to i64
  %23 = getelementptr inbounds i8, ptr %0, i64 %22
  %24 = sext i32 %14 to i64
  %25 = getelementptr inbounds i8, ptr %0, i64 %24
  %26 = load i32, ptr %11, align 4, !tbaa !11
  %27 = tail call i32 @fdt32_to_cpu(i32 noundef %26)
  %28 = zext i32 %27 to i64
  tail call void @llvm.memmove.p0.p0.i64(ptr align 1 %23, ptr align 1 %25, i64 %28, i1 false)
  tail call void @fdt_set_off_dt_strings(ptr noundef %0, i32 noundef %21)
  br label %29

29:                                               ; preds = %40, %6
  %.032 = phi i32 [ 0, %6 ], [ %41, %40 ]
  %30 = call i32 @fdt_next_tag(ptr noundef %0, i32 noundef %.032, ptr noundef nonnull %2) #5
  switch i32 %30, label %40 [
    i32 9, label %42
    i32 3, label %31
  ]

31:                                               ; preds = %29
  %32 = call ptr @_fdt_offset_ptr_w(ptr noundef %0, i32 noundef %.032)
  %33 = getelementptr inbounds i8, ptr %32, i64 8
  %34 = load i32, ptr %33, align 4, !tbaa !14
  %35 = call i32 @fdt32_to_cpu(i32 noundef %34)
  %36 = load i32, ptr %11, align 4, !tbaa !11
  %37 = call i32 @fdt32_to_cpu(i32 noundef %36)
  %38 = add i32 %37, %35
  %39 = call i32 @cpu_to_fdt32(i32 noundef %38)
  store i32 %39, ptr %33, align 4, !tbaa !14
  br label %40

40:                                               ; preds = %29, %31
  %41 = load i32, ptr %2, align 4, !tbaa !5
  br label %29, !llvm.loop !16

42:                                               ; preds = %29
  %43 = load i32, ptr %2, align 4, !tbaa !5
  %44 = icmp slt i32 %43, 0
  br i1 %44, label %49, label %45

45:                                               ; preds = %42
  %46 = load i32, ptr %11, align 4, !tbaa !11
  %47 = call i32 @fdt32_to_cpu(i32 noundef %46)
  %48 = add i32 %47, %21
  call void @fdt_set_totalsize(ptr noundef %0, i32 noundef %48)
  call void @fdt_set_magic(ptr noundef %0, i32 noundef -804389139)
  br label %49

49:                                               ; preds = %42, %4, %1, %45
  %.1 = phi i32 [ 0, %45 ], [ %3, %1 ], [ -3, %4 ], [ %43, %42 ]
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %2) #5
  ret i32 %.1
}

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memmove.p0.p0.i64(ptr nocapture writeonly, ptr nocapture readonly, i64, i1 immarg) #3

declare i32 @fdt_next_tag(ptr noundef, i32 noundef, ptr noundef) local_unnamed_addr #4

; Function Attrs: inlinehint nounwind uwtable
declare hidden ptr @_fdt_offset_ptr_w(ptr noundef, i32 noundef) local_unnamed_addr #1

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

!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 = !{!10, !6, i64 4}
!10 = !{!"fdt_header", !6, i64 0, !6, i64 4, !6, i64 8, !6, i64 12, !6, i64 16, !6, i64 20, !6, i64 24, !6, i64 28, !6, i64 32, !6, i64 36}
!11 = !{!10, !6, i64 32}
!12 = !{!10, !6, i64 8}
!13 = !{!10, !6, i64 36}
!14 = !{!15, !6, i64 8}
!15 = !{!"fdt_property", !6, i64 0, !6, i64 4, !6, i64 8, !7, i64 12}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}


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