#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 */
int /*<<< orphan*/ error(int);
int /*<<< orphan*/ respond(int, char *, int /*<<< orphan*/ *,
                           int /*<<< orphan*/);
scalar_t__ sprintf(char *, char *, char const *);
scalar_t__ strlen(char const *);
void redirect(const char *uri) {
  char *template = "Location: %s\r\n";
  char headers[strlen(template) - 2 + strlen(uri) + 1];
  if (sprintf(headers, template, uri) < 0) {
    error(500);
    return;
  }
  respond(301, headers, NULL, 0);
}


x86 assembly (clang, -O0):
<redirect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
lea    0xead(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x38(%rbp),%rcx
mov    %rax,%rdx
lea    -0x1(%rcx,%rdx,1),%rax
mov    %rsp,%rsi
mov    %rsi,-0x18(%rbp)
lea    0xe(%rcx,%rdx,1),%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x30(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rsi
mov    -0x8(%rbp),%rdx
call   1060 <sprintf@plt>
cmp    $0x0,%rax
jge    11c7 <redirect+0x87>
mov    $0x1f4,%edi
call   1050 <error@plt>
movl   $0x1,-0x24(%rbp)
jmp    11e2 <redirect+0xa2>
mov    -0x30(%rbp),%rsi
mov    $0x12d,%edi
xor    %eax,%eax
mov    %eax,%edx
xor    %ecx,%ecx
call   1030 <respond@plt>
movl   $0x0,-0x24(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<redirect>:
push   %rbp
mov    %rsp,%rbp
push   %r14
push   %rbx
mov    %rdi,%r14
call   1040 <strlen@plt>
mov    %rsp,%rbx
add    $0x1c,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xe99(%rip),%rsi
mov    %rbx,%rdi
mov    %r14,%rdx
call   1060 <sprintf@plt>
test   %rax,%rax
js     118a <redirect+0x4a>
mov    $0x12d,%edi
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1030 <respond@plt>
jmp    1194 <redirect+0x54>
mov    $0x1f4,%edi
call   1050 <error@plt>
lea    -0x10(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [15 x i8] c"Location: %s\0D\0A\00"
define void @redirect(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
store i8* %0, i8** %2
store i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8** %3
%7 = load i8*, i8** %3
%8 = call i64 @strlen(i8* %7)
%9 = sub nsw i64 %8, 2
%10 = load i8*, i8** %2
%11 = call i64 @strlen(i8* %10)
%12 = add nsw i64 %9, %11
%13 = add nsw i64 %12, 1
%14 = call i8* @llvm.stacksave()
store i8* %14, i8** %4
%15 = alloca i8, i64 %13
store i64 %13, i64* %5
%16 = load i8*, i8** %3
%17 = load i8*, i8** %2
%18 = call i64 @sprintf(i8* %15, i8* %16, i8* %17)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %1
%21 = call i32 @error(i32 500)
store i32 1, i32* %6
br label %24
22: ; preds = %1
%23 = call i32 @respond(i32 301, i8* %15, i32* null, i32 0)
store i32 0, i32* %6
br label %24
24: ; preds = %22, %20
%25 = load i8*, i8** %4
%26 = load i32, i32* %6
switch i32 %26, label %28 [
i32 0, label %27
i32 1, label %27
]
27: ; preds = %24, %24
ret void
28: ; preds = %24
unreachable
}

LLVM IR (-O2):
@.str = [15 x i8] c"Location: %s\0D\0A\00"
define void @redirect(i8* %0) {
%2 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%3 = add nsw i64 %2, 13
%4 = alloca i8, i64 %3
%5 = call i64 @sprintf(i8* %4, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %0)
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %1
%8 = call i32 @error(i32 500)
br label %11
9: ; preds = %1
%10 = call i32 @respond(i32 301, i8* %4, i32* null, i32 0)
br label %11
11: ; preds = %9, %7
ret void
}