#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 */
scalar_t__ malloc(int);
char *repeat_string(char *, int);
int /*<<< orphan*/ strcat(char *, char *);
int strlen(char *);
char *right_align_string(char *str, int width) {
  int str_length = strlen(str);
  int left_padding = width - str_length;
  if (left_padding < 0) {
    left_padding = 0;
  }
  char *left_string = repeat_string(" ", left_padding);
  char *final = (char *)malloc(1 + width);
  strcat(final, left_string);
  strcat(final, str);
  return final;
}


x86 assembly (clang, -O0):
<right_align_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%eax
sub    -0x10(%rbp),%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1175 <right_align_string+0x35>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%esi
lea    0xe81(%rip),%rdi
call   1040 <repeat_string@plt>
mov    %rax,-0x20(%rbp)
mov    -0xc(%rbp),%edi
add    $0x1,%edi
call   1050 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1060 <strcat@plt>
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1060 <strcat@plt>
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<right_align_string>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%ebp
mov    %rdi,%r14
call   1030 <strlen@plt>
xor    %ecx,%ecx
mov    %ebp,%esi
sub    %eax,%esi
cmovs  %ecx,%esi
lea    0xea2(%rip),%rdi
call   1040 <repeat_string@plt>
mov    %rax,%rbx
add    $0x1,%ebp
mov    %ebp,%edi
call   1050 <malloc@plt>
mov    %rax,%rbp
mov    %rax,%rdi
mov    %rbx,%rsi
call   1060 <strcat@plt>
mov    %rbp,%rdi
mov    %r14,%rsi
call   1060 <strcat@plt>
mov    %rbp,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c" \00"
define i8* @right_align_string(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
store i8* %0, i8** %3
store i32 %1, i32* %4
%9 = load i8*, i8** %3
%10 = call i32 @strlen(i8* %9)
store i32 %10, i32* %5
%11 = load i32, i32* %4
%12 = load i32, i32* %5
%13 = sub nsw i32 %11, %12
store i32 %13, i32* %6
%14 = load i32, i32* %6
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %2
store i32 0, i32* %6
br label %17
17: ; preds = %16, %2
%18 = load i32, i32* %6
%19 = call i8* @repeat_string(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 %18)
store i8* %19, i8** %7
%20 = load i32, i32* %4
%21 = add nsw i32 1, %20
%22 = call i64 @malloc(i32 %21)
%23 = inttoptr i64 %22 to i8*
store i8* %23, i8** %8
%24 = load i8*, i8** %8
%25 = load i8*, i8** %7
%26 = call i32 @strcat(i8* %24, i8* %25)
%27 = load i8*, i8** %8
%28 = load i8*, i8** %3
%29 = call i32 @strcat(i8* %27, i8* %28)
%30 = load i8*, i8** %8
ret i8* %30
}

LLVM IR (-O2):
@.str = [2 x i8] c" \00"
define i8* @right_align_string(i8* %0, i32 %1) {
%3 = tail call i32 @strlen(i8* %0)
%4 = sub nsw i32 %1, %3
%5 = icmp sgt i32 %4, 0
%6 = select i1 %5, i32 %4, i32 0
%7 = tail call i8* @repeat_string(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = add nsw i32 %1, 1
%9 = tail call i64 @malloc(i32 %8)
%10 = inttoptr i64 %9 to i8*
%11 = tail call i32 @strcat(i8* %10, i8* %7)
%12 = tail call i32 @strcat(i8* %10, i8* %0)
ret i8* %10
}