#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
int /*<<< orphan*/ Msg(char *, unsigned char);
void check_thunks(unsigned char *dest, unsigned char *pc) {
  unsigned char *writeaddr = dest - 4;
  unsigned char *calloffset = *(unsigned char **)writeaddr;
  unsigned char *calladdr = (unsigned char *)(dest + (unsigned int)calloffset);
  if ((*calladdr == 0x8B) && (*(calladdr + 2) == 0x24) &&
      (*(calladdr + 3) == 0xC3)) {
    char movByte = 0xB8;
    switch (*(calladdr + 1)) {
    case 0x04: {
      movByte += 0;
      break;
    }
    case 0x1C: {
      movByte += 3;
      break;
    }
    case 0x0C: {
      movByte += 1;
      break;
    }
    case 0x14: {
      movByte += 2;
      break;
    }
    default: {
      Msg("Unknown thunk: %c\n", *(calladdr + 1));
      break;
    }
    }
    writeaddr--;
    *writeaddr = movByte;
    writeaddr++;
    *(void **)writeaddr = (void *)pc;
    writeaddr += 4;
  }
  return;
}


x86 assembly (clang, -O0):
<check_thunks>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rax
add    $0xfffffffffffffffc,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    %ecx,%ecx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
movzbl (%rax),%eax
cmp    $0x8b,%eax
jne    1238 <check_thunks+0x128>
mov    -0x28(%rbp),%rax
movzbl 0x2(%rax),%eax
cmp    $0x24,%eax
jne    1238 <check_thunks+0x128>
mov    -0x28(%rbp),%rax
movzbl 0x3(%rax),%eax
cmp    $0xc3,%eax
jne    1238 <check_thunks+0x128>
movb   $0xb8,-0x29(%rbp)
mov    -0x28(%rbp),%rax
movzbl 0x1(%rax),%eax
add    $0xfffffffc,%eax
mov    %eax,%ecx
mov    %rcx,-0x38(%rbp)
sub    $0x18,%eax
ja     11ec <check_thunks+0xdc>
mov    -0x38(%rbp),%rax
lea    0xe59(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
movsbl -0x29(%rbp),%eax
add    $0x0,%eax
mov    %al,-0x29(%rbp)
jmp    1200 <check_thunks+0xf0>
movsbl -0x29(%rbp),%eax
add    $0x3,%eax
mov    %al,-0x29(%rbp)
jmp    1200 <check_thunks+0xf0>
movsbl -0x29(%rbp),%eax
add    $0x1,%eax
mov    %al,-0x29(%rbp)
jmp    1200 <check_thunks+0xf0>
movsbl -0x29(%rbp),%eax
add    $0x2,%eax
mov    %al,-0x29(%rbp)
jmp    1200 <check_thunks+0xf0>
mov    -0x28(%rbp),%rax
lea    0xe6d(%rip),%rdi
movzbl 0x1(%rax),%esi
call   1030 <Msg@plt>
mov    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
mov    -0x29(%rbp),%cl
mov    -0x18(%rbp),%rax
mov    %cl,(%rax)
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x18(%rbp)
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_thunks>:
push   %r14
push   %rbx
push   %rax
mov    -0x4(%rdi),%eax
cmpb   $0x8b,(%rdi,%rax,1)
jne    115d <check_thunks+0x4d>
mov    %rdi,%rbx
cmpb   $0x24,0x2(%rdi,%rax,1)
jne    115d <check_thunks+0x4d>
cmpb   $0xc3,0x3(%rbx,%rax,1)
jne    115d <check_thunks+0x4d>
mov    %rsi,%r14
movzbl 0x1(%rbx,%rax,1),%ecx
lea    -0x4(%rcx),%eax
rol    $0x1d,%eax
cmp    $0x4,%eax
jae    1145 <check_thunks+0x35>
add    $0xb8,%al
jmp    1156 <check_thunks+0x46>
movzbl %cl,%esi
lea    0xeb1(%rip),%rdi
call   1030 <Msg@plt>
mov    $0xb8,%al
mov    %al,-0x5(%rbx)
mov    %r14,-0x4(%rbx)
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [19 x i8] c"Unknown thunk: %c\0A\00"
define void @check_thunks(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8
store i8* %0, i8** %3
store i8* %1, i8** %4
%9 = load i8*, i8** %3
%10 = getelementptr inbounds i8, i8* %9, i64 -4
store i8* %10, i8** %5
%11 = load i8*, i8** %5
%12 = bitcast i8* %11 to i8**
%13 = load i8*, i8** %12
store i8* %13, i8** %6
%14 = load i8*, i8** %3
%15 = load i8*, i8** %6
%16 = ptrtoint i8* %15 to i32
%17 = zext i32 %16 to i64
%18 = getelementptr inbounds i8, i8* %14, i64 %17
store i8* %18, i8** %7
%19 = load i8*, i8** %7
%20 = load i8, i8* %19
%21 = zext i8 %20 to i32
%22 = icmp eq i32 %21, 139
br i1 %22, label %23, label %77
23: ; preds = %2
%24 = load i8*, i8** %7
%25 = getelementptr inbounds i8, i8* %24, i64 2
%26 = load i8, i8* %25
%27 = zext i8 %26 to i32
%28 = icmp eq i32 %27, 36
br i1 %28, label %29, label %77
29: ; preds = %23
%30 = load i8*, i8** %7
%31 = getelementptr inbounds i8, i8* %30, i64 3
%32 = load i8, i8* %31
%33 = zext i8 %32 to i32
%34 = icmp eq i32 %33, 195
br i1 %34, label %35, label %77
35: ; preds = %29
store i8 -72, i8* %8
%36 = load i8*, i8** %7
%37 = getelementptr inbounds i8, i8* %36, i64 1
%38 = load i8, i8* %37
%39 = zext i8 %38 to i32
switch i32 %39, label %60 [
i32 4, label %40
i32 28, label %45
i32 12, label %50
i32 20, label %55
]
40: ; preds = %35
%41 = load i8, i8* %8
%42 = sext i8 %41 to i32
%43 = add nsw i32 %42, 0
%44 = trunc i32 %43 to i8
store i8 %44, i8* %8
br label %65
45: ; preds = %35
%46 = load i8, i8* %8
%47 = sext i8 %46 to i32
%48 = add nsw i32 %47, 3
%49 = trunc i32 %48 to i8
store i8 %49, i8* %8
br label %65
50: ; preds = %35
%51 = load i8, i8* %8
%52 = sext i8 %51 to i32
%53 = add nsw i32 %52, 1
%54 = trunc i32 %53 to i8
store i8 %54, i8* %8
br label %65
55: ; preds = %35
%56 = load i8, i8* %8
%57 = sext i8 %56 to i32
%58 = add nsw i32 %57, 2
%59 = trunc i32 %58 to i8
store i8 %59, i8* %8
br label %65
60: ; preds = %35
%61 = load i8*, i8** %7
%62 = getelementptr inbounds i8, i8* %61, i64 1
%63 = load i8, i8* %62
%64 = call i32 @Msg(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8 zeroext %63)
br label %65
65: ; preds = %60, %55, %50, %45, %40
%66 = load i8*, i8** %5
%67 = getelementptr inbounds i8, i8* %66, i32 -1
store i8* %67, i8** %5
%68 = load i8, i8* %8
%69 = load i8*, i8** %5
store i8 %68, i8* %69
%70 = load i8*, i8** %5
%71 = getelementptr inbounds i8, i8* %70, i32 1
store i8* %71, i8** %5
%72 = load i8*, i8** %4
%73 = load i8*, i8** %5
%74 = bitcast i8* %73 to i8**
store i8* %72, i8** %74
%75 = load i8*, i8** %5
%76 = getelementptr inbounds i8, i8* %75, i64 4
store i8* %76, i8** %5
br label %77
77: ; preds = %65, %29, %23, %2
ret void
}

LLVM IR (-O2):
@.str = [19 x i8] c"Unknown thunk: %c\0A\00"
define void @check_thunks(i8* nocapture %0, i8* %1) {
%3 = getelementptr inbounds i8, i8* %0, i64 -4
%4 = bitcast i8* %3 to i8**
%5 = load i8*, i8** %4
%6 = ptrtoint i8* %5 to i64
%7 = and i64 %6, 4294967295
%8 = getelementptr inbounds i8, i8* %0, i64 %7
%9 = load i8, i8* %8
%10 = icmp eq i8 %9, -117
br i1 %10, label %11, label %34
11: ; preds = %2
%12 = getelementptr inbounds i8, i8* %8, i64 2
%13 = load i8, i8* %12
%14 = icmp eq i8 %13, 36
br i1 %14, label %15, label %34
15: ; preds = %11
%16 = getelementptr inbounds i8, i8* %8, i64 3
%17 = load i8, i8* %16
%18 = icmp eq i8 %17, -61
br i1 %18, label %19, label %34
19: ; preds = %15
%20 = getelementptr inbounds i8, i8* %8, i64 1
%21 = load i8, i8* %20
%22 = zext i8 %21 to i32
%23 = add nsw i32 %22, -4
%24 = tail call i32 @llvm.fshl.i32(i32 %23, i32 %23, i32 29)
%25 = icmp ult i32 %24, 4
br i1 %25, label %28, label %26
26: ; preds = %19
%27 = tail call i32 @Msg(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8 zeroext %21)
br label %31
28: ; preds = %19
%29 = trunc i32 %24 to i8
%30 = add i8 %29, -72
br label %31
31: ; preds = %28, %26
%32 = phi i8 [ -72, %26 ], [ %30, %28 ]
%33 = getelementptr inbounds i8, i8* %0, i64 -5
store i8 %32, i8* %33
store i8* %1, i8** %4
br label %34
34: ; preds = %31, %15, %11, %2
ret void
}