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

/* Variables and functions */
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ printf(char *);
int write_hex(unsigned char **, int, int);
void test_write_hex(void) {
  char dest[] = "xmnopqrstz";
  char expected[] = "xa1f009z";
  unsigned char *p = (unsigned char *)dest + 1;
  unsigned char *null_ptr = NULL;
  printf("\tStart  test_write_hex\n");
  assert(write_hex(NULL, 0, 0));
  assert(write_hex(&null_ptr, 0, 0));
  assert(!write_hex(NULL, 3, 0));
  assert(!write_hex(&null_ptr, 0, 2));
  assert(!write_hex(NULL, 99, 5));
  assert(!write_hex(&null_ptr, 50, 8));
  assert(write_hex(&p, 0xa1f, 3));
  assert((char)*p == 'p');
  assert(write_hex(&p, 9, 3));
  assert((char)*p == 's');
  assert(!write_hex(&p, -55, 2));
  assert((char)*p == 's');
  assert(write_hex(&p, 0, 0));
  assert((char)*p == 's');
  assert(!write_hex(&p, 0x54321, 2));
  assert((char)*p == 'z');
  printf("\tFinish test_write_hex\n");
}


x86 assembly (clang, -O0):
<test_write_hex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    0xec1(%rip),%rax
mov    %rax,-0xb(%rbp)
mov    0xebe(%rip),%ax
mov    %ax,-0x3(%rbp)
mov    0xeb6(%rip),%al
mov    %al,-0x1(%rbp)
mov    0xead(%rip),%rax
mov    %rax,-0x14(%rbp)
mov    0xeab(%rip),%al
mov    %al,-0xc(%rbp)
lea    -0xb(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
lea    0xe8e(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
xor    %edx,%edx
mov    %edx,%esi
call   1030 <write_hex@plt>
mov    %eax,%edi
call   1050 <assert@plt>
lea    -0x28(%rbp),%rdi
xor    %edx,%edx
mov    %edx,%esi
call   1030 <write_hex@plt>
mov    %eax,%edi
call   1050 <assert@plt>
xor    %eax,%eax
mov    %eax,%edi
mov    $0x3,%esi
xor    %edx,%edx
call   1030 <write_hex@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x2,%edx
call   1030 <write_hex@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
xor    %eax,%eax
mov    %eax,%edi
mov    $0x63,%esi
mov    $0x5,%edx
call   1030 <write_hex@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
lea    -0x28(%rbp),%rdi
mov    $0x32,%esi
mov    $0x8,%edx
call   1030 <write_hex@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
lea    -0x20(%rbp),%rdi
mov    $0xa1f,%esi
mov    $0x3,%edx
call   1030 <write_hex@plt>
mov    %eax,%edi
call   1050 <assert@plt>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x70,%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
lea    -0x20(%rbp),%rdi
mov    $0x9,%esi
mov    $0x3,%edx
call   1030 <write_hex@plt>
mov    %eax,%edi
call   1050 <assert@plt>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x73,%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
lea    -0x20(%rbp),%rdi
mov    $0xffffffc9,%esi
mov    $0x2,%edx
call   1030 <write_hex@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x73,%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
lea    -0x20(%rbp),%rdi
xor    %edx,%edx
mov    %edx,%esi
call   1030 <write_hex@plt>
mov    %eax,%edi
call   1050 <assert@plt>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x73,%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
lea    -0x20(%rbp),%rdi
mov    $0x54321,%esi
mov    $0x2,%edx
call   1030 <write_hex@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x7a,%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
lea    0xcdf(%rip),%rdi
call   1040 <printf@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_write_hex>:
push   %rbx
sub    $0x20,%rsp
movabs $0x737271706f6e6d78,%rax
71 72 73
mov    %rax,0x8(%rsp)
movl   $0x7a7473,0xf(%rsp)
lea    0x9(%rsp),%rax
mov    %rax,(%rsp)
movq   $0x0,0x18(%rsp)
lea    0xea6(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1040 <write_hex@plt>
mov    %eax,%edi
call   1050 <assert@plt>
lea    0x18(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1040 <write_hex@plt>
mov    %eax,%edi
call   1050 <assert@plt>
xor    %edi,%edi
mov    $0x3,%esi
xor    %edx,%edx
call   1040 <write_hex@plt>
xor    %edi,%edi
test   %eax,%eax
sete   %dil
call   1050 <assert@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x2,%edx
call   1040 <write_hex@plt>
xor    %edi,%edi
test   %eax,%eax
sete   %dil
call   1050 <assert@plt>
xor    %edi,%edi
mov    $0x63,%esi
mov    $0x5,%edx
call   1040 <write_hex@plt>
xor    %edi,%edi
test   %eax,%eax
sete   %dil
call   1050 <assert@plt>
mov    %rbx,%rdi
mov    $0x32,%esi
mov    $0x8,%edx
call   1040 <write_hex@plt>
xor    %edi,%edi
test   %eax,%eax
sete   %dil
call   1050 <assert@plt>
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0xa1f,%esi
mov    $0x3,%edx
call   1040 <write_hex@plt>
mov    %eax,%edi
call   1050 <assert@plt>
mov    (%rsp),%rax
xor    %edi,%edi
cmpb   $0x70,(%rax)
sete   %dil
call   1050 <assert@plt>
mov    %rbx,%rdi
mov    $0x9,%esi
mov    $0x3,%edx
call   1040 <write_hex@plt>
mov    %eax,%edi
call   1050 <assert@plt>
mov    (%rsp),%rax
xor    %edi,%edi
cmpb   $0x73,(%rax)
sete   %dil
call   1050 <assert@plt>
mov    %rbx,%rdi
mov    $0xffffffc9,%esi
mov    $0x2,%edx
call   1040 <write_hex@plt>
xor    %edi,%edi
test   %eax,%eax
sete   %dil
call   1050 <assert@plt>
mov    (%rsp),%rax
xor    %edi,%edi
cmpb   $0x73,(%rax)
sete   %dil
call   1050 <assert@plt>
mov    %rbx,%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1040 <write_hex@plt>
mov    %eax,%edi
call   1050 <assert@plt>
mov    (%rsp),%rax
xor    %edi,%edi
cmpb   $0x73,(%rax)
sete   %dil
call   1050 <assert@plt>
mov    %rbx,%rdi
mov    $0x54321,%esi
mov    $0x2,%edx
call   1040 <write_hex@plt>
xor    %edi,%edi
test   %eax,%eax
sete   %dil
call   1050 <assert@plt>
mov    (%rsp),%rax
xor    %edi,%edi
cmpb   $0x7a,(%rax)
sete   %dil
call   1050 <assert@plt>
lea    0xd33(%rip),%rdi
call   1030 <puts@plt>
add    $0x20,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@__const.test_write_hex.dest = [11 x i8] c"xmnopqrstz\00"
@__const.test_write_hex.expected = [9 x i8] c"xa1f009z\00"
@.str = [24 x i8] c"\09Start test_write_hex\0A\00"
@.str.1 = [24 x i8] c"\09Finish test_write_hex\0A\00"
define void @test_write_hex() {
%1 = alloca [11 x i8]
%2 = alloca [9 x i8]
%3 = alloca i8*
%4 = alloca i8*
%5 = bitcast [11 x i8]* %1 to i8*
%6 = bitcast [9 x i8]* %2 to i8*
%7 = getelementptr inbounds [11 x i8], [11 x i8]* %1, i64 0, i64 0
%8 = getelementptr inbounds i8, i8* %7, i64 1
store i8* %8, i8** %3
store i8* null, i8** %4
%9 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%10 = call i32 @write_hex(i8** null, i32 0, i32 0)
%11 = call i32 @assert(i32 %10)
%12 = call i32 @write_hex(i8** %4, i32 0, i32 0)
%13 = call i32 @assert(i32 %12)
%14 = call i32 @write_hex(i8** null, i32 3, i32 0)
%15 = icmp ne i32 %14, 0
%16 = xor i1 %15, true
%17 = zext i1 %16 to i32
%18 = call i32 @assert(i32 %17)
%19 = call i32 @write_hex(i8** %4, i32 0, i32 2)
%20 = icmp ne i32 %19, 0
%21 = xor i1 %20, true
%22 = zext i1 %21 to i32
%23 = call i32 @assert(i32 %22)
%24 = call i32 @write_hex(i8** null, i32 99, i32 5)
%25 = icmp ne i32 %24, 0
%26 = xor i1 %25, true
%27 = zext i1 %26 to i32
%28 = call i32 @assert(i32 %27)
%29 = call i32 @write_hex(i8** %4, i32 50, i32 8)
%30 = icmp ne i32 %29, 0
%31 = xor i1 %30, true
%32 = zext i1 %31 to i32
%33 = call i32 @assert(i32 %32)
%34 = call i32 @write_hex(i8** %3, i32 2591, i32 3)
%35 = call i32 @assert(i32 %34)
%36 = load i8*, i8** %3
%37 = load i8, i8* %36
%38 = sext i8 %37 to i32
%39 = icmp eq i32 %38, 112
%40 = zext i1 %39 to i32
%41 = call i32 @assert(i32 %40)
%42 = call i32 @write_hex(i8** %3, i32 9, i32 3)
%43 = call i32 @assert(i32 %42)
%44 = load i8*, i8** %3
%45 = load i8, i8* %44
%46 = sext i8 %45 to i32
%47 = icmp eq i32 %46, 115
%48 = zext i1 %47 to i32
%49 = call i32 @assert(i32 %48)
%50 = call i32 @write_hex(i8** %3, i32 -55, i32 2)
%51 = icmp ne i32 %50, 0
%52 = xor i1 %51, true
%53 = zext i1 %52 to i32
%54 = call i32 @assert(i32 %53)
%55 = load i8*, i8** %3
%56 = load i8, i8* %55
%57 = sext i8 %56 to i32
%58 = icmp eq i32 %57, 115
%59 = zext i1 %58 to i32
%60 = call i32 @assert(i32 %59)
%61 = call i32 @write_hex(i8** %3, i32 0, i32 0)
%62 = call i32 @assert(i32 %61)
%63 = load i8*, i8** %3
%64 = load i8, i8* %63
%65 = sext i8 %64 to i32
%66 = icmp eq i32 %65, 115
%67 = zext i1 %66 to i32
%68 = call i32 @assert(i32 %67)
%69 = call i32 @write_hex(i8** %3, i32 344865, i32 2)
%70 = icmp ne i32 %69, 0
%71 = xor i1 %70, true
%72 = zext i1 %71 to i32
%73 = call i32 @assert(i32 %72)
%74 = load i8*, i8** %3
%75 = load i8, i8* %74
%76 = sext i8 %75 to i32
%77 = icmp eq i32 %76, 122
%78 = zext i1 %77 to i32
%79 = call i32 @assert(i32 %78)
%80 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
ret void
}

LLVM IR (-O2):
@__const.test_write_hex.dest = [11 x i8] c"xmnopqrstz\00"
@str = [23 x i8] c"\09Start test_write_hex\00"
@str.2 = [23 x i8] c"\09Finish test_write_hex\00"
define void @test_write_hex() {
%1 = alloca [11 x i8]
%2 = alloca i8*
%3 = alloca i8*
%4 = getelementptr inbounds [11 x i8], [11 x i8]* %1, i64 0, i64 0
%5 = bitcast i8** %2 to i8*
%6 = getelementptr inbounds [11 x i8], [11 x i8]* %1, i64 0, i64 1
store i8* %6, i8** %2
%7 = bitcast i8** %3 to i8*
store i8* null, i8** %3
%8 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str, i64 0, i64 0))
%9 = call i32 @write_hex(i8** null, i32 0, i32 0)
%10 = call i32 @assert(i32 %9)
%11 = call i32 @write_hex(i8** %3, i32 0, i32 0)
%12 = call i32 @assert(i32 %11)
%13 = call i32 @write_hex(i8** null, i32 3, i32 0)
%14 = icmp eq i32 %13, 0
%15 = zext i1 %14 to i32
%16 = call i32 @assert(i32 %15)
%17 = call i32 @write_hex(i8** %3, i32 0, i32 2)
%18 = icmp eq i32 %17, 0
%19 = zext i1 %18 to i32
%20 = call i32 @assert(i32 %19)
%21 = call i32 @write_hex(i8** null, i32 99, i32 5)
%22 = icmp eq i32 %21, 0
%23 = zext i1 %22 to i32
%24 = call i32 @assert(i32 %23)
%25 = call i32 @write_hex(i8** %3, i32 50, i32 8)
%26 = icmp eq i32 %25, 0
%27 = zext i1 %26 to i32
%28 = call i32 @assert(i32 %27)
%29 = call i32 @write_hex(i8** %2, i32 2591, i32 3)
%30 = call i32 @assert(i32 %29)
%31 = load i8*, i8** %2
%32 = load i8, i8* %31
%33 = icmp eq i8 %32, 112
%34 = zext i1 %33 to i32
%35 = call i32 @assert(i32 %34)
%36 = call i32 @write_hex(i8** %2, i32 9, i32 3)
%37 = call i32 @assert(i32 %36)
%38 = load i8*, i8** %2
%39 = load i8, i8* %38
%40 = icmp eq i8 %39, 115
%41 = zext i1 %40 to i32
%42 = call i32 @assert(i32 %41)
%43 = call i32 @write_hex(i8** %2, i32 -55, i32 2)
%44 = icmp eq i32 %43, 0
%45 = zext i1 %44 to i32
%46 = call i32 @assert(i32 %45)
%47 = load i8*, i8** %2
%48 = load i8, i8* %47
%49 = icmp eq i8 %48, 115
%50 = zext i1 %49 to i32
%51 = call i32 @assert(i32 %50)
%52 = call i32 @write_hex(i8** %2, i32 0, i32 0)
%53 = call i32 @assert(i32 %52)
%54 = load i8*, i8** %2
%55 = load i8, i8* %54
%56 = icmp eq i8 %55, 115
%57 = zext i1 %56 to i32
%58 = call i32 @assert(i32 %57)
%59 = call i32 @write_hex(i8** %2, i32 344865, i32 2)
%60 = icmp eq i32 %59, 0
%61 = zext i1 %60 to i32
%62 = call i32 @assert(i32 %61)
%63 = load i8*, i8** %2
%64 = load i8, i8* %63
%65 = icmp eq i8 %64, 122
%66 = zext i1 %65 to i32
%67 = call i32 @assert(i32 %66)
%68 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str.2, i64 0, i64 0))
ret void
}