#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 */
char *find_nth_chr(char const *, char, int);
int /*<<< orphan*/ snprintf(char *, size_t, char *, char const *);
char *substitute_in_name(char const *, char *, char *, int);
int apply_s_gs_mod(const char *path, const char *mod, char *buf,
                   size_t buf_len) {
  char pattern[256], sub[256];
  int global;
  const char *start = mod;
  char c = (mod[1] == 'g') ? mod++[3] : mod[2];
  const char *t, *p = find_nth_chr(mod, c, 3);
  if (p == NULL) {
    snprintf(buf, buf_len, "%s", path);
    return 0;
  }
  t = find_nth_chr(mod, c, 2);
  snprintf(pattern, t - (mod + 3) + 1, "%s", mod + 3);
  snprintf(sub, p - (t + 1) + 1, "%s", t + 1);
  global = (mod[0] == 'g');
  snprintf(buf, buf_len, "%s", substitute_in_name(path, pattern, sub, global));
  return (p + 1) - start - 2;
}


x86 assembly (clang, -O0):
<apply_s_gs_mod>:
push   %rbp
mov    %rsp,%rbp
sub    $0x270,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x240(%rbp)
mov    -0x18(%rbp),%rax
movsbl 0x1(%rax),%eax
cmp    $0x67,%eax
jne    1185 <apply_s_gs_mod+0x55>
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x18(%rbp)
movsbl 0x3(%rax),%eax
mov    %eax,-0x25c(%rbp)
jmp    1193 <apply_s_gs_mod+0x63>
mov    -0x18(%rbp),%rax
movsbl 0x2(%rax),%eax
mov    %eax,-0x25c(%rbp)
mov    -0x25c(%rbp),%eax
mov    %al,-0x241(%rbp)
mov    -0x18(%rbp),%rdi
mov    $0x3,%edx
movsbl -0x241(%rbp),%esi
call   1040 <find_nth_chr@plt>
mov    %rax,-0x258(%rbp)
cmpq   $0x0,-0x258(%rbp)
jne    11ed <apply_s_gs_mod+0xbd>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x10(%rbp),%rcx
lea    0xe24(%rip),%rdx
call   1030 <snprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12f5 <apply_s_gs_mod+0x1c5>
mov    -0x18(%rbp),%rdi
mov    $0x2,%edx
movsbl -0x241(%rbp),%esi
call   1040 <find_nth_chr@plt>
mov    %rax,-0x250(%rbp)
lea    -0x130(%rbp),%rdi
mov    -0x250(%rbp),%rsi
mov    -0x18(%rbp),%rax
add    $0x3,%rax
sub    %rax,%rsi
add    $0x1,%rsi
mov    -0x18(%rbp),%rcx
add    $0x3,%rcx
lea    0xdcb(%rip),%rdx
call   1030 <snprintf@plt>
lea    -0x230(%rbp),%rdi
mov    -0x258(%rbp),%rsi
mov    -0x250(%rbp),%rax
add    $0x1,%rax
sub    %rax,%rsi
add    $0x1,%rsi
mov    -0x250(%rbp),%rcx
add    $0x1,%rcx
lea    0xd94(%rip),%rdx
call   1030 <snprintf@plt>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x67,%eax
sete   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x234(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x270(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x268(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x130(%rbp),%rsi
lea    -0x230(%rbp),%rdx
mov    -0x234(%rbp),%ecx
call   1050 <substitute_in_name@plt>
mov    -0x270(%rbp),%rdi
mov    -0x268(%rbp),%rsi
mov    %rax,%rcx
lea    0xd2c(%rip),%rdx
call   1030 <snprintf@plt>
mov    -0x258(%rbp),%rax
add    $0x1,%rax
mov    -0x240(%rbp),%rcx
sub    %rcx,%rax
sub    $0x2,%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x270,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<apply_s_gs_mod>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x228,%rsp
mov    %rcx,%r15
mov    %rdx,0x10(%rsp)
mov    %rsi,%r12
mov    %rdi,0x8(%rsp)
lea    0x1(%rsi),%rbp
xor    %r13d,%r13d
cmpb   $0x67,0x1(%rsi)
sete   %r13b
cmovne %rsi,%rbp
movsbl 0x2(%r13,%rsi,1),%r14d
mov    %rbp,%rdi
mov    %r14d,%esi
mov    $0x3,%edx
call   1040 <find_nth_chr@plt>
test   %rax,%rax
je     1220 <apply_s_gs_mod+0xf0>
mov    %rax,%rbx
mov    %rbp,%rdi
mov    %r14d,%esi
mov    $0x2,%edx
call   1040 <find_nth_chr@plt>
mov    %r15,0x18(%rsp)
mov    %rax,%r15
add    $0x3,%rbp
mov    %rax,%rsi
sub    %rbp,%rsi
add    $0x1,%rsi
lea    0xe4d(%rip),%r14
lea    0x20(%rsp),%rdi
mov    %r14,%rdx
mov    %rbp,%rcx
call   1030 <snprintf@plt>
add    $0x1,%r15
add    $0x1,%rbx
mov    %rbx,%rsi
sub    %r15,%rsi
lea    0x120(%rsp),%rbp
mov    %rbp,%rdi
mov    %r14,%rdx
mov    %r15,%rcx
call   1030 <snprintf@plt>
xor    %ecx,%ecx
cmpb   $0x67,(%r12,%r13,1)
sete   %cl
mov    0x8(%rsp),%rdi
lea    0x20(%rsp),%rsi
mov    %rbp,%rdx
call   1050 <substitute_in_name@plt>
mov    0x10(%rsp),%rdi
mov    0x18(%rsp),%rsi
mov    %r14,%rdx
mov    %rax,%rcx
call   1030 <snprintf@plt>
sub    %r12d,%ebx
add    $0xfffffffe,%ebx
jmp    123b <apply_s_gs_mod+0x10b>
lea    0xdd9(%rip),%rdx
mov    0x10(%rsp),%rdi
mov    %r15,%rsi
mov    0x8(%rsp),%rcx
call   1030 <snprintf@plt>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x228,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%s\00"
define i32 @apply_s_gs_mod(i8* %0, i8* %1, i8* %2, i64 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i64
%10 = alloca [256 x i8]
%11 = alloca [256 x i8]
%12 = alloca i32
%13 = alloca i8*
%14 = alloca i8
%15 = alloca i8*
%16 = alloca i8*
store i8* %0, i8** %6
store i8* %1, i8** %7
store i8* %2, i8** %8
store i64 %3, i64* %9
%17 = load i8*, i8** %7
store i8* %17, i8** %13
%18 = load i8*, i8** %7
%19 = getelementptr inbounds i8, i8* %18, i64 1
%20 = load i8, i8* %19
%21 = sext i8 %20 to i32
%22 = icmp eq i32 %21, 103
br i1 %22, label %23, label %29
23: ; preds = %4
%24 = load i8*, i8** %7
%25 = getelementptr inbounds i8, i8* %24, i32 1
store i8* %25, i8** %7
%26 = getelementptr inbounds i8, i8* %24, i64 3
%27 = load i8, i8* %26
%28 = sext i8 %27 to i32
br label %34
29: ; preds = %4
%30 = load i8*, i8** %7
%31 = getelementptr inbounds i8, i8* %30, i64 2
%32 = load i8, i8* %31
%33 = sext i8 %32 to i32
br label %34
34: ; preds = %29, %23
%35 = phi i32 [ %28, %23 ], [ %33, %29 ]
%36 = trunc i32 %35 to i8
store i8 %36, i8* %14
%37 = load i8*, i8** %7
%38 = load i8, i8* %14
%39 = call i8* @find_nth_chr(i8* %37, i8 signext %38, i32 3)
store i8* %39, i8** %16
%40 = load i8*, i8** %16
%41 = icmp eq i8* %40, null
br i1 %41, label %42, label %47
42: ; preds = %34
%43 = load i8*, i8** %8
%44 = load i64, i64* %9
%45 = load i8*, i8** %6
%46 = call i32 @snprintf(i8* %43, i64 %44, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %45)
store i32 0, i32* %5
br label %95
47: ; preds = %34
%48 = load i8*, i8** %7
%49 = load i8, i8* %14
%50 = call i8* @find_nth_chr(i8* %48, i8 signext %49, i32 2)
store i8* %50, i8** %15
%51 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%52 = load i8*, i8** %15
%53 = load i8*, i8** %7
%54 = getelementptr inbounds i8, i8* %53, i64 3
%55 = ptrtoint i8* %52 to i64
%56 = ptrtoint i8* %54 to i64
%57 = sub i64 %55, %56
%58 = add nsw i64 %57, 1
%59 = load i8*, i8** %7
%60 = getelementptr inbounds i8, i8* %59, i64 3
%61 = call i32 @snprintf(i8* %51, i64 %58, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %60)
%62 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%63 = load i8*, i8** %16
%64 = load i8*, i8** %15
%65 = getelementptr inbounds i8, i8* %64, i64 1
%66 = ptrtoint i8* %63 to i64
%67 = ptrtoint i8* %65 to i64
%68 = sub i64 %66, %67
%69 = add nsw i64 %68, 1
%70 = load i8*, i8** %15
%71 = getelementptr inbounds i8, i8* %70, i64 1
%72 = call i32 @snprintf(i8* %62, i64 %69, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %71)
%73 = load i8*, i8** %7
%74 = getelementptr inbounds i8, i8* %73, i64 0
%75 = load i8, i8* %74
%76 = sext i8 %75 to i32
%77 = icmp eq i32 %76, 103
%78 = zext i1 %77 to i32
store i32 %78, i32* %12
%79 = load i8*, i8** %8
%80 = load i64, i64* %9
%81 = load i8*, i8** %6
%82 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%83 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%84 = load i32, i32* %12
%85 = call i8* @substitute_in_name(i8* %81, i8* %82, i8* %83, i32 %84)
%86 = call i32 @snprintf(i8* %79, i64 %80, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %85)
%87 = load i8*, i8** %16
%88 = getelementptr inbounds i8, i8* %87, i64 1
%89 = load i8*, i8** %13
%90 = ptrtoint i8* %88 to i64
%91 = ptrtoint i8* %89 to i64
%92 = sub i64 %90, %91
%93 = sub nsw i64 %92, 2
%94 = trunc i64 %93 to i32
store i32 %94, i32* %5
br label %95
95: ; preds = %47, %42
%96 = load i32, i32* %5
ret i32 %96
}

LLVM IR (-O2):
@.str = [3 x i8] c"%s\00"
define i32 @apply_s_gs_mod(i8* %0, i8* %1, i8* %2, i64 %3) {
%5 = alloca [256 x i8]
%6 = alloca [256 x i8]
%7 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%8 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%9 = getelementptr inbounds i8, i8* %1, i64 1
%10 = load i8, i8* %9
%11 = icmp eq i8 %10, 103
%12 = select i1 %11, i8* %9, i8* %1
%13 = select i1 %11, i64 3, i64 2
%14 = getelementptr inbounds i8, i8* %1, i64 %13
%15 = load i8, i8* %14
%16 = tail call i8* @find_nth_chr(i8* %12, i8 signext %15, i32 3)
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %20
18: ; preds = %4
%19 = tail call i32 @snprintf(i8* %2, i64 %3, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %45
20: ; preds = %4
%21 = tail call i8* @find_nth_chr(i8* %12, i8 signext %15, i32 2)
%22 = getelementptr inbounds i8, i8* %12, i64 3
%23 = ptrtoint i8* %21 to i64
%24 = ptrtoint i8* %22 to i64
%25 = sub i64 1, %24
%26 = add i64 %25, %23
%27 = call i32 @snprintf(i8* %7, i64 %26, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %22)
%28 = getelementptr inbounds i8, i8* %21, i64 1
%29 = ptrtoint i8* %16 to i64
%30 = ptrtoint i8* %28 to i64
%31 = add i64 %29, 1
%32 = sub i64 %31, %30
%33 = call i32 @snprintf(i8* %8, i64 %32, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %28)
%34 = load i8, i8* %12
%35 = icmp eq i8 %34, 103
%36 = zext i1 %35 to i32
%37 = call i8* @substitute_in_name(i8* %0, i8* %7, i8* %8, i32 %36)
%38 = call i32 @snprintf(i8* %2, i64 %3, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %37)
%39 = getelementptr inbounds i8, i8* %16, i64 1
%40 = ptrtoint i8* %39 to i64
%41 = ptrtoint i8* %1 to i64
%42 = sub i64 %40, %41
%43 = trunc i64 %42 to i32
%44 = add i32 %43, -2
br label %45
45: ; preds = %20, %18
%46 = phi i32 [ 0, %18 ], [ %44, %20 ]
ret i32 %46
}