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

/* Variables and functions */
int snprintf(char *, int, char *, char);
int /*<<< orphan*/ write(int, char *, int);
int write_hex(int fd, char *data, int datalen) {
  int i;
  char pb[10];
  int len;
  for (i = 0; i < datalen; i++) {
    if (i != 0) {
      if (i % 16 == 0)
        write(fd, "\n", 1);
      else if (i % 4 == 0)
        write(fd, " ", 1);
    }
    len = snprintf(pb, sizeof(pb), "%02x", data[i] & 0xff);
    write(fd, pb, len);
  }
  write(fd, "\n", 1);
  return 0;
}


x86 assembly (clang, -O0):
<write_hex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11f7 <write_hex+0xd7>
cmpl   $0x0,-0x18(%rbp)
je     11ae <write_hex+0x8e>
mov    -0x18(%rbp),%eax
mov    $0x10,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    117c <write_hex+0x5c>
mov    -0x4(%rbp),%edi
lea    0xe93(%rip),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
jmp    11a9 <write_hex+0x89>
mov    -0x18(%rbp),%eax
mov    $0x4,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    11a4 <write_hex+0x84>
mov    -0x4(%rbp),%edi
lea    0xe68(%rip),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
jmp    11a9 <write_hex+0x89>
jmp    11ae <write_hex+0x8e>
lea    -0x22(%rbp),%rdi
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
and    $0xff,%eax
mov    $0xa,%esi
lea    0xe35(%rip),%rdx
movsbl %al,%ecx
call   1040 <snprintf@plt>
mov    %eax,-0x28(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x22(%rbp),%rsi
mov    -0x28(%rbp),%edx
call   1030 <write@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1139 <write_hex+0x19>
mov    -0x4(%rbp),%edi
lea    0xdff(%rip),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_hex>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebx
test   %edx,%edx
jle    1166 <write_hex+0x46>
mov    %edx,%ebp
mov    %rsi,%r14
movsbl (%rsi),%ecx
lea    0xec1(%rip),%rdx
lea    0xe(%rsp),%r15
mov    %r15,%rdi
mov    $0xa,%esi
call   1040 <snprintf@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    $0x1,%ebp
jne    118a <write_hex+0x6a>
lea    0xe93(%rip),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1030 <write@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %ebp,%r13d
mov    $0x1,%ebp
lea    0xe6b(%rip),%r15
lea    0xe(%rsp),%r12
jmp    11d6 <write_hex+0xb6>
mov    %ebx,%edi
mov    $0x1,%edx
call   1030 <write@plt>
movsbl (%r14,%rbp,1),%ecx
mov    %r12,%rdi
mov    $0xa,%esi
mov    %r15,%rdx
call   1040 <snprintf@plt>
mov    %ebx,%edi
mov    %r12,%rsi
mov    %eax,%edx
call   1030 <write@plt>
add    $0x1,%rbp
cmp    %rbp,%r13
je     1166 <write_hex+0x46>
lea    0xe23(%rip),%rsi
test   $0xf,%bpl
je     11a0 <write_hex+0x80>
lea    0xe18(%rip),%rsi
test   $0x3,%bpl
jne    11ac <write_hex+0x8c>
jmp    11a0 <write_hex+0x80>

LLVM IR (-O0):
@.str = [2 x i8] c"\0A\00"
@.str.1 = [2 x i8] c" \00"
@.str.2 = [5 x i8] c"%02x\00"
define i32 @write_hex(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca [10 x i8]
%9 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
store i32 0, i32* %7
br label %10
10: ; preds = %48, %3
%11 = load i32, i32* %7
%12 = load i32, i32* %6
%13 = icmp slt i32 %11, %12
br i1 %13, label %14, label %51
14: ; preds = %10
%15 = load i32, i32* %7
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %33
17: ; preds = %14
%18 = load i32, i32* %7
%19 = srem i32 %18, 16
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %17
%22 = load i32, i32* %4
%23 = call i32 @write(i32 %22, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 1)
br label %32
24: ; preds = %17
%25 = load i32, i32* %7
%26 = srem i32 %25, 4
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %24
%29 = load i32, i32* %4
%30 = call i32 @write(i32 %29, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 1)
br label %31
31: ; preds = %28, %24
br label %32
32: ; preds = %31, %21
br label %33
33: ; preds = %32, %14
%34 = getelementptr inbounds [10 x i8], [10 x i8]* %8, i64 0, i64 0
%35 = load i8*, i8** %5
%36 = load i32, i32* %7
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i8, i8* %35, i64 %37
%39 = load i8, i8* %38
%40 = sext i8 %39 to i32
%41 = and i32 %40, 255
%42 = trunc i32 %41 to i8
%43 = call i32 @snprintf(i8* %34, i32 10, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8 signext %42)
store i32 %43, i32* %9
%44 = load i32, i32* %4
%45 = getelementptr inbounds [10 x i8], [10 x i8]* %8, i64 0, i64 0
%46 = load i32, i32* %9
%47 = call i32 @write(i32 %44, i8* %45, i32 %46)
br label %48
48: ; preds = %33
%49 = load i32, i32* %7
%50 = add nsw i32 %49, 1
store i32 %50, i32* %7
br label %10
51: ; preds = %10
%52 = load i32, i32* %4
%53 = call i32 @write(i32 %52, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 1)
ret i32 0
}

LLVM IR (-O2):
@.str = [2 x i8] c"\0A\00"
@.str.1 = [2 x i8] c" \00"
@.str.2 = [5 x i8] c"%02x\00"
define i32 @write_hex(i32 %0, i8* nocapture %1, i32 %2) {
%4 = alloca [10 x i8]
%5 = getelementptr inbounds [10 x i8], [10 x i8]* %4, i64 0, i64 0
%6 = icmp sgt i32 %2, 0
br i1 %6, label %7, label %31
7: ; preds = %3
%8 = zext i32 %2 to i64
%9 = load i8, i8* %1
%10 = call i32 @snprintf(i8* %5, i32 10, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8 signext %9)
%11 = call i32 @write(i32 %0, i8* %5, i32 %10)
%12 = icmp eq i32 %2, 1
br i1 %12, label %31, label %13
13: ; preds = %7, %24
%14 = phi i64 [ %29, %24 ], [ 1, %7 ]
%15 = trunc i64 %14 to i32
%16 = and i32 %15, 15
%17 = icmp eq i32 %16, 0
br i1 %17, label %21, label %18
18: ; preds = %13
%19 = and i32 %15, 3
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %18, %13
%22 = phi i8* [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), %13 ], [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), %18 ]
%23 = call i32 @write(i32 %0, i8* %22, i32 1)
br label %24
24: ; preds = %21, %18
%25 = getelementptr inbounds i8, i8* %1, i64 %14
%26 = load i8, i8* %25
%27 = call i32 @snprintf(i8* %5, i32 10, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8 signext %26)
%28 = call i32 @write(i32 %0, i8* %5, i32 %27)
%29 = add nuw nsw i64 %14, 1
%30 = icmp eq i64 %29, %8
br i1 %30, label %31, label %13
31: ; preds = %24, %7, %3
%32 = call i32 @write(i32 %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 1)
ret i32 0
}