#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 stat {
  int /*<<< orphan*/ st_mode;
};

/* Variables and functions */
scalar_t__ S_ISDIR(int /*<<< orphan*/);
int /*<<< orphan*/ asprintf(char **, char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ basename(char *);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ printAndFail(char *, char *, int /*<<< orphan*/);
char *realpath(char *, int /*<<< orphan*/ *);
int stat(char *, struct stat *);
char *strdup(char *);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
char *createDestFilenamePath(char *destFilenameArgument, char *sourceFilename) {
  struct stat fileStat;
  if (stat(destFilenameArgument, &fileStat) == -1) {
    return strdup(destFilenameArgument);
  }
  char *destFilenameNormalized = realpath(destFilenameArgument, NULL);
  if (!destFilenameNormalized) {
    printAndFail("Cannot open (0) %s: %s\n", destFilenameArgument,
                 strerror(errno));
  }
  if (S_ISDIR(fileStat.st_mode)) {
    char *destFilename;
    asprintf(&destFilename, "%s/%s", destFilenameNormalized,
             basename(sourceFilename));
    free(destFilenameNormalized);
    return destFilename;
  }
  return destFilenameNormalized;
}


x86 assembly (clang, -O0):
<createDestFilenamePath>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0xffffffff,%eax
jne    11c8 <createDestFilenamePath+0x38>
mov    -0x10(%rbp),%rdi
call   10a0 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    1263 <createDestFilenamePath+0xd3>
mov    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1080 <realpath@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    120c <createDestFilenamePath+0x7c>
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2ded(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x38(%rbp),%rsi
mov    %eax,%edx
lea    0xdf9(%rip),%rdi
call   1050 <printAndFail@plt>
mov    -0x20(%rbp),%edi
call   1040 <S_ISDIR@plt>
cmp    $0x0,%rax
je     125b <createDestFilenamePath+0xcb>
mov    -0x28(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <basename@plt>
mov    -0x40(%rbp),%rdx
mov    %eax,%ecx
lea    -0x30(%rbp),%rdi
lea    0xdd8(%rip),%rsi
call   1090 <asprintf@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1263 <createDestFilenamePath+0xd3>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<createDestFilenamePath>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
mov    %rsp,%rsi
call   1070 <stat@plt>
cmp    $0xffffffff,%eax
je     1222 <createDestFilenamePath+0x92>
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <realpath@plt>
mov    %rax,%r15
test   %rax,%rax
jne    11dd <createDestFilenamePath+0x4d>
mov    0x2e1b(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xe2d(%rip),%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1050 <printAndFail@plt>
mov    (%rsp),%edi
call   1040 <S_ISDIR@plt>
test   %rax,%rax
je     1215 <createDestFilenamePath+0x85>
mov    %r14,%rdi
call   1060 <basename@plt>
lea    0xe1f(%rip),%rsi
lea    0x8(%rsp),%rdi
mov    %r15,%rdx
mov    %eax,%ecx
call   1090 <asprintf@plt>
mov    %r15,%rdi
call   1030 <free@plt>
mov    0x8(%rsp),%r15
mov    %r15,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
mov    %rbx,%rdi
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
jmp    10a0 <strdup@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [24 x i8] c"Cannot open (0) %s: %s\0A\00"
@errno = global i32 0
@.str.1 = [6 x i8] c"%s/%s\00"
define i8* @createDestFilenamePath(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca %struct.stat
%7 = alloca i8*
%8 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
%9 = load i8*, i8** %4
%10 = call i32 @stat(i8* %9, %struct.stat* %6)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = load i8*, i8** %4
%14 = call i8* @strdup(i8* %13)
store i8* %14, i8** %3
br label %40
15: ; preds = %2
%16 = load i8*, i8** %4
%17 = call i8* @realpath(i8* %16, i32* null)
store i8* %17, i8** %7
%18 = load i8*, i8** %7
%19 = icmp ne i8* %18, null
br i1 %19, label %25, label %20
20: ; preds = %15
%21 = load i8*, i8** %4
%22 = load i32, i32* @errno
%23 = call i32 @strerror(i32 %22)
%24 = call i32 @printAndFail(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %21, i32 %23)
br label %25
25: ; preds = %20, %15
%26 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%27 = load i32, i32* %26
%28 = call i64 @S_ISDIR(i32 %27)
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %38
30: ; preds = %25
%31 = load i8*, i8** %7
%32 = load i8*, i8** %5
%33 = call i32 @basename(i8* %32)
%34 = call i32 @asprintf(i8** %8, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %31, i32 %33)
%35 = load i8*, i8** %7
%36 = call i32 @free(i8* %35)
%37 = load i8*, i8** %8
store i8* %37, i8** %3
br label %40
38: ; preds = %25
%39 = load i8*, i8** %7
store i8* %39, i8** %3
br label %40
40: ; preds = %38, %30, %12
%41 = load i8*, i8** %3
ret i8* %41
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [24 x i8] c"Cannot open (0) %s: %s\0A\00"
@errno = global i32 0
@.str.1 = [6 x i8] c"%s/%s\00"
define i8* @createDestFilenamePath(i8* %0, i8* %1) {
%3 = alloca %struct.stat
%4 = alloca i8*
%5 = bitcast %struct.stat* %3 to i8*
%6 = call i32 @stat(i8* %0, %struct.stat* %3)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = tail call i8* @strdup(i8* %0)
br label %28
10: ; preds = %2
%11 = tail call i8* @realpath(i8* %0, i32* null)
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %17
13: ; preds = %10
%14 = load i32, i32* @errno
%15 = tail call i32 @strerror(i32 %14)
%16 = tail call i32 @printAndFail(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %15)
br label %17
17: ; preds = %13, %10
%18 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%19 = load i32, i32* %18
%20 = tail call i64 @S_ISDIR(i32 %19)
%21 = icmp eq i64 %20, 0
br i1 %21, label %28, label %22
22: ; preds = %17
%23 = bitcast i8** %4 to i8*
%24 = tail call i32 @basename(i8* %1)
%25 = call i32 @asprintf(i8** %4, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %11, i32 %24)
%26 = call i32 @free(i8* %11)
%27 = load i8*, i8** %4
br label %28
28: ; preds = %22, %17, %8
%29 = phi i8* [ %9, %8 ], [ %27, %22 ], [ %11, %17 ]
ret i8* %29
}