#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 */
scalar_t__ checkBackend();
scalar_t__ checkBackendRepairAndSet(char *, char *);
int /*<<< orphan*/ curlGetValue(char *, char *);
int /*<<< orphan*/ getDocumentName(char *, char *);
int /*<<< orphan*/ getIP(char *);
char *getenv(char *);
int /*<<< orphan*/ memcpy(char *, char *, int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
unsigned long long rdtsc();
int /*<<< orphan*/ sprintf(char *, char *, char *, unsigned long long);
char *strstr(char *, char *);
int main(int argc, char **argv) {
  char *cookie_env = NULL, *cookie_ptr = NULL;
  char url[256];
  char cookie[128];
  char order_number[16];
  char docName[128];
  cookie_env = getenv("HTTP_COOKIE");
  if (cookie_env) {
    cookie_ptr = strstr(cookie_env, "MWS=");
    if (cookie_ptr) {
      memset(cookie, 0, 128);
      memcpy(cookie, cookie_ptr + 4, 64);
      memset(docName, 0, 128);
      getDocumentName(getenv("REQUEST_URI"), docName);
      if (checkBackendRepairAndSet(cookie, docName)) {
        printf("Content-Type: text/plain;charset=us-ascii\n\n");
        printf("<script>$(location).attr('href', "
               "'https://www.marionsworkshop.com/maintenance.html')</script>");
        return 0;
      }
      return 0;
    }
  }
  unsigned long long cpucyles;
  cpucyles = rdtsc();
  char ip_addr[32];
  memset(ip_addr, 0, 32);
  getIP(ip_addr);
  printf("Content-Type: text/plain;charset=us-ascii\n\n");
  if (checkBackend()) {
    printf("Content-Type: text/plain;charset=us-ascii\n\n");
    printf("<script>$(location).attr('href', "
           "'https://www.marionsworkshop.com/maintenance.html')</script>");
    return 0;
  }
  memset(url, 0, 256);
  memset(cookie, 0, 128);
  sprintf(url, "http://rest:9000/newSession?ip=%s&cycles=%llu", ip_addr,
          cpucyles);
  if (!curlGetValue(url, cookie))
    printf("<script "
           "type=\"text/"
           "javascript\">document.cookie=\"MWS=%s;max-age=14400\";</script>\n",
           cookie);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x260,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
lea    0xe10(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     12d3 <main+0x113>
mov    -0x18(%rbp),%rdi
lea    0xdfd(%rip),%rsi
call   10e0 <strstr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     12ce <main+0x10e>
lea    -0x1a0(%rbp),%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1070 <memset@plt>
lea    -0x1a0(%rbp),%rdi
mov    -0x20(%rbp),%rsi
add    $0x4,%rsi
mov    $0x40,%edx
call   1090 <memcpy@plt>
lea    -0x230(%rbp),%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1070 <memset@plt>
lea    0xda8(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rdi
lea    -0x230(%rbp),%rsi
call   10b0 <getDocumentName@plt>
lea    -0x1a0(%rbp),%rdi
lea    -0x230(%rbp),%rsi
call   1080 <checkBackendRepairAndSet@plt>
cmp    $0x0,%rax
je     12c2 <main+0x102>
lea    0xd7c(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xd9a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    13c6 <main+0x206>
movl   $0x0,-0x4(%rbp)
jmp    13c6 <main+0x206>
jmp    12d3 <main+0x113>
mov    $0x0,%al
call   1060 <rdtsc@plt>
mov    %rax,-0x238(%rbp)
lea    -0x260(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1070 <memset@plt>
lea    -0x260(%rbp),%rdi
call   10d0 <getIP@plt>
lea    0xd16(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x0,%al
call   10a0 <checkBackend@plt>
cmp    $0x0,%rax
je     1347 <main+0x187>
lea    0xcf7(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xd15(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    13c6 <main+0x206>
lea    -0x120(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1070 <memset@plt>
lea    -0x1a0(%rbp),%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1070 <memset@plt>
lea    -0x120(%rbp),%rdi
lea    -0x260(%rbp),%rdx
mov    -0x238(%rbp),%rcx
lea    0xd1e(%rip),%rsi
call   10c0 <sprintf@plt>
lea    -0x120(%rbp),%rdi
lea    -0x1a0(%rbp),%rsi
call   1040 <curlGetValue@plt>
cmp    $0x0,%eax
jne    13bf <main+0x1ff>
lea    -0x1a0(%rbp),%rsi
lea    0xd1d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x260,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x1a0,%rsp
lea    0xe1d(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     126c <main+0x9c>
lea    0xe18(%rip),%rsi
mov    %rax,%rdi
call   10f0 <strstr@plt>
test   %rax,%rax
je     126c <main+0x9c>
mov    %rax,%rbx
lea    0x20(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1080 <memset@plt>
add    $0x4,%rbx
mov    %r14,%rdi
mov    %rbx,%rsi
mov    $0x40,%edx
call   10a0 <memcpy@plt>
lea    0xa0(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1080 <memset@plt>
lea    0xdc7(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rdi
mov    %rbx,%rsi
call   10c0 <getDocumentName@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1090 <checkBackendRepairAndSet@plt>
test   %rax,%rax
jne    12a8 <main+0xd8>
jmp    12c2 <main+0xf2>
xor    %eax,%eax
call   1070 <rdtsc@plt>
mov    %rax,%r14
mov    %rsp,%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1080 <memset@plt>
mov    %rbx,%rdi
call   10e0 <getIP@plt>
lea    0xe63(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
call   10b0 <checkBackend@plt>
test   %rax,%rax
je     12d1 <main+0x101>
lea    0xe4b(%rip),%rdi
call   1040 <puts@plt>
lea    0xd62(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
xor    %eax,%eax
add    $0x1a0,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xa0(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1080 <memset@plt>
lea    0x20(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1080 <memset@plt>
lea    0xd78(%rip),%rsi
mov    %rsp,%rdx
mov    %rbx,%rdi
mov    %r14,%rcx
call   10d0 <sprintf@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1050 <curlGetValue@plt>
test   %eax,%eax
jne    12c2 <main+0xf2>
lea    0xd82(%rip),%rdi
lea    0x20(%rsp),%rsi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    12c2 <main+0xf2>

LLVM IR (-O0):
@.str = [12 x i8] c"HTTP_COOKIE\00"
@.str.1 = [5 x i8] c"MWS=\00"
@.str.2 = [12 x i8] c"REQUEST_URI\00"
@.str.3 = [44 x i8] c"Content-Type: text/plain;charset=us-ascii\0A\0A\00"
@.str.4 = [94 x i8] c"<script>$(location).attr('href', 'https://www.marionsworkshop.com/maintenance.html')</script>\00"
@.str.5 = [46 x i8] c"http://rest:9000/newSession?ip=%s&cycles=%llu\00"
@.str.6 = [81 x i8] c"<script type=\22text/javascript\22>document.cookie=\22MWS=%s;max-age=14400\22;</script>\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca [256 x i8]
%9 = alloca [128 x i8]
%10 = alloca [16 x i8]
%11 = alloca [128 x i8]
%12 = alloca i64
%13 = alloca [32 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* null, i8** %6
store i8* null, i8** %7
%14 = call i8* @getenv(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
store i8* %14, i8** %6
%15 = load i8*, i8** %6
%16 = icmp ne i8* %15, null
br i1 %16, label %17, label %43
17: ; preds = %2
%18 = load i8*, i8** %6
%19 = call i8* @strstr(i8* %18, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i8* %19, i8** %7
%20 = load i8*, i8** %7
%21 = icmp ne i8* %20, null
br i1 %21, label %22, label %42
22: ; preds = %17
%23 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 0
%24 = call i32 @memset(i8* %23, i32 0, i32 128)
%25 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 0
%26 = load i8*, i8** %7
%27 = getelementptr inbounds i8, i8* %26, i64 4
%28 = call i32 @memcpy(i8* %25, i8* %27, i32 64)
%29 = getelementptr inbounds [128 x i8], [128 x i8]* %11, i64 0, i64 0
%30 = call i32 @memset(i8* %29, i32 0, i32 128)
%31 = call i8* @getenv(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%32 = getelementptr inbounds [128 x i8], [128 x i8]* %11, i64 0, i64 0
%33 = call i32 @getDocumentName(i8* %31, i8* %32)
%34 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 0
%35 = getelementptr inbounds [128 x i8], [128 x i8]* %11, i64 0, i64 0
%36 = call i64 @checkBackendRepairAndSet(i8* %34, i8* %35)
%37 = icmp ne i64 %36, 0
br i1 %37, label %38, label %41
38: ; preds = %22
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.3, i64 0, i64 0))
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([94 x i8], [94 x i8]* @.str.4, i64 0, i64 0))
store i32 0, i32* %3
br label %72
41: ; preds = %22
store i32 0, i32* %3
br label %72
42: ; preds = %17
br label %43
43: ; preds = %42, %2
%44 = call i64 (...) @rdtsc()
store i64 %44, i64* %12
%45 = getelementptr inbounds [32 x i8], [32 x i8]* %13, i64 0, i64 0
%46 = call i32 @memset(i8* %45, i32 0, i32 32)
%47 = getelementptr inbounds [32 x i8], [32 x i8]* %13, i64 0, i64 0
%48 = call i32 @getIP(i8* %47)
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.3, i64 0, i64 0))
%50 = call i64 (...) @checkBackend()
%51 = icmp ne i64 %50, 0
br i1 %51, label %52, label %55
52: ; preds = %43
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.3, i64 0, i64 0))
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([94 x i8], [94 x i8]* @.str.4, i64 0, i64 0))
store i32 0, i32* %3
br label %72
55: ; preds = %43
%56 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%57 = call i32 @memset(i8* %56, i32 0, i32 256)
%58 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 0
%59 = call i32 @memset(i8* %58, i32 0, i32 128)
%60 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%61 = getelementptr inbounds [32 x i8], [32 x i8]* %13, i64 0, i64 0
%62 = load i64, i64* %12
%63 = call i32 @sprintf(i8* %60, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.5, i64 0, i64 0), i8* %61, i64 %62)
%64 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%65 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 0
%66 = call i32 @curlGetValue(i8* %64, i8* %65)
%67 = icmp ne i32 %66, 0
br i1 %67, label %71, label %68
68: ; preds = %55
%69 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 0
%70 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([81 x i8], [81 x i8]* @.str.6, i64 0, i64 0), i8* %69)
br label %71
71: ; preds = %68, %55
store i32 0, i32* %3
br label %72
72: ; preds = %71, %52, %41, %38
%73 = load i32, i32* %3
ret i32 %73
}

LLVM IR (-O2):
@.str = [12 x i8] c"HTTP_COOKIE\00"
@.str.1 = [5 x i8] c"MWS=\00"
@.str.2 = [12 x i8] c"REQUEST_URI\00"
@.str.4 = [94 x i8] c"<script>$(location).attr('href', 'https://www.marionsworkshop.com/maintenance.html')</script>\00"
@.str.5 = [46 x i8] c"http://rest:9000/newSession?ip=%s&cycles=%llu\00"
@.str.6 = [81 x i8] c"<script type=\22text/javascript\22>document.cookie=\22MWS=%s;max-age=14400\22;</script>\0A\00"
@str.8 = [43 x i8] c"Content-Type: text/plain;charset=us-ascii\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [256 x i8]
%4 = alloca [128 x i8]
%5 = alloca [128 x i8]
%6 = alloca [32 x i8]
%7 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%8 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%9 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%10 = tail call i8* @getenv(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%11 = icmp eq i8* %10, null
br i1 %11, label %27, label %12
12: ; preds = %2
%13 = tail call i8* @strstr(i8* %10, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%14 = icmp eq i8* %13, null
br i1 %14, label %27, label %15
15: ; preds = %12
%16 = call i32 @memset(i8* %8, i32 0, i32 128)
%17 = getelementptr inbounds i8, i8* %13, i64 4
%18 = call i32 @memcpy(i8* %8, i8* %17, i32 64)
%19 = call i32 @memset(i8* %9, i32 0, i32 128)
%20 = call i8* @getenv(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%21 = call i32 @getDocumentName(i8* %20, i8* %9)
%22 = call i64 @checkBackendRepairAndSet(i8* %8, i8* %9)
%23 = icmp eq i64 %22, 0
br i1 %23, label %47, label %24
24: ; preds = %15
%25 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @str.8, i64 0, i64 0))
%26 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([94 x i8], [94 x i8]* @.str.4, i64 0, i64 0))
br label %47
27: ; preds = %12, %2
%28 = tail call i64 (...) @rdtsc()
%29 = getelementptr inbounds [32 x i8], [32 x i8]* %6, i64 0, i64 0
%30 = call i32 @memset(i8* %29, i32 0, i32 32)
%31 = call i32 @getIP(i8* %29)
%32 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @str.8, i64 0, i64 0))
%33 = call i64 (...) @checkBackend()
%34 = icmp eq i64 %33, 0
br i1 %34, label %38, label %35
35: ; preds = %27
%36 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @str.8, i64 0, i64 0))
%37 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([94 x i8], [94 x i8]* @.str.4, i64 0, i64 0))
br label %46
38: ; preds = %27
%39 = call i32 @memset(i8* %7, i32 0, i32 256)
%40 = call i32 @memset(i8* %8, i32 0, i32 128)
%41 = call i32 @sprintf(i8* %7, i8* dereferenceable(1) getelementptr inbounds ([46 x i8], [46 x i8]* @.str.5, i64 0, i64 0), i8* %29, i64 %28)
%42 = call i32 @curlGetValue(i8* %7, i8* %8)
%43 = icmp eq i32 %42, 0
br i1 %43, label %44, label %46
44: ; preds = %38
%45 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([81 x i8], [81 x i8]* @.str.6, i64 0, i64 0), i8* %8)
br label %46
46: ; preds = %38, %44, %35
br label %47
47: ; preds = %15, %46, %24
ret i32 0
}