#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*/ buf;

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char *);
int /*<<< orphan*/ free(char *);
scalar_t__ realloc(char *, int);
char *sql_generate(char const *, ...);
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ strcat(char *, char *);
unsigned int strlen(char *);
void thread_redirect(const char *board_id, long parent_id, long post_id) {
  const char *redir = "<meta http-equiv=\"refresh\" content=\"%ld; url=%s\">";
  const char *redir_link = "<div class=\"navi controls\">"
                           "If you are not redirected shortly, please [<a "
                           "href=\"%s\">click here</a>]."
                           "</div>";
  const char *url = "%s?board=%s&thread=%ld";
  const char *permalink = "#p%ld";
  char *buf[4] = {0};
  buf[0] = sql_generate(url, "/board.cgi", board_id, parent_id);
  if (post_id > parent_id) {
    buf[1] = sql_generate(permalink, post_id);
    unsigned size = strlen(buf[0]) + strlen(buf[1]);
    buf[0] = (char *)realloc(buf[0], sizeof(char) * size + 1);
    strcat(buf[0], buf[1]);
  }
  buf[2] = sql_generate(redir, 1, buf[0]);
  buf[3] = sql_generate(redir_link, buf[0]);
  fprintf(stdout, "%s%s", buf[2], buf[3]);
  unsigned i;
  for (i = 0; i < (sizeof(buf) / sizeof(*buf)); i++)
    free((!buf[i]) ? NULL : buf[i]);
}


