#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*/ uint16_t;
struct binder_io {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ *bio_alloc(struct binder_io *, size_t);
int /*<<< orphan*/ bio_put_uint32(struct binder_io *, size_t);
size_t strlen(char const *);
void bio_put_string16_x(struct binder_io *bio, const char *_str) {
  unsigned char *str = (unsigned char *)_str;
  size_t len;
  uint16_t *ptr;
  if (!str) {
    bio_put_uint32(bio, 0xffffffff);
    return;
  }
  len = strlen(_str);
  if (len >= ((1 << 30) / sizeof(uint16_t))) {
    bio_put_uint32(bio, 0xffffffff);
    return;
  }
  bio_put_uint32(bio, len);
  ptr = bio_alloc(bio, (len + 1) * sizeof(uint16_t));
  if (!ptr)
    return;
  while (*str)
    *ptr++ = *str++;
  *ptr++ = 0;
}


x86 assembly (clang, -O0):
<bio_put_string16_x>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1166 <bio_put_string16_x+0x36>
mov    -0x8(%rbp),%rdi
mov    $0xffffffff,%esi
call   1050 <bio_put_uint32@plt>
jmp    1219 <bio_put_string16_x+0xe9>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x10000000,-0x20(%rbp)
10
jb     1194 <bio_put_string16_x+0x64>
mov    -0x8(%rbp),%rdi
mov    $0xffffffff,%esi
call   1050 <bio_put_uint32@plt>
jmp    1219 <bio_put_string16_x+0xe9>
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1050 <bio_put_uint32@plt>
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rsi
add    $0x1,%rsi
shl    $0x2,%rsi
call   1030 <bio_alloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11ca <bio_put_string16_x+0x9a>
jmp    1219 <bio_put_string16_x+0xe9>
jmp    11cf <bio_put_string16_x+0x9f>
mov    -0x18(%rbp),%rax
cmpb   $0x0,(%rax)
je     1204 <bio_put_string16_x+0xd4>
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x18(%rbp)
movzbl (%rax),%ecx
mov    -0x28(%rbp),%rax
mov    %rax,%rdx
add    $0x4,%rdx
mov    %rdx,-0x28(%rbp)
mov    %ecx,(%rax)
jmp    11cf <bio_put_string16_x+0x9f>
mov    -0x28(%rbp),%rax
mov    %rax,%rcx
add    $0x4,%rcx
mov    %rcx,-0x28(%rbp)
movl   $0x0,(%rax)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bio_put_string16_x>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
test   %rsi,%rsi
je     1150 <bio_put_string16_x+0x20>
mov    %rsi,%rbx
mov    %rsi,%rdi
call   1040 <strlen@plt>
cmp    $0x10000000,%rax
jb     1162 <bio_put_string16_x+0x32>
mov    $0xffffffff,%esi
mov    %r14,%rdi
pop    %rbx
pop    %r14
pop    %r15
jmp    1050 <bio_put_uint32@plt>
mov    %rax,%r15
mov    %r14,%rdi
mov    %rax,%rsi
call   1050 <bio_put_uint32@plt>
lea    0x4(,%r15,4),%rsi
mov    %r14,%rdi
call   1030 <bio_alloc@plt>
test   %rax,%rax
je     11aa <bio_put_string16_x+0x7a>
mov    (%rbx),%cl
test   %cl,%cl
je     11a4 <bio_put_string16_x+0x74>
add    $0x1,%rbx
nop
movzbl %cl,%ecx
mov    %ecx,(%rax)
add    $0x4,%rax
movzbl (%rbx),%ecx
add    $0x1,%rbx
test   %cl,%cl
jne    1190 <bio_put_string16_x+0x60>
movl   $0x0,(%rax)
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.binder_io = type { i32 }
define void @bio_put_string16_x(%struct.binder_io* %0, i8* %1) {
%3 = alloca %struct.binder_io*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32*
store %struct.binder_io* %0, %struct.binder_io** %3
store i8* %1, i8** %4
%8 = load i8*, i8** %4
store i8* %8, i8** %5
%9 = load i8*, i8** %5
%10 = icmp ne i8* %9, null
br i1 %10, label %14, label %11
11: ; preds = %2
%12 = load %struct.binder_io*, %struct.binder_io** %3
%13 = call i32 @bio_put_uint32(%struct.binder_io* %12, i64 4294967295)
br label %49
14: ; preds = %2
%15 = load i8*, i8** %4
%16 = call i64 @strlen(i8* %15)
store i64 %16, i64* %6
%17 = load i64, i64* %6
%18 = icmp uge i64 %17, 268435456
br i1 %18, label %19, label %22
19: ; preds = %14
%20 = load %struct.binder_io*, %struct.binder_io** %3
%21 = call i32 @bio_put_uint32(%struct.binder_io* %20, i64 4294967295)
br label %49
22: ; preds = %14
%23 = load %struct.binder_io*, %struct.binder_io** %3
%24 = load i64, i64* %6
%25 = call i32 @bio_put_uint32(%struct.binder_io* %23, i64 %24)
%26 = load %struct.binder_io*, %struct.binder_io** %3
%27 = load i64, i64* %6
%28 = add i64 %27, 1
%29 = mul i64 %28, 4
%30 = call i32* @bio_alloc(%struct.binder_io* %26, i64 %29)
store i32* %30, i32** %7
%31 = load i32*, i32** %7
%32 = icmp ne i32* %31, null
br i1 %32, label %34, label %33
33: ; preds = %22
br label %49
34: ; preds = %22
br label %35
35: ; preds = %39, %34
%36 = load i8*, i8** %5
%37 = load i8, i8* %36
%38 = icmp ne i8 %37, 0
br i1 %38, label %39, label %46
39: ; preds = %35
%40 = load i8*, i8** %5
%41 = getelementptr inbounds i8, i8* %40, i32 1
store i8* %41, i8** %5
%42 = load i8, i8* %40
%43 = zext i8 %42 to i32
%44 = load i32*, i32** %7
%45 = getelementptr inbounds i32, i32* %44, i32 1
store i32* %45, i32** %7
store i32 %43, i32* %44
br label %35
46: ; preds = %35
%47 = load i32*, i32** %7
%48 = getelementptr inbounds i32, i32* %47, i32 1
store i32* %48, i32** %7
store i32 0, i32* %47
br label %49
49: ; preds = %46, %33, %19, %11
ret void
}

LLVM IR (-O2):
%struct.binder_io = type { i32 }
define void @bio_put_string16_x(%struct.binder_io* %0, i8* %1) {
%3 = icmp eq i8* %1, null
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = tail call i32 @bio_put_uint32(%struct.binder_io* %0, i64 4294967295)
br label %31
6: ; preds = %2
%7 = tail call i64 @strlen(i8* %1)
%8 = icmp ugt i64 %7, 268435455
br i1 %8, label %9, label %11
9: ; preds = %6
%10 = tail call i32 @bio_put_uint32(%struct.binder_io* %0, i64 4294967295)
br label %31
11: ; preds = %6
%12 = tail call i32 @bio_put_uint32(%struct.binder_io* %0, i64 %7)
%13 = shl nuw nsw i64 %7, 2
%14 = add nuw nsw i64 %13, 4
%15 = tail call i32* @bio_alloc(%struct.binder_io* %0, i64 %14)
%16 = icmp eq i32* %15, null
br i1 %16, label %31, label %17
17: ; preds = %11
%18 = load i8, i8* %1
%19 = icmp eq i8 %18, 0
br i1 %19, label %29, label %20
20: ; preds = %17, %20
%21 = phi i8 [ %27, %20 ], [ %18, %17 ]
%22 = phi i32* [ %26, %20 ], [ %15, %17 ]
%23 = phi i8* [ %24, %20 ], [ %1, %17 ]
%24 = getelementptr inbounds i8, i8* %23, i64 1
%25 = zext i8 %21 to i32
%26 = getelementptr inbounds i32, i32* %22, i64 1
store i32 %25, i32* %22
%27 = load i8, i8* %24
%28 = icmp eq i8 %27, 0
br i1 %28, label %29, label %20
29: ; preds = %20, %17
%30 = phi i32* [ %15, %17 ], [ %26, %20 ]
store i32 0, i32* %30
br label %31
31: ; preds = %11, %29, %9, %4
ret void
}