#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 *calloc(size_t, int);
int /*<<< orphan*/ err(int, char *);
char *alloc_string(size_t size, size_t misalignment) {
  size_t cacheline = 64;
  size_t adjusted = cacheline + misalignment + size + 1;
  char *buf = calloc(adjusted, 1);
  if (buf == NULL) {
    err(1, "calloc");
  }
  uintptr_t cacheline_mask = cacheline - 1U;
  buf = (char *)(((uintptr_t)buf + cacheline_mask) & ~cacheline_mask);
  buf += misalignment;
  buf[size] = '\0';
  return buf;
}


x86 assembly (clang, -O0):
<alloc_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x40,-0x18(%rbp)
mov    -0x18(%rbp),%rax
add    -0x10(%rbp),%rax
add    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    $0x1,%esi
call   1030 <calloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    117a <alloc_string+0x5a>
mov    $0x1,%edi
lea    0xe8b(%rip),%rsi
call   1040 <err@plt>
mov    -0x18(%rbp),%rax
sub    $0x1,%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
add    -0x30(%rbp),%rax
mov    -0x30(%rbp),%rcx
xor    $0xffffffffffffffff,%rcx
and    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    -0x8(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<alloc_string>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
add    %rsi,%rdi
add    $0x41,%rdi
mov    $0x1,%esi
call   1030 <calloc@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1155 <alloc_string+0x35>
lea    0xeb5(%rip),%rsi
mov    $0x1,%edi
call   1040 <err@plt>
add    $0x3f,%rbx
and    $0xffffffffffffffc0,%rbx
add    %r14,%rbx
movb   $0x0,(%r15,%rbx,1)
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [7 x i8] c"calloc\00"
define i8* @alloc_string(i64 %0, i64 %1) {
%3 = alloca i64
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i64
store i64 %0, i64* %3
store i64 %1, i64* %4
store i64 64, i64* %5
%9 = load i64, i64* %5
%10 = load i64, i64* %4
%11 = add i64 %9, %10
%12 = load i64, i64* %3
%13 = add i64 %11, %12
%14 = add i64 %13, 1
store i64 %14, i64* %6
%15 = load i64, i64* %6
%16 = call i8* @calloc(i64 %15, i32 1)
store i8* %16, i8** %7
%17 = load i8*, i8** %7
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %21
19: ; preds = %2
%20 = call i32 @err(i32 1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %21
21: ; preds = %19, %2
%22 = load i64, i64* %5
%23 = sub i64 %22, 1
store i64 %23, i64* %8
%24 = load i8*, i8** %7
%25 = ptrtoint i8* %24 to i64
%26 = load i64, i64* %8
%27 = add i64 %25, %26
%28 = load i64, i64* %8
%29 = xor i64 %28, -1
%30 = and i64 %27, %29
%31 = inttoptr i64 %30 to i8*
store i8* %31, i8** %7
%32 = load i64, i64* %4
%33 = load i8*, i8** %7
%34 = getelementptr inbounds i8, i8* %33, i64 %32
store i8* %34, i8** %7
%35 = load i8*, i8** %7
%36 = load i64, i64* %3
%37 = getelementptr inbounds i8, i8* %35, i64 %36
store i8 0, i8* %37
%38 = load i8*, i8** %7
ret i8* %38
}

LLVM IR (-O2):
@.str = [7 x i8] c"calloc\00"
define i8* @alloc_string(i64 %0, i64 %1) {
%3 = add i64 %0, 65
%4 = add i64 %3, %1
%5 = tail call i8* @calloc(i64 %4, i32 1)
%6 = icmp eq i8* %5, null
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %9
9: ; preds = %7, %2
%10 = ptrtoint i8* %5 to i64
%11 = add i64 %10, 63
%12 = and i64 %11, -64
%13 = inttoptr i64 %12 to i8*
%14 = getelementptr inbounds i8, i8* %13, i64 %1
%15 = getelementptr inbounds i8, i8* %14, i64 %0
store i8 0, i8* %15
ret i8* %14
}