#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 *generate_space(char, int);
char *my_strcat(char *, char *);
int my_strlen(char *);
char *generate_padded_chain(char *str, int left, char pad, int width) {
  width = width - my_strlen(str);
  char *new_pad = generate_space(pad, width);
  if (left) {
    str = my_strcat(str, new_pad);
  } else {
    str = my_strcat(new_pad, str);
  }
  return str;
}


x86 assembly (clang, -O0):
<generate_padded_chain>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %dl,%al
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %al,-0xd(%rbp)
mov    %ecx,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <my_strlen@plt>
mov    %eax,%ecx
mov    -0x24(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x14(%rbp)
mov    -0xd(%rbp),%al
mov    -0x14(%rbp),%esi
movsbl %al,%edi
call   1050 <generate_space@plt>
mov    %rax,-0x20(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     1192 <generate_padded_chain+0x62>
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1040 <my_strcat@plt>
mov    %rax,-0x8(%rbp)
jmp    11a3 <generate_padded_chain+0x73>
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1040 <my_strcat@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<generate_padded_chain>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %ecx,%ebp
mov    %edx,%r14d
mov    %esi,%r15d
mov    %rdi,%rbx
call   1030 <my_strlen@plt>
sub    %eax,%ebp
mov    %r14d,%edi
mov    %ebp,%esi
call   1050 <generate_space@plt>
test   %r15d,%r15d
je     1160 <generate_padded_chain+0x30>
mov    %rbx,%rdi
mov    %rax,%rsi
jmp    1166 <generate_padded_chain+0x36>
mov    %rax,%rdi
mov    %rbx,%rsi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <my_strcat@plt>

LLVM IR (-O0):
define i8* @generate_padded_chain(i8* %0, i32 %1, i8 signext %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8
%8 = alloca i32
%9 = alloca i8*
store i8* %0, i8** %5
store i32 %1, i32* %6
store i8 %2, i8* %7
store i32 %3, i32* %8
%10 = load i32, i32* %8
%11 = load i8*, i8** %5
%12 = call i32 @my_strlen(i8* %11)
%13 = sub nsw i32 %10, %12
store i32 %13, i32* %8
%14 = load i8, i8* %7
%15 = load i32, i32* %8
%16 = call i8* @generate_space(i8 signext %14, i32 %15)
store i8* %16, i8** %9
%17 = load i32, i32* %6
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %23
19: ; preds = %4
%20 = load i8*, i8** %5
%21 = load i8*, i8** %9
%22 = call i8* @my_strcat(i8* %20, i8* %21)
store i8* %22, i8** %5
br label %27
23: ; preds = %4
%24 = load i8*, i8** %9
%25 = load i8*, i8** %5
%26 = call i8* @my_strcat(i8* %24, i8* %25)
store i8* %26, i8** %5
br label %27
27: ; preds = %23, %19
%28 = load i8*, i8** %5
ret i8* %28
}

LLVM IR (-O2):
define i8* @generate_padded_chain(i8* %0, i32 %1, i8 signext %2, i32 %3) {
%5 = tail call i32 @my_strlen(i8* %0)
%6 = sub nsw i32 %3, %5
%7 = tail call i8* @generate_space(i8 signext %2, i32 %6)
%8 = icmp eq i32 %1, 0
br i1 %8, label %11, label %9
9: ; preds = %4
%10 = tail call i8* @my_strcat(i8* %0, i8* %7)
br label %13
11: ; preds = %4
%12 = tail call i8* @my_strcat(i8* %7, i8* %0)
br label %13
13: ; preds = %11, %9
%14 = phi i8* [ %10, %9 ], [ %12, %11 ]
ret i8* %14
}