#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 */
size_t strlen(char const *);
scalar_t__ strncmp(char const *, char const *, size_t);
const char *startswith(const char *s, const char *prefix) {
  size_t sz = prefix ? strlen(prefix) : 0;
  if (s && sz && strncmp(s, prefix, sz) == 0)
    return s + sz;
  return NULL;
}


x86 assembly (clang, -O0):
<startswith>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     114d <startswith+0x2d>
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x28(%rbp)
jmp    1158 <startswith+0x38>
xor    %eax,%eax
mov    %rax,-0x28(%rbp)
jmp    1158 <startswith+0x38>
mov    -0x28(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11a2 <startswith+0x82>
cmpq   $0x0,-0x20(%rbp)
je     11a2 <startswith+0x82>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    11a2 <startswith+0x82>
mov    -0x10(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11aa <startswith+0x8a>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<startswith>:
push   %r15
push   %r14
push   %rbx
test   %rsi,%rsi
je     1166 <startswith+0x46>
mov    %rsi,%rbx
mov    %rdi,%r14
mov    %rsi,%rdi
call   1040 <strlen@plt>
mov    %rax,%r15
xor    %eax,%eax
test   %r14,%r14
je     1168 <startswith+0x48>
test   %r15,%r15
je     1168 <startswith+0x48>
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %r15,%rdx
call   1030 <strncmp@plt>
mov    %rax,%rcx
add    %r15,%r14
xor    %eax,%eax
test   %rcx,%rcx
cmove  %r14,%rax
jmp    1168 <startswith+0x48>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i8* @startswith(i8* %0, i8* %1) {
%3 = icmp eq i8* %1, null
br i1 %3, label %14, label %4
4: ; preds = %2
%5 = tail call i64 @strlen(i8* %1)
%6 = icmp ne i8* %0, null
%7 = icmp ne i64 %5, 0
%8 = select i1 %6, i1 %7, i1 false
br i1 %8, label %9, label %14
9: ; preds = %4
%10 = tail call i64 @strncmp(i8* %0, i8* %1, i64 %5)
%11 = icmp eq i64 %10, 0
%12 = getelementptr inbounds i8, i8* %0, i64 %5
%13 = select i1 %11, i8* %12, i8* null
br label %14
14: ; preds = %2, %9, %4
%15 = phi i8* [ null, %4 ], [ %13, %9 ], [ null, %2 ]
ret i8* %15
}