; LLVM IR from square/subzero :: core/trezor-crypto (segwit_addr)

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

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

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

; Function Attrs: nounwind uwtable
declare dso_local i32 @bech32_decode(ptr noundef, ptr noundef, ptr noundef, ptr noundef) #1

; Function Attrs: nounwind uwtable
declare hidden i32 @convert_bits(ptr noundef, ptr noundef, i32 noundef, ptr noundef, i64 noundef, i32 noundef, i32 noundef) #1

; Function Attrs: nounwind uwtable
define dso_local i32 @segwit_addr_decode(ptr noundef %0, ptr noundef %1, ptr noundef %2, ptr noundef %3, ptr noundef %4) #1 {
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca ptr, align 8
  %11 = alloca ptr, align 8
  %12 = alloca [84 x i8], align 16
  %13 = alloca [84 x i8], align 16
  %14 = alloca i64, align 8
  %15 = alloca i32, align 4
  store ptr %0, ptr %7, align 8, !tbaa !5
  store ptr %1, ptr %8, align 8, !tbaa !5
  store ptr %2, ptr %9, align 8, !tbaa !5
  store ptr %3, ptr %10, align 8, !tbaa !5
  store ptr %4, ptr %11, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 84, ptr %12) #3
  call void @llvm.lifetime.start.p0(i64 84, ptr %13) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %14) #3
  %16 = getelementptr inbounds [84 x i8], ptr %13, i64 0, i64 0
  %17 = getelementptr inbounds [84 x i8], ptr %12, i64 0, i64 0
  %18 = load ptr, ptr %11, align 8, !tbaa !5
  %19 = call i32 @bech32_decode(ptr noundef %16, ptr noundef %17, ptr noundef %14, ptr noundef %18)
  %20 = icmp ne i32 %19, 0
  br i1 %20, label %22, label %21

21:                                               ; preds = %5
  store i32 0, ptr %6, align 4
  store i32 1, ptr %15, align 4
  br label %80

22:                                               ; preds = %5
  %23 = load i64, ptr %14, align 8, !tbaa !9
  %24 = icmp eq i64 %23, 0
  br i1 %24, label %28, label %25

25:                                               ; preds = %22
  %26 = load i64, ptr %14, align 8, !tbaa !9
  %27 = icmp ugt i64 %26, 65
  br i1 %27, label %28, label %29

28:                                               ; preds = %25, %22
  store i32 0, ptr %6, align 4
  store i32 1, ptr %15, align 4
  br label %80

29:                                               ; preds = %25
  %30 = load ptr, ptr %10, align 8, !tbaa !5
  %31 = getelementptr inbounds [84 x i8], ptr %13, i64 0, i64 0
  %32 = call i32 @strncmp(ptr noundef %30, ptr noundef %31, i64 noundef 84) #4
  %33 = icmp ne i32 %32, 0
  br i1 %33, label %34, label %35

34:                                               ; preds = %29
  store i32 0, ptr %6, align 4
  store i32 1, ptr %15, align 4
  br label %80

35:                                               ; preds = %29
  %36 = getelementptr inbounds [84 x i8], ptr %12, i64 0, i64 0
  %37 = load i8, ptr %36, align 16, !tbaa !11
  %38 = zext i8 %37 to i32
  %39 = icmp sgt i32 %38, 16
  br i1 %39, label %40, label %41

40:                                               ; preds = %35
  store i32 0, ptr %6, align 4
  store i32 1, ptr %15, align 4
  br label %80

41:                                               ; preds = %35
  %42 = load ptr, ptr %9, align 8, !tbaa !5
  store i64 0, ptr %42, align 8, !tbaa !9
  %43 = load ptr, ptr %8, align 8, !tbaa !5
  %44 = load ptr, ptr %9, align 8, !tbaa !5
  %45 = getelementptr inbounds [84 x i8], ptr %12, i64 0, i64 0
  %46 = getelementptr inbounds i8, ptr %45, i64 1
  %47 = load i64, ptr %14, align 8, !tbaa !9
  %48 = sub i64 %47, 1
  %49 = call i32 @convert_bits(ptr noundef %43, ptr noundef %44, i32 noundef 8, ptr noundef %46, i64 noundef %48, i32 noundef 5, i32 noundef 0)
  %50 = icmp ne i32 %49, 0
  br i1 %50, label %52, label %51

51:                                               ; preds = %41
  store i32 0, ptr %6, align 4
  store i32 1, ptr %15, align 4
  br label %80

52:                                               ; preds = %41
  %53 = load ptr, ptr %9, align 8, !tbaa !5
  %54 = load i64, ptr %53, align 8, !tbaa !9
  %55 = icmp ult i64 %54, 2
  br i1 %55, label %60, label %56

56:                                               ; preds = %52
  %57 = load ptr, ptr %9, align 8, !tbaa !5
  %58 = load i64, ptr %57, align 8, !tbaa !9
  %59 = icmp ugt i64 %58, 40
  br i1 %59, label %60, label %61

60:                                               ; preds = %56, %52
  store i32 0, ptr %6, align 4
  store i32 1, ptr %15, align 4
  br label %80

61:                                               ; preds = %56
  %62 = getelementptr inbounds [84 x i8], ptr %12, i64 0, i64 0
  %63 = load i8, ptr %62, align 16, !tbaa !11
  %64 = zext i8 %63 to i32
  %65 = icmp eq i32 %64, 0
  br i1 %65, label %66, label %75

66:                                               ; preds = %61
  %67 = load ptr, ptr %9, align 8, !tbaa !5
  %68 = load i64, ptr %67, align 8, !tbaa !9
  %69 = icmp ne i64 %68, 20
  br i1 %69, label %70, label %75

