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

/* Variables and functions */
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ memset(char *, char, int);
scalar_t__ strlen(char *);
size_t b_string_strlen(void *dummy) {
  char *buf = (char *)malloc(500000);
  size_t i;
  size_t cs = 0;
  memset(buf, 'a', 500000 - 1);
  buf[500000 - 1] = 0;
  for (i = 0; i < 100; i++) {
    buf[i] = '0' + i % 8;
    cs += strlen(buf);
  }
  free(buf);
  return cs;
}


x86 assembly (clang, -O0):
<b_string_strlen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x7a120,%edi
call   1060 <malloc@plt>
mov    %rax,-0x10(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x61,%esi
mov    $0x7a11f,%edx
call   1050 <memset@plt>
mov    -0x10(%rbp),%rax
movb   $0x0,0x7a11f(%rax)
movq   $0x0,-0x18(%rbp)
cmpq   $0x64,-0x18(%rbp)
jae    11ce <b_string_strlen+0x8e>
mov    -0x18(%rbp),%rax
and    $0x7,%rax
add    $0x30,%rax
mov    %al,%dl
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1188 <b_string_strlen+0x48>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<b_string_strlen>:
push   %r15
push   %r14
push   %rbx
mov    $0x7a120,%edi
call   1060 <malloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    $0x61,%esi
mov    $0x7a11f,%edx
call   1050 <memset@plt>
movb   $0x0,0x7a11f(%r14)
xor    %r15d,%r15d
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %ebx,%eax
and    $0x7,%al
or     $0x30,%al
mov    %al,(%r14,%rbx,1)
mov    %r14,%rdi
call   1040 <strlen@plt>
add    %rax,%r15
add    $0x1,%rbx
cmp    $0x64,%rbx
jne    1180 <b_string_strlen+0x40>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i64 @b_string_strlen(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i64
store i8* %0, i8** %2
%6 = call i64 @malloc(i32 500000)
%7 = inttoptr i64 %6 to i8*
store i8* %7, i8** %3
store i64 0, i64* %5
%8 = load i8*, i8** %3
%9 = call i32 @memset(i8* %8, i8 signext 97, i32 499999)
%10 = load i8*, i8** %3
%11 = getelementptr inbounds i8, i8* %10, i64 499999
store i8 0, i8* %11
store i64 0, i64* %4
br label %12
12: ; preds = %27, %1
%13 = load i64, i64* %4
%14 = icmp ult i64 %13, 100
br i1 %14, label %15, label %30
15: ; preds = %12
%16 = load i64, i64* %4
%17 = urem i64 %16, 8
%18 = add i64 48, %17
%19 = trunc i64 %18 to i8
%20 = load i8*, i8** %3
%21 = load i64, i64* %4
%22 = getelementptr inbounds i8, i8* %20, i64 %21
store i8 %19, i8* %22
%23 = load i8*, i8** %3
%24 = call i64 @strlen(i8* %23)
%25 = load i64, i64* %5
%26 = add i64 %25, %24
store i64 %26, i64* %5
br label %27
27: ; preds = %15
%28 = load i64, i64* %4
%29 = add i64 %28, 1
store i64 %29, i64* %4
br label %12
30: ; preds = %12
%31 = load i8*, i8** %3
%32 = call i32 @free(i8* %31)
%33 = load i64, i64* %5
ret i64 %33
}

LLVM IR (-O2):
define i64 @b_string_strlen(i8* nocapture readnone %0) {
%2 = tail call i64 @malloc(i32 500000)
%3 = inttoptr i64 %2 to i8*
%4 = tail call i32 @memset(i8* %3, i8 signext 97, i32 499999)
%5 = getelementptr inbounds i8, i8* %3, i64 499999
store i8 0, i8* %5
br label %6
6: ; preds = %1, %6
%7 = phi i64 [ 0, %1 ], [ %14, %6 ]
%8 = phi i64 [ 0, %1 ], [ %15, %6 ]
%9 = trunc i64 %8 to i8
%10 = and i8 %9, 7
%11 = or i8 %10, 48
%12 = getelementptr inbounds i8, i8* %3, i64 %8
store i8 %11, i8* %12
%13 = tail call i64 @strlen(i8* dereferenceable(1) %3)
%14 = add i64 %13, %7
%15 = add nuw nsw i64 %8, 1
%16 = icmp eq i64 %15, 100
br i1 %16, label %17, label %6
17: ; preds = %6
%18 = tail call i32 @free(i8* %3)
ret i64 %14
}