#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 rand();
int sprintf(char *, char *, int, int, char const);
int build_packet(char *buffer) {
  static const char types[] = {'g', 'c', 'C', 'h'};
  int stat = rand() % 128;
  return sprintf(buffer, "github.test.packet.%d:%d|%c", stat, rand() % 1024,
                 types[(stat * 0x37) % 4]);
}


x86 assembly (clang, -O0):
<build_packet>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x0,%al
call   1040 <rand@plt>
mov    $0x80,%ecx
cltd
idiv   %ecx
mov    %edx,-0xc(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    $0x0,%al
call   1040 <rand@plt>
mov    -0x18(%rbp),%rdi
mov    $0x400,%ecx
cltd
idiv   %ecx
mov    %edx,%ecx
imul   $0x37,-0xc(%rbp),%eax
mov    $0x4,%esi
cltd
idiv   %esi
mov    %edx,%eax
mov    -0x10(%rbp),%edx
movslq %eax,%r8
lea    0xe88(%rip),%rsi
lea    0xe7d(%rip),%rax
movsbl (%rax,%r8,1),%r8d
call   1030 <sprintf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<build_packet>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
xor    %eax,%eax
call   1040 <rand@plt>
mov    %eax,%ebx
lea    0x7f(%rbx),%eax
test   %ebx,%ebx
cmovns %ebx,%eax
and    $0xffffff80,%eax
sub    %eax,%ebx
xor    %eax,%eax
call   1040 <rand@plt>
lea    0x3ff(%rax),%ecx
test   %eax,%eax
cmovns %eax,%ecx
and    $0xfffffc00,%ecx
sub    %ecx,%eax
imul   $0x37,%ebx,%ecx
lea    0x3(%rcx),%edx
test   %cx,%cx
cmovns %ecx,%edx
and    $0xfffffffc,%edx
sub    %edx,%ecx
movswq %cx,%rcx
lea    0xe8d(%rip),%rdx
movsbl (%rcx,%rdx,1),%r8d
lea    0xe85(%rip),%rsi
mov    %r14,%rdi
mov    %ebx,%edx
mov    %eax,%ecx
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1030 <sprintf@plt>

LLVM IR (-O0):
@build_packet.types = internal constant [4 x i8] c"gcCh"
@.str = [28 x i8] c"github.test.packet.%d:%d|%c\00"
define i32 @build_packet(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
store i8* %0, i8** %2
%4 = call i32 (...) @rand()
%5 = srem i32 %4, 128
store i32 %5, i32* %3
%6 = load i8*, i8** %2
%7 = load i32, i32* %3
%8 = call i32 (...) @rand()
%9 = srem i32 %8, 1024
%10 = load i32, i32* %3
%11 = mul nsw i32 %10, 55
%12 = srem i32 %11, 4
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds [4 x i8], [4 x i8]* @build_packet.types, i64 0, i64 %13
%15 = load i8, i8* %14
%16 = call i32 @sprintf(i8* %6, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 %7, i32 %9, i8 signext %15)
ret i32 %16
}

LLVM IR (-O2):
@build_packet.types = internal unnamed_addr constant [4 x i8] c"gcCh"
@.str = [28 x i8] c"github.test.packet.%d:%d|%c\00"
define i32 @build_packet(i8* nocapture writeonly %0) {
%2 = tail call i32 (...) @rand()
%3 = srem i32 %2, 128
%4 = tail call i32 (...) @rand()
%5 = srem i32 %4, 1024
%6 = trunc i32 %3 to i16
%7 = mul nsw i16 %6, 55
%8 = srem i16 %7, 4
%9 = sext i16 %8 to i64
%10 = getelementptr inbounds [4 x i8], [4 x i8]* @build_packet.types, i64 0, i64 %9
%11 = load i8, i8* %10
%12 = tail call i32 @sprintf(i8* dereferenceable(1) %0, i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 %3, i32 %5, i8 signext %11)
ret i32 %12
}