#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*/ function_pointer;

/* Variables and functions */
int /*<<< orphan*/ sprintf(char *, char *, ...);
void format_function_pointer(char *buffer, void (*function_pointer)(void)) {
  int direction = 0x01020304;
  int i, p, start, end;
  char *endian = (char *)&direction;
  if (endian[0] == 0x01) {
    direction = 1;
    start = 0;
    end = sizeof(function_pointer);
  } else {
    direction = -1;
    start = sizeof(function_pointer) - 1;
    end = -1;
  }
  sprintf(buffer, "0x");
  p = 1;
  for (i = start; i != end; i += direction) {
    sprintf(&buffer[p * 2], "%02X", ((unsigned char *)&function_pointer)[i]);
    p++;
  }
}


x86 assembly (clang, -O0):
<format_function_pointer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x1020304,-0x14(%rbp)
lea    -0x14(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x1,%eax
jne    1159 <format_function_pointer+0x49>
movl   $0x1,-0x14(%rbp)
movl   $0x0,-0x20(%rbp)
movl   $0x8,-0x24(%rbp)
jmp    116e <format_function_pointer+0x5e>
movl   $0xffffffff,-0x14(%rbp)
movl   $0x7,-0x20(%rbp)
movl   $0xffffffff,-0x24(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe87(%rip),%rsi
mov    $0x0,%al
call   1030 <sprintf@plt>
movl   $0x1,-0x1c(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x24(%rbp),%eax
je     11d6 <format_function_pointer+0xc6>
mov    -0x8(%rbp),%rdi
mov    -0x1c(%rbp),%eax
shl    $0x1,%eax
cltq
add    %rax,%rdi
movslq -0x18(%rbp),%rax
movzbl -0x10(%rbp,%rax,1),%edx
lea    0xe4b(%rip),%rsi
mov    $0x0,%al
call   1030 <sprintf@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    118d <format_function_pointer+0x7d>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<format_function_pointer>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%rbx
movb   $0x0,0x2(%rdi)
movw   $0x7830,(%rdi)
add    $0x2,%rdi
mov    %rsi,%rdx
mov    %rsi,%r15
shr    $0x38,%rdx
lea    0xecd(%rip),%r14
mov    %r14,%rsi
xor    %eax,%eax
call   1030 <sprintf@plt>
lea    0x4(%rbx),%rdi
mov    %r15,%rax
shr    $0x30,%rax
movzbl %al,%edx
mov    %r14,%rsi
xor    %eax,%eax
call   1030 <sprintf@plt>
lea    0x6(%rbx),%rdi
mov    %r15,%rax
shr    $0x28,%rax
movzbl %al,%edx
mov    %r14,%rsi
xor    %eax,%eax
call   1030 <sprintf@plt>
lea    0x8(%rbx),%rdi
mov    %r15,%rax
shr    $0x20,%rax
movzbl %al,%edx
mov    %r14,%rsi
xor    %eax,%eax
call   1030 <sprintf@plt>
lea    0xa(%rbx),%rdi
mov    %r15d,%edx
shr    $0x18,%edx
mov    %r14,%rsi
xor    %eax,%eax
call   1030 <sprintf@plt>
lea    0xc(%rbx),%rdi
mov    %r15d,%eax
shr    $0x10,%eax
movzbl %al,%edx
mov    %r14,%rsi
xor    %eax,%eax
call   1030 <sprintf@plt>
lea    0xe(%rbx),%rdi
mov    %r15,%rax
movzbl %ah,%edx
mov    %r14,%rsi
xor    %eax,%eax
call   1030 <sprintf@plt>
add    $0x10,%rbx
movzbl %r15b,%edx
mov    %rbx,%rdi
mov    %r14,%rsi
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
jmp    1030 <sprintf@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"0x\00"
@.str.1 = [5 x i8] c"%02X\00"
define void @format_function_pointer(i8* %0, void ()* %1) {
%3 = alloca i8*
%4 = alloca void ()*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
store i8* %0, i8** %3
store void ()* %1, void ()** %4
store i32 16909060, i32* %5
%11 = bitcast i32* %5 to i8*
store i8* %11, i8** %10
%12 = load i8*, i8** %10
%13 = getelementptr inbounds i8, i8* %12, i64 0
%14 = load i8, i8* %13
%15 = sext i8 %14 to i32
%16 = icmp eq i32 %15, 1
br i1 %16, label %17, label %18
17: ; preds = %2
store i32 1, i32* %5
store i32 0, i32* %8
store i32 8, i32* %9
br label %19
18: ; preds = %2
store i32 -1, i32* %5
store i32 7, i32* %8
store i32 -1, i32* %9
br label %19
19: ; preds = %18, %17
%20 = load i8*, i8** %3
%21 = call i32 (i8*, i8*, ...) @sprintf(i8* %20, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %7
%22 = load i32, i32* %8
store i32 %22, i32* %6
br label %23
23: ; preds = %42, %19
%24 = load i32, i32* %6
%25 = load i32, i32* %9
%26 = icmp ne i32 %24, %25
br i1 %26, label %27, label %46
27: ; preds = %23
%28 = load i8*, i8** %3
%29 = load i32, i32* %7
%30 = mul nsw i32 %29, 2
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %28, i64 %31
%33 = bitcast void ()** %4 to i8*
%34 = load i32, i32* %6
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %33, i64 %35
%37 = load i8, i8* %36
%38 = zext i8 %37 to i32
%39 = call i32 (i8*, i8*, ...) @sprintf(i8* %32, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %38)
%40 = load i32, i32* %7
%41 = add nsw i32 %40, 1
store i32 %41, i32* %7
br label %42
42: ; preds = %27
%43 = load i32, i32* %5
%44 = load i32, i32* %6
%45 = add nsw i32 %44, %43
store i32 %45, i32* %6
br label %23
46: ; preds = %23
ret void
}

LLVM IR (-O2):
@.str = [3 x i8] c"0x\00"
@.str.1 = [5 x i8] c"%02X\00"
define void @format_function_pointer(i8* nocapture writeonly %0, void ()* %1) {
tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 1 dereferenceable(3) %0, i8* align 1 dereferenceable(3) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i64 3, i1 false)
%3 = getelementptr inbounds i8, i8* %0, i64 2
%4 = ptrtoint void ()* %1 to i64
%5 = lshr i64 %4, 56
%6 = trunc i64 %5 to i32
%7 = tail call i32 (i8*, i8*, ...) @sprintf(i8* %3, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %6)
%8 = getelementptr inbounds i8, i8* %0, i64 4
%9 = lshr i64 %4, 48
%10 = trunc i64 %9 to i32
%11 = and i32 %10, 255
%12 = tail call i32 (i8*, i8*, ...) @sprintf(i8* %8, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %11)
%13 = getelementptr inbounds i8, i8* %0, i64 6
%14 = lshr i64 %4, 40
%15 = trunc i64 %14 to i32
%16 = and i32 %15, 255
%17 = tail call i32 (i8*, i8*, ...) @sprintf(i8* %13, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %16)
%18 = getelementptr inbounds i8, i8* %0, i64 8
%19 = lshr i64 %4, 32
%20 = trunc i64 %19 to i32
%21 = and i32 %20, 255
%22 = tail call i32 (i8*, i8*, ...) @sprintf(i8* %18, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %21)
%23 = getelementptr inbounds i8, i8* %0, i64 10
%24 = trunc i64 %4 to i32
%25 = lshr i32 %24, 24
%26 = tail call i32 (i8*, i8*, ...) @sprintf(i8* %23, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %25)
%27 = getelementptr inbounds i8, i8* %0, i64 12
%28 = trunc i64 %4 to i32
%29 = lshr i32 %28, 16
%30 = and i32 %29, 255
%31 = tail call i32 (i8*, i8*, ...) @sprintf(i8* %27, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %30)
%32 = getelementptr inbounds i8, i8* %0, i64 14
%33 = trunc i64 %4 to i32
%34 = lshr i32 %33, 8
%35 = and i32 %34, 255
%36 = tail call i32 (i8*, i8*, ...) @sprintf(i8* %32, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %35)
%37 = getelementptr inbounds i8, i8* %0, i64 16
%38 = trunc i64 %4 to i32
%39 = and i32 %38, 255
%40 = tail call i32 (i8*, i8*, ...) @sprintf(i8* %37, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %39)
ret void
}