#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 */
struct mg_request_info {
  int dummy;
};
struct mg_connection {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ mg_free(char *);
char *mg_get_header(struct mg_connection *, char *);
char *mg_get_var(struct mg_connection *, char *);
int /*<<< orphan*/ mg_printf(struct mg_connection *, char *, ...);
int sscanf(char const *, char *, char *);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
void login_page(struct mg_connection *conn, const struct mg_request_info *ri,
                void *data) {
  char *name, *pass, uri[100];
  const char *cookie;
  name = mg_get_var(conn, "name");
  pass = mg_get_var(conn, "pass");
  cookie = mg_get_header(conn, "Cookie");
  if (name != NULL && pass != NULL && strcmp(name, "admin") == 0 &&
      strcmp(pass, "admin") == 0) {
    if (cookie == NULL || sscanf(cookie, "uri=%99s", uri) != 1)
      (void)strcpy(uri, "/");
    mg_printf(conn,
              "HTTP/1.1 301 Moved Permanently\r\n"
              "Location: %s\r\n"
              "Set-Cookie: allow=yes;\r\n\r\n",
              uri);
  } else {
    mg_printf(conn, "HTTP/1.1 200 OK\r\n"
                    "content-Type: text/html\r\n\r\n"
                    "Please login (enter admin/admin to pass)<br>"
                    "<form method=post>"
                    "Name: <input type=text name=name></input><br/>"
                    "Password: <input type=password name=pass></input><br/>"
                    "<input type=submit value=Login></input>"
                    "</form>");
  }
  if (name != NULL)
    mg_free(name);
  if (pass != NULL)
    mg_free(pass);
}


x86 assembly (clang, -O0):
<login_page>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe6e(%rip),%rsi
call   1070 <mg_get_var@plt>
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe5f(%rip),%rsi
call   1070 <mg_get_var@plt>
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe50(%rip),%rsi
call   1030 <mg_get_header@plt>
mov    %rax,-0x98(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1272 <login_page+0x102>
cmpq   $0x0,-0x28(%rbp)
je     1272 <login_page+0x102>
mov    -0x20(%rbp),%rdi
lea    0xe2a(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    1272 <login_page+0x102>
mov    -0x28(%rbp),%rdi
lea    0xe10(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    1272 <login_page+0x102>
cmpq   $0x0,-0x98(%rbp)
je     1241 <login_page+0xd1>
mov    -0x98(%rbp),%rdi
lea    -0x90(%rbp),%rdx
lea    0xde4(%rip),%rsi
call   1090 <sscanf@plt>
cmp    $0x1,%eax
je     1254 <login_page+0xe4>
lea    -0x90(%rbp),%rdi
lea    0xdd1(%rip),%rsi
call   1040 <strcpy@plt>
mov    -0x8(%rbp),%rdi
lea    -0x90(%rbp),%rdx
lea    0xdbc(%rip),%rsi
mov    $0x0,%al
call   1050 <mg_printf@plt>
jmp    1284 <login_page+0x114>
mov    -0x8(%rbp),%rdi
lea    0xdee(%rip),%rsi
mov    $0x0,%al
call   1050 <mg_printf@plt>
cmpq   $0x0,-0x20(%rbp)
je     1298 <login_page+0x128>
mov    -0x20(%rbp),%rdi
call   1080 <mg_free@plt>
cmpq   $0x0,-0x28(%rbp)
je     12ac <login_page+0x13c>
mov    -0x28(%rbp),%rdi
call   1080 <mg_free@plt>
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<login_page>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x68,%rsp
mov    %rdi,%rbx
lea    0xe7b(%rip),%rsi
call   1070 <mg_get_var@plt>
mov    %rax,%r15
lea    0xe71(%rip),%rsi
mov    %rbx,%rdi
call   1070 <mg_get_var@plt>
mov    %rax,%r14
lea    0xe64(%rip),%rsi
mov    %rbx,%rdi
call   1030 <mg_get_header@plt>
test   %r15,%r15
je     11e3 <login_page+0x73>
test   %r14,%r14
je     11e3 <login_page+0x73>
mov    %rax,%r12
lea    0xe4f(%rip),%rsi
mov    %r15,%rdi
call   1060 <strcmp@plt>
test   %rax,%rax
jne    11e3 <login_page+0x73>
lea    0xe3b(%rip),%rsi
mov    %r14,%rdi
call   1060 <strcmp@plt>
test   %rax,%rax
je     121a <login_page+0xaa>
lea    0xe81(%rip),%rsi
mov    %rbx,%rdi
xor    %eax,%eax
call   1050 <mg_printf@plt>
test   %r15,%r15
je     1201 <login_page+0x91>
mov    %r15,%rdi
call   1080 <mg_free@plt>
test   %r14,%r14
je     120e <login_page+0x9e>
mov    %r14,%rdi
call   1080 <mg_free@plt>
add    $0x68,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
test   %r12,%r12
je     1236 <login_page+0xc6>
lea    0xdf1(%rip),%rsi
mov    %rsp,%rdx
mov    %r12,%rdi
call   1090 <sscanf@plt>
cmp    $0x1,%eax
je     1245 <login_page+0xd5>
lea    0xde3(%rip),%rsi
mov    %rsp,%rdi
call   1040 <strcpy@plt>
lea    0xdd6(%rip),%rsi
mov    %rsp,%rdx
mov    %rbx,%rdi
xor    %eax,%eax
call   1050 <mg_printf@plt>
test   %r15,%r15
jne    11f9 <login_page+0x89>
jmp    1201 <login_page+0x91>

LLVM IR (-O0):
%struct.mg_connection = type { i32 }
%struct.mg_request_info = type { i32 }
@.str = [5 x i8] c"name\00"
@.str.1 = [5 x i8] c"pass\00"
@.str.2 = [7 x i8] c"Cookie\00"
@.str.3 = [6 x i8] c"admin\00"
@.str.4 = [9 x i8] c"uri=%99s\00"
@.str.5 = [2 x i8] c"/\00"
@.str.6 = [73 x i8] c"HTTP/1.1 301 Moved Permanently\0D\0ALocation: %s\0D\0ASet-Cookie: allow=yes;\0D\0A\0D\0A\00"
@.str.7 = [253 x i8] c"HTTP/1.1 200 OK\0D\0Acontent-Type: text/html\0D\0A\0D\0APlease login (enter admin/admin to pass)<br><form method=post>Name: <input type=text name=name></input><br/>Password: <input type=password name=pass></input><br/><input type=submit value=Login></input></form>\00"
define void @login_page(%struct.mg_connection* %0, %struct.mg_request_info* %1, i8* %2) {
%4 = alloca %struct.mg_connection*
%5 = alloca %struct.mg_request_info*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca [100 x i8]
%10 = alloca i8*
store %struct.mg_connection* %0, %struct.mg_connection** %4
store %struct.mg_request_info* %1, %struct.mg_request_info** %5
store i8* %2, i8** %6
%11 = load %struct.mg_connection*, %struct.mg_connection** %4
%12 = call i8* @mg_get_var(%struct.mg_connection* %11, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i8* %12, i8** %7
%13 = load %struct.mg_connection*, %struct.mg_connection** %4
%14 = call i8* @mg_get_var(%struct.mg_connection* %13, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i8* %14, i8** %8
%15 = load %struct.mg_connection*, %struct.mg_connection** %4
%16 = call i8* @mg_get_header(%struct.mg_connection* %15, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
store i8* %16, i8** %10
%17 = load i8*, i8** %7
%18 = icmp ne i8* %17, null
br i1 %18, label %19, label %45
19: ; preds = %3
%20 = load i8*, i8** %8
%21 = icmp ne i8* %20, null
br i1 %21, label %22, label %45
22: ; preds = %19
%23 = load i8*, i8** %7
%24 = call i64 @strcmp(i8* %23, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %45
26: ; preds = %22
%27 = load i8*, i8** %8
%28 = call i64 @strcmp(i8* %27, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %45
30: ; preds = %26
%31 = load i8*, i8** %10
%32 = icmp eq i8* %31, null
br i1 %32, label %38, label %33
33: ; preds = %30
%34 = load i8*, i8** %10
%35 = getelementptr inbounds [100 x i8], [100 x i8]* %9, i64 0, i64 0
%36 = call i32 @sscanf(i8* %34, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i8* %35)
%37 = icmp ne i32 %36, 1
br i1 %37, label %38, label %41
38: ; preds = %33, %30
%39 = getelementptr inbounds [100 x i8], [100 x i8]* %9, i64 0, i64 0
%40 = call i32 @strcpy(i8* %39, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
br label %41
41: ; preds = %38, %33
%42 = load %struct.mg_connection*, %struct.mg_connection** %4
%43 = getelementptr inbounds [100 x i8], [100 x i8]* %9, i64 0, i64 0
%44 = call i32 (%struct.mg_connection*, i8*, ...) @mg_printf(%struct.mg_connection* %42, i8* getelementptr inbounds ([73 x i8], [73 x i8]* @.str.6, i64 0, i64 0), i8* %43)
br label %48
45: ; preds = %26, %22, %19, %3
%46 = load %struct.mg_connection*, %struct.mg_connection** %4
%47 = call i32 (%struct.mg_connection*, i8*, ...) @mg_printf(%struct.mg_connection* %46, i8* getelementptr inbounds ([253 x i8], [253 x i8]* @.str.7, i64 0, i64 0))
br label %48
48: ; preds = %45, %41
%49 = load i8*, i8** %7
%50 = icmp ne i8* %49, null
br i1 %50, label %51, label %54
51: ; preds = %48
%52 = load i8*, i8** %7
%53 = call i32 @mg_free(i8* %52)
br label %54
54: ; preds = %51, %48
%55 = load i8*, i8** %8
%56 = icmp ne i8* %55, null
br i1 %56, label %57, label %60
57: ; preds = %54
%58 = load i8*, i8** %8
%59 = call i32 @mg_free(i8* %58)
br label %60
60: ; preds = %57, %54
ret void
}

LLVM IR (-O2):
%struct.mg_connection = type { i32 }
%struct.mg_request_info = type { i32 }
@.str = [5 x i8] c"name\00"
@.str.1 = [5 x i8] c"pass\00"
@.str.2 = [7 x i8] c"Cookie\00"
@.str.3 = [6 x i8] c"admin\00"
@.str.4 = [9 x i8] c"uri=%99s\00"
@.str.5 = [2 x i8] c"/\00"
@.str.6 = [73 x i8] c"HTTP/1.1 301 Moved Permanently\0D\0ALocation: %s\0D\0ASet-Cookie: allow=yes;\0D\0A\0D\0A\00"
@.str.7 = [253 x i8] c"HTTP/1.1 200 OK\0D\0Acontent-Type: text/html\0D\0A\0D\0APlease login (enter admin/admin to pass)<br><form method=post>Name: <input type=text name=name></input><br/>Password: <input type=password name=pass></input><br/><input type=submit value=Login></input></form>\00"
define void @login_page(%struct.mg_connection* %0, %struct.mg_request_info* nocapture readnone %1, i8* nocapture readnone %2) {
%4 = alloca [100 x i8]
%5 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%6 = tail call i8* @mg_get_var(%struct.mg_connection* %0, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%7 = tail call i8* @mg_get_var(%struct.mg_connection* %0, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%8 = tail call i8* @mg_get_header(%struct.mg_connection* %0, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%9 = icmp ne i8* %6, null
%10 = icmp ne i8* %7, null
%11 = select i1 %9, i1 %10, i1 false
br i1 %11, label %12, label %27
12: ; preds = %3
%13 = tail call i64 @strcmp(i8* %6, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %27
15: ; preds = %12
%16 = tail call i64 @strcmp(i8* %7, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %27
18: ; preds = %15
%19 = icmp eq i8* %8, null
br i1 %19, label %23, label %20
20: ; preds = %18
%21 = call i32 @sscanf(i8* %8, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i8* %5)
%22 = icmp eq i32 %21, 1
br i1 %22, label %25, label %23
23: ; preds = %20, %18
%24 = call i32 @strcpy(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
br label %25
25: ; preds = %23, %20
%26 = call i32 (%struct.mg_connection*, i8*, ...) @mg_printf(%struct.mg_connection* %0, i8* getelementptr inbounds ([73 x i8], [73 x i8]* @.str.6, i64 0, i64 0), i8* %5)
br label %29
27: ; preds = %15, %12, %3
%28 = tail call i32 (%struct.mg_connection*, i8*, ...) @mg_printf(%struct.mg_connection* %0, i8* getelementptr inbounds ([253 x i8], [253 x i8]* @.str.7, i64 0, i64 0))
br label %29
29: ; preds = %27, %25
br i1 %9, label %30, label %32
30: ; preds = %29
%31 = call i32 @mg_free(i8* %6)
br label %32
32: ; preds = %30, %29
br i1 %10, label %33, label %35
33: ; preds = %32
%34 = call i32 @mg_free(i8* %7)
br label %35
35: ; preds = %33, %32
ret void
}