#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 uint32_t;
typedef scalar_t__ uint16_t;
struct binder_io {
  int dummy;
};

/* Variables and functions */
scalar_t__ *bio_alloc(struct binder_io *, size_t);
int /*<<< orphan*/ bio_put_uint32(struct binder_io *, int);
int /*<<< orphan*/ memcpy(scalar_t__ *, scalar_t__ const *, size_t);
void bio_put_string16(struct binder_io *bio, const uint16_t *str) {
  size_t len;
  uint16_t *ptr;
  if (!str) {
    bio_put_uint32(bio, 0xffffffff);
    return;
  }
  len = 0;
  while (str[len])
    len++;
  if (len >= ((1 << 30) / sizeof(uint16_t))) {
    bio_put_uint32(bio, 0xffffffff);
    return;
  }
  bio_put_uint32(bio, (uint32_t)len);
  len = (len + 1) * sizeof(uint16_t);
  ptr = bio_alloc(bio, len);
  if (ptr)
    memcpy(ptr, str, len);
}


x86 assembly (clang, -O0):
<bio_put_string16>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    115e <bio_put_string16+0x2e>
mov    -0x8(%rbp),%rdi
mov    $0xffffffff,%esi
call   1040 <bio_put_uint32@plt>
jmp    11f7 <bio_put_string16+0xc7>
movq   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
cmpq   $0x0,(%rax,%rcx,8)
je     118a <bio_put_string16+0x5a>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1166 <bio_put_string16+0x36>
cmpq   $0x8000000,-0x18(%rbp)
08
jb     11ab <bio_put_string16+0x7b>
mov    -0x8(%rbp),%rdi
mov    $0xffffffff,%esi
call   1040 <bio_put_uint32@plt>
jmp    11f7 <bio_put_string16+0xc7>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    %eax,%esi
call   1040 <bio_put_uint32@plt>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
shl    $0x3,%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <bio_alloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11f7 <bio_put_string16+0xc7>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1050 <memcpy@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bio_put_string16>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
test   %rsi,%rsi
je     1167 <bio_put_string16+0x37>
mov    %rsi,%r15
xor    %eax,%eax
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rax,%rbx
add    $0x1,%rax
cmpq   $0x0,(%r15,%rbx,8)
jne    1150 <bio_put_string16+0x20>
cmp    $0x8000000,%rbx
jb     1179 <bio_put_string16+0x49>
mov    %r14,%rdi
mov    $0xffffffff,%esi
pop    %rbx
pop    %r14
pop    %r15
jmp    1040 <bio_put_uint32@plt>
mov    %r14,%rdi
mov    %ebx,%esi
call   1040 <bio_put_uint32@plt>
lea    0x8(,%rbx,8),%rbx
mov    %r14,%rdi
mov    %rbx,%rsi
call   1030 <bio_alloc@plt>
test   %rax,%rax
je     11ae <bio_put_string16+0x7e>
mov    %rax,%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
pop    %rbx
pop    %r14
pop    %r15
jmp    1050 <memcpy@plt>
pop    %rbx
pop    %r14
pop    %r15
ret

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

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