#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*/ memcpy(char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ memset(char *, char, int);
int /*<<< orphan*/ strlen(char *);
int str_pad(char *s, int len, char c, char *to) {
  if (!s || !c || !to)
    return -1;
  memset(to, c, len);
  memcpy(to, s, strlen(s));
  return 0;
}


x86 assembly (clang, -O0):
<str_pad>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %dl,%al
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %al,-0x15(%rbp)
mov    %rcx,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1168 <str_pad+0x38>
cmpb   $0x0,-0x15(%rbp)
je     1168 <str_pad+0x38>
cmpq   $0x0,-0x20(%rbp)
jne    1174 <str_pad+0x44>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b5 <str_pad+0x85>
mov    -0x20(%rbp),%rdi
mov    -0x15(%rbp),%al
mov    -0x14(%rbp),%edx
movsbl %al,%esi
call   1040 <memset@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    %eax,%edx
call   1050 <memcpy@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_pad>:
push   %r14
push   %rbx
push   %rax
mov    $0xffffffff,%eax
test   %rdi,%rdi
je     1173 <str_pad+0x43>
test   %dl,%dl
je     1173 <str_pad+0x43>
mov    %rcx,%r14
test   %rcx,%rcx
je     1173 <str_pad+0x43>
mov    %esi,%ecx
mov    %rdi,%rbx
movsbl %dl,%esi
mov    %r14,%rdi
mov    %ecx,%edx
call   1040 <memset@plt>
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1050 <memcpy@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
define i32 @str_pad(i8* %0, i32 %1, i8 signext %2, i8* %3) {
%5 = icmp ne i8* %0, null
%6 = icmp ne i8 %2, 0
%7 = and i1 %5, %6
%8 = icmp ne i8* %3, null
%9 = and i1 %7, %8
br i1 %9, label %10, label %14
10: ; preds = %4
%11 = tail call i32 @memset(i8* %3, i8 signext %2, i32 %1)
%12 = tail call i32 @strlen(i8* %0)
%13 = tail call i32 @memcpy(i8* %3, i8* %0, i32 %12)
br label %14
14: ; preds = %4, %10
%15 = phi i32 [ 0, %10 ], [ -1, %4 ]
ret i32 %15
}