#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*/ buf;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fwrite(char const *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(char *, int, int);
int /*<<< orphan*/ sprintf(char *, char *, unsigned char);
int strlen(char const *);
void logging(FILE *logfile, const char *prefix, const unsigned char *buffer,
             int len) {
  if (NULL != logfile) {
    int i = 0;
    if (NULL != prefix) {
      fwrite(prefix, 1, strlen(prefix), logfile);
    }
    fwrite("  ", 1, 2, logfile);
    while (i < len) {
      char buf[100];
      sprintf(buf, "%02x ", (unsigned char)buffer[i]);
      fwrite(buf, 1, strlen(buf), logfile);
      i++;
      if (0 == (i & 15) && (i < len)) {
        fwrite("\n", 1, 1, logfile);
        memset(buf, 0x20, sizeof(buf));
        if (NULL != prefix) {
          fwrite(buf, 1, strlen(prefix) + 2, logfile);
        }
      }
    }
    fwrite("\n", 1, 1, logfile);
  }
}


x86 assembly (clang, -O0):
<logging>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
xor    %eax,%eax
cmp    -0x8(%rbp),%rax
je     12ca <logging+0x18a>
movl   $0x0,-0x20(%rbp)
xor    %eax,%eax
cmp    -0x10(%rbp),%rax
je     11a4 <logging+0x64>
mov    -0x10(%rbp),%rax
mov    %rax,-0x98(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x98(%rbp),%rdi
mov    %eax,%edx
mov    -0x8(%rbp),%rcx
mov    $0x1,%esi
call   1060 <fwrite@plt>
mov    -0x8(%rbp),%rcx
lea    0xe51(%rip),%rdi
mov    $0x1,%esi
mov    $0x2,%edx
call   1060 <fwrite@plt>
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    12b3 <logging+0x173>
lea    -0x90(%rbp),%rdi
mov    -0x18(%rbp),%rax
movslq -0x20(%rbp),%rcx
lea    0xe23(%rip),%rsi
movzbl (%rax,%rcx,1),%edx
call   1050 <sprintf@plt>
lea    -0x90(%rbp),%rax
mov    %rax,-0xa0(%rbp)
lea    -0x90(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0xa0(%rbp),%rdi
mov    %eax,%edx
mov    -0x8(%rbp),%rcx
mov    $0x1,%esi
call   1060 <fwrite@plt>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%ecx
and    $0xf,%ecx
xor    %eax,%eax
cmp    %ecx,%eax
jne    12ae <logging+0x16e>
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    12ae <logging+0x16e>
mov    -0x8(%rbp),%rcx
lea    0xdbf(%rip),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1060 <fwrite@plt>
lea    -0x90(%rbp),%rdi
mov    $0x20,%esi
mov    $0x64,%edx
call   1040 <memset@plt>
xor    %eax,%eax
cmp    -0x10(%rbp),%rax
je     12a9 <logging+0x169>
lea    -0x90(%rbp),%rax
mov    %rax,-0xa8(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0xa8(%rbp),%rdi
mov    %eax,%edx
add    $0x2,%edx
mov    -0x8(%rbp),%rcx
mov    $0x1,%esi
call   1060 <fwrite@plt>
jmp    12ae <logging+0x16e>
jmp    11be <logging+0x7e>
mov    -0x8(%rbp),%rcx
lea    0xd4b(%rip),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1060 <fwrite@plt>
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<logging>:
test   %rdi,%rdi
je     1289 <logging+0x139>
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x78,%rsp
mov    %ecx,%ebx
mov    %rdx,%r14
mov    %rsi,%rbp
mov    %rdi,%r13
test   %rsi,%rsi
je     1191 <logging+0x41>
mov    %rbp,%rdi
call   1030 <strlen@plt>
mov    %rbp,%rdi
mov    $0x1,%esi
mov    %eax,%edx
mov    %r13,%rcx
call   1070 <fwrite@plt>
mov    %rbp,0x8(%rsp)
lea    0xe63(%rip),%rdi
mov    $0x1,%esi
mov    $0x2,%edx
mov    %r13,%rcx
call   1070 <fwrite@plt>
test   %ebx,%ebx
jle    126e <logging+0x11e>
mov    %ebx,%r12d
mov    %r12,%rbp
neg    %rbp
mov    $0x1,%r15d
lea    0x10(%rsp),%rbx
jmp    11e6 <logging+0x96>
nopl   (%rax)
lea    (%r15,%rbp,1),%rax
add    $0x1,%rax
add    $0x1,%r15
cmp    $0x1,%rax
je     126e <logging+0x11e>
movzbl -0x1(%r14,%r15,1),%edx
mov    %rbx,%rdi
lea    0xe0d(%rip),%rsi
call   1060 <sprintf@plt>
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %eax,%edx
mov    %r13,%rcx
call   1070 <fwrite@plt>
cmp    %r12,%r15
jae    11d0 <logging+0x80>
mov    %r15d,%eax
and    $0xf,%eax
test   %rax,%rax
jne    11d0 <logging+0x80>
mov    $0xa,%edi
mov    %r13,%rsi
call   1050 <fputc@plt>
mov    %rbx,%rdi
mov    $0x20,%esi
mov    $0x64,%edx
call   1040 <memset@plt>
cmpq   $0x0,0x8(%rsp)
je     11d0 <logging+0x80>
mov    0x8(%rsp),%rdi
call   1030 <strlen@plt>
lea    0x2(%rax),%edx
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %r13,%rcx
call   1070 <fwrite@plt>
jmp    11d0 <logging+0x80>
mov    $0xa,%edi
mov    %r13,%rsi
call   1050 <fputc@plt>
add    $0x78,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [3 x i8] c" \00"
@.str.1 = [6 x i8] c"%02x \00"
define void @logging(i32* %0, i8* %1, i8* nocapture %2, i32 %3) {
%5 = alloca [100 x i8]
%6 = icmp eq i32* %0, null
br i1 %6, label %42, label %7
7: ; preds = %4
%8 = icmp eq i8* %1, null
br i1 %8, label %12, label %9
9: ; preds = %7
%10 = tail call i32 @strlen(i8* %1)
%11 = tail call i32 @fwrite(i8* %1, i32 1, i32 %10, i32* %0)
br label %12
12: ; preds = %9, %7
%13 = tail call i32 @fwrite(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 1, i32 2, i32* %0)
%14 = icmp sgt i32 %3, 0
br i1 %14, label %15, label %40
15: ; preds = %12
%16 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 0
%17 = zext i32 %3 to i64
%18 = zext i32 %3 to i64
br label %19
19: ; preds = %15, %38
%20 = phi i64 [ 0, %15 ], [ %26, %38 ]
%21 = getelementptr inbounds i8, i8* %2, i64 %20
%22 = load i8, i8* %21
%23 = call i32 @sprintf(i8* %16, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8 zeroext %22)
%24 = call i32 @strlen(i8* %16)
%25 = call i32 @fwrite(i8* %16, i32 1, i32 %24, i32* %0)
%26 = add nuw nsw i64 %20, 1
%27 = and i64 %26, 15
%28 = icmp eq i64 %27, 0
%29 = icmp ult i64 %26, %17
%30 = and i1 %29, %28
br i1 %30, label %31, label %38
31: ; preds = %19
%32 = call i32 @fputc(i32 10, i32* %0)
%33 = call i32 @memset(i8* %16, i32 32, i32 100)
br i1 %8, label %38, label %34
34: ; preds = %31
%35 = call i32 @strlen(i8* %1)
%36 = add nsw i32 %35, 2
%37 = call i32 @fwrite(i8* %16, i32 1, i32 %36, i32* %0)
br label %38
38: ; preds = %31, %34, %19
%39 = icmp eq i64 %26, %18
br i1 %39, label %40, label %19
40: ; preds = %38, %12
%41 = call i32 @fputc(i32 10, i32* %0)
br label %42
42: ; preds = %40, %4
ret void
}