; LLVM IR from freakout42/macrocalc :: arx/src (lib_stat)

; 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.file = type { ptr, i16, i32, ptr, i32, ptr, i64, ptr }
%struct.stat = type { i64, i64, i64, i32, i32, i32, i32, i64, i64, i64, i64, %struct.timespec, %struct.timespec, %struct.timespec, [3 x i64] }
%struct.timespec = type { i64, i64 }
%struct.group = type { ptr, ptr, i32, ptr }
%struct.passwd = type { ptr, ptr, i32, i32, ptr, ptr, ptr }

@lib_stat.fi = external hidden global %struct.file, align 8
@lib_stat.basename = external hidden global [256 x i8], align 16

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

12:                                               ; preds = %1
  store ptr null, ptr %2, align 8
  store i32 1, ptr %8, align 4
  br label %50

13:                                               ; preds = %1
  %14 = load ptr, ptr %3, align 8, !tbaa !5
  %15 = call ptr @strrchr(ptr noundef %14, i32 noundef 47) #6
  store ptr %15, ptr %7, align 8, !tbaa !5
  %16 = load ptr, ptr %7, align 8, !tbaa !5
  %17 = icmp ne ptr %16, null
  br i1 %17, label %18, label %21

18:                                               ; preds = %13
  %19 = load ptr, ptr %7, align 8, !tbaa !5
  %20 = getelementptr inbounds i8, ptr %19, i64 1
  br label %23

21:                                               ; preds = %13
  %22 = load ptr, ptr %3, align 8, !tbaa !5
  br label %23

23:                                               ; preds = %21, %18
  %24 = phi ptr [ %20, %18 ], [ %22, %21 ]
  %25 = call ptr @strncpy(ptr noundef @lib_stat.basename, ptr noundef %24, i64 noundef 256) #5
  store ptr @lib_stat.basename, ptr @lib_stat.fi, align 8, !tbaa !9
  %26 = getelementptr inbounds %struct.stat, ptr %4, i32 0, i32 3
  %27 = load i32, ptr %26, align 8, !tbaa !14
  %28 = trunc i32 %27 to i16
  store i16 %28, ptr getelementptr inbounds (%struct.file, ptr @lib_stat.fi, i32 0, i32 1), align 8, !tbaa !17
  %29 = getelementptr inbounds %struct.stat, ptr %4, i32 0, i32 5
  %30 = load i32, ptr %29, align 8, !tbaa !18
  store i32 %30, ptr getelementptr inbounds (%struct.file, ptr @lib_stat.fi, i32 0, i32 2), align 4, !tbaa !19
  %31 = getelementptr inbounds %struct.stat, ptr %4, i32 0, i32 5
  %32 = load i32, ptr %31, align 8, !tbaa !18
  %33 = call ptr @getgrgid(i32 noundef %32)
  store ptr %33, ptr %6, align 8, !tbaa !5
  %34 = load ptr, ptr %6, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.group, ptr %34, i32 0, i32 0
  %36 = load ptr, ptr %35, align 8, !tbaa !20
  store ptr %36, ptr getelementptr inbounds (%struct.file, ptr @lib_stat.fi, i32 0, i32 3), align 8, !tbaa !22
  %37 = getelementptr inbounds %struct.stat, ptr %4, i32 0, i32 4
  %38 = load i32, ptr %37, align 4, !tbaa !23
  store i32 %38, ptr getelementptr inbounds (%struct.file, ptr @lib_stat.fi, i32 0, i32 4), align 8, !tbaa !24
  %39 = getelementptr inbounds %struct.stat, ptr %4, i32 0, i32 4
  %40 = load i32, ptr %39, align 4, !tbaa !23
  %41 = call ptr @getpwuid(i32 noundef %40)
  store ptr %41, ptr %5, align 8, !tbaa !5
  %42 = load ptr, ptr %5, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct.passwd, ptr %42, i32 0, i32 0
  %44 = load ptr, ptr %43, align 8, !tbaa !25
  store ptr %44, ptr getelementptr inbounds (%struct.file, ptr @lib_stat.fi, i32 0, i32 5), align 8, !tbaa !27
  %45 = getelementptr inbounds %struct.stat, ptr %4, i32 0, i32 8
  %46 = load i64, ptr %45, align 8, !tbaa !28
  store i64 %46, ptr getelementptr inbounds (%struct.file, ptr @lib_stat.fi, i32 0, i32 6), align 8, !tbaa !29
  %47 = getelementptr inbounds %struct.stat, ptr %4, i32 0, i32 12
  %48 = getelementptr inbounds %struct.timespec, ptr %47, i32 0, i32 0
  %49 = call ptr @localtime(ptr noundef %48) #5
  store ptr %49, ptr getelementptr inbounds (%struct.file, ptr @lib_stat.fi, i32 0, i32 7), align 8, !tbaa !30
  store ptr @lib_stat.fi, ptr %2, align 8
  store i32 1, ptr %8, align 4
  br label %50

