; LLVM IR from shixiongfei/nio4c :: nio4c_socket (nio_gethwaddr)

; 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.ifreq = type { %union.anon.0, %union.anon.1 }
%union.anon.0 = type { [16 x i8] }
%union.anon.1 = type { %struct.ifmap }
%struct.ifmap = type { i64, i64, i16, i8, i8, i8 }
%struct.ifaddrs = type { ptr, ptr, i32, ptr, ptr, %union.anon, ptr }
%union.anon = type { ptr }
%struct.sockaddr = type { i16, [14 x i8] }
%struct.niohwaddr_s = type { [6 x i8] }

; Function Attrs: nounwind
declare ptr @strcpy(ptr noundef, ptr noundef) #0

; Function Attrs: nounwind uwtable
define dso_local i32 @nio_gethwaddr(ptr noundef %0, i32 noundef %1) #1 {
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca %struct.ifreq, align 8
  %11 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #5
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #5
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #5
  store i32 0, ptr %8, align 4, !tbaa !9
  %12 = call i32 @getifaddrs(ptr noundef %6) #5
  %13 = icmp ne i32 0, %12
  br i1 %13, label %14, label %15

14:                                               ; preds = %2
  store i32 0, ptr %3, align 4
  store i32 1, ptr %9, align 4
  br label %82

15:                                               ; preds = %2
  %16 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr %16, ptr %7, align 8, !tbaa !5
  br label %17

17:                                               ; preds = %75, %15
  %18 = load ptr, ptr %7, align 8, !tbaa !5
  %19 = icmp ne ptr null, %18
  br i1 %19, label %20, label %24

20:                                               ; preds = %17
  %21 = load i32, ptr %8, align 4, !tbaa !9
  %22 = load i32, ptr %5, align 4, !tbaa !9
  %23 = icmp slt i32 %21, %22
  br label %24

24:                                               ; preds = %20, %17
  %25 = phi i1 [ false, %17 ], [ %23, %20 ]
  br i1 %25, label %26, label %79

26:                                               ; preds = %24
  %27 = load ptr, ptr %7, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct.ifaddrs, ptr %27, i32 0, i32 3
  %29 = load ptr, ptr %28, align 8, !tbaa !11
  %30 = icmp ne ptr %29, null
  br i1 %30, label %31, label %74

31:                                               ; preds = %26
  %32 = load ptr, ptr %7, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.ifaddrs, ptr %32, i32 0, i32 3
  %34 = load ptr, ptr %33, align 8, !tbaa !11
  %35 = getelementptr inbounds %struct.sockaddr, ptr %34, i32 0, i32 0
  %36 = load i16, ptr %35, align 2, !tbaa !13
  %37 = zext i16 %36 to i32
  %38 = icmp eq i32 17, %37
  br i1 %38, label %39, label %74

39:                                               ; preds = %31
  call void @llvm.lifetime.start.p0(i64 40, ptr %10) #5
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #5
  %40 = call i32 @socket(i32 noundef 2, i32 noundef 2, i32 noundef 0) #5
  store i32 %40, ptr %11, align 4, !tbaa !9
  %41 = getelementptr inbounds %struct.ifreq, ptr %10, i32 0, i32 0
  %42 = getelementptr inbounds [16 x i8], ptr %41, i64 0, i64 0
  %43 = load ptr, ptr %7, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.ifaddrs, ptr %43, i32 0, i32 1
  %45 = load ptr, ptr %44, align 8, !tbaa !16
  %46 = call ptr @strcpy(ptr noundef %42, ptr noundef %45) #5
  %47 = load i32, ptr %11, align 4, !tbaa !9
  %48 = call i32 (i32, i64, ...) @ioctl(i32 noundef %47, i64 noundef 35111, ptr noundef %10) #5
  %49 = load i32, ptr %11, align 4, !tbaa !9
  %50 = call i32 @close(i32 noundef %49)
  %51 = getelementptr inbounds %struct.ifreq, ptr %10, i32 0, i32 1
  %52 = getelementptr inbounds %struct.sockaddr, ptr %51, i32 0, i32 0
  %53 = load i16, ptr %52, align 8, !tbaa !17
  %54 = zext i16 %53 to i32
  %55 = icmp eq i32 1, %54
  br i1 %55, label %62, label %56

56:                                               ; preds = %39
  %57 = getelementptr inbounds %struct.ifreq, ptr %10, i32 0, i32 1
  %58 = getelementptr inbounds %struct.sockaddr, ptr %57, i32 0, i32 0
  %59 = load i16, ptr %58, align 8, !tbaa !17
  %60 = zext i16 %59 to i32
  %61 = icmp eq i32 801, %60
  br i1 %61, label %62, label %73

