#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 /*<<< orphan*/ u_char;

/* Variables and functions */
int /*<<< orphan*/ NS_PUT16(size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ errno;
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *, char *);
char *strerror(int /*<<< orphan*/);
char *newstr(size_t len, int needpanic) {
  u_char *buf, *bp;
  assert(len <= 65536);
  buf = (u_char *)malloc(2 + len + 1);
  if (buf == NULL) {
    if (needpanic)
      if (0)
        printf("savestr: malloc failed (%s)", strerror(errno));
      else
        return (NULL);
  }
  bp = buf;
  NS_PUT16(len, bp);
  return ((char *)bp);
}


x86 assembly (clang, -O0):
<newstr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpq   $0x10000,-0x10(%rbp)
setbe  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x10(%rbp),%rax
add    $0x2,%rax
add    $0x1,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1192 <newstr+0x62>
cmpl   $0x0,-0x14(%rbp)
je     118d <newstr+0x5d>
movq   $0x0,-0x8(%rbp)
jmp    11af <newstr+0x7f>
jmp    1192 <newstr+0x62>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1030 <NS_PUT16@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<newstr>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%ebp
mov    %rdi,%r14
xor    %edi,%edi
cmp    $0x10001,%r14
setb   %dil
call   1040 <assert@plt>
lea    0x3(%r14),%edi
call   1050 <malloc@plt>
mov    %rax,%rbx
test   %ebp,%ebp
je     1164 <newstr+0x34>
test   %rbx,%rbx
jne    1164 <newstr+0x34>
xor    %ebx,%ebx
jmp    116f <newstr+0x3f>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1030 <NS_PUT16@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i32 0
define i8* @newstr(i64 %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32*
store i64 %0, i64* %4
store i32 %1, i32* %5
%8 = load i64, i64* %4
%9 = icmp ule i64 %8, 65536
%10 = zext i1 %9 to i32
%11 = call i32 @assert(i32 %10)
%12 = load i64, i64* %4
%13 = add i64 2, %12
%14 = add i64 %13, 1
%15 = trunc i64 %14 to i32
%16 = call i64 @malloc(i32 %15)
%17 = inttoptr i64 %16 to i32*
store i32* %17, i32** %6
%18 = load i32*, i32** %6
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %25
20: ; preds = %2
%21 = load i32, i32* %5
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %20
store i8* null, i8** %3
br label %32
24: ; preds = %20
br label %25
25: ; preds = %24, %2
%26 = load i32*, i32** %6
store i32* %26, i32** %7
%27 = load i64, i64* %4
%28 = load i32*, i32** %7
%29 = call i32 @NS_PUT16(i64 %27, i32* %28)
%30 = load i32*, i32** %7
%31 = bitcast i32* %30 to i8*
store i8* %31, i8** %3
br label %32
32: ; preds = %25, %23
%33 = load i8*, i8** %3
ret i8* %33
}

LLVM IR (-O2):
@errno = global i32 0
define i8* @newstr(i64 %0, i32 %1) {
%3 = icmp ult i64 %0, 65537
%4 = zext i1 %3 to i32
%5 = tail call i32 @assert(i32 %4)
%6 = trunc i64 %0 to i32
%7 = add i32 %6, 3
%8 = tail call i64 @malloc(i32 %7)
%9 = icmp eq i64 %8, 0
%10 = icmp ne i32 %1, 0
%11 = and i1 %10, %9
br i1 %11, label %16, label %12
12: ; preds = %2
%13 = inttoptr i64 %8 to i32*
%14 = tail call i32 @NS_PUT16(i64 %0, i32* %13)
%15 = inttoptr i64 %8 to i8*
br label %16
16: ; preds = %2, %12
%17 = phi i8* [ %15, %12 ], [ null, %2 ]
ret i8* %17
}