int generate_data(buf, rec_no, test_var)
char *buf;
int rec_no, test_var;
{
  const char *str = "abcdefghijklmnopqrst";
  int len = (int)strlen(str);
  if (test_var == 1)
    len = rand() % (len - 2) + 1;
  (void)sprintf(buf, "%04d_%*s", rec_no, len, str);
  return (0);
}


x86 assembly (clang, -O0):
<generate_data>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
lea    0xeb7(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x1,-0x10(%rbp)
jne    1179 <generate_data+0x49>
mov    $0x0,%al
call   1050 <rand@plt>
mov    -0x1c(%rbp),%ecx
sub    $0x2,%ecx
cltd
idiv   %ecx
add    $0x1,%edx
mov    %edx,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%edx
mov    -0x1c(%rbp),%ecx
mov    -0x18(%rbp),%r8
lea    0xe87(%rip),%rsi
mov    $0x0,%al
call   1040 <sprintf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<generate_data>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
mov    $0x14,%ecx
cmp    $0x1,%edx
jne    115b <generate_data+0x3b>
xor    %eax,%eax
call   1040 <rand@plt>
cltq
imul   $0x38e38e39,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x22,%rcx
add    %edx,%ecx
add    %ecx,%ecx
lea    (%rcx,%rcx,8),%ecx
neg    %ecx
add    %eax,%ecx
add    $0x1,%ecx
lea    0xeb3(%rip),%rsi
lea    0xe97(%rip),%r8
mov    %rbx,%rdi
mov    %ebp,%edx
xor    %eax,%eax
call   1030 <sprintf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [21 x i8] c"abcdefghijklmnopqrst\00"
@.str.1 = [9 x i8] c"%04d_%*s\00"
define i32 @generate_data(i8* %0, i32 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
store i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8** %7
%9 = load i8*, i8** %7
%10 = call i64 @strlen(i8* %9)
%11 = trunc i64 %10 to i32
store i32 %11, i32* %8
%12 = load i32, i32* %6
%13 = icmp eq i32 %12, 1
br i1 %13, label %14, label %20
14: ; preds = %3
%15 = call i32 (...) @rand()
%16 = load i32, i32* %8
%17 = sub nsw i32 %16, 2
%18 = srem i32 %15, %17
%19 = add nsw i32 %18, 1
store i32 %19, i32* %8
br label %20
20: ; preds = %14, %3
%21 = load i8*, i8** %4
%22 = load i32, i32* %5
%23 = load i32, i32* %8
%24 = load i8*, i8** %7
%25 = call i32 (i8*, i8*, ...) @sprintf(i8* %21, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %22, i32 %23, i8* %24)
ret i32 0
}

LLVM IR (-O2):
@.str = [21 x i8] c"abcdefghijklmnopqrst\00"
@.str.1 = [9 x i8] c"%04d_%*s\00"
define i32 @generate_data(i8* nocapture writeonly %0, i32 %1, i32 %2) {
%4 = icmp eq i32 %2, 1
br i1 %4, label %5, label %9
5: ; preds = %3
%6 = tail call i32 (...) @rand()
%7 = srem i32 %6, 18
%8 = add nsw i32 %7, 1
br label %9
9: ; preds = %5, %3
%10 = phi i32 [ %8, %5 ], [ 20, %3 ]
%11 = tail call i32 (i8*, i8*, ...) @sprintf(i8* dereferenceable(1) %0, i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %1, i32 %10, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
ret i32 0
}