#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*/ exit(int);
scalar_t__ malloc(int);
int /*<<< orphan*/ memcpy(char *, char *, size_t);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ strcat(char *, char *);
size_t strlen(char *);
char *strstr(char *, char *);
char *getServerDownloadUrl(char *serverUrl) {
  size_t urlSize = strlen(serverUrl);
  char *upload = strstr(serverUrl, "upload.php");
  if (upload == NULL) {
    printf("Download URL parsing error - cannot find upload.php in %s\n",
           serverUrl);
    exit(1);
  }
  size_t uploadSize = strlen(upload);
  size_t totalSize = (urlSize - uploadSize) + strlen("random4000x4000.jpg") + 1;
  char *result = (char *)malloc(sizeof(char) * totalSize);
  result[(urlSize - uploadSize)] = '\0';
  memcpy(result, serverUrl, urlSize - uploadSize);
  strcat(result, "random4000x4000.jpg");
  return result;
}


x86 assembly (clang, -O0):
<getServerDownloadUrl>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe6c(%rip),%rsi
call   1090 <strstr@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11c2 <getServerDownloadUrl+0x52>
mov    -0x8(%rbp),%rsi
lea    0xe58(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
sub    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
lea    0xe64(%rip),%rdi
call   1030 <strlen@plt>
mov    %rax,%rcx
mov    -0x38(%rbp),%rax
add    %rcx,%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x30(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
sub    -0x20(%rbp),%rdx
call   1050 <memcpy@plt>
mov    -0x30(%rbp),%rdi
lea    0xe0a(%rip),%rsi
call   1070 <strcat@plt>
mov    -0x30(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getServerDownloadUrl>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
lea    0xe81(%rip),%rsi
call   1090 <strstr@plt>
test   %rax,%rax
je     11d7 <getServerDownloadUrl+0x67>
mov    %rax,%r15
mov    %r14,%rdi
call   1030 <strlen@plt>
mov    %rax,%rbx
mov    %r15,%rdi
call   1030 <strlen@plt>
sub    %rax,%rbx
lea    0x14(%rbx),%edi
call   1060 <malloc@plt>
mov    %rax,%r15
movb   $0x0,(%rax,%rbx,1)
mov    %rax,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1050 <memcpy@plt>
lea    0xe80(%rip),%rsi
mov    %r15,%rdi
call   1070 <strcat@plt>
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe2d(%rip),%rdi
mov    %r14,%rsi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [11 x i8] c"upload.php\00"
@.str.1 = [59 x i8] c"Download URL parsing error - cannot find upload.php in %s\0A\00"
@.str.2 = [20 x i8] c"random4000x4000.jpg\00"
define i8* @getServerDownloadUrl(i8* %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i64
%7 = alloca i8*
store i8* %0, i8** %2
%8 = load i8*, i8** %2
%9 = call i64 @strlen(i8* %8)
store i64 %9, i64* %3
%10 = load i8*, i8** %2
%11 = call i8* @strstr(i8* %10, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
store i8* %11, i8** %4
%12 = load i8*, i8** %4
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %18
14: ; preds = %1
%15 = load i8*, i8** %2
%16 = call i32 @printf(i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.1, i64 0, i64 0), i8* %15)
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %1
%19 = load i8*, i8** %4
%20 = call i64 @strlen(i8* %19)
store i64 %20, i64* %5
%21 = load i64, i64* %3
%22 = load i64, i64* %5
%23 = sub i64 %21, %22
%24 = call i64 @strlen(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
%25 = add i64 %23, %24
%26 = add i64 %25, 1
store i64 %26, i64* %6
%27 = load i64, i64* %6
%28 = mul i64 1, %27
%29 = trunc i64 %28 to i32
%30 = call i64 @malloc(i32 %29)
%31 = inttoptr i64 %30 to i8*
store i8* %31, i8** %7
%32 = load i8*, i8** %7
%33 = load i64, i64* %3
%34 = load i64, i64* %5
%35 = sub i64 %33, %34
%36 = getelementptr inbounds i8, i8* %32, i64 %35
store i8 0, i8* %36
%37 = load i8*, i8** %7
%38 = load i8*, i8** %2
%39 = load i64, i64* %3
%40 = load i64, i64* %5
%41 = sub i64 %39, %40
%42 = call i32 @memcpy(i8* %37, i8* %38, i64 %41)
%43 = load i8*, i8** %7
%44 = call i32 @strcat(i8* %43, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
%45 = load i8*, i8** %7
ret i8* %45
}

LLVM IR (-O2):
@.str = [11 x i8] c"upload.php\00"
@.str.1 = [59 x i8] c"Download URL parsing error - cannot find upload.php in %s\0A\00"
@.str.2 = [20 x i8] c"random4000x4000.jpg\00"
define i8* @getServerDownloadUrl(i8* %0) {
%2 = tail call i8* @strstr(i8* dereferenceable(1) %0, i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i8* %2, null
br i1 %3, label %4, label %7
4: ; preds = %1
%5 = tail call i32 @printf(i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%6 = tail call i32 @exit(i32 1)
unreachable
7: ; preds = %1
%8 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%9 = tail call i64 @strlen(i8* %2)
%10 = sub i64 %8, %9
%11 = trunc i64 %10 to i32
%12 = add i32 %11, 20
%13 = tail call i64 @malloc(i32 %12)
%14 = inttoptr i64 %13 to i8*
%15 = getelementptr inbounds i8, i8* %14, i64 %10
store i8 0, i8* %15
%16 = tail call i32 @memcpy(i8* %14, i8* %0, i64 %10)
%17 = tail call i32 @strcat(i8* %14, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
ret i8* %14
}