50:                                               ; preds = %23, %12
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #5
  call void @llvm.lifetime.end.p0(i64 144, ptr %4) #5
  %51 = load ptr, ptr %2, align 8
  ret ptr %51
}

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

; Function Attrs: nounwind
declare i32 @stat(ptr noundef, ptr noundef) #2

; Function Attrs: nounwind willreturn memory(read)
declare ptr @strrchr(ptr noundef, i32 noundef) #3

; Function Attrs: nounwind
declare ptr @strncpy(ptr noundef, ptr noundef, i64 noundef) #2

declare ptr @getgrgid(i32 noundef) #4

declare ptr @getpwuid(i32 noundef) #4

; Function Attrs: nounwind
declare ptr @localtime(ptr noundef) #2

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

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

!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 0}
!10 = !{!"file", !6, i64 0, !11, i64 8, !12, i64 12, !6, i64 16, !12, i64 24, !6, i64 32, !13, i64 40, !6, i64 48}
!11 = !{!"short", !7, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!"long", !7, i64 0}
!14 = !{!15, !12, i64 24}
!15 = !{!"stat", !13, i64 0, !13, i64 8, !13, i64 16, !12, i64 24, !12, i64 28, !12, i64 32, !12, i64 36, !13, i64 40, !13, i64 48, !13, i64 56, !13, i64 64, !16, i64 72, !16, i64 88, !16, i64 104, !7, i64 120}
!16 = !{!"timespec", !13, i64 0, !13, i64 8}
!17 = !{!10, !11, i64 8}
!18 = !{!15, !12, i64 32}
!19 = !{!10, !12, i64 12}
!20 = !{!21, !6, i64 0}
!21 = !{!"group", !6, i64 0, !6, i64 8, !12, i64 16, !6, i64 24}
!22 = !{!10, !6, i64 16}
!23 = !{!15, !12, i64 28}
!24 = !{!10, !12, i64 24}
!25 = !{!26, !6, i64 0}
!26 = !{!"passwd", !6, i64 0, !6, i64 8, !12, i64 16, !12, i64 20, !6, i64 24, !6, i64 32, !6, i64 40}
!27 = !{!10, !6, i64 32}
!28 = !{!15, !13, i64 48}
!29 = !{!10, !13, i64 40}
!30 = !{!10, !6, i64 48}


; 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.file = type { ptr, i16, i32, ptr, i32, ptr, i64, ptr }
%struct.stat = type { i64, i64, i64, i32, i32, i32, i32, i64, i64, i64, i64, %struct.timespec, %struct.timespec, %struct.timespec, [3 x i64] }
%struct.timespec = type { i64, i64 }

@lib_stat.fi = external hidden global %struct.file, align 8
@lib_stat.basename = external hidden global [256 x i8], align 16

; Function Attrs: nounwind uwtable
define dso_local noundef ptr @lib_stat(ptr noundef readonly %0) local_unnamed_addr #0 {
  %2 = alloca %struct.stat, align 8
  call void @llvm.lifetime.start.p0(i64 144, ptr nonnull %2) #7
  %3 = call i32 @stat(ptr noundef %0, ptr noundef nonnull %2) #7
  %4 = icmp eq i32 %3, -1
  br i1 %4, label %25, label %5