70:                                               ; preds = %66
  %71 = load ptr, ptr %9, align 8, !tbaa !5
  %72 = load i64, ptr %71, align 8, !tbaa !9
  %73 = icmp ne i64 %72, 32
  br i1 %73, label %74, label %75

74:                                               ; preds = %70
  store i32 0, ptr %6, align 4
  store i32 1, ptr %15, align 4
  br label %80

75:                                               ; preds = %70, %66, %61
  %76 = getelementptr inbounds [84 x i8], ptr %12, i64 0, i64 0
  %77 = load i8, ptr %76, align 16, !tbaa !11
  %78 = zext i8 %77 to i32
  %79 = load ptr, ptr %7, align 8, !tbaa !5
  store i32 %78, ptr %79, align 4, !tbaa !12
  store i32 1, ptr %6, align 4
  store i32 1, ptr %15, align 4
  br label %80

80:                                               ; preds = %75, %74, %60, %51, %40, %34, %28, %21
  call void @llvm.lifetime.end.p0(i64 8, ptr %14) #3
  call void @llvm.lifetime.end.p0(i64 84, ptr %13) #3
  call void @llvm.lifetime.end.p0(i64 84, ptr %12) #3
  %81 = load i32, ptr %6, align 4
  ret i32 %81
}

; Function Attrs: nounwind willreturn memory(read)
declare i32 @strncmp(ptr noundef, ptr noundef, i64 noundef) #2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { 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 #3 = { nounwind }
attributes #4 = { 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, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!7, !7, i64 0}
!12 = !{!13, !13, i64 0}
!13 = !{!"int", !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"

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

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

; Function Attrs: nounwind uwtable
declare dso_local i32 @bech32_decode(ptr noundef, ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare hidden i32 @convert_bits(ptr noundef, ptr noundef, i32 noundef, ptr noundef, i64 noundef, i32 noundef, i32 noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local range(i32 0, 2) i32 @segwit_addr_decode(ptr nocapture noundef writeonly %0, ptr noundef %1, ptr noundef %2, ptr nocapture noundef readonly %3, ptr noundef %4) local_unnamed_addr #1 {
  %6 = alloca [84 x i8], align 16
  %7 = alloca [84 x i8], align 16
  %8 = alloca i64, align 8
  call void @llvm.lifetime.start.p0(i64 84, ptr nonnull %6) #3
  call void @llvm.lifetime.start.p0(i64 84, ptr nonnull %7) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %8) #3
  %9 = call i32 @bech32_decode(ptr noundef nonnull %7, ptr noundef nonnull %6, ptr noundef nonnull %8, ptr noundef %4)
  %.not = icmp eq i32 %9, 0
  br i1 %.not, label %28, label %10

10:                                               ; preds = %5
  %11 = load i64, ptr %8, align 8
  %12 = add i64 %11, -66
  %or.cond = icmp ult i64 %12, -65
  br i1 %or.cond, label %28, label %13

13:                                               ; preds = %10
  %14 = call i32 @strncmp(ptr noundef nonnull dereferenceable(1) %3, ptr noundef nonnull dereferenceable(1) %7, i64 noundef 84) #4
  %.not12 = icmp ne i32 %14, 0
  %15 = load i8, ptr %6, align 16
  %16 = icmp ugt i8 %15, 16
  %or.cond20 = select i1 %.not12, i1 true, i1 %16
  br i1 %or.cond20, label %28, label %17

17:                                               ; preds = %13
  store i64 0, ptr %2, align 8, !tbaa !5
  %18 = getelementptr inbounds i8, ptr %6, i64 1
  %19 = add nsw i64 %11, -1
  %20 = call i32 @convert_bits(ptr noundef %1, ptr noundef nonnull %2, i32 noundef 8, ptr noundef nonnull %18, i64 noundef %19, i32 noundef 5, i32 noundef 0)
  %.not13 = icmp eq i32 %20, 0
  br i1 %.not13, label %28, label %21

21:                                               ; preds = %17
  %22 = load i64, ptr %2, align 8, !tbaa !5
  %23 = add i64 %22, -41
  %or.cond16 = icmp ult i64 %23, -39
  br i1 %or.cond16, label %28, label %24

24:                                               ; preds = %21
  %25 = load i8, ptr %6, align 16, !tbaa !9
  %.fr = freeze i8 %25
  %.not21 = icmp eq i8 %.fr, 0
  br i1 %.not21, label %switch.early.test, label %26

switch.early.test:                                ; preds = %24
  switch i64 %22, label %28 [
    i64 32, label %26
    i64 20, label %26
  ]

26:                                               ; preds = %switch.early.test, %switch.early.test, %24
  %27 = zext i8 %.fr to i32
  store i32 %27, ptr %0, align 4, !tbaa !10
  br label %28

28:                                               ; preds = %switch.early.test, %21, %17, %13, %10, %5, %26
  %.0 = phi i32 [ 1, %26 ], [ 0, %5 ], [ 0, %10 ], [ 0, %13 ], [ 0, %17 ], [ 0, %21 ], [ 0, %switch.early.test ]
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %8) #3
  call void @llvm.lifetime.end.p0(i64 84, ptr nonnull %7) #3
  call void @llvm.lifetime.end.p0(i64 84, ptr nonnull %6) #3
  ret i32 %.0
}

; Function Attrs: mustprogress nofree nounwind willreturn memory(argmem: read)
declare i32 @strncmp(ptr nocapture noundef, ptr nocapture noundef, i64 noundef) local_unnamed_addr #2

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 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 #3 = { nounwind }
attributes #4 = { 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 = !{!"long", !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 = !{!"int", !7, i64 0}


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