#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 const *);
size_t strlen(char const *);
char *strstr(char *, char *);
char *modifyServerUrl(char *serverUrl, const char *urlFile) {
  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(urlFile) + 1;
  char *result = (char *)malloc(sizeof(char) * totalSize);
  result[(urlSize - uploadSize)] = '\0';
  memcpy(result, serverUrl, urlSize - uploadSize);
  strcat(result, urlFile);
  return result;
}


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

x86 assembly (clang, -O2):
<modifyServerUrl>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
lea    0xe7a(%rip),%rsi
call   1090 <strstr@plt>
test   %rax,%rax
je     11eb <modifyServerUrl+0x7b>
mov    %rax,%r12
mov    %r15,%rdi
call   1030 <strlen@plt>
mov    %rax,%r13
mov    %r12,%rdi
call   1030 <strlen@plt>
sub    %rax,%r13
mov    %r14,%rdi
call   1030 <strlen@plt>
lea    (%rax,%r13,1),%edi
add    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
movb   $0x0,(%rax,%r13,1)
mov    %rax,%rdi
mov    %r15,%rsi
mov    %r13,%rdx
call   1050 <memcpy@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1070 <strcat@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
lea    0xe19(%rip),%rdi
mov    %r15,%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"
define i8* @modifyServerUrl(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i8*
store i8* %0, i8** %3
store i8* %1, i8** %4
%10 = load i8*, i8** %3
%11 = call i64 @strlen(i8* %10)
store i64 %11, i64* %5
%12 = load i8*, i8** %3
%13 = call i8* @strstr(i8* %12, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
store i8* %13, i8** %6
%14 = load i8*, i8** %6
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %20
16: ; preds = %2
%17 = load i8*, i8** %3
%18 = call i32 @printf(i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.1, i64 0, i64 0), i8* %17)
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %2
%21 = load i8*, i8** %6
%22 = call i64 @strlen(i8* %21)
store i64 %22, i64* %7
%23 = load i64, i64* %5
%24 = load i64, i64* %7
%25 = sub i64 %23, %24
%26 = load i8*, i8** %4
%27 = call i64 @strlen(i8* %26)
%28 = add i64 %25, %27
%29 = add i64 %28, 1
store i64 %29, i64* %8
%30 = load i64, i64* %8
%31 = mul i64 1, %30
%32 = trunc i64 %31 to i32
%33 = call i64 @malloc(i32 %32)
%34 = inttoptr i64 %33 to i8*
store i8* %34, i8** %9
%35 = load i8*, i8** %9
%36 = load i64, i64* %5
%37 = load i64, i64* %7
%38 = sub i64 %36, %37
%39 = getelementptr inbounds i8, i8* %35, i64 %38
store i8 0, i8* %39
%40 = load i8*, i8** %9
%41 = load i8*, i8** %3
%42 = load i64, i64* %5
%43 = load i64, i64* %7
%44 = sub i64 %42, %43
%45 = call i32 @memcpy(i8* %40, i8* %41, i64 %44)
%46 = load i8*, i8** %9
%47 = load i8*, i8** %4
%48 = call i32 @strcat(i8* %46, i8* %47)
%49 = load i8*, i8** %9
ret i8* %49
}

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"
define i8* @modifyServerUrl(i8* %0, i8* %1) {
%3 = tail call i8* @strstr(i8* dereferenceable(1) %0, i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i8* %3, null
br i1 %4, label %5, label %8
5: ; preds = %2
%6 = tail call i32 @printf(i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %2
%9 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%10 = tail call i64 @strlen(i8* %3)
%11 = sub i64 %9, %10
%12 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%13 = add i64 %11, %12
%14 = trunc i64 %13 to i32
%15 = add i32 %14, 1
%16 = tail call i64 @malloc(i32 %15)
%17 = inttoptr i64 %16 to i8*
%18 = getelementptr inbounds i8, i8* %17, i64 %11
store i8 0, i8* %18
%19 = tail call i32 @memcpy(i8* %17, i8* %0, i64 %11)
%20 = tail call i32 @strcat(i8* %17, i8* %1)
ret i8* %17
}