#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*/ fprintf(int /*<<< orphan*/, char *, char *, int, char *);
char *getenv(char *);
int /*<<< orphan*/ stderr;
char *strchr(char *, char);
int strlen(char *);
int /*<<< orphan*/ strncat(char *, char *, int);
int /*<<< orphan*/ strncpy(char *, char *, int);
void subs_env(char *dest, char *src, int dest_size) {
  char *last_src, *bp, *bp1;
  last_src = src;
  if ((bp = strchr(src, '$')) == NULL) {
    strncpy(dest, src, dest_size);
    dest[dest_size - 1] = '\0';
  } else {
    *dest = '\0';
    while (strlen(dest) < dest_size - 1 && bp != NULL) {
      *bp = '\0';
      strncpy(dest, last_src, dest_size);
      *bp = '$';
      if (*(bp + 1) != '{') {
        strncat(dest, "$", dest_size - strlen(dest) - 1);
        dest[dest_size - 1] = '\0';
        bp += 1;
      } else {
        if ((bp1 = strchr(bp + 2, '}')) == NULL) {
          fprintf(
              stderr, "*** ERROR [%s:%d] - Unterminated ENV: %s\n",
              "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
              "2022-01-22/02-19-57/repos/uwbmrb/BMRB-API/refs/heads/master/"
              "server/wsgi/bmrbapi/submodules/fasta36/src/compacc2.c",
              252, src);
          break;
        } else {
          *bp1 = '\0';
          if (getenv(bp + 2) != NULL) {
            strncat(dest, getenv(bp + 2), dest_size - strlen(dest) - 1);
            dest[dest_size - 1] = '\0';
            *bp1 = '}';
          }
          bp = bp1 + 1;
        }
      }
      last_src = bp;
      bp = strchr(last_src, '$');
    }
    strncat(dest, last_src, dest_size - strlen(dest) - 1);
    dest[dest_size - 1] = '\0';
  }
}


