; LLVM IR from openssl/openssl :: crypto/des (str2key)

; 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.DES_ks = type { [16 x %union.anon] }
%union.anon = type { [2 x i32] }

; Function Attrs: nounwind uwtable
define dso_local void @DES_string_to_key(ptr noundef %0, ptr noundef %1) #0 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca %struct.DES_ks, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i8, align 1
  store ptr %0, ptr %3, align 8, !tbaa !5
  store ptr %1, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 128, ptr %5) #5
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #5
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #5
  %9 = load ptr, ptr %4, align 8, !tbaa !5
  call void @llvm.memset.p0.i64(ptr align 1 %9, i8 0, i64 8, i1 false)
  %10 = load ptr, ptr %3, align 8, !tbaa !5
  %11 = call i64 @strlen(ptr noundef %10) #6
  %12 = trunc i64 %11 to i32
  store i32 %12, ptr %7, align 4, !tbaa !9
  store i32 0, ptr %6, align 4, !tbaa !9
  br label %13

13:                                               ; preds = %83, %2
  %14 = load i32, ptr %6, align 4, !tbaa !9
  %15 = load i32, ptr %7, align 4, !tbaa !9
  %16 = icmp slt i32 %14, %15
  br i1 %16, label %17, label %86

17:                                               ; preds = %13
  call void @llvm.lifetime.start.p0(i64 1, ptr %8) #5
  %18 = load ptr, ptr %3, align 8, !tbaa !5
  %19 = load i32, ptr %6, align 4, !tbaa !9
  %20 = sext i32 %19 to i64
  %21 = getelementptr inbounds i8, ptr %18, i64 %20
  %22 = load i8, ptr %21, align 1, !tbaa !11
  store i8 %22, ptr %8, align 1, !tbaa !11
  %23 = load i32, ptr %6, align 4, !tbaa !9
  %24 = srem i32 %23, 16
  %25 = icmp slt i32 %24, 8
  br i1 %25, label %26, label %39

26:                                               ; preds = %17
  %27 = load i8, ptr %8, align 1, !tbaa !11
  %28 = zext i8 %27 to i32
  %29 = shl i32 %28, 1
  %30 = load ptr, ptr %4, align 8, !tbaa !5
  %31 = load i32, ptr %6, align 4, !tbaa !9
  %32 = srem i32 %31, 8
  %33 = sext i32 %32 to i64
  %34 = getelementptr inbounds [8 x i8], ptr %30, i64 0, i64 %33
  %35 = load i8, ptr %34, align 1, !tbaa !11
  %36 = zext i8 %35 to i32
  %37 = xor i32 %36, %29
  %38 = trunc i32 %37 to i8
  store i8 %38, ptr %34, align 1, !tbaa !11
  br label %82

39:                                               ; preds = %17
  %40 = load i8, ptr %8, align 1, !tbaa !11
  %41 = zext i8 %40 to i32
  %42 = shl i32 %41, 4
  %43 = and i32 %42, 240
  %44 = load i8, ptr %8, align 1, !tbaa !11
  %45 = zext i8 %44 to i32
  %46 = ashr i32 %45, 4
  %47 = and i32 %46, 15
  %48 = or i32 %43, %47
  %49 = trunc i32 %48 to i8
  store i8 %49, ptr %8, align 1, !tbaa !11
  %50 = load i8, ptr %8, align 1, !tbaa !11
  %51 = zext i8 %50 to i32
  %52 = shl i32 %51, 2
  %53 = and i32 %52, 204
  %54 = load i8, ptr %8, align 1, !tbaa !11
  %55 = zext i8 %54 to i32
  %56 = ashr i32 %55, 2
  %57 = and i32 %56, 51
  %58 = or i32 %53, %57
  %59 = trunc i32 %58 to i8
  store i8 %59, ptr %8, align 1, !tbaa !11
  %60 = load i8, ptr %8, align 1, !tbaa !11
  %61 = zext i8 %60 to i32
  %62 = shl i32 %61, 1
  %63 = and i32 %62, 170
  %64 = load i8, ptr %8, align 1, !tbaa !11
  %65 = zext i8 %64 to i32
  %66 = ashr i32 %65, 1
  %67 = and i32 %66, 85
  %68 = or i32 %63, %67
  %69 = trunc i32 %68 to i8
  store i8 %69, ptr %8, align 1, !tbaa !11
  %70 = load i8, ptr %8, align 1, !tbaa !11
  %71 = zext i8 %70 to i32
  %72 = load ptr, ptr %4, align 8, !tbaa !5
  %73 = load i32, ptr %6, align 4, !tbaa !9
  %74 = srem i32 %73, 8
  %75 = sub nsw i32 7, %74
  %76 = sext i32 %75 to i64
  %77 = getelementptr inbounds [8 x i8], ptr %72, i64 0, i64 %76
  %78 = load i8, ptr %77, align 1, !tbaa !11
  %79 = zext i8 %78 to i32
  %80 = xor i32 %79, %71
  %81 = trunc i32 %80 to i8
  store i8 %81, ptr %77, align 1, !tbaa !11
  br label %82

82:                                               ; preds = %39, %26
  call void @llvm.lifetime.end.p0(i64 1, ptr %8) #5
  br label %83

83:                                               ; preds = %82
  %84 = load i32, ptr %6, align 4, !tbaa !9
  %85 = add nsw i32 %84, 1
  store i32 %85, ptr %6, align 4, !tbaa !9
  br label %13, !llvm.loop !12

