; LLVM IR from Columbitech/rohc :: src/network.ll (ZN4ROHC26calculateUdpHeaderCheckSumEPNS_5iphdrE)

; 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.ROHC::udphdr" = type { i16, i16, i16, i16 }
%"struct.ROHC::iphdr" = type { i8, i8, i16, i16, i16, i8, i8, i16, i32, i32 }

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN4ROHC26calculateUdpHeaderCheckSumEPNS_5iphdrE(ptr noundef %0) #0 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i16, align 2
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #4
  %8 = load ptr, ptr %2, align 8, !tbaa !5
  %9 = load ptr, ptr %2, align 8, !tbaa !5
  %10 = load i8, ptr %9, align 4
  %11 = and i8 %10, 15
  %12 = zext i8 %11 to i32
  %13 = mul nsw i32 %12, 4
  %14 = sext i32 %13 to i64
  %15 = getelementptr inbounds i8, ptr %8, i64 %14
  store ptr %15, ptr %3, align 8, !tbaa !5
  %16 = load ptr, ptr %3, align 8, !tbaa !5
  %17 = getelementptr inbounds %"struct.ROHC::udphdr", ptr %16, i32 0, i32 3
  store i16 0, ptr %17, align 2, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #4
  %18 = load ptr, ptr %3, align 8, !tbaa !5
  %19 = load ptr, ptr %3, align 8, !tbaa !5
  %20 = getelementptr inbounds %"struct.ROHC::udphdr", ptr %19, i32 0, i32 2
  %21 = load i16, ptr %20, align 2, !tbaa !12
  %22 = call noundef zeroext i16 @_ZN4ROHC10rohc_htonsEt(i16 noundef zeroext %21)
  %23 = zext i16 %22 to i64
  %24 = call noundef i32 @_ZN12_GLOBAL__N_111OneChecksumEPKhm(ptr noundef %18, i64 noundef %23)
  store i32 %24, ptr %4, align 4, !tbaa !13
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #4
  %25 = load ptr, ptr %2, align 8, !tbaa !5
  %26 = getelementptr inbounds %"struct.ROHC::iphdr", ptr %25, i32 0, i32 8
  store ptr %26, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #4
  %27 = load ptr, ptr %2, align 8, !tbaa !5
  %28 = getelementptr inbounds %"struct.ROHC::iphdr", ptr %27, i32 0, i32 9
  store ptr %28, ptr %6, align 8, !tbaa !5
  %29 = load ptr, ptr %2, align 8, !tbaa !5
  %30 = getelementptr inbounds %"struct.ROHC::iphdr", ptr %29, i32 0, i32 6
  %31 = load i8, ptr %30, align 1, !tbaa !15
  %32 = zext i8 %31 to i16
  %33 = call noundef zeroext i16 @_ZN4ROHC10rohc_htonsEt(i16 noundef zeroext %32)
  %34 = zext i16 %33 to i32
  %35 = load ptr, ptr %3, align 8, !tbaa !5
  %36 = getelementptr inbounds %"struct.ROHC::udphdr", ptr %35, i32 0, i32 2
  %37 = load i16, ptr %36, align 2, !tbaa !12
  %38 = zext i16 %37 to i32
  %39 = add nsw i32 %34, %38
  %40 = load ptr, ptr %5, align 8, !tbaa !5
  %41 = getelementptr inbounds i16, ptr %40, i64 0
  %42 = load i16, ptr %41, align 2, !tbaa !17
  %43 = zext i16 %42 to i32
  %44 = add nsw i32 %39, %43
  %45 = load ptr, ptr %5, align 8, !tbaa !5
  %46 = getelementptr inbounds i16, ptr %45, i64 1
  %47 = load i16, ptr %46, align 2, !tbaa !17
  %48 = zext i16 %47 to i32
  %49 = add nsw i32 %44, %48
  %50 = load ptr, ptr %6, align 8, !tbaa !5
  %51 = getelementptr inbounds i16, ptr %50, i64 0
  %52 = load i16, ptr %51, align 2, !tbaa !17
  %53 = zext i16 %52 to i32
  %54 = add nsw i32 %49, %53
  %55 = load ptr, ptr %6, align 8, !tbaa !5
  %56 = getelementptr inbounds i16, ptr %55, i64 1
  %57 = load i16, ptr %56, align 2, !tbaa !17
  %58 = zext i16 %57 to i32
  %59 = add nsw i32 %54, %58
  %60 = load i32, ptr %4, align 4, !tbaa !13
  %61 = add i32 %60, %59
  store i32 %61, ptr %4, align 4, !tbaa !13
  br label %62

62:                                               ; preds = %66, %1
  %63 = load i32, ptr %4, align 4, !tbaa !13
  %64 = lshr i32 %63, 16
  %65 = icmp ne i32 %64, 0
  br i1 %65, label %66, label %72

66:                                               ; preds = %62
  %67 = load i32, ptr %4, align 4, !tbaa !13
  %68 = and i32 %67, 65535
  %69 = load i32, ptr %4, align 4, !tbaa !13
  %70 = lshr i32 %69, 16
  %71 = add i32 %68, %70
  store i32 %71, ptr %4, align 4, !tbaa !13
  br label %62, !llvm.loop !18

72:                                               ; preds = %62
  %73 = load i32, ptr %4, align 4, !tbaa !13
  %74 = xor i32 %73, -1
  store i32 %74, ptr %4, align 4, !tbaa !13
  call void @llvm.lifetime.start.p0(i64 2, ptr %7) #4
  %75 = load i32, ptr %4, align 4, !tbaa !13
  %76 = trunc i32 %75 to i16
  store i16 %76, ptr %7, align 2, !tbaa !17
  %77 = load i16, ptr %7, align 2, !tbaa !17
  %78 = icmp ne i16 %77, 0
  br i1 %78, label %80, label %79