5:                                                ; preds = %1
  %6 = tail call ptr @strrchr(ptr noundef nonnull dereferenceable(1) %0, i32 noundef 47) #8
  %.not = icmp eq ptr %6, null
  %7 = getelementptr inbounds i8, ptr %6, i64 1
  %8 = select i1 %.not, ptr %0, ptr %7
  %9 = tail call ptr @strncpy(ptr noundef nonnull dereferenceable(1) @lib_stat.basename, ptr noundef nonnull dereferenceable(1) %8, i64 noundef 256) #7
  store ptr @lib_stat.basename, ptr @lib_stat.fi, align 8, !tbaa !5
  %10 = getelementptr inbounds i8, ptr %2, i64 24
  %11 = load i32, ptr %10, align 8, !tbaa !13
  %12 = trunc i32 %11 to i16
  store i16 %12, ptr getelementptr inbounds (i8, ptr @lib_stat.fi, i64 8), align 8, !tbaa !16
  %13 = getelementptr inbounds i8, ptr %2, i64 32
  %14 = load i32, ptr %13, align 8, !tbaa !17
  store i32 %14, ptr getelementptr inbounds (i8, ptr @lib_stat.fi, i64 12), align 4, !tbaa !18
  %15 = tail call ptr @getgrgid(i32 noundef %14) #7
  %16 = load ptr, ptr %15, align 8, !tbaa !19
  store ptr %16, ptr getelementptr inbounds (i8, ptr @lib_stat.fi, i64 16), align 8, !tbaa !21
  %17 = getelementptr inbounds i8, ptr %2, i64 28
  %18 = load i32, ptr %17, align 4, !tbaa !22
  store i32 %18, ptr getelementptr inbounds (i8, ptr @lib_stat.fi, i64 24), align 8, !tbaa !23
  %19 = tail call ptr @getpwuid(i32 noundef %18) #7
  %20 = load ptr, ptr %19, align 8, !tbaa !24
  store ptr %20, ptr getelementptr inbounds (i8, ptr @lib_stat.fi, i64 32), align 8, !tbaa !26
  %21 = getelementptr inbounds i8, ptr %2, i64 48
  %22 = load i64, ptr %21, align 8, !tbaa !27
  store i64 %22, ptr getelementptr inbounds (i8, ptr @lib_stat.fi, i64 40), align 8, !tbaa !28
  %23 = getelementptr inbounds i8, ptr %2, i64 88
  %24 = call ptr @localtime(ptr noundef nonnull %23) #7
  store ptr %24, ptr getelementptr inbounds (i8, ptr @lib_stat.fi, i64 48), align 8, !tbaa !29
  br label %25

25:                                               ; preds = %1, %5
  %.0 = phi ptr [ @lib_stat.fi, %5 ], [ null, %1 ]
  call void @llvm.lifetime.end.p0(i64 144, ptr nonnull %2) #7
  ret ptr %.0
}

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

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

; Function Attrs: mustprogress nofree nounwind willreturn memory(argmem: read)
declare ptr @strrchr(ptr noundef, i32 noundef) local_unnamed_addr #3

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

declare ptr @getgrgid(i32 noundef) local_unnamed_addr #5

declare ptr @getpwuid(i32 noundef) local_unnamed_addr #5

; Function Attrs: nounwind
declare ptr @localtime(ptr noundef) local_unnamed_addr #6

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

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

!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 0}
!6 = !{!"file", !7, i64 0, !10, i64 8, !11, i64 12, !7, i64 16, !11, i64 24, !7, i64 32, !12, i64 40, !7, i64 48}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"short", !8, i64 0}
!11 = !{!"int", !8, i64 0}
!12 = !{!"long", !8, i64 0}
!13 = !{!14, !11, i64 24}
!14 = !{!"stat", !12, i64 0, !12, i64 8, !12, i64 16, !11, i64 24, !11, i64 28, !11, i64 32, !11, i64 36, !12, i64 40, !12, i64 48, !12, i64 56, !12, i64 64, !15, i64 72, !15, i64 88, !15, i64 104, !8, i64 120}
!15 = !{!"timespec", !12, i64 0, !12, i64 8}
!16 = !{!6, !10, i64 8}
!17 = !{!14, !11, i64 32}
!18 = !{!6, !11, i64 12}
!19 = !{!20, !7, i64 0}
!20 = !{!"group", !7, i64 0, !7, i64 8, !11, i64 16, !7, i64 24}
!21 = !{!6, !7, i64 16}
!22 = !{!14, !11, i64 28}
!23 = !{!6, !11, i64 24}
!24 = !{!25, !7, i64 0}
!25 = !{!"passwd", !7, i64 0, !7, i64 8, !11, i64 16, !11, i64 20, !7, i64 24, !7, i64 32, !7, i64 40}
!26 = !{!6, !7, i64 32}
!27 = !{!14, !12, i64 48}
!28 = !{!6, !12, i64 40}
!29 = !{!6, !7, i64 48}


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