#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*/ sprintf(char *, char *, unsigned long);
int /*<<< orphan*/ strcat(char *, char *);
char *serpent_convert_to_string(int len, unsigned long val[8], char *str) {
  int i;
  if (len < 0)
    return (char *)-1;
  str[0] = 0;
  i = len / 32;
  if (len & 31 > 0) {
    char tmp[10];
    sprintf(tmp, "%08lX", val[i] & (((len & 31) << 1) - 1));
    strcat(str, &tmp[8 - (((len & 31) + 3) / 4)]);
  }
  for (i--; i >= 0; i--) {
    char tmp[10];
    sprintf(tmp, "%08lX", val[i]);
    strcat(str, tmp);
  }
  return str;
}


x86 assembly (clang, -O0):
<serpent_convert_to_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    114d <serpent_convert_to_string+0x2d>
mov    $0xffffffffffffffff,%rax
mov    %rax,-0x8(%rbp)
jmp    121c <serpent_convert_to_string+0xfc>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0xc(%rbp),%eax
mov    $0x20,%ecx
cltd
idiv   %ecx
mov    %eax,-0x24(%rbp)
mov    -0xc(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     11ca <serpent_convert_to_string+0xaa>
lea    -0x2e(%rbp),%rdi
mov    -0x18(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,8),%rdx
mov    -0xc(%rbp),%eax
and    $0x1f,%eax
shl    $0x1,%eax
sub    $0x1,%eax
cltq
and    %rax,%rdx
lea    0xe67(%rip),%rsi
call   1040 <sprintf@plt>
mov    -0x20(%rbp),%rdi
mov    -0xc(%rbp),%eax
and    $0x1f,%eax
add    $0x3,%eax
mov    $0x4,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    $0x8,%eax
sub    %ecx,%eax
cltq
lea    -0x2e(%rbp),%rsi
add    %rax,%rsi
call   1030 <strcat@plt>
mov    -0x24(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jl     1214 <serpent_convert_to_string+0xf4>
lea    -0x38(%rbp),%rdi
mov    -0x18(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,8),%rdx
lea    0xe0c(%rip),%rsi
call   1040 <sprintf@plt>
mov    -0x20(%rbp),%rdi
lea    -0x38(%rbp),%rsi
call   1030 <strcat@plt>
mov    -0x24(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x24(%rbp)
jmp    11d3 <serpent_convert_to_string+0xb3>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<serpent_convert_to_string>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
test   %edi,%edi
js     11d9 <serpent_convert_to_string+0xb9>
mov    %rdx,%r15
mov    %rsi,%r14
mov    %edi,%ebx
movb   $0x0,(%rdx)
test   $0x1,%bl
je     1190 <serpent_convert_to_string+0x70>
mov    %ebx,%eax
shr    $0x5,%eax
mov    %ebx,%r12d
and    $0x1f,%r12d
lea    (%r12,%r12,1),%ecx
add    $0xffffffff,%ecx
movslq %ecx,%rdx
and    (%r14,%rax,8),%rdx
lea    0xe9a(%rip),%rsi
lea    0x6(%rsp),%rdi
call   1040 <sprintf@plt>
add    $0x3,%r12d
shr    $0x2,%r12d
mov    $0x8,%eax
sub    %r12d,%eax
lea    (%rsp,%rax,1),%rsi
add    $0x6,%rsi
mov    %r15,%rdi
call   1030 <strcat@plt>
cmp    $0x20,%ebx
jb     11e0 <serpent_convert_to_string+0xc0>
shr    $0x5,%ebx
add    $0x1,%rbx
lea    0xe5d(%rip),%r12
lea    0x6(%rsp),%r13
nopl   0x0(%rax,%rax,1)
lea    -0x2(%rbx),%eax
mov    (%r14,%rax,8),%rdx
mov    %r13,%rdi
mov    %r12,%rsi
call   1040 <sprintf@plt>
mov    %r15,%rdi
mov    %r13,%rsi
call   1030 <strcat@plt>
add    $0xffffffffffffffff,%rbx
cmp    $0x1,%rbx
ja     11b0 <serpent_convert_to_string+0x90>
jmp    11e0 <serpent_convert_to_string+0xc0>
mov    $0xffffffffffffffff,%r15
mov    %r15,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@.str = [6 x i8] c"%08lX\00"
define i8* @serpent_convert_to_string(i32 %0, i64* nocapture %1, i8* %2) {
%4 = alloca [10 x i8]
%5 = alloca [10 x i8]
%6 = icmp slt i32 %0, 0
br i1 %6, label %43, label %7
7: ; preds = %3
store i8 0, i8* %2
%8 = and i32 %0, 1
%9 = icmp eq i32 %8, 0
br i1 %9, label %28, label %10
10: ; preds = %7
%11 = lshr i32 %0, 5
%12 = getelementptr inbounds [10 x i8], [10 x i8]* %4, i64 0, i64 0
%13 = zext i32 %11 to i64
%14 = getelementptr inbounds i64, i64* %1, i64 %13
%15 = load i64, i64* %14
%16 = and i32 %0, 31
%17 = shl nuw nsw i32 %16, 1
%18 = add nsw i32 %17, -1
%19 = sext i32 %18 to i64
%20 = and i64 %15, %19
%21 = call i32 @sprintf(i8* %12, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i64 %20)
%22 = add nuw nsw i32 %16, 3
%23 = lshr i32 %22, 2
%24 = sub nuw nsw i32 8, %23
%25 = zext i32 %24 to i64
%26 = getelementptr inbounds [10 x i8], [10 x i8]* %4, i64 0, i64 %25
%27 = call i32 @strcat(i8* %2, i8* %26)
br label %28
28: ; preds = %10, %7
%29 = icmp ult i32 %0, 32
br i1 %29, label %43, label %30
30: ; preds = %28
%31 = getelementptr inbounds [10 x i8], [10 x i8]* %5, i64 0, i64 0
%32 = lshr i32 %0, 5
%33 = zext i32 %32 to i64
br label %34
34: ; preds = %30, %34
%35 = phi i64 [ %33, %30 ], [ %36, %34 ]
%36 = add nsw i64 %35, -1
%37 = and i64 %36, 4294967295
%38 = getelementptr inbounds i64, i64* %1, i64 %37
%39 = load i64, i64* %38
%40 = call i32 @sprintf(i8* %31, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i64 %39)
%41 = call i32 @strcat(i8* %2, i8* %31)
%42 = icmp ugt i64 %35, 1
br i1 %42, label %34, label %43
43: ; preds = %34, %28, %3
%44 = phi i8* [ inttoptr (i64 -1 to i8*), %3 ], [ %2, %28 ], [ %2, %34 ]
ret i8* %44
}