#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 */
typedef int ptrdiff_t;

/* Variables and functions */
int /*<<< orphan*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ printf(char *, ...);
size_t strlen(char *);
int main(void) {
  char *s, *buf;
  size_t n;
  if ((buf = malloc(32)) == NULL)
    return (255);
  s = (void *)((((ptrdiff_t)buf + 15) & ~15) + 12);
  s[0] = '\001';
  s[1] = '\0';
  n = strlen(s);
  free(buf);
  if (n == 1) {
    printf("ok\n");
    return (0);
  }
  printf("broken: <%zu>\n", n);
  return (1);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x20,%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1173 <main+0x33>
movl   $0xff,-0x4(%rbp)
jmp    11e9 <main+0xa9>
mov    -0x18(%rbp),%rax
add    $0xf,%eax
and    $0xfffffff0,%eax
add    $0xc,%eax
cltq
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movb   $0x1,(%rax)
mov    -0x10(%rbp),%rax
movb   $0x0,0x1(%rax)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
cmpq   $0x1,-0x20(%rbp)
jne    11d0 <main+0x90>
lea    0xe43(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    11e9 <main+0xa9>
mov    -0x20(%rbp),%rsi
lea    0xe29(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    $0x20,%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     11a0 <main+0x50>
mov    %rax,%rbx
add    $0xf,%eax
movslq %eax,%rdi
and    $0xfffffffffffffff0,%rdi
movw   $0x1,0xc(%rdi)
or     $0xc,%rdi
call   1050 <strlen@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1030 <free@plt>
cmp    $0x1,%r14
jne    11a7 <main+0x57>
lea    0xe78(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
jmp    11bd <main+0x6d>
mov    $0xff,%eax
jmp    11bd <main+0x6d>
lea    0xe52(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [4 x i8] c"ok\0A\00"
@.str.1 = [15 x i8] c"broken: <%zu>\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i64
store i32 0, i32* %1
%5 = call i8* @malloc(i32 32)
store i8* %5, i8** %3
%6 = icmp eq i8* %5, null
br i1 %6, label %7, label %8
7: ; preds = %0
store i32 255, i32* %1
br label %31
8: ; preds = %0
%9 = load i8*, i8** %3
%10 = ptrtoint i8* %9 to i32
%11 = add nsw i32 %10, 15
%12 = and i32 %11, -16
%13 = add nsw i32 %12, 12
%14 = sext i32 %13 to i64
%15 = inttoptr i64 %14 to i8*
store i8* %15, i8** %2
%16 = load i8*, i8** %2
%17 = getelementptr inbounds i8, i8* %16, i64 0
store i8 1, i8* %17
%18 = load i8*, i8** %2
%19 = getelementptr inbounds i8, i8* %18, i64 1
store i8 0, i8* %19
%20 = load i8*, i8** %2
%21 = call i64 @strlen(i8* %20)
store i64 %21, i64* %4
%22 = load i8*, i8** %3
%23 = call i32 @free(i8* %22)
%24 = load i64, i64* %4
%25 = icmp eq i64 %24, 1
br i1 %25, label %26, label %28
26: ; preds = %8
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %1
br label %31
28: ; preds = %8
%29 = load i64, i64* %4
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i64 %29)
store i32 1, i32* %1
br label %31
31: ; preds = %28, %26, %7
%32 = load i32, i32* %1
ret i32 %32
}

LLVM IR (-O2):
@.str.1 = [15 x i8] c"broken: <%zu>\0A\00"
@str = [3 x i8] c"ok\00"
define i32 @main() {
%1 = tail call dereferenceable_or_null(32) i8* @malloc(i32 32)
%2 = icmp eq i8* %1, null
br i1 %2, label %19, label %3
3: ; preds = %0
%4 = ptrtoint i8* %1 to i64
%5 = shl i64 %4, 32
%6 = add i64 %5, 64424509440
%7 = ashr exact i64 %6, 32
%8 = and i64 %7, -16
%9 = or i64 %8, 12
%10 = inttoptr i64 %9 to i8*
store i8 1, i8* %10
%11 = getelementptr inbounds i8, i8* %10, i64 1
store i8 0, i8* %11
%12 = tail call i64 @strlen(i8* %10)
%13 = tail call i32 @free(i8* %1)
%14 = icmp eq i64 %12, 1
br i1 %14, label %15, label %17
15: ; preds = %3
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @str, i64 0, i64 0))
br label %19
17: ; preds = %3
%18 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i64 %12)
br label %19
19: ; preds = %0, %17, %15
%20 = phi i32 [ 0, %15 ], [ 1, %17 ], [ 255, %0 ]
ret i32 %20
}