#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 /*<<< orphan*/ _itoa(int, char *, int);
char *_strupr(char *);
int /*<<< orphan*/ fwrite(char *, int, int, int /*<<< orphan*/ *);
void datawrite(unsigned char *buf, int len, FILE *fp, unsigned short *chk) {
  int i, n;
  char charbuffer[1];
  char *charbufferp;
  for (i = 0; i < len; i++) {
    n = (unsigned char)buf[i];
    _itoa(n, charbuffer, 16);
    if (charbuffer[1] == 0) {
      charbuffer[1] = charbuffer[0];
      charbuffer[0] = '0';
    }
    charbufferp = _strupr(charbuffer);
    fwrite(charbufferp, 2, 1, fp);
    *chk += charbuffer[0];
    *chk += charbuffer[1];
  }
}


x86 assembly (clang, -O0):
<datawrite>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11e3 <datawrite+0xb3>
mov    -0x8(%rbp),%rax
movslq -0x24(%rbp),%rcx
movzbl (%rax,%rcx,1),%eax
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%edi
lea    -0x29(%rbp),%rsi
mov    $0x10,%edx
call   1050 <_itoa@plt>
movsbl -0x28(%rbp),%eax
cmp    $0x0,%eax
jne    1191 <datawrite+0x61>
mov    -0x29(%rbp),%al
mov    %al,-0x28(%rbp)
movb   $0x30,-0x29(%rbp)
lea    -0x29(%rbp),%rdi
call   1030 <_strupr@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x2,%esi
mov    $0x1,%edx
call   1040 <fwrite@plt>
movsbl -0x29(%rbp),%edx
mov    -0x20(%rbp),%rax
movzwl (%rax),%ecx
add    %edx,%ecx
mov    %cx,(%rax)
movsbl -0x28(%rbp),%edx
mov    -0x20(%rbp),%rax
movzwl (%rax),%ecx
add    %edx,%ecx
mov    %cx,(%rax)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    114e <datawrite+0x1e>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<datawrite>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %esi,%esi
jle    11b8 <datawrite+0x88>
mov    %rcx,%rbx
mov    %rdx,%r14
mov    %rdi,%r15
mov    %esi,%r13d
xor    %ebp,%ebp
lea    0x7(%rsp),%r12
jmp    1198 <datawrite+0x68>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r12,%rdi
call   1030 <_strupr@plt>
mov    %rax,%rdi
mov    $0x2,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   1040 <fwrite@plt>
movsbl 0x7(%rsp),%eax
add    (%rbx),%ax
movsbl 0x8(%rsp),%ecx
add    %eax,%ecx
mov    %cx,(%rbx)
add    $0x1,%rbp
cmp    %rbp,%r13
je     11b8 <datawrite+0x88>
movzbl (%r15,%rbp,1),%edi
mov    %r12,%rsi
mov    $0x10,%edx
call   1050 <_itoa@plt>
cmpb   $0x0,0x8(%rsp)
jne    1160 <datawrite+0x30>
movb   $0x30,0x7(%rsp)
jmp    1160 <datawrite+0x30>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @datawrite(i8* %0, i32 %1, i32* %2, i16* %3) {
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i16*
%9 = alloca i32
%10 = alloca i32
%11 = alloca [1 x i8]
%12 = alloca i8*
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32* %2, i32** %7
store i16* %3, i16** %8
store i32 0, i32* %9
br label %13
13: ; preds = %58, %4
%14 = load i32, i32* %9
%15 = load i32, i32* %6
%16 = icmp slt i32 %14, %15
br i1 %16, label %17, label %61
17: ; preds = %13
%18 = load i8*, i8** %5
%19 = load i32, i32* %9
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %18, i64 %20
%22 = load i8, i8* %21
%23 = zext i8 %22 to i32
store i32 %23, i32* %10
%24 = load i32, i32* %10
%25 = getelementptr inbounds [1 x i8], [1 x i8]* %11, i64 0, i64 0
%26 = call i32 @_itoa(i32 %24, i8* %25, i32 16)
%27 = getelementptr inbounds [1 x i8], [1 x i8]* %11, i64 0, i64 1
%28 = load i8, i8* %27
%29 = sext i8 %28 to i32
%30 = icmp eq i32 %29, 0
br i1 %30, label %31, label %36
31: ; preds = %17
%32 = getelementptr inbounds [1 x i8], [1 x i8]* %11, i64 0, i64 0
%33 = load i8, i8* %32
%34 = getelementptr inbounds [1 x i8], [1 x i8]* %11, i64 0, i64 1
store i8 %33, i8* %34
%35 = getelementptr inbounds [1 x i8], [1 x i8]* %11, i64 0, i64 0
store i8 48, i8* %35
br label %36
36: ; preds = %31, %17
%37 = getelementptr inbounds [1 x i8], [1 x i8]* %11, i64 0, i64 0
%38 = call i8* @_strupr(i8* %37)
store i8* %38, i8** %12
%39 = load i8*, i8** %12
%40 = load i32*, i32** %7
%41 = call i32 @fwrite(i8* %39, i32 2, i32 1, i32* %40)
%42 = getelementptr inbounds [1 x i8], [1 x i8]* %11, i64 0, i64 0
%43 = load i8, i8* %42
%44 = sext i8 %43 to i32
%45 = load i16*, i16** %8
%46 = load i16, i16* %45
%47 = zext i16 %46 to i32
%48 = add nsw i32 %47, %44
%49 = trunc i32 %48 to i16
store i16 %49, i16* %45
%50 = getelementptr inbounds [1 x i8], [1 x i8]* %11, i64 0, i64 1
%51 = load i8, i8* %50
%52 = sext i8 %51 to i32
%53 = load i16*, i16** %8
%54 = load i16, i16* %53
%55 = zext i16 %54 to i32
%56 = add nsw i32 %55, %52
%57 = trunc i32 %56 to i16
store i16 %57, i16* %53
br label %58
58: ; preds = %36
%59 = load i32, i32* %9
%60 = add nsw i32 %59, 1
store i32 %60, i32* %9
br label %13
61: ; preds = %13
ret void
}