62:                                               ; preds = %56, %39
  %63 = load ptr, ptr %4, align 8, !tbaa !5
  %64 = load i32, ptr %8, align 4, !tbaa !9
  %65 = add nsw i32 %64, 1
  store i32 %65, ptr %8, align 4, !tbaa !9
  %66 = sext i32 %64 to i64
  %67 = getelementptr inbounds %struct.niohwaddr_s, ptr %63, i64 %66
  %68 = getelementptr inbounds %struct.niohwaddr_s, ptr %67, i32 0, i32 0
  %69 = getelementptr inbounds [6 x i8], ptr %68, i64 0, i64 0
  %70 = getelementptr inbounds %struct.ifreq, ptr %10, i32 0, i32 1
  %71 = getelementptr inbounds %struct.sockaddr, ptr %70, i32 0, i32 1
  %72 = getelementptr inbounds [14 x i8], ptr %71, i64 0, i64 0
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %69, ptr align 2 %72, i64 6, i1 false)
  br label %73

73:                                               ; preds = %62, %56
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #5
  call void @llvm.lifetime.end.p0(i64 40, ptr %10) #5
  br label %74

74:                                               ; preds = %73, %31, %26
  br label %75

75:                                               ; preds = %74
  %76 = load ptr, ptr %7, align 8, !tbaa !5
  %77 = getelementptr inbounds %struct.ifaddrs, ptr %76, i32 0, i32 0
  %78 = load ptr, ptr %77, align 8, !tbaa !18
  store ptr %78, ptr %7, align 8, !tbaa !5
  br label %17, !llvm.loop !19

79:                                               ; preds = %24
  %80 = load ptr, ptr %6, align 8, !tbaa !5
  call void @freeifaddrs(ptr noundef %80) #5
  %81 = load i32, ptr %8, align 4, !tbaa !9
  store i32 %81, ptr %3, align 4
  store i32 1, ptr %9, align 4
  br label %82

82:                                               ; preds = %79, %14
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #5
  %83 = load i32, ptr %3, align 4
  ret i32 %83
}

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

; Function Attrs: nounwind
declare i32 @getifaddrs(ptr noundef) #0

; Function Attrs: nounwind
declare i32 @socket(i32 noundef, i32 noundef, i32 noundef) #0

; Function Attrs: nounwind
declare i32 @ioctl(i32 noundef, i64 noundef, ...) #0

declare i32 @close(i32 noundef) #3

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

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

; Function Attrs: nounwind
declare void @freeifaddrs(ptr noundef) #0

attributes #0 = { 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 #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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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, !6, i64 24}
!12 = !{!"ifaddrs", !6, i64 0, !6, i64 8, !10, i64 16, !6, i64 24, !6, i64 32, !7, i64 40, !6, i64 48}
!13 = !{!14, !15, i64 0}
!14 = !{!"sockaddr", !15, i64 0, !7, i64 2}
!15 = !{!"short", !7, i64 0}
!16 = !{!12, !6, i64 8}
!17 = !{!7, !7, i64 0}
!18 = !{!12, !6, i64 0}
!19 = distinct !{!19, !20}
!20 = !{!"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.ifreq = type { %union.anon.0, %union.anon.1 }
%union.anon.0 = type { [16 x i8] }
%union.anon.1 = type { %struct.ifmap }
%struct.ifmap = type { i64, i64, i16, i8, i8, i8 }
%struct.niohwaddr_s = type { [6 x i8] }

; Function Attrs: mustprogress nofree nounwind willreturn memory(argmem: readwrite)
declare ptr @strcpy(ptr noalias noundef returned writeonly, ptr noalias nocapture noundef readonly) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local i32 @nio_gethwaddr(ptr nocapture noundef writeonly %0, i32 noundef %1) local_unnamed_addr #1 {
  %3 = alloca ptr, align 8
  %4 = alloca %struct.ifreq, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %3) #6
  %5 = call i32 @getifaddrs(ptr noundef nonnull %3) #6
  %.not = icmp eq i32 %5, 0
  br i1 %.not, label %.preheader, label %35

.preheader:                                       ; preds = %2
  %.01720 = load ptr, ptr %3, align 8, !tbaa !5
  %6 = icmp ne ptr %.01720, null
  %7 = icmp sgt i32 %1, 0
  %8 = and i1 %6, %7
  br i1 %8, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %.preheader
  %9 = getelementptr inbounds i8, ptr %4, i64 16
  %10 = getelementptr inbounds i8, ptr %4, i64 18
  br label %11

