int bsearch_my(int *a, int len, int search) {
  int s, m, e;
  s = 0;
  e = len - 1;
  printf("%s %d\n", __func__, 22);
  while (s <= e) {
    m = (s + e) >> 1;
    printf("s: %d e:%d m: %d\n", s, e, m);
    if (search < a[m]) {
      e = m - 1;
    } else if (search == a[m]) {
      printf("find in index %d\n", m);
      break;
    } else if (search > a[m]) {
      s = m + 1;
    }
  }
  printf("s: %d e:%d m: %d\n", s, e, m);
}


x86 assembly (clang, -O0):
<bsearch_my>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x14(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x24(%rbp)
lea    0xec7(%rip),%rdi
lea    0xec7(%rip),%rsi
mov    $0x16,%edx
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x1c(%rbp),%eax
cmp    -0x24(%rbp),%eax
jg     11f3 <bsearch_my+0xe3>
mov    -0x1c(%rbp),%eax
add    -0x24(%rbp),%eax
sar    $0x1,%eax
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%esi
mov    -0x24(%rbp),%edx
mov    -0x20(%rbp),%ecx
lea    0xe9e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%eax
mov    -0x10(%rbp),%rcx
movslq -0x20(%rbp),%rdx
cmp    (%rcx,%rdx,4),%eax
jge    119d <bsearch_my+0x8d>
mov    -0x20(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11ee <bsearch_my+0xde>
mov    -0x18(%rbp),%eax
mov    -0x10(%rbp),%rcx
movslq -0x20(%rbp),%rdx
cmp    (%rcx,%rdx,4),%eax
jne    11c7 <bsearch_my+0xb7>
mov    -0x20(%rbp),%esi
lea    0xe69(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11f3 <bsearch_my+0xe3>
mov    -0x18(%rbp),%eax
mov    -0x10(%rbp),%rcx
movslq -0x20(%rbp),%rdx
cmp    (%rcx,%rdx,4),%eax
jle    11e4 <bsearch_my+0xd4>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11e9 <bsearch_my+0xd9>
jmp    11ee <bsearch_my+0xde>
jmp    114c <bsearch_my+0x3c>
mov    -0x1c(%rbp),%esi
mov    -0x24(%rbp),%edx
mov    -0x20(%rbp),%ecx
lea    0xe0f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bsearch_my>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %esi,%r12d
mov    %rdi,%r15
add    $0xffffffff,%r12d
lea    0xed1(%rip),%rdi
lea    0xed1(%rip),%rsi
mov    $0x16,%edx
xor    %eax,%eax
call   1030 <printf@plt>
lea    0xec9(%rip),%r13
xor    %ebp,%ebp
jmp    1159 <bsearch_my+0x49>
nopl   (%rax)
lea    -0x1(%rbx),%r12d
cmp    %r12d,%ebp
jg     1195 <bsearch_my+0x85>
lea    (%r12,%rbp,1),%ebx
sar    %ebx
mov    %r13,%rdi
mov    %ebp,%esi
mov    %r12d,%edx
mov    %ebx,%ecx
xor    %eax,%eax
call   1030 <printf@plt>
movslq %ebx,%rax
mov    (%r15,%rax,4),%eax
cmp    %r14d,%eax
jg     1150 <bsearch_my+0x40>
je     1185 <bsearch_my+0x75>
jge    1154 <bsearch_my+0x44>
lea    0x1(%rbx),%ebp
jmp    1154 <bsearch_my+0x44>
lea    0xe98(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1030 <printf@plt>
lea    0xe76(%rip),%rdi
mov    %ebp,%esi
mov    %r12d,%edx
mov    %ebx,%ecx
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <printf@plt>

LLVM IR (-O0):
@.str = [7 x i8] c"%s %d\0A\00"
@__func__.bsearch_my = [11 x i8] c"bsearch_my\00"
@.str.1 = [18 x i8] c"s: %d e:%d m: %d\0A\00"
@.str.2 = [18 x i8] c"find in index %d\0A\00"
define i32 @bsearch_my(i32* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32* %0, i32** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32 0, i32* %8
%11 = load i32, i32* %6
%12 = sub nsw i32 %11, 1
store i32 %12, i32* %10
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @__func__.bsearch_my, i64 0, i64 0), i32 22)
br label %14
14: ; preds = %61, %3
%15 = load i32, i32* %8
%16 = load i32, i32* %10
%17 = icmp sle i32 %15, %16
br i1 %17, label %18, label %62
18: ; preds = %14
%19 = load i32, i32* %8
%20 = load i32, i32* %10
%21 = add nsw i32 %19, %20
%22 = ashr i32 %21, 1
store i32 %22, i32* %9
%23 = load i32, i32* %8
%24 = load i32, i32* %10
%25 = load i32, i32* %9
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32 %23, i32 %24, i32 %25)
%27 = load i32, i32* %7
%28 = load i32*, i32** %5
%29 = load i32, i32* %9
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i32, i32* %28, i64 %30
%32 = load i32, i32* %31
%33 = icmp slt i32 %27, %32
br i1 %33, label %34, label %37
34: ; preds = %18
%35 = load i32, i32* %9
%36 = sub nsw i32 %35, 1
store i32 %36, i32* %10
br label %61
37: ; preds = %18
%38 = load i32, i32* %7
%39 = load i32*, i32** %5
%40 = load i32, i32* %9
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i32, i32* %39, i64 %41
%43 = load i32, i32* %42
%44 = icmp eq i32 %38, %43
br i1 %44, label %45, label %48
45: ; preds = %37
%46 = load i32, i32* %9
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i32 %46)
br label %62
48: ; preds = %37
%49 = load i32, i32* %7
%50 = load i32*, i32** %5
%51 = load i32, i32* %9
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds i32, i32* %50, i64 %52
%54 = load i32, i32* %53
%55 = icmp sgt i32 %49, %54
br i1 %55, label %56, label %59
56: ; preds = %48
%57 = load i32, i32* %9
%58 = add nsw i32 %57, 1
store i32 %58, i32* %8
br label %59
59: ; preds = %56, %48
br label %60
60: ; preds = %59
br label %61
61: ; preds = %60, %34
br label %14
62: ; preds = %45, %14
%63 = load i32, i32* %8
%64 = load i32, i32* %10
%65 = load i32, i32* %9
%66 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32 %63, i32 %64, i32 %65)
%67 = load i32, i32* %4
ret i32 %67
}

