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

/* Variables and functions */
int /*<<< orphan*/ adb_encode(unsigned int *, char **, char *, char const **,
                              char const *);
int /*<<< orphan*/ write_all(int, char *, int);
void write_all_adb_encoded(int fd, const void *buf, size_t sz) {
  char encbuf[4096];
  unsigned state = 0;
  const char *in = buf;
  const char *inend = in + sz;
  size_t nr_written = 0;
  while (nr_written < sz) {
    char *enc = encbuf;
    char *encend = enc + sizeof(encbuf);
    adb_encode(&state, &enc, encend, &in, inend);
    write_all(fd, encbuf, enc - encbuf);
    nr_written += enc - encbuf;
  }
}


x86 assembly (clang, -O0):
<write_all_adb_encoded>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1050,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x1024(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x1030(%rbp)
mov    -0x1030(%rbp),%rax
add    -0x18(%rbp),%rax
mov    %rax,-0x1038(%rbp)
movq   $0x0,-0x1040(%rbp)
mov    -0x1040(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    1209 <write_all_adb_encoded+0xe9>
lea    -0x1020(%rbp),%rax
mov    %rax,-0x1048(%rbp)
mov    -0x1048(%rbp),%rax
add    $0x1000,%rax
mov    %rax,-0x1050(%rbp)
mov    -0x1050(%rbp),%rdx
mov    -0x1038(%rbp),%r8
lea    -0x1024(%rbp),%rdi
lea    -0x1048(%rbp),%rsi
lea    -0x1030(%rbp),%rcx
call   1040 <adb_encode@plt>
mov    -0x4(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    -0x1048(%rbp),%rax
lea    -0x1020(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
call   1030 <write_all@plt>
mov    -0x1048(%rbp),%rax
lea    -0x1020(%rbp),%rcx
sub    %rcx,%rax
add    -0x1040(%rbp),%rax
mov    %rax,-0x1040(%rbp)
jmp    1168 <write_all_adb_encoded+0x48>
add    $0x1050,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_all_adb_encoded>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1028,%rsp
movl   $0x0,0x14(%rsp)
mov    %rsi,0x18(%rsp)
test   %rdx,%rdx
je     11a0 <write_all_adb_encoded+0x80>
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %edi,%r12d
add    %rdx,%rbx
xor    %r15d,%r15d
lea    0x20(%rsp),%rbp
lea    0x18(%rsp),%r13
nopl   0x0(%rax)
mov    %rbp,0x8(%rsp)
lea    0x14(%rsp),%rdi
lea    0x8(%rsp),%rsi
lea    0x1020(%rsp),%rdx
mov    %r13,%rcx
mov    %rbx,%r8
call   1040 <adb_encode@plt>
mov    0x8(%rsp),%edx
sub    %ebp,%edx
mov    %r12d,%edi
mov    %rbp,%rsi
call   1030 <write_all@plt>
sub    %rbp,%r15
add    0x8(%rsp),%r15
cmp    %r14,%r15
jb     1160 <write_all_adb_encoded+0x40>
add    $0x1028,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @write_all_adb_encoded(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca [4096 x i8]
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i64
%12 = alloca i8*
%13 = alloca i8*
store i32 %0, i32* %4
store i8* %1, i8** %5
store i64 %2, i64* %6
store i32 0, i32* %8
%14 = load i8*, i8** %5
store i8* %14, i8** %9
%15 = load i8*, i8** %9
%16 = load i64, i64* %6
%17 = getelementptr inbounds i8, i8* %15, i64 %16
store i8* %17, i8** %10
store i64 0, i64* %11
br label %18
18: ; preds = %22, %3
%19 = load i64, i64* %11
%20 = load i64, i64* %6
%21 = icmp ult i64 %19, %20
br i1 %21, label %22, label %45
22: ; preds = %18
%23 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
store i8* %23, i8** %12
%24 = load i8*, i8** %12
%25 = getelementptr inbounds i8, i8* %24, i64 4096
store i8* %25, i8** %13
%26 = load i8*, i8** %13
%27 = load i8*, i8** %10
%28 = call i32 @adb_encode(i32* %8, i8** %12, i8* %26, i8** %9, i8* %27)
%29 = load i32, i32* %4
%30 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%31 = load i8*, i8** %12
%32 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%33 = ptrtoint i8* %31 to i64
%34 = ptrtoint i8* %32 to i64
%35 = sub i64 %33, %34
%36 = trunc i64 %35 to i32
%37 = call i32 @write_all(i32 %29, i8* %30, i32 %36)
%38 = load i8*, i8** %12
%39 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%40 = ptrtoint i8* %38 to i64
%41 = ptrtoint i8* %39 to i64
%42 = sub i64 %40, %41
%43 = load i64, i64* %11
%44 = add i64 %43, %42
store i64 %44, i64* %11
br label %18
45: ; preds = %18
ret void
}

LLVM IR (-O2):
define void @write_all_adb_encoded(i32 %0, i8* %1, i64 %2) {
%4 = alloca [4096 x i8]
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%9 = bitcast i32* %5 to i8*
store i32 0, i32* %5
%10 = bitcast i8** %6 to i8*
store i8* %1, i8** %6
%11 = getelementptr inbounds i8, i8* %1, i64 %2
%12 = icmp eq i64 %2, 0
br i1 %12, label %30, label %13
13: ; preds = %3
%14 = bitcast i8** %7 to i8*
%15 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 4096
%16 = ptrtoint [4096 x i8]* %4 to i64
br label %17
17: ; preds = %13, %17
%18 = phi i64 [ 0, %13 ], [ %28, %17 ]
store i8* %8, i8** %7
%19 = call i32 @adb_encode(i32* %5, i8** %7, i8* %15, i8** %6, i8* %11)
%20 = load i8*, i8** %7
%21 = ptrtoint i8* %20 to i64
%22 = sub i64 %21, %16
%23 = trunc i64 %22 to i32
%24 = call i32 @write_all(i32 %0, i8* %8, i32 %23)
%25 = load i8*, i8** %7
%26 = ptrtoint i8* %25 to i64
%27 = sub i64 %18, %16
%28 = add i64 %27, %26
%29 = icmp ult i64 %28, %2
br i1 %29, label %17, label %30
30: ; preds = %17, %3
ret void
}