#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 */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ memcmp(char *, char *, int);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ sprintf(char *, char *, char *);
int /*<<< orphan*/ stderr;
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *strstr(char *, char *);
int proc_cgi(char *outdir, FILE *outfp, char *basic_name, char *infile,
             int filelen) {
  int newblk_len, flag;
  char *blk_end, *newblk;
  char cgilist[255];
  FILE *cgifp;
  char *end = infile + filelen;
  flag = 0;
  while (infile < end) {
    if (!memcmp(infile, "<!--DO_CGI", strlen("<!--DO_CGI"))) {
      flag = 1;
      if ((blk_end = strstr(infile, "DO_CGI-->")) == NULL) {
        printf("can not find %s\n", "DO_CGI-->");
        return 0;
      }
      break;
    }
    infile++;
  }
  if (!flag)
    return 0;
  newblk_len = blk_end - infile;
  newblk = (char *)malloc(newblk_len + 1);
  strncpy(newblk, infile, newblk_len);
  *(newblk + newblk_len) = '\0';
  newblk = newblk + strlen("<!--DO_CGI");
  sprintf(cgilist, "%s/cgi_list", outdir);
  if ((cgifp = fopen(cgilist, "a")) == NULL) {
    fprintf(stderr, "Can't open %s\n", cgilist);
    return 0;
  }
  fprintf(cgifp, "extern char *cgi_%s(struct connstruct *cn);\n", basic_name);
  fclose(cgifp);
  fprintf(outfp, "\nchar *cgi_%s(struct connstruct *cn)\n{", basic_name);
  fprintf(outfp, "%s", newblk);
  fprintf(outfp, "}\n");
  free(newblk);
  return 1;
}