LLVM IR (-O2):
@.str = [7 x i8] c"%s %d\0A\00"
@__func__.bsearch_my = [11 x i8] c"bsearch_my\00"
@.str.1 = [18 x i8] c"s: %d e:%d m: %d\0A\00"
@.str.2 = [18 x i8] c"find in index %d\0A\00"
define i32 @bsearch_my(i32* nocapture %0, i32 %1, i32 %2) {
%4 = add nsw i32 %1, -1
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @__func__.bsearch_my, i64 0, i64 0), i32 22)
%6 = icmp sgt i32 %1, 0
br label %7
7: ; preds = %3, %27
%8 = phi i32 [ %29, %27 ], [ %4, %3 ]
%9 = phi i32 [ %28, %27 ], [ 0, %3 ]
%10 = add nsw i32 %8, %9
%11 = ashr i32 %10, 1
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32 %9, i32 %8, i32 %11)
%13 = sext i32 %11 to i64
%14 = getelementptr inbounds i32, i32* %0, i64 %13
%15 = load i32, i32* %14
%16 = icmp sgt i32 %15, %2
br i1 %16, label %17, label %19
17: ; preds = %7
%18 = add nsw i32 %11, -1
br label %27
19: ; preds = %7
%20 = icmp eq i32 %15, %2
br i1 %20, label %21, label %23
21: ; preds = %19
%22 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i32 %11)
br label %31
23: ; preds = %19
%24 = icmp slt i32 %15, %2
%25 = add nsw i32 %11, 1
%26 = select i1 %24, i32 %25, i32 %9
br label %27
27: ; preds = %23, %17
%28 = phi i32 [ %9, %17 ], [ %26, %23 ]
%29 = phi i32 [ %18, %17 ], [ %8, %23 ]
%30 = icmp sgt i32 %28, %29
br i1 %30, label %31, label %7
31: ; preds = %27, %21
%32 = phi i32 [ %9, %21 ], [ %28, %27 ]
%33 = phi i32 [ %8, %21 ], [ %29, %27 ]
%34 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32 %32, i32 %33, i32 %11)
ret i32 undef
}