#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__ strcmp(char *, char *);
scalar_t__ strlen(char *);
char *substr(char *, int /*<<< orphan*/, scalar_t__);
char strBeginsWith(char *haystack, char *needle) {
  char result;
  if (strlen(haystack) < strlen(needle)) {
    return 0;
  } else {
    char *sub = substr(haystack, 0, strlen(needle));
    result = (strcmp(sub, needle) == 0);
    free(sub);
    sub = NULL;
  }
  return result;
}


x86 assembly (clang, -O0):
<strBeginsWith>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
mov    -0x30(%rbp),%rax
cmp    %rcx,%rax
jge    117f <strBeginsWith+0x3f>
movb   $0x0,-0x1(%rbp)
jmp    11d5 <strBeginsWith+0x95>
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x38(%rbp),%rdi
mov    %rax,%rdx
xor    %esi,%esi
call   1050 <substr@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
sete   %al
and    $0x1,%al
movzbl %al,%eax
mov    %al,-0x19(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x28(%rbp)
mov    -0x19(%rbp),%al
mov    %al,-0x1(%rbp)
movsbl -0x1(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strBeginsWith>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
call   1040 <strlen@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1040 <strlen@plt>
cmp    %rax,%rbx
jge    1164 <strBeginsWith+0x24>
xor    %ebx,%ebx
jmp    118d <strBeginsWith+0x4d>
mov    %r15,%rdi
xor    %esi,%esi
mov    %rax,%rdx
call   1050 <substr@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    %r14,%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
sete   %bl
mov    %r15,%rdi
call   1030 <free@plt>
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define signext i8 @strBeginsWith(i8* %0, i8* %1) {
%3 = alloca i8
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8
%7 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
%8 = load i8*, i8** %4
%9 = call i64 @strlen(i8* %8)
%10 = load i8*, i8** %5
%11 = call i64 @strlen(i8* %10)
%12 = icmp slt i64 %9, %11
br i1 %12, label %13, label %14
13: ; preds = %2
store i8 0, i8* %3
br label %29
14: ; preds = %2
%15 = load i8*, i8** %4
%16 = load i8*, i8** %5
%17 = call i64 @strlen(i8* %16)
%18 = call i8* @substr(i8* %15, i32 0, i64 %17)
store i8* %18, i8** %7
%19 = load i8*, i8** %7
%20 = load i8*, i8** %5
%21 = call i64 @strcmp(i8* %19, i8* %20)
%22 = icmp eq i64 %21, 0
%23 = zext i1 %22 to i32
%24 = trunc i32 %23 to i8
store i8 %24, i8* %6
%25 = load i8*, i8** %7
%26 = call i32 @free(i8* %25)
store i8* null, i8** %7
br label %27
27: ; preds = %14
%28 = load i8, i8* %6
store i8 %28, i8* %3
br label %29
29: ; preds = %27, %13
%30 = load i8, i8* %3
ret i8 %30
}

LLVM IR (-O2):
define signext i8 @strBeginsWith(i8* %0, i8* %1) {
%3 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%4 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%5 = icmp slt i64 %3, %4
br i1 %5, label %12, label %6
6: ; preds = %2
%7 = tail call i8* @substr(i8* %0, i32 0, i64 %4)
%8 = tail call i64 @strcmp(i8* %7, i8* %1)
%9 = icmp eq i64 %8, 0
%10 = zext i1 %9 to i8
%11 = tail call i32 @free(i8* %7)
br label %12
12: ; preds = %2, %6
%13 = phi i8 [ %10, %6 ], [ 0, %2 ]
ret i8 %13
}