x86 assembly (clang, -O0):
<proc_cgi>:
push   %rbp
mov    %rsp,%rbp
sub    $0x170,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8d,-0x2c(%rbp)
mov    -0x28(%rbp),%rax
movslq -0x2c(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x160(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x160(%rbp),%rax
jae    1288 <proc_cgi+0xd8>
mov    -0x28(%rbp),%rax
mov    %rax,-0x168(%rbp)
lea    0xdf5(%rip),%rdi
call   1060 <strlen@plt>
mov    -0x168(%rbp),%rdi
mov    %eax,%edx
lea    0xde0(%rip),%rsi
call   1080 <memcmp@plt>
cmp    $0x0,%eax
jne    1277 <proc_cgi+0xc7>
movl   $0x1,-0x34(%rbp)
mov    -0x28(%rbp),%rdi
lea    0xdcb(%rip),%rsi
call   10d0 <strstr@plt>
mov    %rax,-0x40(%rbp)
cmp    $0x0,%rax
jne    1272 <proc_cgi+0xc2>
lea    0xdbb(%rip),%rdi
lea    0xdaa(%rip),%rsi
call   1070 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    13da <proc_cgi+0x22a>
jmp    1288 <proc_cgi+0xd8>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    11e8 <proc_cgi+0x38>
cmpl   $0x0,-0x34(%rbp)
jne    129e <proc_cgi+0xee>
movl   $0x0,-0x4(%rbp)
jmp    13da <proc_cgi+0x22a>
mov    -0x40(%rbp),%rax
mov    -0x28(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%edi
add    $0x1,%edi
call   10a0 <malloc@plt>
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x30(%rbp),%edx
call   1040 <strncpy@plt>
mov    -0x48(%rbp),%rax
movslq -0x30(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x48(%rbp),%rax
mov    %rax,-0x170(%rbp)
lea    0xd17(%rip),%rdi
call   1060 <strlen@plt>
mov    %eax,%ecx
mov    -0x170(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x48(%rbp)
lea    -0x150(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xd13(%rip),%rsi
call   10c0 <sprintf@plt>
lea    -0x150(%rbp),%rdi
lea    0xd0c(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x158(%rbp)
cmp    $0x0,%rax
jne    1367 <proc_cgi+0x1b7>
mov    0x2c9d(%rip),%rax
movslq (%rax),%rdi
lea    -0x150(%rbp),%rdx
lea    0xce0(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    13da <proc_cgi+0x22a>
mov    -0x158(%rbp),%rdi
mov    -0x20(%rbp),%rdx
lea    0xcca(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    -0x158(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rdx
lea    0xcd5(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    -0x18(%rbp),%rdi
mov    -0x48(%rbp),%rdx
lea    0xce6(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    -0x18(%rbp),%rdi
lea    0xcd7(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    -0x48(%rbp),%rdi
call   1030 <free@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x170,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<proc_cgi>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x118,%rsp
xor    %r13d,%r13d
test   %r8d,%r8d
jle    1364 <proc_cgi+0x194>
mov    %rcx,%rbx
mov    %rdi,%r12
mov    %rdx,(%rsp)
mov    %rsi,0x8(%rsp)
movslq %r8d,%rbp
add    %rcx,%rbp
lea    0xdf7(%rip),%r14
mov    %rcx,%r15
nopl   0x0(%rax)
mov    %r14,%rdi
call   1060 <strlen@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %eax,%edx
call   1090 <memcmp@plt>
test   %eax,%eax
je     123b <proc_cgi+0x6b>
add    $0x1,%rbx
add    $0x1,%r15
cmp    %rbp,%rbx
jb     1210 <proc_cgi+0x40>
jmp    1364 <proc_cgi+0x194>
lea    0xdc9(%rip),%rsi
mov    %rbx,%rdi
call   10f0 <strstr@plt>
test   %rax,%rax
je     132f <proc_cgi+0x15f>
mov    %rax,%r14
sub    %r15,%r14
lea    0x1(%r14),%edi
call   10b0 <malloc@plt>
mov    %rax,%rbp
mov    %rax,%rdi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1040 <strncpy@plt>
movslq %r14d,%rax
movb   $0x0,0x0(%rbp,%rax,1)
lea    0xd7e(%rip),%rdi
call   1060 <strlen@plt>
mov    %eax,%r14d
lea    0xd95(%rip),%rsi
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    %r12,%rdx
call   10d0 <sprintf@plt>
lea    0xd8a(%rip),%rsi
mov    %rbx,%rdi
call   10c0 <fopen@plt>
test   %rax,%rax
je     1344 <proc_cgi+0x174>
mov    %rax,%rbx
movslq %r14d,%rax
add    %rax,%rbp
lea    0xd7a(%rip),%rsi
mov    %rbx,%rdi
mov    (%rsp),%r14
mov    %r14,%rdx
xor    %eax,%eax
call   10a0 <fprintf@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
lea    0xd87(%rip),%rsi
mov    0x8(%rsp),%rbx
mov    %rbx,%rdi
mov    %r14,%rdx
xor    %eax,%eax
call   10a0 <fprintf@plt>
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1080 <fputs@plt>
lea    0xd8a(%rip),%rdi
mov    $0x2,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   10e0 <fwrite@plt>
mov    %rbp,%rdi
call   1030 <free@plt>
mov    $0x1,%r13d
jmp    1364 <proc_cgi+0x194>
lea    0xcdf(%rip),%rdi
lea    0xcce(%rip),%rsi
call   1070 <printf@plt>
jmp    1364 <proc_cgi+0x194>
mov    0x2c95(%rip),%rax
movslq (%rax),%rdi
lea    0xcdf(%rip),%rsi
xor    %r13d,%r13d
lea    0x10(%rsp),%rdx
xor    %eax,%eax
call   10a0 <fprintf@plt>
mov    %r13d,%eax
add    $0x118,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [11 x i8] c"<!--DO_CGI\00"
@.str.1 = [10 x i8] c"DO_CGI-->\00"
@.str.2 = [17 x i8] c"can not find %s\0A\00"
@.str.3 = [12 x i8] c"%s/cgi_list\00"
@.str.4 = [2 x i8] c"a\00"
@stderr = global i32 0
@.str.5 = [15 x i8] c"Can't open %s\0A\00"
@.str.6 = [45 x i8] c"extern char *cgi_%s(struct connstruct *cn);\0A\00"
@.str.7 = [39 x i8] c"\0Achar *cgi_%s(struct connstruct *cn)\0A{\00"
@.str.8 = [3 x i8] c"%s\00"
@.str.9 = [3 x i8] c"}\0A\00"
define i32 @proc_cgi(i8* %0, i32* %1, i8* %2, i8* %3, i32 %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i8*
%15 = alloca i8*
%16 = alloca [255 x i8]
%17 = alloca i32*
%18 = alloca i8*
store i8* %0, i8** %7
store i32* %1, i32** %8
store i8* %2, i8** %9
store i8* %3, i8** %10
store i32 %4, i32* %11
%19 = load i8*, i8** %10
%20 = load i32, i32* %11
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i8, i8* %19, i64 %21
store i8* %22, i8** %18
store i32 0, i32* %13
br label %23
23: ; preds = %39, %5
%24 = load i8*, i8** %10
%25 = load i8*, i8** %18
%26 = icmp ult i8* %24, %25
br i1 %26, label %27, label %42
27: ; preds = %23
%28 = load i8*, i8** %10
%29 = call i32 @strlen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%30 = call i32 @memcmp(i8* %28, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %29)
%31 = icmp ne i32 %30, 0
br i1 %31, label %39, label %32
32: ; preds = %27
store i32 1, i32* %13
%33 = load i8*, i8** %10
%34 = call i8* @strstr(i8* %33, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
store i8* %34, i8** %14
%35 = icmp eq i8* %34, null
br i1 %35, label %36, label %38
36: ; preds = %32
%37 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %6
br label %97
38: ; preds = %32
br label %42
39: ; preds = %27
%40 = load i8*, i8** %10
%41 = getelementptr inbounds i8, i8* %40, i32 1
store i8* %41, i8** %10
br label %23
42: ; preds = %38, %23
%43 = load i32, i32* %13
%44 = icmp ne i32 %43, 0
br i1 %44, label %46, label %45
45: ; preds = %42
store i32 0, i32* %6
br label %97
46: ; preds = %42
%47 = load i8*, i8** %14
%48 = load i8*, i8** %10
%49 = ptrtoint i8* %47 to i64
%50 = ptrtoint i8* %48 to i64
%51 = sub i64 %49, %50
%52 = trunc i64 %51 to i32
store i32 %52, i32* %12
%53 = load i32, i32* %12
%54 = add nsw i32 %53, 1
%55 = call i64 @malloc(i32 %54)
%56 = inttoptr i64 %55 to i8*
store i8* %56, i8** %15
%57 = load i8*, i8** %15
%58 = load i8*, i8** %10
%59 = load i32, i32* %12
%60 = call i32 @strncpy(i8* %57, i8* %58, i32 %59)
%61 = load i8*, i8** %15
%62 = load i32, i32* %12
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds i8, i8* %61, i64 %63
store i8 0, i8* %64
%65 = load i8*, i8** %15
%66 = call i32 @strlen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%67 = sext i32 %66 to i64
%68 = getelementptr inbounds i8, i8* %65, i64 %67
store i8* %68, i8** %15
%69 = getelementptr inbounds [255 x i8], [255 x i8]* %16, i64 0, i64 0
%70 = load i8*, i8** %7
%71 = call i32 @sprintf(i8* %69, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i8* %70)
%72 = getelementptr inbounds [255 x i8], [255 x i8]* %16, i64 0, i64 0
%73 = call i32* @fopen(i8* %72, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %73, i32** %17
%74 = icmp eq i32* %73, null
br i1 %74, label %75, label %81
75: ; preds = %46
%76 = load i32, i32* @stderr
%77 = sext i32 %76 to i64
%78 = inttoptr i64 %77 to i32*
%79 = getelementptr inbounds [255 x i8], [255 x i8]* %16, i64 0, i64 0
%80 = call i32 (i32*, i8*, ...) @fprintf(i32* %78, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0), i8* %79)
store i32 0, i32* %6
br label %97
81: ; preds = %46
%82 = load i32*, i32** %17
%83 = load i8*, i8** %9
%84 = call i32 (i32*, i8*, ...) @fprintf(i32* %82, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.6, i64 0, i64 0), i8* %83)
%85 = load i32*, i32** %17
%86 = call i32 @fclose(i32* %85)
%87 = load i32*, i32** %8
%88 = load i8*, i8** %9
%89 = call i32 (i32*, i8*, ...) @fprintf(i32* %87, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.7, i64 0, i64 0), i8* %88)
%90 = load i32*, i32** %8
%91 = load i8*, i8** %15
%92 = call i32 (i32*, i8*, ...) @fprintf(i32* %90, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.8, i64 0, i64 0), i8* %91)
%93 = load i32*, i32** %8
%94 = call i32 (i32*, i8*, ...) @fprintf(i32* %93, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.9, i64 0, i64 0))
%95 = load i8*, i8** %15
%96 = call i32 @free(i8* %95)
store i32 1, i32* %6
br label %97
97: ; preds = %81, %75, %45, %36
%98 = load i32, i32* %6
ret i32 %98
}

LLVM IR (-O2):
@.str = [11 x i8] c"<!--DO_CGI\00"
@.str.1 = [10 x i8] c"DO_CGI-->\00"
@.str.2 = [17 x i8] c"can not find %s\0A\00"
@.str.3 = [12 x i8] c"%s/cgi_list\00"
@.str.4 = [2 x i8] c"a\00"
@stderr = global i32 0
@.str.5 = [15 x i8] c"Can't open %s\0A\00"
@.str.6 = [45 x i8] c"extern char *cgi_%s(struct connstruct *cn);\0A\00"
@.str.7 = [39 x i8] c"\0Achar *cgi_%s(struct connstruct *cn)\0A{\00"
@.str.9 = [3 x i8] c"}\0A\00"
define i32 @proc_cgi(i8* %0, i32* nocapture %1, i8* %2, i8* %3, i32 %4) {
%6 = alloca [255 x i8]
%7 = getelementptr inbounds [255 x i8], [255 x i8]* %6, i64 0, i64 0
%8 = sext i32 %4 to i64
%9 = getelementptr inbounds i8, i8* %3, i64 %8
%10 = icmp sgt i32 %4, 0
br i1 %10, label %11, label %54
11: ; preds = %5, %21
%12 = phi i8* [ %22, %21 ], [ %3, %5 ]
%13 = tail call i32 @strlen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%14 = tail call i32 @memcmp(i8* %12, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %13)
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %21
16: ; preds = %11
%17 = tail call i8* @strstr(i8* dereferenceable(1) %12, i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %24
19: ; preds = %16
%20 = tail call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
br label %54
21: ; preds = %11
%22 = getelementptr inbounds i8, i8* %12, i64 1
%23 = icmp ult i8* %22, %9
br i1 %23, label %11, label %54
24: ; preds = %16
%25 = ptrtoint i8* %17 to i64
%26 = ptrtoint i8* %12 to i64
%27 = sub i64 %25, %26
%28 = trunc i64 %27 to i32
%29 = add nsw i32 %28, 1
%30 = tail call i64 @malloc(i32 %29)
%31 = inttoptr i64 %30 to i8*
%32 = tail call i32 @strncpy(i8* %31, i8* %12, i32 %28)
%33 = shl i64 %27, 32
%34 = ashr exact i64 %33, 32
%35 = getelementptr inbounds i8, i8* %31, i64 %34
store i8 0, i8* %35
%36 = tail call i32 @strlen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i8, i8* %31, i64 %37
%39 = call i32 @sprintf(i8* %7, i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i8* %0)
%40 = call i32* @fopen(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%41 = icmp eq i32* %40, null
br i1 %41, label %42, label %47
42: ; preds = %24
%43 = load i32, i32* @stderr
%44 = sext i32 %43 to i64
%45 = inttoptr i64 %44 to i32*
%46 = call i32 (i32*, i8*, ...) @fprintf(i32* %45, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0), i8* %7)
br label %54
47: ; preds = %24
%48 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %40, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.6, i64 0, i64 0), i8* %2)
%49 = tail call i32 @fclose(i32* %40)
%50 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %1, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.7, i64 0, i64 0), i8* %2)
%51 = tail call i32 @fputs(i8* %38, i32* %1)
%52 = tail call i64 @fwrite(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.9, i64 0, i64 0), i64 2, i64 1, i32* %1)
%53 = tail call i32 @free(i8* %38)
br label %54
54: ; preds = %21, %5, %47, %42, %19
%55 = phi i32 [ 0, %19 ], [ 0, %42 ], [ 1, %47 ], [ 0, %5 ], [ 0, %21 ]
ret i32 %55
}