79:                                               ; preds = %72
  store i16 -1, ptr %7, align 2, !tbaa !17
  br label %80

80:                                               ; preds = %79, %72
  %81 = load i16, ptr %7, align 2, !tbaa !17
  %82 = load ptr, ptr %3, align 8, !tbaa !5
  %83 = getelementptr inbounds %"struct.ROHC::udphdr", ptr %82, i32 0, i32 3
  store i16 %81, ptr %83, align 2, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 2, 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
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #4
  ret void
}

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

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

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef zeroext i16 @_ZN4ROHC10rohc_htonsEt(i16 noundef zeroext) #3

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

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

!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++ TBAA"}
!9 = !{!10, !11, i64 6}
!10 = !{!"_ZTSN4ROHC6udphdrE", !11, i64 0, !11, i64 2, !11, i64 4, !11, i64 6}
!11 = !{!"short", !7, i64 0}
!12 = !{!10, !11, i64 4}
!13 = !{!14, !14, i64 0}
!14 = !{!"int", !7, i64 0}
!15 = !{!16, !7, i64 9}
!16 = !{!"_ZTSN4ROHC5iphdrE", !7, i64 0, !7, i64 0, !7, i64 1, !11, i64 2, !11, i64 4, !11, i64 6, !7, i64 8, !7, i64 9, !11, i64 10, !14, i64 12, !14, i64 16}
!17 = !{!11, !11, i64 0}
!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 nounwind uwtable
define dso_local void @_ZN4ROHC26calculateUdpHeaderCheckSumEPNS_5iphdrE(ptr noundef %0) local_unnamed_addr #0 {
  %2 = load i8, ptr %0, align 4
  %3 = shl i8 %2, 2
  %4 = and i8 %3, 60
  %5 = zext nneg i8 %4 to i64
  %6 = getelementptr inbounds i8, ptr %0, i64 %5
  %7 = getelementptr inbounds i8, ptr %6, i64 6
  store i16 0, ptr %7, align 2, !tbaa !5
  %8 = getelementptr inbounds i8, ptr %6, i64 4
  %9 = load i16, ptr %8, align 2, !tbaa !10
  %10 = tail call noundef zeroext i16 @_ZN4ROHC10rohc_htonsEt(i16 noundef zeroext %9)
  %11 = zext i16 %10 to i64
  %12 = tail call noundef i32 @_ZN12_GLOBAL__N_111OneChecksumEPKhm(ptr noundef nonnull %6, i64 noundef %11)
  %13 = getelementptr inbounds i8, ptr %0, i64 12
  %14 = getelementptr inbounds i8, ptr %0, i64 9
  %15 = load i8, ptr %14, align 1, !tbaa !11
  %16 = zext i8 %15 to i16
  %17 = tail call noundef zeroext i16 @_ZN4ROHC10rohc_htonsEt(i16 noundef zeroext %16)
  %18 = zext i16 %17 to i32
  %19 = load i16, ptr %8, align 2, !tbaa !10
  %20 = zext i16 %19 to i32
  %21 = load <4 x i16>, ptr %13, align 2, !tbaa !14
  %22 = zext <4 x i16> %21 to <4 x i32>
  %23 = tail call i32 @llvm.vector.reduce.add.v4i32(<4 x i32> %22)
  %op.rdx = add i32 %23, %20
  %op.rdx25 = add i32 %12, %18
  %op.rdx26 = add i32 %op.rdx, %op.rdx25
  %.not23 = icmp ult i32 %op.rdx26, 65536
  br i1 %.not23, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %1, %.lr.ph
  %.024 = phi i32 [ %26, %.lr.ph ], [ %op.rdx26, %1 ]
  %24 = lshr i32 %.024, 16
  %25 = and i32 %.024, 65535
  %26 = add nuw nsw i32 %25, %24
  %.not = icmp ult i32 %26, 65536
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !15

._crit_edge:                                      ; preds = %.lr.ph, %1
  %.0.lcssa = phi i32 [ %op.rdx26, %1 ], [ %26, %.lr.ph ]
  %27 = trunc nuw i32 %.0.lcssa to i16
  %28 = xor i16 %27, -1
  %.not22 = icmp eq i32 %.0.lcssa, 65535
  %spec.store.select = select i1 %.not22, i16 -1, i16 %28
  store i16 %spec.store.select, ptr %7, align 2, !tbaa !5
  ret void
}

; Function Attrs: mustprogress nounwind uwtable
declare hidden noundef i32 @_ZN12_GLOBAL__N_111OneChecksumEPKhm(ptr noundef, i64 noundef) local_unnamed_addr #0

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef zeroext i16 @_ZN4ROHC10rohc_htonsEt(i16 noundef zeroext) local_unnamed_addr #1

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.vector.reduce.add.v4i32(<4 x i32>) #2

attributes #0 = { mustprogress 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 mustprogress 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 = { 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, !7, i64 6}
!6 = !{!"_ZTSN4ROHC6udphdrE", !7, i64 0, !7, i64 2, !7, i64 4, !7, i64 6}
!7 = !{!"short", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!6, !7, i64 4}
!11 = !{!12, !8, i64 9}
!12 = !{!"_ZTSN4ROHC5iphdrE", !8, i64 0, !8, i64 0, !8, i64 1, !7, i64 2, !7, i64 4, !7, i64 6, !8, i64 8, !8, i64 9, !7, i64 10, !13, i64 12, !13, i64 16}
!13 = !{!"int", !8, i64 0}
!14 = !{!7, !7, i64 0}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}


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