#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 wchar_t;
struct process {
  int dummy;
};

/* Variables and functions */
int trace_mem_readw(struct process *, size_t);
size_t trace_mem_readwstr(struct process *proc, size_t addr, wchar_t *buffer,
                          size_t size) {
  int bytes = 0;
  int c = 0xff;
  int limit = size - 1;
  while ((c = trace_mem_readw(proc, addr))) {
    addr += sizeof(wchar_t *);
    if (limit > bytes++) {
      *buffer++ = c;
    }
  }
  if (size)
    *buffer = L'\0';
  return bytes + 1;
}


x86 assembly (clang, -O0):
<trace_mem_readwstr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0xff,-0x28(%rbp)
mov    -0x20(%rbp),%rax
sub    $0x1,%rax
mov    %eax,-0x2c(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <trace_mem_readw@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x0,%eax
je     1195 <trace_mem_readwstr+0x85>
mov    -0x10(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x10(%rbp)
mov    -0x2c(%rbp),%eax
mov    -0x24(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0x24(%rbp)
cmp    %ecx,%eax
jle    1190 <trace_mem_readwstr+0x80>
mov    -0x28(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0x4,%rdx
mov    %rdx,-0x18(%rbp)
mov    %ecx,(%rax)
jmp    1141 <trace_mem_readwstr+0x31>
cmpq   $0x0,-0x20(%rbp)
je     11aa <trace_mem_readwstr+0x9a>
mov    -0x18(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
cltq
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<trace_mem_readwstr>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rsi,%r13
mov    %rdi,%r12
call   1030 <trace_mem_readw@plt>
test   %eax,%eax
je     1167 <trace_mem_readwstr+0x57>
lea    -0x1(%r14),%ebp
add    $0x8,%r13
mov    $0x1,%ebx
jmp    1157 <trace_mem_readwstr+0x47>
nop
mov    %r12,%rdi
mov    %r13,%rsi
call   1030 <trace_mem_readw@plt>
add    $0x8,%r13
add    $0x1,%rbx
test   %eax,%eax
je     116c <trace_mem_readwstr+0x5c>
lea    -0x1(%rbx),%ecx
cmp    %ecx,%ebp
jle    1140 <trace_mem_readwstr+0x30>
mov    %eax,(%r15)
add    $0x4,%r15
jmp    1140 <trace_mem_readwstr+0x30>
mov    $0x1,%ebx
test   %r14,%r14
je     1178 <trace_mem_readwstr+0x68>
movl   $0x0,(%r15)
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.process = type { i32 }
define i64 @trace_mem_readwstr(%struct.process* %0, i64 %1, i32* %2, i64 %3) {
%5 = alloca %struct.process*
%6 = alloca i64
%7 = alloca i32*
%8 = alloca i64
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store %struct.process* %0, %struct.process** %5
store i64 %1, i64* %6
store i32* %2, i32** %7
store i64 %3, i64* %8
store i32 0, i32* %9
store i32 255, i32* %10
%12 = load i64, i64* %8
%13 = sub i64 %12, 1
%14 = trunc i64 %13 to i32
store i32 %14, i32* %11
br label %15
15: ; preds = %31, %4
%16 = load %struct.process*, %struct.process** %5
%17 = load i64, i64* %6
%18 = call i32 @trace_mem_readw(%struct.process* %16, i64 %17)
store i32 %18, i32* %10
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %32
20: ; preds = %15
%21 = load i64, i64* %6
%22 = add i64 %21, 8
store i64 %22, i64* %6
%23 = load i32, i32* %11
%24 = load i32, i32* %9
%25 = add nsw i32 %24, 1
store i32 %25, i32* %9
%26 = icmp sgt i32 %23, %24
br i1 %26, label %27, label %31
27: ; preds = %20
%28 = load i32, i32* %10
%29 = load i32*, i32** %7
%30 = getelementptr inbounds i32, i32* %29, i32 1
store i32* %30, i32** %7
store i32 %28, i32* %29
br label %31
31: ; preds = %27, %20
br label %15
32: ; preds = %15
%33 = load i64, i64* %8
%34 = icmp ne i64 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %32
%36 = load i32*, i32** %7
store i32 0, i32* %36
br label %37
37: ; preds = %35, %32
%38 = load i32, i32* %9
%39 = add nsw i32 %38, 1
%40 = sext i32 %39 to i64
ret i64 %40
}

LLVM IR (-O2):
%struct.process = type { i32 }
define i64 @trace_mem_readwstr(%struct.process* %0, i64 %1, i32* nocapture writeonly %2, i64 %3) {
%5 = trunc i64 %3 to i32
%6 = add i32 %5, -1
%7 = tail call i32 @trace_mem_readw(%struct.process* %0, i64 %1)
%8 = icmp eq i32 %7, 0
br i1 %8, label %26, label %9
9: ; preds = %4, %19
%10 = phi i32 [ %21, %19 ], [ %7, %4 ]
%11 = phi i64 [ %14, %19 ], [ %1, %4 ]
%12 = phi i32 [ %15, %19 ], [ 0, %4 ]
%13 = phi i32* [ %20, %19 ], [ %2, %4 ]
%14 = add i64 %11, 8
%15 = add nuw nsw i32 %12, 1
%16 = icmp sgt i32 %6, %12
br i1 %16, label %17, label %19
17: ; preds = %9
%18 = getelementptr inbounds i32, i32* %13, i64 1
store i32 %10, i32* %13
br label %19
19: ; preds = %17, %9
%20 = phi i32* [ %18, %17 ], [ %13, %9 ]
%21 = tail call i32 @trace_mem_readw(%struct.process* %0, i64 %14)
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %9
23: ; preds = %19
%24 = add nuw i32 %12, 2
%25 = zext i32 %24 to i64
br label %26
26: ; preds = %23, %4
%27 = phi i32* [ %2, %4 ], [ %20, %23 ]
%28 = phi i64 [ 1, %4 ], [ %25, %23 ]
%29 = icmp eq i64 %3, 0
br i1 %29, label %31, label %30
30: ; preds = %26
store i32 0, i32* %27
br label %31
31: ; preds = %30, %26
ret i64 %28
}