#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*/ free(char *);
char *ft_strfjoin(char *, char *, int, int);
int ft_strlen(char *);
char *ft_strsub(char *, int, int);
char *ft_strfinsert(char *a, char *b, int pos, int frees) {
  char *s1;
  char *s2;
  s1 = ft_strsub(a, 0, pos);
  s2 = ft_strsub(a, pos, ft_strlen(a));
  s1 = ft_strfjoin(s1, b, 1, 0);
  s1 = ft_strfjoin(s1, s2, 1, 1);
  if (frees == 1 || frees == 3)
    free(a);
  if (frees == 2 || frees == 3)
    free(b);
  return (s1);
}


x86 assembly (clang, -O0):
<ft_strfinsert>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x14(%rbp),%edx
xor    %esi,%esi
call   1050 <ft_strsub@plt>
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <ft_strlen@plt>
mov    -0x38(%rbp),%rdi
mov    -0x2c(%rbp),%esi
mov    %eax,%edx
call   1050 <ft_strsub@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1060 <ft_strfjoin@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    $0x1,%ecx
mov    %ecx,%edx
call   1060 <ft_strfjoin@plt>
mov    %rax,-0x20(%rbp)
cmpl   $0x1,-0x18(%rbp)
je     11d5 <ft_strfinsert+0x95>
cmpl   $0x3,-0x18(%rbp)
jne    11de <ft_strfinsert+0x9e>
mov    -0x8(%rbp),%rdi
call   1030 <free@plt>
cmpl   $0x2,-0x18(%rbp)
je     11f2 <ft_strfinsert+0xb2>
cmpl   $0x3,-0x18(%rbp)
jne    11fb <ft_strfinsert+0xbb>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ft_strfinsert>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %ecx,%r12d
mov    %edx,%ebp
mov    %rsi,%r14
mov    %rdi,%rbx
xor    %esi,%esi
call   1050 <ft_strsub@plt>
mov    %rax,%r15
mov    %rbx,%rdi
call   1040 <ft_strlen@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
mov    %eax,%edx
call   1050 <ft_strsub@plt>
mov    %rax,%rbp
mov    %r15,%rdi
mov    %r14,%rsi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1060 <ft_strfjoin@plt>
mov    %rax,%rdi
mov    %rbp,%rsi
mov    $0x1,%edx
mov    $0x1,%ecx
call   1060 <ft_strfjoin@plt>
mov    %rax,%r15
mov    %r12d,%eax
and    $0xfffffffd,%eax
cmp    $0x1,%eax
jne    11b1 <ft_strfinsert+0x71>
mov    %rbx,%rdi
call   1030 <free@plt>
and    $0xfffffffe,%r12d
cmp    $0x2,%r12d
jne    11c3 <ft_strfinsert+0x83>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %r15,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @ft_strfinsert(i8* %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i8*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 %3, i32* %8
%11 = load i8*, i8** %5
%12 = load i32, i32* %7
%13 = call i8* @ft_strsub(i8* %11, i32 0, i32 %12)
store i8* %13, i8** %9
%14 = load i8*, i8** %5
%15 = load i32, i32* %7
%16 = load i8*, i8** %5
%17 = call i32 @ft_strlen(i8* %16)
%18 = call i8* @ft_strsub(i8* %14, i32 %15, i32 %17)
store i8* %18, i8** %10
%19 = load i8*, i8** %9
%20 = load i8*, i8** %6
%21 = call i8* @ft_strfjoin(i8* %19, i8* %20, i32 1, i32 0)
store i8* %21, i8** %9
%22 = load i8*, i8** %9
%23 = load i8*, i8** %10
%24 = call i8* @ft_strfjoin(i8* %22, i8* %23, i32 1, i32 1)
store i8* %24, i8** %9
%25 = load i32, i32* %8
%26 = icmp eq i32 %25, 1
br i1 %26, label %30, label %27
27: ; preds = %4
%28 = load i32, i32* %8
%29 = icmp eq i32 %28, 3
br i1 %29, label %30, label %33
30: ; preds = %27, %4
%31 = load i8*, i8** %5
%32 = call i32 @free(i8* %31)
br label %33
33: ; preds = %30, %27
%34 = load i32, i32* %8
%35 = icmp eq i32 %34, 2
br i1 %35, label %39, label %36
36: ; preds = %33
%37 = load i32, i32* %8
%38 = icmp eq i32 %37, 3
br i1 %38, label %39, label %42
39: ; preds = %36, %33
%40 = load i8*, i8** %6
%41 = call i32 @free(i8* %40)
br label %42
42: ; preds = %39, %36
%43 = load i8*, i8** %9
ret i8* %43
}

LLVM IR (-O2):
define i8* @ft_strfinsert(i8* %0, i8* %1, i32 %2, i32 %3) {
%5 = tail call i8* @ft_strsub(i8* %0, i32 0, i32 %2)
%6 = tail call i32 @ft_strlen(i8* %0)
%7 = tail call i8* @ft_strsub(i8* %0, i32 %2, i32 %6)
%8 = tail call i8* @ft_strfjoin(i8* %5, i8* %1, i32 1, i32 0)
%9 = tail call i8* @ft_strfjoin(i8* %8, i8* %7, i32 1, i32 1)
%10 = and i32 %3, -3
%11 = icmp eq i32 %10, 1
br i1 %11, label %12, label %14
12: ; preds = %4
%13 = tail call i32 @free(i8* %0)
br label %14
14: ; preds = %4, %12
%15 = and i32 %3, -2
%16 = icmp eq i32 %15, 2
br i1 %16, label %17, label %19
17: ; preds = %14
%18 = tail call i32 @free(i8* %1)
br label %19
19: ; preds = %14, %17
ret i8* %9
}