; LLVM IR from Montimage/mmt-security :: src/lib (send_msg_to_file)

; 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"

%union.pthread_mutex_t = type { %struct.__pthread_mutex_s }
%struct.__pthread_mutex_s = type { i32, i32, i32, i32, i32, i16, i16, %struct.__pthread_internal_list }
%struct.__pthread_internal_list = type { ptr, ptr }

@send_message_fn = external hidden global ptr, align 8
@file_name = external hidden global [5000 x i8], align 16
@period = external hidden global i16, align 2
@mutex_lock = external hidden global %union.pthread_mutex_t, align 8
@.str = external hidden unnamed_addr constant [72 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [94 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [88 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [22 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [2 x i8], align 1
@file = external hidden global ptr, align 8
@.str.5 = external hidden unnamed_addr constant [32 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local void @init_file(ptr noundef %0, i32 noundef %1) #0 {
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca [5000 x i8], align 16
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i32 %1, ptr %4, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 5000, ptr %5) #8
  call void @llvm.memset.p0.i64(ptr align 16 %5, i8 0, i64 5000, i1 false)
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #8
  store i32 0, ptr %6, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #8
  %8 = load ptr, ptr %3, align 8, !tbaa !5
  %9 = call ptr @strcpy(ptr noundef @file_name, ptr noundef %8) #8
  %10 = load i32, ptr %4, align 4, !tbaa !9
  %11 = trunc i32 %10 to i16
  store i16 %11, ptr @period, align 2, !tbaa !11
  %12 = call i32 @pthread_mutex_init(ptr noundef @mutex_lock, ptr noundef null) #8
  store i32 %12, ptr %7, align 4, !tbaa !9
  %13 = load i32, ptr %7, align 4, !tbaa !9
  switch i32 %13, label %17 [
    i32 11, label %14
    i32 16, label %15
    i32 14, label %16
  ]

14:                                               ; preds = %2
  call void (i32, ptr, ...) @mmt_sec_log(i32 noundef 4, ptr noundef @.str)
  br label %17

15:                                               ; preds = %2
  call void (i32, ptr, ...) @mmt_sec_log(i32 noundef 4, ptr noundef @.str.1)
  br label %17

16:                                               ; preds = %2
  call void (i32, ptr, ...) @mmt_sec_log(i32 noundef 4, ptr noundef @.str.2)
  br label %17

17:                                               ; preds = %16, %15, %14, %2
  %18 = load i16, ptr @period, align 2, !tbaa !11
  %19 = zext i16 %18 to i32
  %20 = icmp eq i32 %19, 0
  br i1 %20, label %21, label %46

21:                                               ; preds = %17
  %22 = getelementptr inbounds [5000 x i8], ptr %5, i64 0, i64 0
  %23 = call i32 @getpid() #8
  %24 = call i32 (ptr, i64, ptr, ...) @snprintf(ptr noundef %22, i64 noundef 5000, ptr noundef @.str.3, ptr noundef @file_name, i32 noundef %23) #8
  store i32 %24, ptr %6, align 4, !tbaa !9
  %25 = load i32, ptr %6, align 4, !tbaa !9
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds [5000 x i8], ptr %5, i64 0, i64 %26
  store i8 0, ptr %27, align 1, !tbaa !13
  %28 = getelementptr inbounds [5000 x i8], ptr %5, i64 0, i64 0
  %29 = call noalias ptr @fopen(ptr noundef %28, ptr noundef @.str.4)
  store ptr %29, ptr @file, align 8, !tbaa !5
  %30 = load ptr, ptr @file, align 8, !tbaa !5
  %31 = icmp eq ptr %30, null
  %32 = xor i1 %31, true
  %33 = xor i1 %32, true
  %34 = zext i1 %33 to i32
  %35 = sext i32 %34 to i64
  %36 = call i64 @llvm.expect.i64(i64 %35, i64 0)
  %37 = icmp ne i64 %36, 0
  br i1 %37, label %38, label %45

38:                                               ; preds = %21
  %39 = call ptr @__errno_location() #9
  %40 = load i32, ptr %39, align 4, !tbaa !9
  %41 = getelementptr inbounds [5000 x i8], ptr %5, i64 0, i64 0
  %42 = call ptr @__errno_location() #9
  %43 = load i32, ptr %42, align 4, !tbaa !9
  %44 = call ptr @strerror(i32 noundef %43) #8
  call void (i32, ptr, ...) @mmt_sec_log(i32 noundef 4, ptr noundef @.str.5, i32 noundef %40, ptr noundef %41, ptr noundef %44)
  br label %45

45:                                               ; preds = %38, %21
  store ptr @send_message_to_single_file, ptr @send_message_fn, align 8, !tbaa !5
  br label %47

46:                                               ; preds = %17
  store ptr @send_message_to_sampled_file, ptr @send_message_fn, align 8, !tbaa !5
  br label %47

47:                                               ; preds = %46, %45
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #8
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #8
  call void @llvm.lifetime.end.p0(i64 5000, ptr %5) #8
  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: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #2

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

; Function Attrs: nounwind
declare i32 @pthread_mutex_init(ptr noundef, ptr noundef) #3

declare void @mmt_sec_log(i32 noundef, ptr noundef, ...) #4

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

; Function Attrs: nounwind
declare i32 @getpid() #3

declare noalias ptr @fopen(ptr noundef, ptr noundef) #4

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(none)
declare i64 @llvm.expect.i64(i64, i64) #5

; Function Attrs: nounwind willreturn memory(none)
declare ptr @__errno_location() #6

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

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @send_message_to_single_file(ptr noundef) #7

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @send_message_to_sampled_file(ptr noundef) #7

; 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 = { nocallback nofree nounwind willreturn memory(argmem: write) }
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 = { nocallback nofree nosync nounwind willreturn memory(none) }
attributes #6 = { nounwind willreturn memory(none) "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 = { 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 #8 = { nounwind }
attributes #9 = { nounwind 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, !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, !12, i64 0}
!12 = !{!"short", !7, i64 0}
!13 = !{!7, !7, i64 0}


; 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"

%union.pthread_mutex_t = type { %struct.__pthread_mutex_s }
%struct.__pthread_mutex_s = type { i32, i32, i32, i32, i32, i16, i16, %struct.__pthread_internal_list }
%struct.__pthread_internal_list = type { ptr, ptr }

@send_message_fn = external hidden local_unnamed_addr global ptr, align 8
@file_name = external hidden global [5000 x i8], align 16
@period = external hidden local_unnamed_addr global i16, align 2
@mutex_lock = external hidden global %union.pthread_mutex_t, align 8
@.str = external hidden unnamed_addr constant [72 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [94 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [88 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [22 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [2 x i8], align 1
@file = external hidden local_unnamed_addr global ptr, align 8
@.str.5 = external hidden unnamed_addr constant [32 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local void @init_file(ptr nocapture noundef readonly %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = alloca [5000 x i8], align 16
  call void @llvm.lifetime.start.p0(i64 5000, ptr nonnull %3) #9
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(5000) %3, i8 0, i64 5000, i1 false)
  %4 = tail call ptr @strcpy(ptr noundef nonnull dereferenceable(1) @file_name, ptr noundef nonnull dereferenceable(1) %0) #9
  %5 = trunc i32 %1 to i16
  store i16 %5, ptr @period, align 2, !tbaa !5
  %6 = tail call i32 @pthread_mutex_init(ptr noundef nonnull @mutex_lock, ptr noundef null) #9
  switch i32 %6, label %9 [
    i32 11, label %.sink.split
    i32 16, label %7
    i32 14, label %8
  ]

7:                                                ; preds = %2
  br label %.sink.split

8:                                                ; preds = %2
  br label %.sink.split

.sink.split:                                      ; preds = %2, %7, %8
  %.str.2.sink = phi ptr [ @.str.2, %8 ], [ @.str.1, %7 ], [ @.str, %2 ]
  tail call void (i32, ptr, ...) @mmt_sec_log(i32 noundef 4, ptr noundef nonnull %.str.2.sink) #9
  br label %9

9:                                                ; preds = %.sink.split, %2
  %10 = load i16, ptr @period, align 2, !tbaa !5
  %11 = icmp eq i16 %10, 0
  br i1 %11, label %12, label %23

12:                                               ; preds = %9
  %13 = tail call i32 @getpid() #9
  %14 = call i32 (ptr, i64, ptr, ...) @snprintf(ptr noundef nonnull dereferenceable(1) %3, i64 noundef 5000, ptr noundef nonnull @.str.3, ptr noundef nonnull @file_name, i32 noundef %13) #9
  %15 = sext i32 %14 to i64
  %16 = getelementptr inbounds [5000 x i8], ptr %3, i64 0, i64 %15
  store i8 0, ptr %16, align 1, !tbaa !9
  %17 = call noalias ptr @fopen(ptr noundef nonnull %3, ptr noundef nonnull @.str.4)
  store ptr %17, ptr @file, align 8, !tbaa !10
  %18 = icmp eq ptr %17, null
  br i1 %18, label %19, label %23, !prof !12

19:                                               ; preds = %12
  %20 = tail call ptr @__errno_location() #10
  %21 = load i32, ptr %20, align 4, !tbaa !13
  %22 = tail call ptr @strerror(i32 noundef %21) #9
  call void (i32, ptr, ...) @mmt_sec_log(i32 noundef 4, ptr noundef nonnull @.str.5, i32 noundef %21, ptr noundef nonnull %3, ptr noundef %22) #9
  br label %23

23:                                               ; preds = %9, %12, %19
  %storemerge = phi ptr [ @send_message_to_single_file, %19 ], [ @send_message_to_single_file, %12 ], [ @send_message_to_sampled_file, %9 ]
  store ptr %storemerge, ptr @send_message_fn, align 8, !tbaa !10
  call void @llvm.lifetime.end.p0(i64 5000, ptr nonnull %3) #9
  ret void
}

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

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #2

; 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 #3

; Function Attrs: nounwind
declare i32 @pthread_mutex_init(ptr noundef, ptr noundef) local_unnamed_addr #4

declare void @mmt_sec_log(i32 noundef, ptr noundef, ...) local_unnamed_addr #5

; Function Attrs: nofree nounwind
declare noundef i32 @snprintf(ptr noalias nocapture noundef writeonly, i64 noundef, ptr nocapture noundef readonly, ...) local_unnamed_addr #6

; Function Attrs: nounwind
declare i32 @getpid() local_unnamed_addr #4

; Function Attrs: nofree nounwind
declare noalias noundef ptr @fopen(ptr nocapture noundef readonly, ptr nocapture noundef readonly) local_unnamed_addr #6

; Function Attrs: mustprogress nofree nosync nounwind willreturn memory(none)
declare ptr @__errno_location() local_unnamed_addr #7

; Function Attrs: nounwind
declare ptr @strerror(i32 noundef) local_unnamed_addr #4

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @send_message_to_single_file(ptr noundef) #8

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @send_message_to_sampled_file(ptr noundef) #8

; 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 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #3 = { 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 #4 = { 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 #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 = { 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 #7 = { mustprogress nofree nosync nounwind willreturn memory(none) "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 #8 = { 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 #9 = { nounwind }
attributes #10 = { nounwind 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, !6, i64 0}
!6 = !{!"short", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!7, !7, i64 0}
!10 = !{!11, !11, i64 0}
!11 = !{!"any pointer", !7, i64 0}
!12 = !{!"branch_weights", !"expected", i32 1, i32 2000}
!13 = !{!14, !14, i64 0}
!14 = !{!"int", !7, i64 0}


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