86:                                               ; preds = %13
  %87 = load ptr, ptr %4, align 8, !tbaa !5
  call void @DES_set_odd_parity(ptr noundef %87)
  %88 = load ptr, ptr %4, align 8, !tbaa !5
  call void @DES_set_key_unchecked(ptr noundef %88, ptr noundef %5)
  %89 = load ptr, ptr %3, align 8, !tbaa !5
  %90 = load ptr, ptr %4, align 8, !tbaa !5
  %91 = load i32, ptr %7, align 4, !tbaa !9
  %92 = sext i32 %91 to i64
  %93 = load ptr, ptr %4, align 8, !tbaa !5
  %94 = call i32 @DES_cbc_cksum(ptr noundef %89, ptr noundef %90, i64 noundef %92, ptr noundef %5, ptr noundef %93)
  call void @OPENSSL_cleanse(ptr noundef %5, i64 noundef 128)
  %95 = load ptr, ptr %4, align 8, !tbaa !5
  call void @DES_set_odd_parity(ptr noundef %95)
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #5
  call void @llvm.lifetime.end.p0(i64 128, ptr %5) #5
  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 willreturn memory(read)
declare i64 @strlen(ptr noundef) #3

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

declare void @DES_set_odd_parity(ptr noundef) #4

declare void @DES_set_key_unchecked(ptr noundef, ptr noundef) #4

declare i32 @DES_cbc_cksum(ptr noundef, ptr noundef, i64 noundef, ptr noundef, ptr noundef) #4

declare void @OPENSSL_cleanse(ptr noundef, i64 noundef) #4

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 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, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!7, !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"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.DES_ks = type { [16 x %union.anon] }
%union.anon = type { [2 x i32] }

; Function Attrs: nounwind uwtable
define dso_local void @DES_string_to_key(ptr noundef %0, ptr noundef %1) local_unnamed_addr #0 {
  %3 = alloca %struct.DES_ks, align 4
  call void @llvm.lifetime.start.p0(i64 128, ptr nonnull %3) #5
  store i64 0, ptr %1, align 1
  %4 = tail call i64 @strlen(ptr noundef nonnull dereferenceable(1) %0) #6
  %5 = trunc i64 %4 to i32
  %6 = icmp sgt i32 %5, 0
  br i1 %6, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %2
  %wide.trip.count = and i64 %4, 2147483647
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader, %15
  %indvars.iv = phi i64 [ 0, %.lr.ph.preheader ], [ %indvars.iv.next, %15 ]
  %7 = getelementptr inbounds i8, ptr %0, i64 %indvars.iv
  %8 = load i8, ptr %7, align 1, !tbaa !5
  %9 = and i64 %indvars.iv, 8
  %.not.not = icmp eq i64 %9, 0
  %10 = and i64 %indvars.iv, 7
  br i1 %.not.not, label %11, label %13

11:                                               ; preds = %.lr.ph
  %12 = shl i8 %8, 1
  br label %15

13:                                               ; preds = %.lr.ph
  %rev = tail call i8 @llvm.bitreverse.i8(i8 %8)
  %14 = xor i64 %10, 7
  br label %15

15:                                               ; preds = %13, %11
  %.sink31 = phi i64 [ %14, %13 ], [ %10, %11 ]
  %rev.sink = phi i8 [ %rev, %13 ], [ %12, %11 ]
  %16 = getelementptr inbounds [8 x i8], ptr %1, i64 0, i64 %.sink31
  %17 = load i8, ptr %16, align 1, !tbaa !5
  %18 = xor i8 %17, %rev.sink
  store i8 %18, ptr %16, align 1, !tbaa !5
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count
  br i1 %exitcond.not, label %._crit_edge, label %.lr.ph, !llvm.loop !8

._crit_edge:                                      ; preds = %15, %2
  tail call void @DES_set_odd_parity(ptr noundef nonnull %1) #5
  call void @DES_set_key_unchecked(ptr noundef nonnull %1, ptr noundef nonnull %3) #5
  %sext = shl i64 %4, 32
  %19 = ashr exact i64 %sext, 32
  %20 = call i32 @DES_cbc_cksum(ptr noundef %0, ptr noundef nonnull %1, i64 noundef %19, ptr noundef nonnull %3, ptr noundef nonnull %1) #5
  call void @OPENSSL_cleanse(ptr noundef nonnull %3, i64 noundef 128) #5
  call void @DES_set_odd_parity(ptr noundef nonnull %1) #5
  call void @llvm.lifetime.end.p0(i64 128, ptr nonnull %3) #5
  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 nofree nounwind willreturn memory(argmem: read)
declare i64 @strlen(ptr nocapture noundef) local_unnamed_addr #2

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

declare void @DES_set_odd_parity(ptr noundef) local_unnamed_addr #3

declare void @DES_set_key_unchecked(ptr noundef, ptr noundef) local_unnamed_addr #3

declare i32 @DES_cbc_cksum(ptr noundef, ptr noundef, i64 noundef, ptr noundef, ptr noundef) local_unnamed_addr #3

declare void @OPENSSL_cleanse(ptr noundef, i64 noundef) local_unnamed_addr #3

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i8 @llvm.bitreverse.i8(i8) #4

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 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 = { "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 nosync nounwind speculatable willreturn memory(none) }
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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = distinct !{!8, !9}
!9 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass