char *uint128_str(unsigned __int128 n) {
  if (n == 0)
    return "0";
  char *str = (char *)malloc(40 * sizeof(char));
  char *s = str + (40 * sizeof(char)) - 1;
  while (n != 0) {
    *--s = "0123456789"[n % 10];
    n /= 10;
  }
  return str;
}


x86 assembly (clang, -O0):
<uint128_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x20(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%rcx
mov    %rcx,-0x28(%rbp)
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    -0x28(%rbp),%rcx
or     %rcx,%rax
jne    1156 <uint128_str+0x46>
jmp    1146 <uint128_str+0x36>
lea    0xeb3(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    11f0 <uint128_str+0xe0>
mov    $0x28,%edi
call   1030 <malloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
add    $0x28,%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x40(%rbp)
mov    -0x30(%rbp),%rax
mov    -0x28(%rbp),%rcx
or     %rcx,%rax
je     11e8 <uint128_str+0xd8>
jmp    118a <uint128_str+0x7a>
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,-0x48(%rbp)
mov    $0xa,%edx
mov    %rdx,-0x50(%rbp)
call   1300 <__umodti3>
mov    -0x50(%rbp),%rdx
mov    -0x48(%rbp),%rcx
lea    0xe4b(%rip),%rsi
mov    (%rax,%rsi,1),%sil
mov    -0x40(%rbp),%rax
mov    %rax,%rdi
add    $0xffffffffffffffff,%rdi
mov    %rdi,-0x40(%rbp)
mov    %sil,-0x1(%rax)
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1200 <__udivti3>
mov    %rdx,-0x28(%rbp)
mov    %rax,-0x30(%rbp)
jmp    1174 <uint128_str+0x64>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret
nopw   0x0(%rax,%rax,1)

x86 assembly (clang, -O2):
<uint128_str>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rax
or     %rsi,%rax
je     1193 <uint128_str+0x83>
mov    %rsi,%r14
mov    %rdi,%r12
mov    $0x28,%edi
call   1030 <malloc@plt>
mov    %rax,%r15
mov    %rax,%rbx
add    $0x26,%rbx
lea    0xebe(%rip),%r13
mov    $0x9,%ebp
nopl   0x0(%rax)
mov    $0xa,%edx
mov    %r12,%rdi
mov    %r14,%rsi
xor    %ecx,%ecx
call   11b0 <__udivti3>
mov    %rax,%rcx
add    %rax,%rcx
lea    (%rcx,%rcx,4),%rcx
mov    %r12,%rsi
sub    %rcx,%rsi
movzbl 0x0(%r13,%rsi,1),%ecx
mov    %cl,(%rbx)
add    $0xffffffffffffffff,%rbx
cmp    %r12,%rbp
mov    $0x0,%ecx
sbb    %r14,%rcx
mov    %rax,%r12
mov    %rdx,%r14
jb     1150 <uint128_str+0x40>
jmp    119a <uint128_str+0x8a>
lea    0xe66(%rip),%r15
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
nopl   0x0(%rax)

LLVM IR (-O0):
@.str = [2 x i8] c"0\00"
@.str.1 = [11 x i8] c"0123456789\00"
define i8* @uint128_str(i64 %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i128
%5 = alloca i128
%6 = alloca i8*
%7 = alloca i8*
%8 = bitcast i128* %4 to { i64, i64 }*
%9 = getelementptr inbounds { i64, i64 }, { i64, i64 }* %8, i32 0, i32 0
store i64 %0, i64* %9
%10 = getelementptr inbounds { i64, i64 }, { i64, i64 }* %8, i32 0, i32 1
store i64 %1, i64* %10
%11 = load i128, i128* %4
store i128 %11, i128* %5
%12 = load i128, i128* %5
%13 = icmp eq i128 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %2
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %3
br label %35
15: ; preds = %2
%16 = call i8* @malloc(i64 40)
store i8* %16, i8** %6
%17 = load i8*, i8** %6
%18 = getelementptr inbounds i8, i8* %17, i64 40
%19 = getelementptr inbounds i8, i8* %18, i64 -1
store i8* %19, i8** %7
br label %20
20: ; preds = %23, %15
%21 = load i128, i128* %5
%22 = icmp ne i128 %21, 0
br i1 %22, label %23, label %33
23: ; preds = %20
%24 = load i128, i128* %5
%25 = urem i128 %24, 10
%26 = trunc i128 %25 to i64
%27 = getelementptr inbounds [11 x i8], [11 x i8]* @.str.1, i64 0, i64 %26
%28 = load i8, i8* %27
%29 = load i8*, i8** %7
%30 = getelementptr inbounds i8, i8* %29, i32 -1
store i8* %30, i8** %7
store i8 %28, i8* %30
%31 = load i128, i128* %5
%32 = udiv i128 %31, 10
store i128 %32, i128* %5
br label %20
33: ; preds = %20
%34 = load i8*, i8** %6
store i8* %34, i8** %3
br label %35
35: ; preds = %33, %14
%36 = load i8*, i8** %3
ret i8* %36
}

LLVM IR (-O2):
@.str = [2 x i8] c"0\00"
@.str.1 = [11 x i8] c"0123456789\00"
define i8* @uint128_str(i64 %0, i64 %1) {
%3 = zext i64 %1 to i128
%4 = shl nuw i128 %3, 64
%5 = zext i64 %0 to i128
%6 = or i128 %4, %5
%7 = icmp eq i128 %6, 0
br i1 %7, label %23, label %8
8: ; preds = %2
%9 = tail call dereferenceable_or_null(40) i8* @malloc(i64 40)
%10 = getelementptr inbounds i8, i8* %9, i64 39
br label %11
11: ; preds = %8, %11
%12 = phi i8* [ %10, %8 ], [ %21, %11 ]
%13 = phi i128 [ %6, %8 ], [ %15, %11 ]
%14 = freeze i128 %13
%15 = udiv i128 %14, 10
%16 = mul i128 %15, 10
%17 = sub i128 %14, %16
%18 = trunc i128 %17 to i64
%19 = getelementptr inbounds [11 x i8], [11 x i8]* @.str.1, i64 0, i64 %18
%20 = load i8, i8* %19
%21 = getelementptr inbounds i8, i8* %12, i64 -1
store i8 %20, i8* %21
%22 = icmp ult i128 %13, 10
br i1 %22, label %23, label %11
23: ; preds = %11, %2
%24 = phi i8* [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), %2 ], [ %9, %11 ]
ret i8* %24
}