#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*/ CUTIL_GenerateRandomString(char *, size_t);
size_t strlen(char const *);
int /*<<< orphan*/ strncpy(char *, char const *, size_t);
char *CUTIL_GenerateRandomStringWithSurfix(const char *surfix, char *buf,
                                           size_t size) {
  if (surfix) {
    size_t length = strlen(surfix);
    if (length > size) {
      return NULL;
    }
    strncpy(buf + size - length, surfix, length);
    CUTIL_GenerateRandomString(buf, size - length);
  } else {
    CUTIL_GenerateRandomString(buf, size);
  }
  return buf;
}


x86 assembly (clang, -O0):
<CUTIL_GenerateRandomStringWithSurfix>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11ab <CUTIL_GenerateRandomStringWithSurfix+0x7b>
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
jbe    1177 <CUTIL_GenerateRandomStringWithSurfix+0x47>
movq   $0x0,-0x8(%rbp)
jmp    11c0 <CUTIL_GenerateRandomStringWithSurfix+0x90>
mov    -0x18(%rbp),%rdi
add    -0x20(%rbp),%rdi
xor    %eax,%eax
sub    -0x28(%rbp),%rax
add    %rax,%rdi
mov    -0x10(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1030 <strncpy@plt>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
sub    -0x28(%rbp),%rsi
call   1040 <CUTIL_GenerateRandomString@plt>
jmp    11b8 <CUTIL_GenerateRandomStringWithSurfix+0x88>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1040 <CUTIL_GenerateRandomString@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<CUTIL_GenerateRandomStringWithSurfix>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%r14
test   %rdi,%rdi
je     116d <CUTIL_GenerateRandomStringWithSurfix+0x3d>
mov    %rdi,%rbx
call   1050 <strlen@plt>
cmp    %r15,%rax
jbe    1155 <CUTIL_GenerateRandomStringWithSurfix+0x25>
xor    %r14d,%r14d
jmp    1178 <CUTIL_GenerateRandomStringWithSurfix+0x48>
mov    %rax,%r12
lea    (%r14,%r15,1),%rdi
sub    %rax,%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
call   1030 <strncpy@plt>
sub    %r12,%r15
mov    %r14,%rdi
mov    %r15,%rsi
call   1040 <CUTIL_GenerateRandomString@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @CUTIL_GenerateRandomStringWithSurfix(i8* %0, i8* %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
store i8* %0, i8** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%9 = load i8*, i8** %5
%10 = icmp ne i8* %9, null
br i1 %10, label %11, label %33
11: ; preds = %3
%12 = load i8*, i8** %5
%13 = call i64 @strlen(i8* %12)
store i64 %13, i64* %8
%14 = load i64, i64* %8
%15 = load i64, i64* %7
%16 = icmp ugt i64 %14, %15
br i1 %16, label %17, label %18
17: ; preds = %11
store i8* null, i8** %4
br label %39
18: ; preds = %11
%19 = load i8*, i8** %6
%20 = load i64, i64* %7
%21 = getelementptr inbounds i8, i8* %19, i64 %20
%22 = load i64, i64* %8
%23 = sub i64 0, %22
%24 = getelementptr inbounds i8, i8* %21, i64 %23
%25 = load i8*, i8** %5
%26 = load i64, i64* %8
%27 = call i32 @strncpy(i8* %24, i8* %25, i64 %26)
%28 = load i8*, i8** %6
%29 = load i64, i64* %7
%30 = load i64, i64* %8
%31 = sub i64 %29, %30
%32 = call i32 @CUTIL_GenerateRandomString(i8* %28, i64 %31)
br label %37
33: ; preds = %3
%34 = load i8*, i8** %6
%35 = load i64, i64* %7
%36 = call i32 @CUTIL_GenerateRandomString(i8* %34, i64 %35)
br label %37
37: ; preds = %33, %18
%38 = load i8*, i8** %6
store i8* %38, i8** %4
br label %39
39: ; preds = %37, %17
%40 = load i8*, i8** %4
ret i8* %40
}

LLVM IR (-O2):
define i8* @CUTIL_GenerateRandomStringWithSurfix(i8* %0, i8* %1, i64 %2) {
%4 = icmp eq i8* %0, null
br i1 %4, label %14, label %5
5: ; preds = %3
%6 = tail call i64 @strlen(i8* %0)
%7 = icmp ugt i64 %6, %2
br i1 %7, label %17, label %8
8: ; preds = %5
%9 = getelementptr inbounds i8, i8* %1, i64 %2
%10 = sub i64 0, %6
%11 = getelementptr inbounds i8, i8* %9, i64 %10
%12 = tail call i32 @strncpy(i8* %11, i8* %0, i64 %6)
%13 = sub i64 %2, %6
br label %14
14: ; preds = %3, %8
%15 = phi i64 [ %13, %8 ], [ %2, %3 ]
%16 = tail call i32 @CUTIL_GenerateRandomString(i8* %1, i64 %15)
br label %17
17: ; preds = %14, %5
%18 = phi i8* [ null, %5 ], [ %1, %14 ]
ret i8* %18
}