#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*/ free(char *);
scalar_t__ memcmp(char *, char *, int);
int /*<<< orphan*/ str_add(char **, char *, char *, ...);
char *strchr(char *, char);
char *strdup(char *);
char *strstr(char *, char *);
const char *get_location(const char *host, int port, char use_ssl,
                         char *reply) {
  if (reply) {
    char *copy = strdup(reply);
    char *head = strstr(copy, "\nLocation:");
    char *lf = head ? strchr(head + 1, '\n') : NULL;
    if (head) {
      char *buffer = NULL;
      char *dest = head + 11;
      if (lf)
        *lf = 0x00;
      if (memcmp(dest, "http", 4) == 0)
        str_add(&buffer, "%s", dest);
      else
        str_add(&buffer, "http%s://%s:%d%s", use_ssl ? "s" : "", host, port,
                dest);
      free(copy);
      return buffer;
    }
    free(copy);
  }
  return NULL;
}


x86 assembly (clang, -O0):
<get_location>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %dl,%al
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %al,-0x15(%rbp)
mov    %rcx,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     129e <get_location+0x13e>
mov    -0x20(%rbp),%rdi
call   1070 <strdup@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
lea    0xe65(%rip),%rsi
call   1080 <strstr@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     11ca <get_location+0x6a>
mov    -0x30(%rbp),%rdi
add    $0x1,%rdi
mov    $0xa,%esi
call   1040 <strchr@plt>
mov    %rax,-0x50(%rbp)
jmp    11d5 <get_location+0x75>
xor    %eax,%eax
mov    %rax,-0x50(%rbp)
jmp    11d5 <get_location+0x75>
mov    -0x50(%rbp),%rax
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     1295 <get_location+0x135>
movq   $0x0,-0x40(%rbp)
mov    -0x30(%rbp),%rax
add    $0xb,%rax
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     120e <get_location+0xae>
mov    -0x38(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x48(%rbp),%rdi
lea    0xdf2(%rip),%rsi
mov    $0x4,%edx
call   1050 <memcmp@plt>
cmp    $0x0,%rax
jne    1248 <get_location+0xe8>
mov    -0x48(%rbp),%rdx
lea    -0x40(%rbp),%rdi
lea    0xde2(%rip),%rsi
mov    $0x0,%al
call   1060 <str_add@plt>
jmp    127f <get_location+0x11f>
movsbl -0x15(%rbp),%ecx
lea    0xdb7(%rip),%rdx
lea    0xdc5(%rip),%rax
cmp    $0x0,%ecx
cmovne %rax,%rdx
mov    -0x10(%rbp),%rcx
mov    -0x14(%rbp),%r8d
mov    -0x48(%rbp),%r9
lea    -0x40(%rbp),%rdi
lea    0xd98(%rip),%rsi
mov    $0x0,%al
call   1060 <str_add@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x40(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12a6 <get_location+0x146>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_location>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
test   %rcx,%rcx
je     120f <get_location+0xaf>
mov    %edx,%r12d
mov    %esi,%r14d
mov    %rdi,%r15
mov    %rcx,%rdi
call   1070 <strdup@plt>
mov    %rax,%rbx
lea    0xe70(%rip),%rsi
mov    %rax,%rdi
call   1080 <strstr@plt>
test   %rax,%rax
je     1213 <get_location+0xb3>
mov    %rax,%rbp
lea    0x1(%rax),%rdi
mov    $0xa,%esi
call   1040 <strchr@plt>
movq   $0x0,0x8(%rsp)
add    $0xb,%rbp
test   %rax,%rax
je     11c3 <get_location+0x63>
movb   $0x0,(%rax)
lea    0xe41(%rip),%rsi
mov    %rbp,%rdi
mov    $0x4,%edx
call   1050 <memcmp@plt>
test   %rax,%rax
je     121f <get_location+0xbf>
test   %r12b,%r12b
lea    0xe24(%rip),%rax
lea    0xe32(%rip),%rdx
cmove  %rax,%rdx
lea    0xe18(%rip),%rsi
lea    0x8(%rsp),%rdi
mov    %r15,%rcx
mov    %r14d,%r8d
mov    %rbp,%r9
xor    %eax,%eax
call   1060 <str_add@plt>
jmp    1235 <get_location+0xd5>
xor    %eax,%eax
jmp    1242 <get_location+0xe2>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %eax,%eax
jmp    1242 <get_location+0xe2>
lea    0xdf8(%rip),%rsi
lea    0x8(%rsp),%rdi
mov    %rbp,%rdx
xor    %eax,%eax
call   1060 <str_add@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    0x8(%rsp),%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [11 x i8] c"\0ALocation:\00"
@.str.1 = [5 x i8] c"http\00"
@.str.2 = [3 x i8] c"%s\00"
@.str.3 = [17 x i8] c"http%s://%s:%d%s\00"
@.str.4 = [2 x i8] c"s\00"
@.str.5 = [1 x i8] zeroinitializer
define i8* @get_location(i8* %0, i32 %1, i8 signext %2, i8* %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca i8*
%14 = alloca i8*
store i8* %0, i8** %6
store i32 %1, i32* %7
store i8 %2, i8* %8
store i8* %3, i8** %9
%15 = load i8*, i8** %9
%16 = icmp ne i8* %15, null
br i1 %16, label %17, label %64
17: ; preds = %4
%18 = load i8*, i8** %9
%19 = call i8* @strdup(i8* %18)
store i8* %19, i8** %10
%20 = load i8*, i8** %10
%21 = call i8* @strstr(i8* %20, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
store i8* %21, i8** %11
%22 = load i8*, i8** %11
%23 = icmp ne i8* %22, null
br i1 %23, label %24, label %28
24: ; preds = %17
%25 = load i8*, i8** %11
%26 = getelementptr inbounds i8, i8* %25, i64 1
%27 = call i8* @strchr(i8* %26, i8 signext 10)
br label %29
28: ; preds = %17
br label %29
29: ; preds = %28, %24
%30 = phi i8* [ %27, %24 ], [ null, %28 ]
store i8* %30, i8** %12
%31 = load i8*, i8** %11
%32 = icmp ne i8* %31, null
br i1 %32, label %33, label %61
33: ; preds = %29
store i8* null, i8** %13
%34 = load i8*, i8** %11
%35 = getelementptr inbounds i8, i8* %34, i64 11
store i8* %35, i8** %14
%36 = load i8*, i8** %12
%37 = icmp ne i8* %36, null
br i1 %37, label %38, label %40
38: ; preds = %33
%39 = load i8*, i8** %12
store i8 0, i8* %39
br label %40
40: ; preds = %38, %33
%41 = load i8*, i8** %14
%42 = call i64 @memcmp(i8* %41, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 4)
%43 = icmp eq i64 %42, 0
br i1 %43, label %44, label %47
44: ; preds = %40
%45 = load i8*, i8** %14
%46 = call i32 (i8**, i8*, i8*, ...) @str_add(i8** %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %45)
br label %57
47: ; preds = %40
%48 = load i8, i8* %8
%49 = sext i8 %48 to i32
%50 = icmp ne i32 %49, 0
%51 = zext i1 %50 to i64
%52 = select i1 %50, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.5, i64 0, i64 0)
%53 = load i8*, i8** %6
%54 = load i32, i32* %7
%55 = load i8*, i8** %14
%56 = call i32 (i8**, i8*, i8*, ...) @str_add(i8** %13, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i8* %52, i8* %53, i32 %54, i8* %55)
br label %57
57: ; preds = %47, %44
%58 = load i8*, i8** %10
%59 = call i32 @free(i8* %58)
%60 = load i8*, i8** %13
store i8* %60, i8** %5
br label %65
61: ; preds = %29
%62 = load i8*, i8** %10
%63 = call i32 @free(i8* %62)
br label %64
64: ; preds = %61, %4
store i8* null, i8** %5
br label %65
65: ; preds = %64, %57
%66 = load i8*, i8** %5
ret i8* %66
}

LLVM IR (-O2):
@.str = [11 x i8] c"\0ALocation:\00"
@.str.1 = [5 x i8] c"http\00"
@.str.2 = [3 x i8] c"%s\00"
@.str.3 = [17 x i8] c"http%s://%s:%d%s\00"
@.str.4 = [2 x i8] c"s\00"
@.str.5 = [1 x i8] zeroinitializer
define i8* @get_location(i8* %0, i32 %1, i8 signext %2, i8* %3) {
%5 = alloca i8*
%6 = icmp eq i8* %3, null
br i1 %6, label %32, label %7
7: ; preds = %4
%8 = tail call i8* @strdup(i8* %3)
%9 = tail call i8* @strstr(i8* dereferenceable(1) %8, i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%10 = icmp eq i8* %9, null
br i1 %10, label %27, label %11
11: ; preds = %7
%12 = getelementptr inbounds i8, i8* %9, i64 1
%13 = tail call i8* @strchr(i8* %12, i8 signext 10)
%14 = bitcast i8** %5 to i8*
store i8* null, i8** %5
%15 = getelementptr inbounds i8, i8* %9, i64 11
%16 = icmp eq i8* %13, null
br i1 %16, label %18, label %17
17: ; preds = %11
store i8 0, i8* %13
br label %18
18: ; preds = %17, %11
%19 = tail call i64 @memcmp(i8* %15, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 4)
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %18
%22 = call i32 (i8**, i8*, i8*, ...) @str_add(i8** %5, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %15)
br label %29
23: ; preds = %18
%24 = icmp eq i8 %2, 0
%25 = select i1 %24, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0)
%26 = call i32 (i8**, i8*, i8*, ...) @str_add(i8** %5, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i8* %25, i8* %0, i32 %1, i8* %15)
br label %29
27: ; preds = %7
%28 = tail call i32 @free(i8* %8)
br label %32
29: ; preds = %21, %23
%30 = call i32 @free(i8* %8)
%31 = load i8*, i8** %5
br label %32
32: ; preds = %4, %27, %29
%33 = phi i8* [ %31, %29 ], [ null, %27 ], [ null, %4 ]
ret i8* %33
}