x86 assembly (clang, -O0):
<thread_redirect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
lea    0xe72(%rip),%rax
mov    %rax,-0x20(%rbp)
lea    0xe99(%rip),%rax
mov    %rax,-0x28(%rbp)
lea    0xef8(%rip),%rax
mov    %rax,-0x30(%rbp)
lea    0xf04(%rip),%rax
mov    %rax,-0x38(%rbp)
lea    -0x60(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1060 <memset@plt>
mov    -0x30(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0xee3(%rip),%rsi
mov    $0x0,%al
call   1040 <sql_generate@plt>
mov    %rax,-0x60(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x10(%rbp),%rax
jle    1248 <thread_redirect+0xd8>
mov    -0x38(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    $0x0,%al
call   1040 <sql_generate@plt>
mov    %rax,-0x58(%rbp)
mov    -0x60(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x6c(%rbp)
mov    -0x58(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x6c(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x64(%rbp)
mov    -0x60(%rbp),%rdi
mov    -0x64(%rbp),%eax
shl    $0x0,%rax
add    $0x1,%rax
mov    %eax,%esi
call   1080 <realloc@plt>
mov    %rax,-0x60(%rbp)
mov    -0x60(%rbp),%rdi
mov    -0x58(%rbp),%rsi
call   1090 <strcat@plt>
mov    -0x20(%rbp),%rdi
mov    -0x60(%rbp),%rdx
mov    $0x1,%esi
mov    $0x0,%al
call   1040 <sql_generate@plt>
mov    %rax,-0x50(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x60(%rbp),%rsi
mov    $0x0,%al
call   1040 <sql_generate@plt>
mov    %rax,-0x48(%rbp)
mov    0x2d66(%rip),%rax
mov    (%rax),%edi
mov    -0x50(%rbp),%rdx
mov    -0x48(%rbp),%rcx
lea    0xe39(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0x0,-0x68(%rbp)
mov    -0x68(%rbp),%eax
cmp    $0x4,%rax
jae    12e1 <thread_redirect+0x171>
mov    -0x68(%rbp),%eax
cmpq   $0x0,-0x60(%rbp,%rax,8)
jne    12be <thread_redirect+0x14e>
xor    %eax,%eax
mov    %rax,-0x78(%rbp)
jmp    12ca <thread_redirect+0x15a>
mov    -0x68(%rbp),%eax
mov    -0x60(%rbp,%rax,8),%rax
mov    %rax,-0x78(%rbp)
mov    -0x78(%rbp),%rdi
call   1030 <free@plt>
mov    -0x68(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x68(%rbp)
jmp    1297 <thread_redirect+0x127>
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<thread_redirect>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%rbx
mov    %rdi,%rdx
lea    0xf24(%rip),%rdi
lea    0xf3a(%rip),%rsi
xor    %r14d,%r14d
mov    %rbx,%rcx
xor    %eax,%eax
call   1040 <sql_generate@plt>
mov    %rax,%r12
cmp    %rbx,%r15
jle    11d6 <thread_redirect+0x76>
lea    0xf18(%rip),%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1040 <sql_generate@plt>
mov    %rax,%r14
mov    %r12,%rdi
call   1050 <strlen@plt>
mov    %eax,%ebx
mov    %r14,%rdi
call   1050 <strlen@plt>
lea    (%rbx,%rax,1),%esi
add    $0x1,%esi
mov    %r12,%rdi
call   1070 <realloc@plt>
mov    %rax,%r12
mov    %rax,%rdi
mov    %r14,%rsi
call   1080 <strcat@plt>
lea    0xe23(%rip),%rdi
mov    $0x1,%esi
mov    %r12,%rdx
xor    %eax,%eax
call   1040 <sql_generate@plt>
mov    %rax,%r15
lea    0xe3c(%rip),%rdi
mov    %r12,%rsi
xor    %eax,%eax
call   1040 <sql_generate@plt>
mov    %rax,%rbx
mov    0x2dd6(%rip),%rax
mov    (%rax),%edi
lea    0xeb1(%rip),%rsi
mov    %r15,%rdx
mov    %rbx,%rcx
call   1060 <fprintf@plt>
mov    %r12,%rdi
call   1030 <free@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1030 <free@plt>

LLVM IR (-O0):
@.str = [50 x i8] c"<meta http-equiv=\22refresh\22 content=\22%ld; url=%s\22>\00"
@.str.1 = [106 x i8] c"<div class=\22navi controls\22>If you are not redirected shortly, please [<a href=\22%s\22>click here</a>].</div>\00"
@.str.2 = [23 x i8] c"%s?board=%s&thread=%ld\00"
@.str.3 = [6 x i8] c"#p%ld\00"
@.str.4 = [11 x i8] c"/board.cgi\00"
@stdout = global i32 0
@.str.5 = [5 x i8] c"%s%s\00"
define void @thread_redirect(i8* %0, i64 %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca [4 x i8*]
%12 = alloca i32
%13 = alloca i32
store i8* %0, i8** %4
store i64 %1, i64* %5
store i64 %2, i64* %6
store i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0), i8** %7
store i8* getelementptr inbounds ([106 x i8], [106 x i8]* @.str.1, i64 0, i64 0), i8** %8
store i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8** %9
store i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8** %10
%14 = bitcast [4 x i8*]* %11 to i8*
%15 = load i8*, i8** %9
%16 = load i8*, i8** %4
%17 = load i64, i64* %5
%18 = call i8* (i8*, ...) @sql_generate(i8* %15, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i8* %16, i64 %17)
%19 = getelementptr inbounds [4 x i8*], [4 x i8*]* %11, i64 0, i64 0
store i8* %18, i8** %19
%20 = load i64, i64* %6
%21 = load i64, i64* %5
%22 = icmp sgt i64 %20, %21
br i1 %22, label %23, label %50
23: ; preds = %3
%24 = load i8*, i8** %10
%25 = load i64, i64* %6
%26 = call i8* (i8*, ...) @sql_generate(i8* %24, i64 %25)
%27 = getelementptr inbounds [4 x i8*], [4 x i8*]* %11, i64 0, i64 1
store i8* %26, i8** %27
%28 = getelementptr inbounds [4 x i8*], [4 x i8*]* %11, i64 0, i64 0
%29 = load i8*, i8** %28
%30 = call i32 @strlen(i8* %29)
%31 = getelementptr inbounds [4 x i8*], [4 x i8*]* %11, i64 0, i64 1
%32 = load i8*, i8** %31
%33 = call i32 @strlen(i8* %32)
%34 = add i32 %30, %33
store i32 %34, i32* %12
%35 = getelementptr inbounds [4 x i8*], [4 x i8*]* %11, i64 0, i64 0
%36 = load i8*, i8** %35
%37 = load i32, i32* %12
%38 = zext i32 %37 to i64
%39 = mul i64 1, %38
%40 = add i64 %39, 1
%41 = trunc i64 %40 to i32
%42 = call i64 @realloc(i8* %36, i32 %41)
%43 = inttoptr i64 %42 to i8*
%44 = getelementptr inbounds [4 x i8*], [4 x i8*]* %11, i64 0, i64 0
store i8* %43, i8** %44
%45 = getelementptr inbounds [4 x i8*], [4 x i8*]* %11, i64 0, i64 0
%46 = load i8*, i8** %45
%47 = getelementptr inbounds [4 x i8*], [4 x i8*]* %11, i64 0, i64 1
%48 = load i8*, i8** %47
%49 = call i32 @strcat(i8* %46, i8* %48)
br label %50
50: ; preds = %23, %3
%51 = load i8*, i8** %7
%52 = getelementptr inbounds [4 x i8*], [4 x i8*]* %11, i64 0, i64 0
%53 = load i8*, i8** %52
%54 = call i8* (i8*, ...) @sql_generate(i8* %51, i32 1, i8* %53)
%55 = getelementptr inbounds [4 x i8*], [4 x i8*]* %11, i64 0, i64 2
store i8* %54, i8** %55
%56 = load i8*, i8** %8
%57 = getelementptr inbounds [4 x i8*], [4 x i8*]* %11, i64 0, i64 0
%58 = load i8*, i8** %57
%59 = call i8* (i8*, ...) @sql_generate(i8* %56, i8* %58)
%60 = getelementptr inbounds [4 x i8*], [4 x i8*]* %11, i64 0, i64 3
store i8* %59, i8** %60
%61 = load i32, i32* @stdout
%62 = getelementptr inbounds [4 x i8*], [4 x i8*]* %11, i64 0, i64 2
%63 = load i8*, i8** %62
%64 = getelementptr inbounds [4 x i8*], [4 x i8*]* %11, i64 0, i64 3
%65 = load i8*, i8** %64
%66 = call i32 @fprintf(i32 %61, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0), i8* %63, i8* %65)
store i32 0, i32* %13
br label %67
67: ; preds = %86, %50
%68 = load i32, i32* %13
%69 = zext i32 %68 to i64
%70 = icmp ult i64 %69, 4
br i1 %70, label %71, label %89
71: ; preds = %67
%72 = load i32, i32* %13
%73 = zext i32 %72 to i64
%74 = getelementptr inbounds [4 x i8*], [4 x i8*]* %11, i64 0, i64 %73
%75 = load i8*, i8** %74
%76 = icmp ne i8* %75, null
br i1 %76, label %78, label %77
77: ; preds = %71
br label %83
78: ; preds = %71
%79 = load i32, i32* %13
%80 = zext i32 %79 to i64
%81 = getelementptr inbounds [4 x i8*], [4 x i8*]* %11, i64 0, i64 %80
%82 = load i8*, i8** %81
br label %83
83: ; preds = %78, %77
%84 = phi i8* [ null, %77 ], [ %82, %78 ]
%85 = call i32 @free(i8* %84)
br label %86
86: ; preds = %83
%87 = load i32, i32* %13
%88 = add i32 %87, 1
store i32 %88, i32* %13
br label %67
89: ; preds = %67
ret void
}

LLVM IR (-O2):
@.str = [50 x i8] c"<meta http-equiv=\22refresh\22 content=\22%ld; url=%s\22>\00"
@.str.1 = [106 x i8] c"<div class=\22navi controls\22>If you are not redirected shortly, please [<a href=\22%s\22>click here</a>].</div>\00"
@.str.2 = [23 x i8] c"%s?board=%s&thread=%ld\00"
@.str.3 = [6 x i8] c"#p%ld\00"
@.str.4 = [11 x i8] c"/board.cgi\00"
@stdout = global i32 0
@.str.5 = [5 x i8] c"%s%s\00"
define void @thread_redirect(i8* %0, i64 %1, i64 %2) {
%4 = tail call i8* (i8*, ...) @sql_generate(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i8* %0, i64 %1)
%5 = icmp sgt i64 %2, %1
br i1 %5, label %6, label %15
6: ; preds = %3
%7 = tail call i8* (i8*, ...) @sql_generate(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i64 %2)
%8 = tail call i32 @strlen(i8* %4)
%9 = tail call i32 @strlen(i8* %7)
%10 = add i32 %8, 1
%11 = add i32 %10, %9
%12 = tail call i64 @realloc(i8* %4, i32 %11)
%13 = inttoptr i64 %12 to i8*
%14 = tail call i32 @strcat(i8* %13, i8* %7)
br label %15
15: ; preds = %6, %3
%16 = phi i8* [ %13, %6 ], [ %4, %3 ]
%17 = phi i8* [ %7, %6 ], [ null, %3 ]
%18 = tail call i8* (i8*, ...) @sql_generate(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0), i32 1, i8* %16)
%19 = tail call i8* (i8*, ...) @sql_generate(i8* getelementptr inbounds ([106 x i8], [106 x i8]* @.str.1, i64 0, i64 0), i8* %16)
%20 = load i32, i32* @stdout
%21 = tail call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0), i8* %18, i8* %19)
%22 = tail call i32 @free(i8* %16)
%23 = tail call i32 @free(i8* %17)
%24 = tail call i32 @free(i8* %18)
%25 = tail call i32 @free(i8* %19)
ret void
}