; LLVM IR from shanet/Crypto-Example :: Crypto.ll (ZN6Crypto14writeKeyToFileEP8_IO_FILEi)

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

%class.Crypto = type { ptr, ptr, ptr, ptr, ptr, ptr, ptr, i64, i64 }

@_ZN6Crypto12localKeypairE = external dso_local global ptr, align 8

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_ZN6Crypto14writeKeyToFileEP8_IO_FILEi(ptr noundef nonnull align 8 dereferenceable(72) %0, ptr noundef %1, i32 noundef %2) #0 align 2 {
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store i32 %2, ptr %7, align 4, !tbaa !9
  %8 = load ptr, ptr %5, align 8
  %9 = load i32, ptr %7, align 4, !tbaa !9
  switch i32 %9, label %48 [
    i32 0, label %10
    i32 1, label %17
    i32 2, label %24
    i32 3, label %32
    i32 4, label %40
  ]

10:                                               ; preds = %3
  %11 = load ptr, ptr %6, align 8, !tbaa !5
  %12 = load ptr, ptr @_ZN6Crypto12localKeypairE, align 8, !tbaa !5
  %13 = call i32 @PEM_write_PrivateKey(ptr noundef %11, ptr noundef %12, ptr noundef null, ptr noundef null, i32 noundef 0, ptr noundef null, ptr noundef null)
  %14 = icmp ne i32 %13, 0
  br i1 %14, label %16, label %15

15:                                               ; preds = %10
  store i32 -1, ptr %4, align 4
  br label %50

16:                                               ; preds = %10
  br label %49

17:                                               ; preds = %3
  %18 = load ptr, ptr %6, align 8, !tbaa !5
  %19 = load ptr, ptr @_ZN6Crypto12localKeypairE, align 8, !tbaa !5
  %20 = call i32 @PEM_write_PUBKEY(ptr noundef %18, ptr noundef %19)
  %21 = icmp ne i32 %20, 0
  br i1 %21, label %23, label %22

22:                                               ; preds = %17
  store i32 -1, ptr %4, align 4
  br label %50

23:                                               ; preds = %17
  br label %49

24:                                               ; preds = %3
  %25 = load ptr, ptr %6, align 8, !tbaa !5
  %26 = getelementptr inbounds %class.Crypto, ptr %8, i32 0, i32 0
  %27 = load ptr, ptr %26, align 8, !tbaa !11
  %28 = call i32 @PEM_write_PUBKEY(ptr noundef %25, ptr noundef %27)
  %29 = icmp ne i32 %28, 0
  br i1 %29, label %31, label %30

30:                                               ; preds = %24
  store i32 -1, ptr %4, align 4
  br label %50

31:                                               ; preds = %24
  br label %49

32:                                               ; preds = %3
  %33 = getelementptr inbounds %class.Crypto, ptr %8, i32 0, i32 5
  %34 = load ptr, ptr %33, align 8, !tbaa !14
  %35 = getelementptr inbounds %class.Crypto, ptr %8, i32 0, i32 7
  %36 = load i64, ptr %35, align 8, !tbaa !15
  %37 = mul i64 %36, 8
  %38 = load ptr, ptr %6, align 8, !tbaa !5
  %39 = call i64 @fwrite(ptr noundef %34, i64 noundef 1, i64 noundef %37, ptr noundef %38)
  br label %49

40:                                               ; preds = %3
  %41 = getelementptr inbounds %class.Crypto, ptr %8, i32 0, i32 6
  %42 = load ptr, ptr %41, align 8, !tbaa !16
  %43 = getelementptr inbounds %class.Crypto, ptr %8, i32 0, i32 8
  %44 = load i64, ptr %43, align 8, !tbaa !17
  %45 = mul i64 %44, 8
  %46 = load ptr, ptr %6, align 8, !tbaa !5
  %47 = call i64 @fwrite(ptr noundef %42, i64 noundef 1, i64 noundef %45, ptr noundef %46)
  br label %49

48:                                               ; preds = %3
  store i32 -1, ptr %4, align 4
  br label %50

49:                                               ; preds = %40, %32, %31, %23, %16
  store i32 0, ptr %4, align 4
  br label %50

50:                                               ; preds = %49, %48, %30, %22, %15
  %51 = load i32, ptr %4, align 4
  ret i32 %51
}

