#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*/ isspace(int);
char *trimheadtospc(const char *str, size_t len) {
  char *newstr = (char *)str;
  while (*newstr && !isspace((int)*newstr) && (newstr < str + len)) {
    newstr++;
  }
  return newstr;
}


x86 assembly (clang, -O0):
<trimheadtospc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movsbl (%rax),%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x19(%rbp)
je     116e <trimheadtospc+0x5e>
mov    -0x18(%rbp),%rax
movsbl (%rax),%edi
call   1030 <isspace@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x19(%rbp)
jne    116e <trimheadtospc+0x5e>
mov    -0x18(%rbp),%rax
mov    -0x8(%rbp),%rcx
add    -0x10(%rbp),%rcx
cmp    %rcx,%rax
setb   %al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
test   $0x1,%al
jne    117e <trimheadtospc+0x6e>
jmp    118f <trimheadtospc+0x7f>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1128 <trimheadtospc+0x18>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<trimheadtospc>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    (%rdi),%al
test   %al,%al
je     114d <trimheadtospc+0x3d>
mov    %rsi,%r14
add    %rbx,%r14
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movsbl %al,%edi
call   1030 <isspace@plt>
test   %eax,%eax
jne    114d <trimheadtospc+0x3d>
cmp    %r14,%rbx
jae    114d <trimheadtospc+0x3d>
movzbl 0x1(%rbx),%eax
add    $0x1,%rbx
test   %al,%al
jne    1130 <trimheadtospc+0x20>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
define i8* @trimheadtospc(i8* %0, i64 %1) {
%3 = load i8, i8* %0
%4 = icmp eq i8 %3, 0
br i1 %4, label %19, label %5
5: ; preds = %2
%6 = getelementptr inbounds i8, i8* %0, i64 %1
br label %7
7: ; preds = %5, %15
%8 = phi i8 [ %3, %5 ], [ %17, %15 ]
%9 = phi i8* [ %0, %5 ], [ %16, %15 ]
%10 = sext i8 %8 to i32
%11 = tail call i32 @isspace(i32 %10)
%12 = icmp eq i32 %11, 0
%13 = icmp ult i8* %9, %6
%14 = select i1 %12, i1 %13, i1 false
br i1 %14, label %15, label %19
15: ; preds = %7
%16 = getelementptr inbounds i8, i8* %9, i64 1
%17 = load i8, i8* %16
%18 = icmp eq i8 %17, 0
br i1 %18, label %19, label %7
19: ; preds = %7, %15, %2
%20 = phi i8* [ %0, %2 ], [ %16, %15 ], [ %9, %7 ]
ret i8* %20
}