#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 */
scalar_t__ strcmp(char *, char *);
int num_bytes_until(char *str, int len) {
  int i = 0;
  int bytes = 0;
  char *chunk;
  for (i = 0; i < len; i++) {
    chunk = &str[i + 1];
    if (strcmp(str, chunk) < 0) {
      break;
    } else {
      bytes++;
    }
  }
  return bytes;
}


x86 assembly (clang, -O0):
<num_bytes_until>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    118c <num_bytes_until+0x7c>
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1030 <strcmp@plt>
cmp    $0x0,%rax
jge    1170 <num_bytes_until+0x60>
jmp    118c <num_bytes_until+0x7c>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    117e <num_bytes_until+0x6e>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1134 <num_bytes_until+0x24>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<num_bytes_until>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
test   %esi,%esi
jle    1155 <num_bytes_until+0x45>
mov    %rdi,%r14
mov    %esi,%r12d
lea    0x1(%rdi),%rbx
xor    %r15d,%r15d
nopl   0x0(%rax)
mov    %r14,%rdi
mov    %rbx,%rsi
call   1030 <strcmp@plt>
test   %rax,%rax
js     115a <num_bytes_until+0x4a>
add    $0xffffffffffffffff,%r15
add    $0x1,%rbx
mov    %r12,%rax
add    %r15,%rax
jne    1130 <num_bytes_until+0x20>
mov    %r12d,%r15d
jmp    115d <num_bytes_until+0x4d>
xor    %r15d,%r15d
jmp    115d <num_bytes_until+0x4d>
neg    %r15d
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i32 @num_bytes_until(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
store i8* %0, i8** %3
store i32 %1, i32* %4
store i32 0, i32* %5
store i32 0, i32* %6
store i32 0, i32* %5
br label %8
8: ; preds = %27, %2
%9 = load i32, i32* %5
%10 = load i32, i32* %4
%11 = icmp slt i32 %9, %10
br i1 %11, label %12, label %30
12: ; preds = %8
%13 = load i8*, i8** %3
%14 = load i32, i32* %5
%15 = add nsw i32 %14, 1
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds i8, i8* %13, i64 %16
store i8* %17, i8** %7
%18 = load i8*, i8** %3
%19 = load i8*, i8** %7
%20 = call i64 @strcmp(i8* %18, i8* %19)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %12
br label %30
23: ; preds = %12
%24 = load i32, i32* %6
%25 = add nsw i32 %24, 1
store i32 %25, i32* %6
br label %26
26: ; preds = %23
br label %27
27: ; preds = %26
%28 = load i32, i32* %5
%29 = add nsw i32 %28, 1
store i32 %29, i32* %5
br label %8
30: ; preds = %22, %8
%31 = load i32, i32* %6
ret i32 %31
}

LLVM IR (-O2):
define i32 @num_bytes_until(i8* %0, i32 %1) {
%3 = icmp sgt i32 %1, 0
br i1 %3, label %4, label %16
4: ; preds = %2
%5 = zext i32 %1 to i64
br label %6
6: ; preds = %4, %12
%7 = phi i64 [ 0, %4 ], [ %8, %12 ]
%8 = add nuw nsw i64 %7, 1
%9 = getelementptr inbounds i8, i8* %0, i64 %8
%10 = tail call i64 @strcmp(i8* %0, i8* %9)
%11 = icmp slt i64 %10, 0
br i1 %11, label %14, label %12
12: ; preds = %6
%13 = icmp eq i64 %8, %5
br i1 %13, label %16, label %6
14: ; preds = %6
%15 = trunc i64 %7 to i32
br label %16
16: ; preds = %12, %14, %2
%17 = phi i32 [ 0, %2 ], [ %15, %14 ], [ %1, %12 ]
ret i32 %17
}