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

/* Variables and functions */
scalar_t__ class_addMethod(scalar_t__, int /*<<< orphan*/, int /*<<< orphan*/,
                           int /*<<< orphan*/);
int /*<<< orphan*/ class_getClassMethod(scalar_t__, int /*<<< orphan*/);
int /*<<< orphan*/ class_replaceMethod(scalar_t__, 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*/);
scalar_t__ object_getClass(int /*<<< orphan*/);
void SwizzClassMethod(Class origClass, Class replaceClass, SEL origSel,
                      SEL replaceSel) {
  Method origMethod = class_getClassMethod(origClass, origSel);
  Method newMethod = class_getClassMethod(replaceClass, replaceSel);
  origClass = object_getClass((id)origClass);
  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):
<SwizzClassMethod>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1050 <class_getClassMethod@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%esi
call   1050 <class_getClassMethod@plt>
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    %eax,%edi
call   1060 <object_getClass@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%edi
call   1080 <method_getImplementation@plt>
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%edi
call   1070 <method_getTypeEncoding@plt>
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%esi
mov    -0x24(%rbp),%edx
mov    %eax,%ecx
call   1030 <class_addMethod@plt>
cmp    $0x0,%rax
je     1226 <SwizzClassMethod+0xb6>
mov    -0x8(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x1c(%rbp),%edi
call   1080 <method_getImplementation@plt>
mov    %eax,-0x34(%rbp)
mov    -0x1c(%rbp),%edi
call   1070 <method_getTypeEncoding@plt>
mov    -0x40(%rbp),%rdi
mov    -0x38(%rbp),%esi
mov    -0x34(%rbp),%edx
mov    %eax,%ecx
call   1040 <class_replaceMethod@plt>
jmp    1231 <SwizzClassMethod+0xc1>
mov    -0x1c(%rbp),%edi
mov    -0x20(%rbp),%esi
call   1090 <method_exchangeImplementations@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<SwizzClassMethod>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %ecx,%r14d
mov    %edx,%r13d
mov    %rsi,%rbx
mov    %rdi,%rbp
mov    %edx,%esi
call   1050 <class_getClassMethod@plt>
mov    %eax,%r15d
mov    %rbx,%rdi
mov    %r14d,%esi
call   1050 <class_getClassMethod@plt>
mov    %eax,%ebx
mov    %ebp,%edi
call   1060 <object_getClass@plt>
mov    %rax,%r12
mov    %ebx,%edi
call   1080 <method_getImplementation@plt>
mov    %eax,%ebp
mov    %ebx,%edi
call   1070 <method_getTypeEncoding@plt>
mov    %r12,%rdi
mov    %r13d,%esi
mov    %ebp,%edx
mov    %eax,%ecx
call   1030 <class_addMethod@plt>
test   %rax,%rax
je     11fb <SwizzClassMethod+0x8b>
mov    %r15d,%edi
call   1080 <method_getImplementation@plt>
mov    %eax,%ebx
mov    %r15d,%edi
call   1070 <method_getTypeEncoding@plt>
mov    %r12,%rdi
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    1040 <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    1090 <method_exchangeImplementations@plt>

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

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