#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 strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *strstr(char *, char *);
char *mid_value(char *str, char *left, char *right, char *out, int outlen) {
  char *beg, *end, t;
  beg = strstr(str, left);
  if (beg) {
    beg += strlen(left);
    if (right) {
      end = strstr(beg, right);
    } else {
      end = beg + strlen(beg);
    }
    if (end) {
      t = *end;
      *end = 0;
      strncpy(out, beg, outlen);
      *end = t;
      return end;
    }
  }
  *out = '\0';
  return str;
}


x86 assembly (clang, -O0):
<mid_value>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8d,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <strstr@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     1209 <mid_value+0xd9>
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x38(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11a2 <mid_value+0x72>
mov    -0x38(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1050 <strstr@plt>
mov    %rax,-0x40(%rbp)
jmp    11c3 <mid_value+0x93>
mov    -0x38(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x38(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x50(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
je     1204 <mid_value+0xd4>
mov    -0x40(%rbp),%rax
mov    (%rax),%al
mov    %al,-0x41(%rbp)
mov    -0x40(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x28(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x2c(%rbp),%edx
call   1030 <strncpy@plt>
mov    -0x41(%rbp),%cl
mov    -0x40(%rbp),%rax
mov    %cl,(%rax)
mov    -0x40(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1218 <mid_value+0xe8>
jmp    1209 <mid_value+0xd9>
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mid_value>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8d,%r14d
mov    %rcx,%r15
mov    %rdx,%r12
mov    %rsi,%rbp
mov    %rdi,%r13
call   1050 <strstr@plt>
test   %rax,%rax
je     11ab <mid_value+0x7b>
mov    %rax,%rbx
mov    %rbp,%rdi
call   1040 <strlen@plt>
cltq
add    %rax,%rbx
mov    %rbx,%rdi
test   %r12,%r12
je     119b <mid_value+0x6b>
mov    %r12,%rsi
call   1050 <strstr@plt>
mov    %rax,%rbp
test   %rbp,%rbp
je     11ab <mid_value+0x7b>
mov    0x0(%rbp),%r12b
movb   $0x0,0x0(%rbp)
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1030 <strncpy@plt>
mov    %r12b,0x0(%rbp)
mov    %rbp,%r13
jmp    11af <mid_value+0x7f>
call   1040 <strlen@plt>
movslq %eax,%rbp
add    %rbx,%rbp
test   %rbp,%rbp
jne    117c <mid_value+0x4c>
movb   $0x0,(%r15)
mov    %r13,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @mid_value(i8* %0, i8* %1, i8* %2, i8* %3, i32 %4) {
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i8*
%13 = alloca i8*
%14 = alloca i8
store i8* %0, i8** %7
store i8* %1, i8** %8
store i8* %2, i8** %9
store i8* %3, i8** %10
store i32 %4, i32* %11
%15 = load i8*, i8** %7
%16 = load i8*, i8** %8
%17 = call i8* @strstr(i8* %15, i8* %16)
store i8* %17, i8** %12
%18 = load i8*, i8** %12
%19 = icmp ne i8* %18, null
br i1 %19, label %20, label %53
20: ; preds = %5
%21 = load i8*, i8** %8
%22 = call i32 @strlen(i8* %21)
%23 = load i8*, i8** %12
%24 = sext i32 %22 to i64
%25 = getelementptr inbounds i8, i8* %23, i64 %24
store i8* %25, i8** %12
%26 = load i8*, i8** %9
%27 = icmp ne i8* %26, null
br i1 %27, label %28, label %32
28: ; preds = %20
%29 = load i8*, i8** %12
%30 = load i8*, i8** %9
%31 = call i8* @strstr(i8* %29, i8* %30)
store i8* %31, i8** %13
br label %38
32: ; preds = %20
%33 = load i8*, i8** %12
%34 = load i8*, i8** %12
%35 = call i32 @strlen(i8* %34)
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i8, i8* %33, i64 %36
store i8* %37, i8** %13
br label %38
38: ; preds = %32, %28
%39 = load i8*, i8** %13
%40 = icmp ne i8* %39, null
br i1 %40, label %41, label %52
41: ; preds = %38
%42 = load i8*, i8** %13
%43 = load i8, i8* %42
store i8 %43, i8* %14
%44 = load i8*, i8** %13
store i8 0, i8* %44
%45 = load i8*, i8** %10
%46 = load i8*, i8** %12
%47 = load i32, i32* %11
%48 = call i32 @strncpy(i8* %45, i8* %46, i32 %47)
%49 = load i8, i8* %14
%50 = load i8*, i8** %13
store i8 %49, i8* %50
%51 = load i8*, i8** %13
store i8* %51, i8** %6
br label %56
52: ; preds = %38
br label %53
53: ; preds = %52, %5
%54 = load i8*, i8** %10
store i8 0, i8* %54
%55 = load i8*, i8** %7
store i8* %55, i8** %6
br label %56
56: ; preds = %53, %41
%57 = load i8*, i8** %6
ret i8* %57
}

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