x86 assembly (clang, -O0):
<subs_env>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x24,%esi
call   1060 <strchr@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    11c0 <subs_env+0x60>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1040 <strncpy@plt>
mov    -0x8(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    13b2 <subs_env+0x252>
mov    -0x8(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x14(%rbp),%edx
sub    $0x1,%edx
xor    %eax,%eax
cmp    %edx,%ecx
mov    %al,-0x31(%rbp)
jge    11f0 <subs_env+0x90>
cmpq   $0x0,-0x28(%rbp)
setne  %al
mov    %al,-0x31(%rbp)
mov    -0x31(%rbp),%al
test   $0x1,%al
jne    1200 <subs_env+0xa0>
jmp    136d <subs_env+0x20d>
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1040 <strncpy@plt>
mov    -0x28(%rbp),%rax
movb   $0x24,(%rax)
mov    -0x28(%rbp),%rax
movsbl 0x1(%rax),%eax
cmp    $0x7b,%eax
je     1280 <subs_env+0x120>
mov    -0x8(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x44(%rbp),%edx
mov    -0x40(%rbp),%rdi
sub    %eax,%edx
sub    $0x1,%edx
lea    0xda7(%rip),%rsi
call   1070 <strncat@plt>
mov    -0x8(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    134e <subs_env+0x1ee>
mov    -0x28(%rbp),%rdi
add    $0x2,%rdi
mov    $0x7d,%esi
call   1060 <strchr@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    12ca <subs_env+0x16a>
mov    0x2d39(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%r8
lea    0xd4e(%rip),%rsi
lea    0xd71(%rip),%rdx
mov    $0xfc,%ecx
call   1080 <fprintf@plt>
jmp    136d <subs_env+0x20d>
mov    -0x30(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x28(%rbp),%rdi
add    $0x2,%rdi
call   1030 <getenv@plt>
cmp    $0x0,%rax
je     133d <subs_env+0x1dd>
mov    -0x8(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x28(%rbp),%rdi
add    $0x2,%rdi
call   1030 <getenv@plt>
mov    %rax,-0x50(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x5c(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x5c(%rbp),%edx
mov    -0x58(%rbp),%rdi
mov    -0x50(%rbp),%rsi
sub    %eax,%edx
sub    $0x1,%edx
call   1070 <strncat@plt>
mov    -0x8(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x30(%rbp),%rax
movb   $0x7d,(%rax)
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    134e <subs_env+0x1ee>
mov    -0x28(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    $0x24,%esi
call   1060 <strchr@plt>
mov    %rax,-0x28(%rbp)
jmp    11c7 <subs_env+0x67>
mov    -0x8(%rbp),%rax
mov    %rax,-0x70(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x74(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x74(%rbp),%edx
mov    -0x70(%rbp),%rdi
mov    -0x68(%rbp),%rsi
sub    %eax,%edx
sub    $0x1,%edx
call   1070 <strncat@plt>
mov    -0x8(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<subs_env>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %rsi,%r15
mov    %rdi,%r13
mov    %rsi,%rdi
mov    $0x24,%esi
call   1060 <strchr@plt>
test   %rax,%rax
je     1282 <subs_env+0x122>
mov    %rax,%rbx
movb   $0x0,0x0(%r13)
mov    %r13,%rdi
call   1050 <strlen@plt>
movslq %r14d,%r12
add    $0xffffffffffffffff,%r12
cmp    %r12d,%eax
jge    12be <subs_env+0x15e>
mov    %r15,(%rsp)
xchg   %ax,%ax
movb   $0x0,(%rbx)
mov    %r13,%rdi
mov    %r15,%rsi
mov    %r14d,%edx
call   1040 <strncpy@plt>
movb   $0x24,(%rbx)
cmpb   $0x7b,0x1(%rbx)
jne    1230 <subs_env+0xd0>
add    $0x2,%rbx
mov    %rbx,%rdi
mov    $0x7d,%esi
call   1060 <strchr@plt>
test   %rax,%rax
je     1299 <subs_env+0x139>
mov    %rax,%rbp
movb   $0x0,(%rax)
mov    %rbx,%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     121d <subs_env+0xbd>
mov    %rax,%rbx
mov    %r13,%rdi
call   1050 <strlen@plt>
not    %eax
lea    (%rax,%r14,1),%edx
mov    %r13,%rdi
mov    %rbx,%rsi
call   1070 <strncat@plt>
movb   $0x0,0x0(%r13,%r12,1)
movb   $0x7d,0x0(%rbp)
add    $0x1,%rbp
mov    %rbp,%r15
jmp    125a <subs_env+0xfa>
cs nopw 0x0(%rax,%rax,1)
add    $0x1,%rbx
mov    %r13,%rdi
call   1050 <strlen@plt>
not    %eax
lea    (%rax,%r14,1),%edx
mov    %r13,%rdi
lea    0xdb4(%rip),%rsi
call   1070 <strncat@plt>
movb   $0x0,0x0(%r13,%r12,1)
mov    %rbx,%r15
mov    %r15,%rdi
mov    $0x24,%esi
call   1060 <strchr@plt>
mov    %rax,%rbx
mov    %r13,%rdi
call   1050 <strlen@plt>
cmp    %r12d,%eax
jge    12be <subs_env+0x15e>
test   %rbx,%rbx
jne    11b0 <subs_env+0x50>
jmp    12be <subs_env+0x15e>
mov    %r13,%rdi
mov    %r15,%rsi
mov    %r14d,%edx
call   1040 <strncpy@plt>
add    $0xffffffff,%r14d
movslq %r14d,%r12
jmp    12d9 <subs_env+0x179>
mov    0x2d40(%rip),%rax
mov    (%rax),%edi
lea    0xd59(%rip),%rsi
lea    0xd7c(%rip),%rdx
mov    $0xfc,%ecx
mov    (%rsp),%r8
call   1080 <fprintf@plt>
mov    %r13,%rdi
call   1050 <strlen@plt>
not    %eax
add    %eax,%r14d
mov    %r13,%rdi
mov    %r15,%rsi
mov    %r14d,%edx
call   1070 <strncat@plt>
movb   $0x0,0x0(%r13,%r12,1)
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"
@stderr = global i32 0
@.str.1 = [42 x i8] c"*** ERROR [%s:%d] - Unterminated ENV: %s\0A\00"
@.str.2 = [178 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/uwbmrb/BMRB-API/refs/heads/master/server/wsgi/bmrbapi/submodules/fasta36/src/compacc2.c\00"
define void @subs_env(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%10 = load i8*, i8** %5
store i8* %10, i8** %7
%11 = load i8*, i8** %5
%12 = call i8* @strchr(i8* %11, i8 signext 36)
store i8* %12, i8** %8
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %24
14: ; preds = %3
%15 = load i8*, i8** %4
%16 = load i8*, i8** %5
%17 = load i32, i32* %6
%18 = call i32 @strncpy(i8* %15, i8* %16, i32 %17)
%19 = load i8*, i8** %4
%20 = load i32, i32* %6
%21 = sub nsw i32 %20, 1
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i8, i8* %19, i64 %22
store i8 0, i8* %23
br label %118
24: ; preds = %3
%25 = load i8*, i8** %4
store i8 0, i8* %25
br label %26
26: ; preds = %100, %24
%27 = load i8*, i8** %4
%28 = call i32 @strlen(i8* %27)
%29 = load i32, i32* %6
%30 = sub nsw i32 %29, 1
%31 = icmp slt i32 %28, %30
br i1 %31, label %32, label %35
32: ; preds = %26
%33 = load i8*, i8** %8
%34 = icmp ne i8* %33, null
br label %35
35: ; preds = %32, %26
%36 = phi i1 [ false, %26 ], [ %34, %32 ]
br i1 %36, label %37, label %104
37: ; preds = %35
%38 = load i8*, i8** %8
store i8 0, i8* %38
%39 = load i8*, i8** %4
%40 = load i8*, i8** %7
%41 = load i32, i32* %6
%42 = call i32 @strncpy(i8* %39, i8* %40, i32 %41)
%43 = load i8*, i8** %8
store i8 36, i8* %43
%44 = load i8*, i8** %8
%45 = getelementptr inbounds i8, i8* %44, i64 1
%46 = load i8, i8* %45
%47 = sext i8 %46 to i32
%48 = icmp ne i32 %47, 123
br i1 %48, label %49, label %64
49: ; preds = %37
%50 = load i8*, i8** %4
%51 = load i32, i32* %6
%52 = load i8*, i8** %4
%53 = call i32 @strlen(i8* %52)
%54 = sub nsw i32 %51, %53
%55 = sub nsw i32 %54, 1
%56 = call i32 @strncat(i8* %50, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 %55)
%57 = load i8*, i8** %4
%58 = load i32, i32* %6
%59 = sub nsw i32 %58, 1
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds i8, i8* %57, i64 %60
store i8 0, i8* %61
%62 = load i8*, i8** %8
%63 = getelementptr inbounds i8, i8* %62, i64 1
store i8* %63, i8** %8
br label %100
64: ; preds = %37
%65 = load i8*, i8** %8
%66 = getelementptr inbounds i8, i8* %65, i64 2
%67 = call i8* @strchr(i8* %66, i8 signext 125)
store i8* %67, i8** %9
%68 = icmp eq i8* %67, null
br i1 %68, label %69, label %73
69: ; preds = %64
%70 = load i32, i32* @stderr
%71 = load i8*, i8** %5
%72 = call i32 @fprintf(i32 %70, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([178 x i8], [178 x i8]* @.str.2, i64 0, i64 0), i32 252, i8* %71)
br label %104
73: ; preds = %64
%74 = load i8*, i8** %9
store i8 0, i8* %74
%75 = load i8*, i8** %8
%76 = getelementptr inbounds i8, i8* %75, i64 2
%77 = call i8* @getenv(i8* %76)
%78 = icmp ne i8* %77, null
br i1 %78, label %79, label %96
79: ; preds = %73
%80 = load i8*, i8** %4
%81 = load i8*, i8** %8
%82 = getelementptr inbounds i8, i8* %81, i64 2
%83 = call i8* @getenv(i8* %82)
%84 = load i32, i32* %6
%85 = load i8*, i8** %4
%86 = call i32 @strlen(i8* %85)
%87 = sub nsw i32 %84, %86
%88 = sub nsw i32 %87, 1
%89 = call i32 @strncat(i8* %80, i8* %83, i32 %88)
%90 = load i8*, i8** %4
%91 = load i32, i32* %6
%92 = sub nsw i32 %91, 1
%93 = sext i32 %92 to i64
%94 = getelementptr inbounds i8, i8* %90, i64 %93
store i8 0, i8* %94
%95 = load i8*, i8** %9
store i8 125, i8* %95
br label %96
96: ; preds = %79, %73
%97 = load i8*, i8** %9
%98 = getelementptr inbounds i8, i8* %97, i64 1
store i8* %98, i8** %8
br label %99
99: ; preds = %96
br label %100
100: ; preds = %99, %49
%101 = load i8*, i8** %8
store i8* %101, i8** %7
%102 = load i8*, i8** %7
%103 = call i8* @strchr(i8* %102, i8 signext 36)
store i8* %103, i8** %8
br label %26
104: ; preds = %69, %35
%105 = load i8*, i8** %4
%106 = load i8*, i8** %7
%107 = load i32, i32* %6
%108 = load i8*, i8** %4
%109 = call i32 @strlen(i8* %108)
%110 = sub nsw i32 %107, %109
%111 = sub nsw i32 %110, 1
%112 = call i32 @strncat(i8* %105, i8* %106, i32 %111)
%113 = load i8*, i8** %4
%114 = load i32, i32* %6
%115 = sub nsw i32 %114, 1
%116 = sext i32 %115 to i64
%117 = getelementptr inbounds i8, i8* %113, i64 %116
store i8 0, i8* %117
br label %118
118: ; preds = %104, %14
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"$\00"
@stderr = global i32 0
@.str.1 = [42 x i8] c"*** ERROR [%s:%d] - Unterminated ENV: %s\0A\00"
@.str.2 = [178 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/uwbmrb/BMRB-API/refs/heads/master/server/wsgi/bmrbapi/submodules/fasta36/src/compacc2.c\00"
define void @subs_env(i8* %0, i8* %1, i32 %2) {
%4 = tail call i8* @strchr(i8* dereferenceable(1) %1, i8 signext 36)
%5 = icmp eq i8* %4, null
br i1 %5, label %6, label %10
6: ; preds = %3
%7 = tail call i32 @strncpy(i8* %0, i8* %1, i32 %2)
%8 = add nsw i32 %2, -1
%9 = sext i32 %8 to i64
br label %59
10: ; preds = %3
store i8 0, i8* %0
%11 = tail call i32 @strlen(i8* %0)
%12 = add nsw i32 %2, -1
%13 = icmp slt i32 %11, %12
%14 = sext i32 %12 to i64
br i1 %13, label %15, label %53
15: ; preds = %10
%16 = getelementptr inbounds i8, i8* %0, i64 %14
br label %17
17: ; preds = %15, %46
%18 = phi i8* [ %1, %15 ], [ %47, %46 ]
%19 = phi i8* [ %4, %15 ], [ %48, %46 ]
store i8 0, i8* %19
%20 = tail call i32 @strncpy(i8* %0, i8* %18, i32 %2)
store i8 36, i8* %19
%21 = getelementptr inbounds i8, i8* %19, i64 1
%22 = load i8, i8* %21
%23 = icmp eq i8 %22, 123
br i1 %23, label %29, label %24
24: ; preds = %17
%25 = tail call i32 @strlen(i8* %0)
%26 = xor i32 %25, -1
%27 = add i32 %26, %2
%28 = tail call i32 @strncat(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 %27)
store i8 0, i8* %16
br label %46
29: ; preds = %17
%30 = getelementptr inbounds i8, i8* %19, i64 2
%31 = tail call i8* @strchr(i8* %30, i8 signext 125)
%32 = icmp eq i8* %31, null
br i1 %32, label %33, label %36
33: ; preds = %29
%34 = load i32, i32* @stderr
%35 = tail call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([178 x i8], [178 x i8]* @.str.2, i64 0, i64 0), i32 252, i8* %1)
br label %53
36: ; preds = %29
store i8 0, i8* %31
%37 = tail call i8* @getenv(i8* %30)
%38 = icmp eq i8* %37, null
br i1 %38, label %44, label %39
39: ; preds = %36
%40 = tail call i32 @strlen(i8* %0)
%41 = xor i32 %40, -1
%42 = add i32 %41, %2
%43 = tail call i32 @strncat(i8* %0, i8* %37, i32 %42)
store i8 0, i8* %16
store i8 125, i8* %31
br label %44
44: ; preds = %39, %36
%45 = getelementptr inbounds i8, i8* %31, i64 1
br label %46
46: ; preds = %44, %24
%47 = phi i8* [ %21, %24 ], [ %45, %44 ]
%48 = tail call i8* @strchr(i8* %47, i8 signext 36)
%49 = tail call i32 @strlen(i8* %0)
%50 = icmp slt i32 %49, %12
%51 = icmp ne i8* %48, null
%52 = select i1 %50, i1 %51, i1 false
br i1 %52, label %17, label %53
53: ; preds = %46, %10, %33
%54 = phi i8* [ %18, %33 ], [ %1, %10 ], [ %47, %46 ]
%55 = tail call i32 @strlen(i8* %0)
%56 = xor i32 %55, -1
%57 = add i32 %56, %2
%58 = tail call i32 @strncat(i8* %0, i8* %54, i32 %57)
br label %59
59: ; preds = %53, %6
%60 = phi i64 [ %14, %53 ], [ %9, %6 ]
%61 = getelementptr inbounds i8, i8* %0, i64 %60
store i8 0, i8* %61
ret void
}