#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 *get_value(char *);
int /*<<< orphan*/ memcpy(char *, char const *, int);
char *strchr(char const *, char);
int /*<<< orphan*/ strcpy(char *, char const *);
int strlen(char const *);
int /*<<< orphan*/ strncpy(char *, char const *, int);
int subst_line(const char *origbuffer, char *newbuffer) {
  const char *atptr1, *atptr2, *substval;
  char varname[4096];
  int varnamelen, varoffset, substvarlen;
  atptr1 = strchr(origbuffer, '@');
  if (!atptr1)
    return 0;
  do {
    atptr2 = strchr(atptr1 + 1, '@');
    if (!atptr2)
      return 0;
    varnamelen = atptr2 - atptr1 - 1;
    strncpy(varname, atptr1 + 1, varnamelen);
    varname[varnamelen] = '\0';
    do {
    } while (0);
    substval = get_value(varname);
    if (substval) {
      varoffset = atptr1 - origbuffer;
      substvarlen = strlen(substval);
      memcpy(newbuffer, origbuffer, varoffset);
      memcpy(newbuffer + varoffset, substval, substvarlen);
      strcpy(newbuffer + varoffset + substvarlen, atptr2 + 1);
      return (!0);
    }
    atptr1 = atptr2;
  } while ((!0));
}


