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

/* Variables and functions */
int EOF;
int errno;
int fflush(int /*<<< orphan*/ *);
int fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ flockfile(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int /*<<< orphan*/ funlockfile(int /*<<< orphan*/ *);
int log_data(unsigned char *data, size_t length, FILE *from, FILE *to,
             FILE *protocol) {
  unsigned int i;
  int ret;
  int err;
  flockfile(protocol);
  fprintf(protocol, "%i -> %i: ", fileno(from), fileno(to));
  for (i = 0; i < length; i++)
    fprintf(protocol, "%02X", data[i]);
  fprintf(protocol, "\n");
  funlockfile(protocol);
  ret = fflush(protocol);
  if (ret == EOF)
    err = errno;
  else
    err = 0;
  return err;
}


x86 assembly (clang, -O0):
<log_data>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <flockfile@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <fileno@plt>
mov    %eax,-0x38(%rbp)
mov    -0x20(%rbp),%rdi
call   1060 <fileno@plt>
mov    -0x40(%rbp),%rdi
mov    -0x38(%rbp),%edx
mov    %eax,%ecx
lea    0xe5e(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x10(%rbp),%rax
jae    11e8 <log_data+0x98>
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%rax
mov    -0x2c(%rbp),%ecx
movzbl (%rax,%rcx,1),%edx
lea    0xe38(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11b0 <log_data+0x60>
mov    -0x28(%rbp),%rdi
lea    0xe1d(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x28(%rbp),%rdi
call   1030 <funlockfile@plt>
mov    -0x28(%rbp),%rdi
call   1070 <fflush@plt>
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%eax
mov    0x2dcf(%rip),%rcx
cmp    (%rcx),%eax
jne    1232 <log_data+0xe2>
mov    0x2db0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
jmp    1239 <log_data+0xe9>
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<log_data>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8,%r13
mov    %rcx,%r14
mov    %rdx,%r12
mov    %rsi,%rbp
mov    %rdi,%r15
mov    %r8,%rdi
call   1040 <flockfile@plt>
mov    %r12,%rdi
call   1070 <fileno@plt>
mov    %eax,%r12d
mov    %r14,%rdi
call   1070 <fileno@plt>
lea    0xe64(%rip),%rsi
xor    %r14d,%r14d
mov    %r13,%rdi
mov    %r12d,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1060 <fprintf@plt>
test   %rbp,%rbp
je     11da <log_data+0x7a>
lea    0xe51(%rip),%r12
xor    %ebx,%ebx
nopl   0x0(%rax)
movzbl (%r15,%rbx,1),%edx
mov    %r13,%rdi
mov    %r12,%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
add    $0x1,%ebx
cmp    %rbp,%rbx
jb     11c0 <log_data+0x60>
mov    $0xa,%edi
mov    %r13,%rsi
call   1050 <fputc@plt>
mov    %r13,%rdi
call   1030 <funlockfile@plt>
mov    %r13,%rdi
call   1080 <fflush@plt>
mov    0x2dea(%rip),%rcx
cmp    (%rcx),%eax
jne    120c <log_data+0xac>
mov    0x2dcf(%rip),%rax
mov    (%rax),%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [11 x i8] c"%i -> %i: \00"
@.str.1 = [5 x i8] c"%02X\00"
@.str.2 = [2 x i8] c"\0A\00"
@EOF = global i32 0
@errno = global i32 0
define i32 @log_data(i8* %0, i64 %1, i32* %2, i32* %3, i32* %4) {
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca i32*
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store i8* %0, i8** %6
store i64 %1, i64* %7
store i32* %2, i32** %8
store i32* %3, i32** %9
store i32* %4, i32** %10
%14 = load i32*, i32** %10
%15 = call i32 @flockfile(i32* %14)
%16 = load i32*, i32** %10
%17 = load i32*, i32** %8
%18 = call i32 @fileno(i32* %17)
%19 = load i32*, i32** %9
%20 = call i32 @fileno(i32* %19)
%21 = call i32 (i32*, i8*, ...) @fprintf(i32* %16, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %18, i32 %20)
store i32 0, i32* %11
br label %22
22: ; preds = %36, %5
%23 = load i32, i32* %11
%24 = zext i32 %23 to i64
%25 = load i64, i64* %7
%26 = icmp ult i64 %24, %25
br i1 %26, label %27, label %39
27: ; preds = %22
%28 = load i32*, i32** %10
%29 = load i8*, i8** %6
%30 = load i32, i32* %11
%31 = zext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %29, i64 %31
%33 = load i8, i8* %32
%34 = zext i8 %33 to i32
%35 = call i32 (i32*, i8*, ...) @fprintf(i32* %28, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %34)
br label %36
36: ; preds = %27
%37 = load i32, i32* %11
%38 = add i32 %37, 1
store i32 %38, i32* %11
br label %22
39: ; preds = %22
%40 = load i32*, i32** %10
%41 = call i32 (i32*, i8*, ...) @fprintf(i32* %40, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%42 = load i32*, i32** %10
%43 = call i32 @funlockfile(i32* %42)
%44 = load i32*, i32** %10
%45 = call i32 @fflush(i32* %44)
store i32 %45, i32* %12
%46 = load i32, i32* %12
%47 = load i32, i32* @EOF
%48 = icmp eq i32 %46, %47
br i1 %48, label %49, label %51
49: ; preds = %39
%50 = load i32, i32* @errno
store i32 %50, i32* %13
br label %52
51: ; preds = %39
store i32 0, i32* %13
br label %52
52: ; preds = %51, %49
%53 = load i32, i32* %13
ret i32 %53
}

LLVM IR (-O2):
@.str = [11 x i8] c"%i -> %i: \00"
@.str.1 = [5 x i8] c"%02X\00"
@EOF = global i32 0
@errno = global i32 0
define i32 @log_data(i8* nocapture %0, i64 %1, i32* nocapture %2, i32* nocapture %3, i32* nocapture %4) {
%6 = tail call i32 @flockfile(i32* %4)
%7 = tail call i32 @fileno(i32* %2)
%8 = tail call i32 @fileno(i32* %3)
%9 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %4, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %7, i32 %8)
%10 = icmp eq i64 %1, 0
br i1 %10, label %20, label %11
11: ; preds = %5, %11
%12 = phi i64 [ %18, %11 ], [ 0, %5 ]
%13 = getelementptr inbounds i8, i8* %0, i64 %12
%14 = load i8, i8* %13
%15 = zext i8 %14 to i32
%16 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %4, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %15)
%17 = add nuw nsw i64 %12, 1
%18 = and i64 %17, 4294967295
%19 = icmp ult i64 %18, %1
br i1 %19, label %11, label %20
20: ; preds = %11, %5
%21 = tail call i32 @fputc(i32 10, i32* %4)
%22 = tail call i32 @funlockfile(i32* %4)
%23 = tail call i32 @fflush(i32* %4)
%24 = load i32, i32* @EOF
%25 = icmp eq i32 %23, %24
%26 = load i32, i32* @errno
%27 = select i1 %25, i32 %26, i32 0
ret i32 %27
}