#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 *, ...);
scalar_t__ strlen(char *);
char *padstr(char *s, int length) {
  static char buf[256];
  char fmt[10];
  sprintf(fmt, (int)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 i64 @strlen(i8* %7)
%9 = trunc i64 %8 to i32
%10 = load i32, i32* %4
%11 = icmp sgt i32 %9, %10
%12 = zext i1 %11 to i64
%13 = select i1 %11, 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)
%14 = load i32, i32* %4
%15 = call i32 (i8*, i8*, ...) @sprintf(i8* %6, i8* %13, i32 %14)
%16 = getelementptr inbounds [10 x i8], [10 x i8]* %5, i64 0, i64 0
%17 = load i8*, i8** %3
%18 = call i32 (i8*, i8*, ...) @sprintf(i8* getelementptr inbounds ([256 x i8], [256 x i8]* @padstr.buf, i64 0, i64 0), i8* %16, i8* %17)
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 i64 @strlen(i8* dereferenceable(1) %0)
%6 = trunc i64 %5 to i32
%7 = icmp sgt i32 %6, %1
%8 = select i1 %7, 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)
%9 = call i32 (i8*, i8*, ...) @sprintf(i8* %4, i8* dereferenceable(1) %8, i32 %1)
%10 = 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)
}