#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 */
char *malloc(unsigned int);
int /*<<< orphan*/ strcpy(char *, char *);
unsigned int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, unsigned int);
char *string_pad_right(char *old, unsigned int length) {
  unsigned int i;
  char *s = malloc(length + 1);
  if (!s)
    return 0;
  if (strlen(old) <= length) {
    strcpy(s, old);
    for (i = strlen(old); i < length; i++) {
      s[i] = ' ';
    }
  } else {
    strncpy(s, old, length);
  }
  s[length] = 0;
  return s;
}


x86 assembly (clang, -O0):
<string_pad_right>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x14(%rbp),%edi
add    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1176 <string_pad_right+0x36>
movq   $0x0,-0x8(%rbp)
jmp    11ee <string_pad_right+0xae>
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
cmp    -0x14(%rbp),%eax
ja     11cb <string_pad_right+0x8b>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jae    11c6 <string_pad_right+0x86>
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%ecx
movb   $0x20,(%rax,%rcx,1)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    11a1 <string_pad_right+0x61>
jmp    11db <string_pad_right+0x9b>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <strncpy@plt>
mov    -0x20(%rbp),%rax
mov    -0x14(%rbp),%ecx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<string_pad_right>:
push   %r15
push   %r14
push   %rbx
mov    %esi,%r14d
mov    %rdi,%r15
lea    0x1(%r14),%edi
call   1070 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11c3 <string_pad_right+0x73>
mov    %r15,%rdi
call   1050 <strlen@plt>
mov    %rbx,%rdi
cmp    %r14d,%eax
jbe    1189 <string_pad_right+0x39>
mov    %r15,%rsi
mov    %r14d,%edx
call   1030 <strncpy@plt>
jmp    11bc <string_pad_right+0x6c>
mov    %r15,%rsi
call   1040 <strcpy@plt>
mov    %r15,%rdi
call   1050 <strlen@plt>
cmp    %r14d,%eax
jae    11bc <string_pad_right+0x6c>
mov    %eax,%ecx
mov    %rbx,%rdi
add    %rcx,%rdi
not    %eax
add    %r14d,%eax
add    $0x1,%rax
mov    $0x20,%esi
mov    %rax,%rdx
call   1060 <memset@plt>
mov    %r14d,%eax
movb   $0x0,(%rbx,%rax,1)
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @string_pad_right(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
store i8* %0, i8** %4
store i32 %1, i32* %5
%8 = load i32, i32* %5
%9 = add i32 %8, 1
%10 = call i8* @malloc(i32 %9)
store i8* %10, i8** %7
%11 = load i8*, i8** %7
%12 = icmp ne i8* %11, null
br i1 %12, label %14, label %13
13: ; preds = %2
store i8* null, i8** %3
br label %49
14: ; preds = %2
%15 = load i8*, i8** %4
%16 = call i32 @strlen(i8* %15)
%17 = load i32, i32* %5
%18 = icmp ule i32 %16, %17
br i1 %18, label %19, label %38
19: ; preds = %14
%20 = load i8*, i8** %7
%21 = load i8*, i8** %4
%22 = call i32 @strcpy(i8* %20, i8* %21)
%23 = load i8*, i8** %4
%24 = call i32 @strlen(i8* %23)
store i32 %24, i32* %6
br label %25
25: ; preds = %34, %19
%26 = load i32, i32* %6
%27 = load i32, i32* %5
%28 = icmp ult i32 %26, %27
br i1 %28, label %29, label %37
29: ; preds = %25
%30 = load i8*, i8** %7
%31 = load i32, i32* %6
%32 = zext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %30, i64 %32
store i8 32, i8* %33
br label %34
34: ; preds = %29
%35 = load i32, i32* %6
%36 = add i32 %35, 1
store i32 %36, i32* %6
br label %25
37: ; preds = %25
br label %43
38: ; preds = %14
%39 = load i8*, i8** %7
%40 = load i8*, i8** %4
%41 = load i32, i32* %5
%42 = call i32 @strncpy(i8* %39, i8* %40, i32 %41)
br label %43
43: ; preds = %38, %37
%44 = load i8*, i8** %7
%45 = load i32, i32* %5
%46 = zext i32 %45 to i64
%47 = getelementptr inbounds i8, i8* %44, i64 %46
store i8 0, i8* %47
%48 = load i8*, i8** %7
store i8* %48, i8** %3
br label %49
49: ; preds = %43, %13
%50 = load i8*, i8** %3
ret i8* %50
}

LLVM IR (-O2):
define i8* @string_pad_right(i8* %0, i32 %1) {
%3 = add i32 %1, 1
%4 = tail call i8* @malloc(i32 %3)
%5 = icmp eq i8* %4, null
br i1 %5, label %25, label %6
6: ; preds = %2
%7 = tail call i32 @strlen(i8* %0)
%8 = icmp ugt i32 %7, %1
br i1 %8, label %20, label %9
9: ; preds = %6
%10 = tail call i32 @strcpy(i8* %4, i8* %0)
%11 = tail call i32 @strlen(i8* %0)
%12 = icmp ult i32 %11, %1
br i1 %12, label %13, label %22
13: ; preds = %9
%14 = zext i32 %11 to i64
%15 = getelementptr i8, i8* %4, i64 %14
%16 = xor i32 %11, -1
%17 = add i32 %16, %1
%18 = zext i32 %17 to i64
%19 = add nuw nsw i64 %18, 1
br label %22
20: ; preds = %6
%21 = tail call i32 @strncpy(i8* %4, i8* %0, i32 %1)
br label %22
22: ; preds = %13, %9, %20
%23 = zext i32 %1 to i64
%24 = getelementptr inbounds i8, i8* %4, i64 %23
store i8 0, i8* %24
br label %25
25: ; preds = %2, %22
ret i8* %4
}