#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*/ bzero(char *, int);
scalar_t__ malloc(int);
int /*<<< orphan*/ strncpy(char *, char const *, int);
char *strstr(char const *, char *);
const char *get_protocol(const char *url) {
  const char *first_colon;
  if ((first_colon = strstr(url, "://"))) {
    char *protocol = (char *)malloc(12 * sizeof(char));
    bzero(protocol, 12);
    strncpy(protocol, url, first_colon - url + 3);
    return protocol;
  } else {
    return NULL;
  }
}


x86 assembly (clang, -O0):
<get_protocol>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xea9(%rip),%rsi
call   1060 <strstr@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     11b1 <get_protocol+0x71>
mov    $0xc,%edi
call   1040 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    $0xc,%esi
call   1050 <bzero@plt>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
add    $0x3,%rax
mov    %eax,%edx
call   1030 <strncpy@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11b9 <get_protocol+0x79>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_protocol>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
lea    0xeb1(%rip),%rsi
call   1060 <strstr@plt>
test   %rax,%rax
je     118b <get_protocol+0x4b>
mov    %rax,%rbx
mov    $0xc,%edi
call   1040 <malloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    $0xc,%esi
call   1050 <bzero@plt>
sub    %r14d,%ebx
add    $0x3,%ebx
mov    %r15,%rdi
mov    %r14,%rsi
mov    %ebx,%edx
call   1030 <strncpy@plt>
jmp    118e <get_protocol+0x4e>
xor    %r15d,%r15d
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [4 x i8] c"://\00"
define i8* @get_protocol(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = call i8* @strstr(i8* %6, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
store i8* %7, i8** %4
%8 = icmp ne i8* %7, null
br i1 %8, label %9, label %25
9: ; preds = %1
%10 = call i64 @malloc(i32 12)
%11 = inttoptr i64 %10 to i8*
store i8* %11, i8** %5
%12 = load i8*, i8** %5
%13 = call i32 @bzero(i8* %12, i32 12)
%14 = load i8*, i8** %5
%15 = load i8*, i8** %3
%16 = load i8*, i8** %4
%17 = load i8*, i8** %3
%18 = ptrtoint i8* %16 to i64
%19 = ptrtoint i8* %17 to i64
%20 = sub i64 %18, %19
%21 = add nsw i64 %20, 3
%22 = trunc i64 %21 to i32
%23 = call i32 @strncpy(i8* %14, i8* %15, i32 %22)
%24 = load i8*, i8** %5
store i8* %24, i8** %2
br label %26
25: ; preds = %1
store i8* null, i8** %2
br label %26
26: ; preds = %25, %9
%27 = load i8*, i8** %2
ret i8* %27
}

LLVM IR (-O2):
@.str = [4 x i8] c"://\00"
define i8* @get_protocol(i8* %0) {
%2 = tail call i8* @strstr(i8* dereferenceable(1) %0, i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i8* %2, null
br i1 %3, label %14, label %4
4: ; preds = %1
%5 = tail call i64 @malloc(i32 12)
%6 = inttoptr i64 %5 to i8*
%7 = tail call i32 @bzero(i8* %6, i32 12)
%8 = ptrtoint i8* %2 to i64
%9 = ptrtoint i8* %0 to i64
%10 = sub i64 %8, %9
%11 = trunc i64 %10 to i32
%12 = add i32 %11, 3
%13 = tail call i32 @strncpy(i8* %6, i8* %0, i32 %12)
br label %14
14: ; preds = %1, %4
%15 = phi i8* [ %6, %4 ], [ null, %1 ]
ret i8* %15
}