x86 assembly (clang, -O0):
<subst_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1040,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x40,%esi
call   1060 <strchr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    119c <subst_line+0x3c>
movl   $0x0,-0x4(%rbp)
jmp    12c4 <subst_line+0x164>
jmp    11a1 <subst_line+0x41>
mov    -0x20(%rbp),%rdi
add    $0x1,%rdi
mov    $0x40,%esi
call   1060 <strchr@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11ce <subst_line+0x6e>
movl   $0x0,-0x4(%rbp)
jmp    12c4 <subst_line+0x164>
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    %rcx,%rax
sub    $0x1,%rax
mov    %eax,-0x1034(%rbp)
lea    -0x1030(%rbp),%rdi
mov    -0x20(%rbp),%rsi
add    $0x1,%rsi
mov    -0x1034(%rbp),%edx
call   1030 <strncpy@plt>
movslq -0x1034(%rbp),%rax
movb   $0x0,-0x1030(%rbp,%rax,1)
jmp    1211 <subst_line+0xb1>
lea    -0x1030(%rbp),%rdi
call   1080 <get_value@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     12ad <subst_line+0x14d>
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x1038(%rbp)
mov    -0x30(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x103c(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x1038(%rbp),%edx
call   1070 <memcpy@plt>
mov    -0x18(%rbp),%rdi
movslq -0x1038(%rbp),%rax
add    %rax,%rdi
mov    -0x30(%rbp),%rsi
mov    -0x103c(%rbp),%edx
call   1070 <memcpy@plt>
mov    -0x18(%rbp),%rdi
movslq -0x1038(%rbp),%rax
add    %rax,%rdi
movslq -0x103c(%rbp),%rax
add    %rax,%rdi
mov    -0x28(%rbp),%rsi
add    $0x1,%rsi
call   1040 <strcpy@plt>
movl   $0x1,-0x4(%rbp)
jmp    12c4 <subst_line+0x164>
mov    -0x28(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    $0x1,%al
test   $0x1,%al
jne    11a1 <subst_line+0x41>
jmp    12c4 <subst_line+0x164>
mov    -0x4(%rbp),%eax
add    $0x1040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<subst_line>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1018,%rsp
mov    %rsi,%rbx
mov    %rdi,%rbp
mov    $0x40,%esi
call   1060 <strchr@plt>
mov    %rax,%r15
xor    %eax,%eax
test   %r15,%r15
je     1242 <subst_line+0xe2>
mov    %rbp,(%rsp)
mov    %rbx,0x8(%rsp)
lea    0x10(%rsp),%r13
nopl   (%rax)
mov    %r15,%r12
lea    0x1(%r15),%rbp
mov    %rbp,%rdi
mov    $0x40,%esi
call   1060 <strchr@plt>
test   %rax,%rax
je     1240 <subst_line+0xe0>
mov    %rax,%r15
mov    %r12,%rbx
not    %rbx
add    %rax,%rbx
mov    %r13,%rdi
mov    %rbp,%rsi
mov    %ebx,%edx
call   1030 <strncpy@plt>
movslq %ebx,%rax
movb   $0x0,0x10(%rsp,%rax,1)
mov    %r13,%rdi
call   1080 <get_value@plt>
test   %rax,%rax
je     11a0 <subst_line+0x40>
mov    %rax,%r14
mov    (%rsp),%rbp
sub    %rbp,%r12
mov    %rax,%rdi
call   1050 <strlen@plt>
mov    %eax,%r13d
mov    0x8(%rsp),%rbx
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    %r12d,%edx
call   1070 <memcpy@plt>
movslq %r12d,%rbp
add    %rbx,%rbp
mov    %rbp,%rdi
mov    %r14,%rsi
mov    %r13d,%edx
call   1070 <memcpy@plt>
movslq %r13d,%rdi
add    %rbp,%rdi
add    $0x1,%r15
mov    %r15,%rsi
call   1040 <strcpy@plt>
mov    $0x1,%eax
jmp    1242 <subst_line+0xe2>
xor    %eax,%eax
add    $0x1018,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @subst_line(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca [4096 x i8]
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%13 = load i8*, i8** %4
%14 = call i8* @strchr(i8* %13, i8 signext 64)
store i8* %14, i8** %6
%15 = load i8*, i8** %6
%16 = icmp ne i8* %15, null
br i1 %16, label %18, label %17
17: ; preds = %2
store i32 0, i32* %3
br label %81
18: ; preds = %2
br label %19
19: ; preds = %80, %18
%20 = load i8*, i8** %6
%21 = getelementptr inbounds i8, i8* %20, i64 1
%22 = call i8* @strchr(i8* %21, i8 signext 64)
store i8* %22, i8** %7
%23 = load i8*, i8** %7
%24 = icmp ne i8* %23, null
br i1 %24, label %26, label %25
25: ; preds = %19
store i32 0, i32* %3
br label %81
26: ; preds = %19
%27 = load i8*, i8** %7
%28 = load i8*, i8** %6
%29 = ptrtoint i8* %27 to i64
%30 = ptrtoint i8* %28 to i64
%31 = sub i64 %29, %30
%32 = sub nsw i64 %31, 1
%33 = trunc i64 %32 to i32
store i32 %33, i32* %10
%34 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%35 = load i8*, i8** %6
%36 = getelementptr inbounds i8, i8* %35, i64 1
%37 = load i32, i32* %10
%38 = call i32 @strncpy(i8* %34, i8* %36, i32 %37)
%39 = load i32, i32* %10
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 %40
store i8 0, i8* %41
br label %42
42: ; preds = %26
br label %43
43: ; preds = %42
%44 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%45 = call i8* @get_value(i8* %44)
store i8* %45, i8** %8
%46 = load i8*, i8** %8
%47 = icmp ne i8* %46, null
br i1 %47, label %48, label %78
48: ; preds = %43
%49 = load i8*, i8** %6
%50 = load i8*, i8** %4
%51 = ptrtoint i8* %49 to i64
%52 = ptrtoint i8* %50 to i64
%53 = sub i64 %51, %52
%54 = trunc i64 %53 to i32
store i32 %54, i32* %11
%55 = load i8*, i8** %8
%56 = call i32 @strlen(i8* %55)
store i32 %56, i32* %12
%57 = load i8*, i8** %5
%58 = load i8*, i8** %4
%59 = load i32, i32* %11
%60 = call i32 @memcpy(i8* %57, i8* %58, i32 %59)
%61 = load i8*, i8** %5
%62 = load i32, i32* %11
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds i8, i8* %61, i64 %63
%65 = load i8*, i8** %8
%66 = load i32, i32* %12
%67 = call i32 @memcpy(i8* %64, i8* %65, i32 %66)
%68 = load i8*, i8** %5
%69 = load i32, i32* %11
%70 = sext i32 %69 to i64
%71 = getelementptr inbounds i8, i8* %68, i64 %70
%72 = load i32, i32* %12
%73 = sext i32 %72 to i64
%74 = getelementptr inbounds i8, i8* %71, i64 %73
%75 = load i8*, i8** %7
%76 = getelementptr inbounds i8, i8* %75, i64 1
%77 = call i32 @strcpy(i8* %74, i8* %76)
store i32 1, i32* %3
br label %81
78: ; preds = %43
%79 = load i8*, i8** %7
store i8* %79, i8** %6
br label %80
80: ; preds = %78
br i1 true, label %19, label %81
81: ; preds = %17, %25, %48, %80
%82 = load i32, i32* %3
ret i32 %82
}

LLVM IR (-O2):
define i32 @subst_line(i8* %0, i8* %1) {
%3 = alloca [4096 x i8]
%4 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%5 = tail call i8* @strchr(i8* dereferenceable(1) %0, i8 signext 64)
%6 = icmp eq i8* %5, null
br i1 %6, label %39, label %7
7: ; preds = %2, %12
%8 = phi i8* [ %10, %12 ], [ %5, %2 ]
%9 = getelementptr inbounds i8, i8* %8, i64 1
%10 = call i8* @strchr(i8* %9, i8 signext 64)
%11 = icmp eq i8* %10, null
br i1 %11, label %39, label %12
12: ; preds = %7
%13 = ptrtoint i8* %10 to i64
%14 = ptrtoint i8* %8 to i64
%15 = xor i64 %14, -1
%16 = add i64 %13, %15
%17 = trunc i64 %16 to i32
%18 = call i32 @strncpy(i8* %4, i8* %9, i32 %17)
%19 = shl i64 %16, 32
%20 = ashr exact i64 %19, 32
%21 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 %20
store i8 0, i8* %21
%22 = call i8* @get_value(i8* %4)
%23 = icmp eq i8* %22, null
br i1 %23, label %7, label %24
24: ; preds = %12
%25 = ptrtoint i8* %8 to i64
%26 = ptrtoint i8* %0 to i64
%27 = sub i64 %25, %26
%28 = trunc i64 %27 to i32
%29 = call i32 @strlen(i8* %22)
%30 = call i32 @memcpy(i8* %1, i8* %0, i32 %28)
%31 = shl i64 %27, 32
%32 = ashr exact i64 %31, 32
%33 = getelementptr inbounds i8, i8* %1, i64 %32
%34 = call i32 @memcpy(i8* %33, i8* %22, i32 %29)
%35 = sext i32 %29 to i64
%36 = getelementptr inbounds i8, i8* %33, i64 %35
%37 = getelementptr inbounds i8, i8* %10, i64 1
%38 = call i32 @strcpy(i8* %36, i8* %37)
br label %39
39: ; preds = %7, %2, %24
%40 = phi i32 [ 1, %24 ], [ 0, %2 ], [ 0, %7 ]
ret i32 %40
}