11:                                               ; preds = %.lr.ph, %30
  %.01722 = phi ptr [ %.01720, %.lr.ph ], [ %.017, %30 ]
  %.01621 = phi i32 [ 0, %.lr.ph ], [ %.2, %30 ]
  %12 = getelementptr inbounds i8, ptr %.01722, i64 24
  %13 = load ptr, ptr %12, align 8, !tbaa !9
  %.not19 = icmp eq ptr %13, null
  br i1 %.not19, label %30, label %14

14:                                               ; preds = %11
  %15 = load i16, ptr %13, align 2, !tbaa !12
  %16 = icmp eq i16 %15, 17
  br i1 %16, label %17, label %30

17:                                               ; preds = %14
  call void @llvm.lifetime.start.p0(i64 40, ptr nonnull %4) #6
  %18 = call i32 @socket(i32 noundef 2, i32 noundef 2, i32 noundef 0) #6
  %19 = getelementptr inbounds i8, ptr %.01722, i64 8
  %20 = load ptr, ptr %19, align 8, !tbaa !15
  %21 = call ptr @strcpy(ptr noundef nonnull dereferenceable(1) %4, ptr noundef nonnull dereferenceable(1) %20) #6
  %22 = call i32 (i32, i64, ...) @ioctl(i32 noundef %18, i64 noundef 35111, ptr noundef nonnull %4) #6
  %23 = call i32 @close(i32 noundef %18) #6
  %24 = load i16, ptr %9, align 8
  switch i16 %24, label %29 [
    i16 801, label %25
    i16 1, label %25
  ]

25:                                               ; preds = %17, %17
  %26 = add nsw i32 %.01621, 1
  %27 = sext i32 %.01621 to i64
  %28 = getelementptr inbounds %struct.niohwaddr_s, ptr %0, i64 %27
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 1 dereferenceable(6) %28, ptr noundef nonnull align 2 dereferenceable(6) %10, i64 6, i1 false)
  br label %29

29:                                               ; preds = %17, %25
  %.1 = phi i32 [ %26, %25 ], [ %.01621, %17 ]
  call void @llvm.lifetime.end.p0(i64 40, ptr nonnull %4) #6
  br label %30

30:                                               ; preds = %11, %14, %29
  %.2 = phi i32 [ %.1, %29 ], [ %.01621, %14 ], [ %.01621, %11 ]
  %.017 = load ptr, ptr %.01722, align 8, !tbaa !5
  %31 = icmp ne ptr %.017, null
  %32 = icmp slt i32 %.2, %1
  %33 = select i1 %31, i1 %32, i1 false
  br i1 %33, label %11, label %._crit_edge.loopexit, !llvm.loop !16

._crit_edge.loopexit:                             ; preds = %30
  %.pre = load ptr, ptr %3, align 8, !tbaa !5
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit, %.preheader
  %34 = phi ptr [ %.01720, %.preheader ], [ %.pre, %._crit_edge.loopexit ]
  %.016.lcssa = phi i32 [ 0, %.preheader ], [ %.2, %._crit_edge.loopexit ]
  call void @freeifaddrs(ptr noundef %34) #6
  br label %35

35:                                               ; preds = %2, %._crit_edge
  %.0 = phi i32 [ %.016.lcssa, %._crit_edge ], [ 0, %2 ]
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %3) #6
  ret i32 %.0
}

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

; Function Attrs: nounwind
declare i32 @getifaddrs(ptr noundef) local_unnamed_addr #3

; Function Attrs: nounwind
declare i32 @socket(i32 noundef, i32 noundef, i32 noundef) local_unnamed_addr #3

; Function Attrs: nounwind
declare i32 @ioctl(i32 noundef, i64 noundef, ...) local_unnamed_addr #3

declare i32 @close(i32 noundef) local_unnamed_addr #4

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

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

; Function Attrs: nounwind
declare void @freeifaddrs(ptr noundef) local_unnamed_addr #3

attributes #0 = { mustprogress nofree nounwind willreturn memory(argmem: readwrite) "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 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 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { 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 #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 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #6 = { 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, !6, i64 24}
!10 = !{!"ifaddrs", !6, i64 0, !6, i64 8, !11, i64 16, !6, i64 24, !6, i64 32, !7, i64 40, !6, i64 48}
!11 = !{!"int", !7, i64 0}
!12 = !{!13, !14, i64 0}
!13 = !{!"sockaddr", !14, i64 0, !7, i64 2}
!14 = !{!"short", !7, i64 0}
!15 = !{!10, !6, i64 8}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}


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