declare i32 @PEM_write_PrivateKey(ptr noundef, ptr noundef, ptr noundef, ptr noundef, i32 noundef, ptr noundef, ptr noundef) #1

declare i32 @PEM_write_PUBKEY(ptr noundef, ptr noundef) #1

declare i64 @fwrite(ptr noundef, i64 noundef, i64 noundef, ptr noundef) #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 = { "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" }

!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, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !6, i64 0}
!12 = !{!"_ZTS6Crypto", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32, !6, i64 40, !6, i64 48, !13, i64 56, !13, i64 64}
!13 = !{!"long", !7, i64 0}
!14 = !{!12, !6, i64 40}
!15 = !{!12, !13, i64 56}
!16 = !{!12, !6, i64 48}
!17 = !{!12, !13, i64 64}


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

@_ZN6Crypto12localKeypairE = external dso_local local_unnamed_addr global ptr, align 8

; Function Attrs: mustprogress uwtable
define dso_local noundef range(i32 -1, 1) i32 @_ZN6Crypto14writeKeyToFileEP8_IO_FILEi(ptr nocapture noundef nonnull readonly align 8 dereferenceable(72) %0, ptr noundef %1, i32 noundef %2) local_unnamed_addr #0 align 2 {
  switch i32 %2, label %21 [
    i32 0, label %4
    i32 1, label %7
    i32 2, label %10
    i32 3, label %.sink.split
    i32 4, label %13
  ]

4:                                                ; preds = %3
  %5 = load ptr, ptr @_ZN6Crypto12localKeypairE, align 8, !tbaa !5
  %6 = tail call i32 @PEM_write_PrivateKey(ptr noundef %1, ptr noundef %5, ptr noundef null, ptr noundef null, i32 noundef 0, ptr noundef null, ptr noundef null)
  %.not8 = icmp eq i32 %6, 0
  br i1 %.not8, label %21, label %20

7:                                                ; preds = %3
  %8 = load ptr, ptr @_ZN6Crypto12localKeypairE, align 8, !tbaa !5
  %9 = tail call i32 @PEM_write_PUBKEY(ptr noundef %1, ptr noundef %8)
  %.not7 = icmp eq i32 %9, 0
  br i1 %.not7, label %21, label %20

10:                                               ; preds = %3
  %11 = load ptr, ptr %0, align 8, !tbaa !9
  %12 = tail call i32 @PEM_write_PUBKEY(ptr noundef %1, ptr noundef %11)
  %.not = icmp eq i32 %12, 0
  br i1 %.not, label %21, label %20

13:                                               ; preds = %3
  br label %.sink.split

.sink.split:                                      ; preds = %3, %13
  %.sink = phi i64 [ 48, %13 ], [ 40, %3 ]
  %.sink12 = phi i64 [ 64, %13 ], [ 56, %3 ]
  %14 = getelementptr inbounds i8, ptr %0, i64 %.sink
  %15 = load ptr, ptr %14, align 8, !tbaa !5
  %16 = getelementptr inbounds i8, ptr %0, i64 %.sink12
  %17 = load i64, ptr %16, align 8, !tbaa !12
  %18 = shl i64 %17, 3
  %19 = tail call i64 @fwrite(ptr noundef %15, i64 noundef 1, i64 noundef %18, ptr noundef %1)
  br label %20

20:                                               ; preds = %.sink.split, %10, %7, %4
  br label %21

21:                                               ; preds = %3, %10, %7, %4, %20
  %.0 = phi i32 [ 0, %20 ], [ -1, %4 ], [ -1, %7 ], [ -1, %10 ], [ -1, %3 ]
  ret i32 %.0
}

declare i32 @PEM_write_PrivateKey(ptr noundef, ptr noundef, ptr noundef, ptr noundef, i32 noundef, ptr noundef, ptr noundef) local_unnamed_addr #1

declare i32 @PEM_write_PUBKEY(ptr noundef, ptr noundef) local_unnamed_addr #1

; Function Attrs: nofree nounwind
declare noundef i64 @fwrite(ptr nocapture noundef, i64 noundef, i64 noundef, ptr nocapture noundef) local_unnamed_addr #2

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 = { "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 = { 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" }

!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, !6, i64 0}
!10 = !{!"_ZTS6Crypto", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32, !6, i64 40, !6, i64 48, !11, i64 56, !11, i64 64}
!11 = !{!"long", !7, i64 0}
!12 = !{!11, !11, i64 0}


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