#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*/ sprintf(char *, char *, ...);
int strlen(char *);
char *padstr(char *s, int length) {
  static char buf[256];
  char fmt[10];
  sprintf(fmt, strlen(s) > length ? "%%.%ds" : "%%-%ds", length);
  sprintf(buf, fmt, s);
  return buf;
}


x86 assembly (clang, -O0):
<padstr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    -0x16(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x20(%rbp),%rdi
mov    %eax,%ecx
mov    -0xc(%rbp),%edx
lea    0xeb7(%rip),%rsi
lea    0xea9(%rip),%rax
cmp    %edx,%ecx
cmovg  %rax,%rsi
mov    -0xc(%rbp),%edx
mov    $0x0,%al
call   1040 <sprintf@plt>
lea    -0x16(%rbp),%rsi
mov    -0x8(%rbp),%rdx
lea    0x2eca(%rip),%rdi
mov    $0x0,%al
call   1040 <sprintf@plt>
lea    0x2ebc(%rip),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<padstr>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebx
mov    %rdi,%r14
call   1030 <strlen@plt>
cmp    %ebx,%eax
lea    0xec4(%rip),%rax
lea    0xec4(%rip),%rsi
cmovg  %rax,%rsi
lea    0x6(%rsp),%r15
mov    %r15,%rdi
mov    %ebx,%edx
xor    %eax,%eax
call   1040 <sprintf@plt>
lea    0x2ee1(%rip),%rbx
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1040 <sprintf@plt>
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@padstr.buf = internal global [256 x i8] zeroinitializer
@.str = [7 x i8] c"%%.%ds\00"
@.str.1 = [7 x i8] c"%%-%ds\00"
define i8* @padstr(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca [10 x i8]
store i8* %0, i8** %3
store i32 %1, i32* %4
%6 = getelementptr inbounds [10 x i8], [10 x i8]* %5, i64 0, i64 0
%7 = load i8*, i8** %3
%8 = call i32 @strlen(i8* %7)
%9 = load i32, i32* %4
%10 = icmp sgt i32 %8, %9
%11 = zext i1 %10 to i64
%12 = select i1 %10, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0)
%13 = load i32, i32* %4
%14 = call i32 (i8*, i8*, ...) @sprintf(i8* %6, i8* %12, i32 %13)
%15 = getelementptr inbounds [10 x i8], [10 x i8]* %5, i64 0, i64 0
%16 = load i8*, i8** %3
%17 = call i32 (i8*, i8*, ...) @sprintf(i8* getelementptr inbounds ([256 x i8], [256 x i8]* @padstr.buf, i64 0, i64 0), i8* %15, i8* %16)
ret i8* getelementptr inbounds ([256 x i8], [256 x i8]* @padstr.buf, i64 0, i64 0)
}

LLVM IR (-O2):
@padstr.buf = internal global [256 x i8] zeroinitializer
@.str = [7 x i8] c"%%.%ds\00"
@.str.1 = [7 x i8] c"%%-%ds\00"
define i8* @padstr(i8* %0, i32 %1) {
%3 = alloca [10 x i8]
%4 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 0
%5 = tail call i32 @strlen(i8* %0)
%6 = icmp sgt i32 %5, %1
%7 = select i1 %6, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0)
%8 = call i32 (i8*, i8*, ...) @sprintf(i8* %4, i8* dereferenceable(1) %7, i32 %1)
%9 = call i32 (i8*, i8*, ...) @sprintf(i8* dereferenceable(1) getelementptr inbounds ([256 x i8], [256 x i8]* @padstr.buf, i64 0, i64 0), i8* %4, i8* %0)
ret i8* getelementptr inbounds ([256 x i8], [256 x i8]* @padstr.buf, i64 0, i64 0)
}