#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 */
unsigned int strlen(char const *);
int /*<<< orphan*/ strncpy(char *, char *, unsigned int);
char *strstr(char *, char const *);
char *strtok(char *, char *);
int get_metadata_field(char *metadata, const char *field, char *value) {
  char *split;
  char *occurrence = NULL;
  split = strtok(metadata, ";");
  while (split != NULL) {
    occurrence = strstr(split, field);
    if (occurrence != NULL) {
      unsigned int content_pos = strlen(field) + 2;
      unsigned int content_size = strlen(split) - content_pos - 1;
      strncpy(value, occurrence + content_pos, content_size);
      value[content_size] = '\0';
      return 0;
    }
    split = strtok(NULL, ";");
  }
  value[0] = '\0';
  return 1;
}


x86 assembly (clang, -O0):
<get_metadata_field>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe99(%rip),%rsi
call   1050 <strtok@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11fe <get_metadata_field+0xbe>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <strstr@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     11e5 <get_metadata_field+0xa5>
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x2,%eax
mov    %eax,-0x34(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <strlen@plt>
sub    -0x34(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x38(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x34(%rbp),%eax
add    %rax,%rsi
mov    -0x38(%rbp),%edx
call   1030 <strncpy@plt>
mov    -0x20(%rbp),%rax
mov    -0x38(%rbp),%ecx
movb   $0x0,(%rax,%rcx,1)
movl   $0x0,-0x4(%rbp)
jmp    120c <get_metadata_field+0xcc>
xor    %eax,%eax
mov    %eax,%edi
lea    0xe10(%rip),%rsi
call   1050 <strtok@plt>
mov    %rax,-0x28(%rbp)
jmp    1170 <get_metadata_field+0x30>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_metadata_field>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r12
lea    0xea8(%rip),%rsi
call   1050 <strtok@plt>
mov    $0x1,%r15d
test   %rax,%rax
je     11dc <get_metadata_field+0x9c>
mov    %rax,%rbp
lea    0xe8e(%rip),%r13
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rbp,%rdi
mov    %r12,%rsi
call   1060 <strstr@plt>
test   %rax,%rax
jne    11a4 <get_metadata_field+0x64>
xor    %edi,%edi
mov    %r13,%rsi
call   1050 <strtok@plt>
mov    %rax,%rbp
test   %rax,%rax
jne    1180 <get_metadata_field+0x40>
jmp    11dc <get_metadata_field+0x9c>
mov    %rax,%rbx
mov    %r12,%rdi
call   1040 <strlen@plt>
mov    %eax,%r15d
lea    0x2(%r15),%r12d
mov    %rbp,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebp
sub    %r15d,%ebp
add    $0xfffffffd,%ebp
add    %r12,%rbx
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <strncpy@plt>
add    %rbp,%r14
xor    %r15d,%r15d
movb   $0x0,(%r14)
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c";\00"
define i32 @get_metadata_field(i8* %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
store i8* null, i8** %9
%12 = load i8*, i8** %5
%13 = call i8* @strtok(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %13, i8** %8
br label %14
14: ; preds = %43, %3
%15 = load i8*, i8** %8
%16 = icmp ne i8* %15, null
br i1 %16, label %17, label %45
17: ; preds = %14
%18 = load i8*, i8** %8
%19 = load i8*, i8** %6
%20 = call i8* @strstr(i8* %18, i8* %19)
store i8* %20, i8** %9
%21 = load i8*, i8** %9
%22 = icmp ne i8* %21, null
br i1 %22, label %23, label %43
23: ; preds = %17
%24 = load i8*, i8** %6
%25 = call i32 @strlen(i8* %24)
%26 = add i32 %25, 2
store i32 %26, i32* %10
%27 = load i8*, i8** %8
%28 = call i32 @strlen(i8* %27)
%29 = load i32, i32* %10
%30 = sub i32 %28, %29
%31 = sub i32 %30, 1
store i32 %31, i32* %11
%32 = load i8*, i8** %7
%33 = load i8*, i8** %9
%34 = load i32, i32* %10
%35 = zext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %33, i64 %35
%37 = load i32, i32* %11
%38 = call i32 @strncpy(i8* %32, i8* %36, i32 %37)
%39 = load i8*, i8** %7
%40 = load i32, i32* %11
%41 = zext i32 %40 to i64
%42 = getelementptr inbounds i8, i8* %39, i64 %41
store i8 0, i8* %42
store i32 0, i32* %4
br label %48
43: ; preds = %17
%44 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %44, i8** %8
br label %14
45: ; preds = %14
%46 = load i8*, i8** %7
%47 = getelementptr inbounds i8, i8* %46, i64 0
store i8 0, i8* %47
store i32 1, i32* %4
br label %48
48: ; preds = %45, %23
%49 = load i32, i32* %4
ret i32 %49
}

LLVM IR (-O2):
@.str = [2 x i8] c";\00"
define i32 @get_metadata_field(i8* %0, i8* %1, i8* %2) {
%4 = tail call i8* @strtok(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i8* %4, null
br i1 %5, label %24, label %6
6: ; preds = %3, %21
%7 = phi i8* [ %22, %21 ], [ %4, %3 ]
%8 = tail call i8* @strstr(i8* %7, i8* dereferenceable(1) %1)
%9 = icmp eq i8* %8, null
br i1 %9, label %21, label %10
10: ; preds = %6
%11 = tail call i32 @strlen(i8* %1)
%12 = add i32 %11, 2
%13 = tail call i32 @strlen(i8* %7)
%14 = sub i32 -3, %11
%15 = add i32 %14, %13
%16 = zext i32 %12 to i64
%17 = getelementptr inbounds i8, i8* %8, i64 %16
%18 = tail call i32 @strncpy(i8* %2, i8* %17, i32 %15)
%19 = zext i32 %15 to i64
%20 = getelementptr inbounds i8, i8* %2, i64 %19
br label %24
21: ; preds = %6
%22 = tail call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%23 = icmp eq i8* %22, null
br i1 %23, label %24, label %6
24: ; preds = %21, %3, %10
%25 = phi i8* [ %20, %10 ], [ %2, %3 ], [ %2, %21 ]
%26 = phi i32 [ 0, %10 ], [ 1, %3 ], [ 1, %21 ]
store i8 0, i8* %25
ret i32 %26
}