LLVM IR (-O2):
define void @datawrite(i8* nocapture %0, i32 %1, i32* nocapture %2, i16* nocapture %3) {
%5 = alloca [1 x i8]
%6 = getelementptr inbounds [1 x i8], [1 x i8]* %5, i64 0, i64 0
%7 = icmp sgt i32 %1, 0
br i1 %7, label %8, label %32
8: ; preds = %4
%9 = getelementptr inbounds [1 x i8], [1 x i8]* %5, i64 0, i64 1
%10 = zext i32 %1 to i64
br label %11
11: ; preds = %8, %20
%12 = phi i64 [ 0, %8 ], [ %30, %20 ]
%13 = getelementptr inbounds i8, i8* %0, i64 %12
%14 = load i8, i8* %13
%15 = zext i8 %14 to i32
%16 = call i32 @_itoa(i32 %15, i8* %6, i32 16)
%17 = load i8, i8* %9
%18 = icmp eq i8 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %11
store i8 48, i8* %6
br label %20
20: ; preds = %19, %11
%21 = call i8* @_strupr(i8* %6)
%22 = call i32 @fwrite(i8* %21, i32 2, i32 1, i32* %2)
%23 = load i8, i8* %6
%24 = sext i8 %23 to i16
%25 = load i16, i16* %3
%26 = add i16 %25, %24
%27 = load i8, i8* %9
%28 = sext i8 %27 to i16
%29 = add i16 %26, %28
store i16 %29, i16* %3
%30 = add nuw nsw i64 %12, 1
%31 = icmp eq i64 %30, %10
br i1 %31, label %32, label %11
32: ; preds = %20, %4
ret void
}