#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 */
typedef int /*<<< orphan*/ wchar_t;

/* Variables and functions */
int /*<<< orphan*/ mbstowcs(int /*<<< orphan*/ *, char *, size_t);
char *strchr(char *, char);
int /*<<< orphan*/ strlcpy(char *, char *, size_t);
scalar_t__ strncmp(char *, char *, int);
void parse_git_head(wchar_t *out, char *data, size_t len) {
  char *c;
  if (strncmp(data, "ref: refs/heads/", 16) == 0) {
    data += 16;
    goto done;
  }
  if (strncmp(data, "ref:", 4) == 0) {
    data += 5;
    goto done;
  }
  strlcpy(data + 6, "...", len - 6);
done:
  if ((c = strchr(data, '\n')) != NULL)
    *c = '\0';
  mbstowcs(out, data, len);
}


x86 assembly (clang, -O0):
<parse_git_head>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xea1(%rip),%rsi
mov    $0x10,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    1184 <parse_git_head+0x44>
mov    -0x10(%rbp),%rax
add    $0x10,%rax
mov    %rax,-0x10(%rbp)
jmp    11d0 <parse_git_head+0x90>
mov    -0x10(%rbp),%rdi
lea    0xe82(%rip),%rsi
mov    $0x4,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    11b4 <parse_git_head+0x74>
mov    -0x10(%rbp),%rax
add    $0x5,%rax
mov    %rax,-0x10(%rbp)
jmp    11d0 <parse_git_head+0x90>
mov    -0x10(%rbp),%rdi
add    $0x6,%rdi
mov    -0x18(%rbp),%rdx
sub    $0x6,%rdx
lea    0xe4b(%rip),%rsi
call   1060 <strlcpy@plt>
mov    -0x10(%rbp),%rdi
mov    $0xa,%esi
call   1050 <strchr@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     11f3 <parse_git_head+0xb3>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <mbstowcs@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_git_head>:
push   %r15
push   %r14
push   %rbx
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %rdi,%r15
lea    0xeab(%rip),%rsi
mov    %rbx,%rdi
mov    $0x10,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
je     1196 <parse_git_head+0x56>
lea    0xea3(%rip),%rsi
mov    %rbx,%rdi
mov    $0x4,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
je     119c <parse_git_head+0x5c>
lea    0x6(%rbx),%rdi
lea    -0x6(%r14),%rdx
lea    0xe87(%rip),%rsi
call   1060 <strlcpy@plt>
jmp    11a0 <parse_git_head+0x60>
add    $0x10,%rbx
jmp    11a0 <parse_git_head+0x60>
add    $0x5,%rbx
mov    %rbx,%rdi
mov    $0xa,%esi
call   1050 <strchr@plt>
test   %rax,%rax
je     11b5 <parse_git_head+0x75>
movb   $0x0,(%rax)
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
pop    %rbx
pop    %r14
pop    %r15
jmp    1040 <mbstowcs@plt>

LLVM IR (-O0):
@.str = [17 x i8] c"ref: refs/heads/\00"
@.str.1 = [5 x i8] c"ref:\00"
@.str.2 = [4 x i8] c"...\00"
define void @parse_git_head(i32* %0, i8* %1, i64 %2) {
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
store i32* %0, i32** %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%8 = load i8*, i8** %5
%9 = call i64 @strncmp(i8* %8, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 16)
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %3
%12 = load i8*, i8** %5
%13 = getelementptr inbounds i8, i8* %12, i64 16
store i8* %13, i8** %5
br label %27
14: ; preds = %3
%15 = load i8*, i8** %5
%16 = call i64 @strncmp(i8* %15, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 4)
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %14
%19 = load i8*, i8** %5
%20 = getelementptr inbounds i8, i8* %19, i64 5
store i8* %20, i8** %5
br label %27
21: ; preds = %14
%22 = load i8*, i8** %5
%23 = getelementptr inbounds i8, i8* %22, i64 6
%24 = load i64, i64* %6
%25 = sub i64 %24, 6
%26 = call i32 @strlcpy(i8* %23, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i64 %25)
br label %27
27: ; preds = %21, %18, %11
%28 = load i8*, i8** %5
%29 = call i8* @strchr(i8* %28, i8 signext 10)
store i8* %29, i8** %7
%30 = icmp ne i8* %29, null
br i1 %30, label %31, label %33
31: ; preds = %27
%32 = load i8*, i8** %7
store i8 0, i8* %32
br label %33
33: ; preds = %31, %27
%34 = load i32*, i32** %4
%35 = load i8*, i8** %5
%36 = load i64, i64* %6
%37 = call i32 @mbstowcs(i32* %34, i8* %35, i64 %36)
ret void
}

LLVM IR (-O2):
@.str = [17 x i8] c"ref: refs/heads/\00"
@.str.1 = [5 x i8] c"ref:\00"
@.str.2 = [4 x i8] c"...\00"
define void @parse_git_head(i32* %0, i8* %1, i64 %2) {
%4 = tail call i64 @strncmp(i8* %1, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 16)
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %3
%7 = getelementptr inbounds i8, i8* %1, i64 16
br label %17
8: ; preds = %3
%9 = tail call i64 @strncmp(i8* %1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 4)
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %8
%12 = getelementptr inbounds i8, i8* %1, i64 5
br label %17
13: ; preds = %8
%14 = getelementptr inbounds i8, i8* %1, i64 6
%15 = add i64 %2, -6
%16 = tail call i32 @strlcpy(i8* %14, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i64 %15)
br label %17
17: ; preds = %13, %11, %6
%18 = phi i8* [ %7, %6 ], [ %12, %11 ], [ %1, %13 ]
%19 = tail call i8* @strchr(i8* dereferenceable(1) %18, i8 signext 10)
%20 = icmp eq i8* %19, null
br i1 %20, label %22, label %21
21: ; preds = %17
store i8 0, i8* %19
br label %22
22: ; preds = %21, %17
%23 = tail call i32 @mbstowcs(i32* %0, i8* %18, i64 %2)
ret void
}