#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 *makeSpaceStr(int);
char *malloc(int);
int /*<<< orphan*/ sprintf(char *, char *, ...);
int strlen(char *);
char *justifyCenter(char *str, int msgLength) {
  int length = msgLength * sizeof(char) + 1;
  char *msg = malloc(length);
  char *format = malloc(length);
  sprintf(format, "%%%ds%s",
          (msgLength / 2 + strlen(str) / 2) + (msgLength % 2),
          makeSpaceStr(msgLength / 2 - strlen(str) / 2));
  sprintf(msg, format, str);
  return msg;
}


x86 assembly (clang, -O0):
<justifyCenter>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movslq -0xc(%rbp),%rax
shl    $0x0,%rax
add    $0x1,%rax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x3c(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x3c(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x38(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    -0x38(%rbp),%eax
add    %edx,%eax
mov    %eax,-0x24(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x34(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x34(%rbp),%edi
mov    $0x2,%ecx
cltd
idiv   %ecx
sub    %eax,%edi
call   1030 <makeSpaceStr@plt>
mov    -0x30(%rbp),%rdi
mov    -0x24(%rbp),%edx
mov    %rax,%rcx
lea    0xe0c(%rip),%rsi
mov    $0x0,%al
call   1060 <sprintf@plt>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x8(%rbp),%rdx
mov    $0x0,%al
call   1060 <sprintf@plt>
mov    -0x18(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<justifyCenter>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %esi,%ebx
mov    %rdi,%r15
lea    0x1(%rbx),%ebp
mov    %ebp,%edi
call   1050 <malloc@plt>
mov    %rax,%r14
mov    %ebp,%edi
call   1050 <malloc@plt>
mov    %rax,%r12
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
shr    $0x1f,%ecx
add    %eax,%ecx
sar    %ecx
mov    %ebx,%ebp
shr    $0x1f,%ebp
add    %ebx,%ebp
mov    %ebp,%eax
and    $0xfffffffe,%eax
sub    %eax,%ebx
sar    %ebp
add    %ebp,%ebx
add    %ecx,%ebx
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
shr    $0x1f,%ecx
add    %eax,%ecx
sar    %ecx
sub    %ecx,%ebp
mov    %ebp,%edi
call   1030 <makeSpaceStr@plt>
lea    0xe56(%rip),%rsi
mov    %r12,%rdi
mov    %ebx,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1060 <sprintf@plt>
mov    %r14,%rdi
mov    %r12,%rsi
mov    %r15,%rdx
xor    %eax,%eax
call   1060 <sprintf@plt>
mov    %r14,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [8 x i8] c"%%%ds%s\00"
define i8* @justifyCenter(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
store i8* %0, i8** %3
store i32 %1, i32* %4
%8 = load i32, i32* %4
%9 = sext i32 %8 to i64
%10 = mul i64 %9, 1
%11 = add i64 %10, 1
%12 = trunc i64 %11 to i32
store i32 %12, i32* %5
%13 = load i32, i32* %5
%14 = call i8* @malloc(i32 %13)
store i8* %14, i8** %6
%15 = load i32, i32* %5
%16 = call i8* @malloc(i32 %15)
store i8* %16, i8** %7
%17 = load i8*, i8** %7
%18 = load i32, i32* %4
%19 = sdiv i32 %18, 2
%20 = load i8*, i8** %3
%21 = call i32 @strlen(i8* %20)
%22 = sdiv i32 %21, 2
%23 = add nsw i32 %19, %22
%24 = load i32, i32* %4
%25 = srem i32 %24, 2
%26 = add nsw i32 %23, %25
%27 = load i32, i32* %4
%28 = sdiv i32 %27, 2
%29 = load i8*, i8** %3
%30 = call i32 @strlen(i8* %29)
%31 = sdiv i32 %30, 2
%32 = sub nsw i32 %28, %31
%33 = call i8* @makeSpaceStr(i32 %32)
%34 = call i32 (i8*, i8*, ...) @sprintf(i8* %17, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %26, i8* %33)
%35 = load i8*, i8** %6
%36 = load i8*, i8** %7
%37 = load i8*, i8** %3
%38 = call i32 (i8*, i8*, ...) @sprintf(i8* %35, i8* %36, i8* %37)
%39 = load i8*, i8** %6
ret i8* %39
}

LLVM IR (-O2):
@.str = [8 x i8] c"%%%ds%s\00"
define noalias i8* @justifyCenter(i8* %0, i32 %1) {
%3 = add i32 %1, 1
%4 = tail call i8* @malloc(i32 %3)
%5 = tail call i8* @malloc(i32 %3)
%6 = sdiv i32 %1, 2
%7 = tail call i32 @strlen(i8* %0)
%8 = sdiv i32 %7, 2
%9 = srem i32 %1, 2
%10 = add nsw i32 %9, %6
%11 = add i32 %10, %8
%12 = tail call i32 @strlen(i8* %0)
%13 = sdiv i32 %12, -2
%14 = add nsw i32 %13, %6
%15 = tail call i8* @makeSpaceStr(i32 %14)
%16 = tail call i32 (i8*, i8*, ...) @sprintf(i8* dereferenceable(1) %5, i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %11, i8* %15)
%17 = tail call i32 (i8*, i8*, ...) @sprintf(i8* dereferenceable(1) %4, i8* dereferenceable(1) %5, i8* %0)
ret i8* %4
}