#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*/ memset(char *, char, unsigned long);
int /*<<< orphan*/ strcpy(char *, char const *);
unsigned long strlen(char const *);
int str_to_str(char *buf, const char *str, unsigned long count) {
  unsigned long size;
  size = strlen(str);
  if (count < size)
    count = size;
  else
    memset(buf, ' ', count - size);
  strcpy(buf + (count - size), str);
  return count;
}


x86 assembly (clang, -O0):
<str_to_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    116c <str_to_str+0x3c>
mov    -0x20(%rbp),%rax
mov    %rax,-0x18(%rbp)
jmp    1182 <str_to_str+0x52>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rdx
sub    -0x20(%rbp),%rdx
mov    $0x20,%esi
call   1050 <memset@plt>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rax
sub    -0x20(%rbp),%rax
add    %rax,%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_to_str>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%r12
mov    %rsi,%rdi
call   1040 <strlen@plt>
cmp    %r15,%rax
jbe    1155 <str_to_str+0x25>
xor    %ebx,%ebx
mov    %rax,%r15
jmp    116b <str_to_str+0x3b>
mov    %r15,%rbx
sub    %rax,%rbx
mov    %r12,%rdi
mov    $0x20,%esi
mov    %rbx,%rdx
call   1050 <memset@plt>
add    %rbx,%r12
mov    %r12,%rdi
mov    %r14,%rsi
call   1030 <strcpy@plt>
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i32 @str_to_str(i8* %0, i8* %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
store i8* %0, i8** %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%8 = load i8*, i8** %5
%9 = call i64 @strlen(i8* %8)
store i64 %9, i64* %7
%10 = load i64, i64* %6
%11 = load i64, i64* %7
%12 = icmp ult i64 %10, %11
br i1 %12, label %13, label %15
13: ; preds = %3
%14 = load i64, i64* %7
store i64 %14, i64* %6
br label %21
15: ; preds = %3
%16 = load i8*, i8** %4
%17 = load i64, i64* %6
%18 = load i64, i64* %7
%19 = sub i64 %17, %18
%20 = call i32 @memset(i8* %16, i8 signext 32, i64 %19)
br label %21
21: ; preds = %15, %13
%22 = load i8*, i8** %4
%23 = load i64, i64* %6
%24 = load i64, i64* %7
%25 = sub i64 %23, %24
%26 = getelementptr inbounds i8, i8* %22, i64 %25
%27 = load i8*, i8** %5
%28 = call i32 @strcpy(i8* %26, i8* %27)
%29 = load i64, i64* %6
%30 = trunc i64 %29 to i32
ret i32 %30
}

LLVM IR (-O2):
define i32 @str_to_str(i8* %0, i8* %1, i64 %2) {
%4 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%5 = icmp ugt i64 %4, %2
br i1 %5, label %9, label %6
6: ; preds = %3
%7 = sub i64 %2, %4
%8 = tail call i32 @memset(i8* %0, i8 signext 32, i64 %7)
br label %9
9: ; preds = %3, %6
%10 = phi i64 [ %7, %6 ], [ 0, %3 ]
%11 = phi i64 [ %2, %6 ], [ %4, %3 ]
%12 = getelementptr inbounds i8, i8* %0, i64 %10
%13 = tail call i32 @strcpy(i8* %12, i8* %1)
%14 = trunc i64 %11 to i32
ret i32 %14
}