#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 */
typedef int /*<<< orphan*/ SEL;
typedef int /*<<< orphan*/ Method;
typedef int /*<<< orphan*/ Class;

/* Variables and functions */
scalar_t__ class_addMethod(int /*<<< orphan*/, int /*<<< orphan*/,
                           int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ class_getInstanceMethod(int /*<<< orphan*/,
                                           int /*<<< orphan*/);
int /*<<< orphan*/ class_replaceMethod(int /*<<< orphan*/, int /*<<< orphan*/,
                                       int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ method_exchangeImplementations(int /*<<< orphan*/,
                                                  int /*<<< orphan*/);
int /*<<< orphan*/ method_getImplementation(int /*<<< orphan*/);
int /*<<< orphan*/ method_getTypeEncoding(int /*<<< orphan*/);
void SwizzInstanceMethod(Class origClass, Class replaceClass, SEL origSel,
                         SEL replaceSel) {
  Method origMethod = class_getInstanceMethod(origClass, origSel);
  Method newMethod = class_getInstanceMethod(replaceClass, replaceSel);
  if (class_addMethod(origClass, origSel, method_getImplementation(newMethod),
                      method_getTypeEncoding(newMethod)))
    class_replaceMethod(origClass, replaceSel,
                        method_getImplementation(origMethod),
                        method_getTypeEncoding(origMethod));
  else
    method_exchangeImplementations(origMethod, newMethod);
}


x86 assembly (clang, -O0):
<SwizzInstanceMethod>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    %ecx,-0x10(%rbp)
mov    -0x4(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1030 <class_getInstanceMethod@plt>
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%esi
call   1030 <class_getInstanceMethod@plt>
mov    %eax,-0x18(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%edi
call   1070 <method_getImplementation@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%edi
call   1060 <method_getTypeEncoding@plt>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%esi
mov    -0x1c(%rbp),%edx
mov    %eax,%ecx
call   1040 <class_addMethod@plt>
cmp    $0x0,%rax
je     11fd <SwizzInstanceMethod+0x9d>
mov    -0x4(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x14(%rbp),%edi
call   1070 <method_getImplementation@plt>
mov    %eax,-0x28(%rbp)
mov    -0x14(%rbp),%edi
call   1060 <method_getTypeEncoding@plt>
mov    -0x30(%rbp),%edi
mov    -0x2c(%rbp),%esi
mov    -0x28(%rbp),%edx
mov    %eax,%ecx
call   1050 <class_replaceMethod@plt>
jmp    1208 <SwizzInstanceMethod+0xa8>
mov    -0x14(%rbp),%edi
mov    -0x18(%rbp),%esi
call   1080 <method_exchangeImplementations@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<SwizzInstanceMethod>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %ecx,%r14d
mov    %edx,%r13d
mov    %esi,%ebx
mov    %edi,%r12d
mov    %edx,%esi
call   1030 <class_getInstanceMethod@plt>
mov    %eax,%r15d
mov    %ebx,%edi
mov    %r14d,%esi
call   1030 <class_getInstanceMethod@plt>
mov    %eax,%ebx
mov    %eax,%edi
call   1070 <method_getImplementation@plt>
mov    %eax,%ebp
mov    %ebx,%edi
call   1060 <method_getTypeEncoding@plt>
mov    %r12d,%edi
mov    %r13d,%esi
mov    %ebp,%edx
mov    %eax,%ecx
call   1040 <class_addMethod@plt>
test   %rax,%rax
je     11df <SwizzInstanceMethod+0x7f>
mov    %r15d,%edi
call   1070 <method_getImplementation@plt>
mov    %eax,%ebx
mov    %r15d,%edi
call   1060 <method_getTypeEncoding@plt>
mov    %r12d,%edi
mov    %r14d,%esi
mov    %ebx,%edx
mov    %eax,%ecx
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <class_replaceMethod@plt>
mov    %r15d,%edi
mov    %ebx,%esi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1080 <method_exchangeImplementations@plt>

LLVM IR (-O0):
define void @SwizzInstanceMethod(i32 %0, i32 %1, i32 %2, i32 %3) {
%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 %3, i32* %8
%11 = load i32, i32* %5
%12 = load i32, i32* %7
%13 = call i32 @class_getInstanceMethod(i32 %11, i32 %12)
store i32 %13, i32* %9
%14 = load i32, i32* %6
%15 = load i32, i32* %8
%16 = call i32 @class_getInstanceMethod(i32 %14, i32 %15)
store i32 %16, i32* %10
%17 = load i32, i32* %5
%18 = load i32, i32* %7
%19 = load i32, i32* %10
%20 = call i32 @method_getImplementation(i32 %19)
%21 = load i32, i32* %10
%22 = call i32 @method_getTypeEncoding(i32 %21)
%23 = call i64 @class_addMethod(i32 %17, i32 %18, i32 %20, i32 %22)
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %33
25: ; preds = %4
%26 = load i32, i32* %5
%27 = load i32, i32* %8
%28 = load i32, i32* %9
%29 = call i32 @method_getImplementation(i32 %28)
%30 = load i32, i32* %9
%31 = call i32 @method_getTypeEncoding(i32 %30)
%32 = call i32 @class_replaceMethod(i32 %26, i32 %27, i32 %29, i32 %31)
br label %37
33: ; preds = %4
%34 = load i32, i32* %9
%35 = load i32, i32* %10
%36 = call i32 @method_exchangeImplementations(i32 %34, i32 %35)
br label %37
37: ; preds = %33, %25
ret void
}

LLVM IR (-O2):
define void @SwizzInstanceMethod(i32 %0, i32 %1, i32 %2, i32 %3) {
%5 = tail call i32 @class_getInstanceMethod(i32 %0, i32 %2)
%6 = tail call i32 @class_getInstanceMethod(i32 %1, i32 %3)
%7 = tail call i32 @method_getImplementation(i32 %6)
%8 = tail call i32 @method_getTypeEncoding(i32 %6)
%9 = tail call i64 @class_addMethod(i32 %0, i32 %2, i32 %7, i32 %8)
%10 = icmp eq i64 %9, 0
br i1 %10, label %15, label %11
11: ; preds = %4
%12 = tail call i32 @method_getImplementation(i32 %5)
%13 = tail call i32 @method_getTypeEncoding(i32 %5)
%14 = tail call i32 @class_replaceMethod(i32 %0, i32 %3, i32 %12, i32 %13)
br label %17
15: ; preds = %4
%16 = tail call i32 @method_exchangeImplementations(i32 %5, i32 %6)
br label %17
17: ; preds = %15, %11
ret void
}