#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 */
int ENOMEM;
int S_ISREG(int /*<<< orphan*/);
int /*<<< orphan*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ memcpy(char *, char *, int);
int stat(char *, struct stat *);
int strlen(char *);
int file_exists_with_prefix(char *prefix, char *path) {
  struct stat s;
  int rv;
  char *fullpath;
  int prefix_len = (prefix == NULL) ? 0 : strlen(prefix);
  int path_len = strlen(path);
  fullpath = malloc(prefix_len + path_len + 1);
  if (fullpath == NULL)
    return -ENOMEM;
  memcpy(fullpath, prefix, prefix_len);
  memcpy(fullpath + prefix_len, path, path_len + 1);
  rv = stat(fullpath, &s);
  free(fullpath);
  return rv ? 0 : S_ISREG(s.st_mode);
}


x86 assembly (clang, -O0):
<file_exists_with_prefix>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1185 <file_exists_with_prefix+0x25>
xor    %eax,%eax
mov    %eax,-0x3c(%rbp)
jmp    1191 <file_exists_with_prefix+0x31>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x38(%rbp)
mov    -0x34(%rbp),%edi
add    -0x38(%rbp),%edi
add    $0x1,%edi
call   1080 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11d3 <file_exists_with_prefix+0x73>
xor    %eax,%eax
mov    0x2e17(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    123b <file_exists_with_prefix+0xdb>
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x34(%rbp),%edx
call   1060 <memcpy@plt>
mov    -0x30(%rbp),%rdi
movslq -0x34(%rbp),%rax
add    %rax,%rdi
mov    -0x18(%rbp),%rsi
mov    -0x38(%rbp),%edx
add    $0x1,%edx
call   1060 <memcpy@plt>
mov    -0x30(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1050 <stat@plt>
mov    %eax,-0x24(%rbp)
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
cmpl   $0x0,-0x24(%rbp)
je     122a <file_exists_with_prefix+0xca>
xor    %eax,%eax
mov    %eax,-0x40(%rbp)
jmp    1235 <file_exists_with_prefix+0xd5>
mov    -0x20(%rbp),%edi
call   1070 <S_ISREG@plt>
mov    %eax,-0x40(%rbp)
mov    -0x40(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_exists_with_prefix>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
test   %rdi,%rdi
je     1183 <file_exists_with_prefix+0x23>
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %eax,%r12d
jmp    1186 <file_exists_with_prefix+0x26>
xor    %r12d,%r12d
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %eax,%r13d
lea    (%r12,%r13,1),%edi
add    $0x1,%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     11f9 <file_exists_with_prefix+0x99>
mov    %rax,%rbx
add    $0x1,%r13d
mov    %rax,%rdi
mov    %r15,%rsi
mov    %r12d,%edx
call   1060 <memcpy@plt>
movslq %r12d,%rdi
add    %rbx,%rdi
mov    %r14,%rsi
mov    %r13d,%edx
call   1060 <memcpy@plt>
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1050 <stat@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %eax,%eax
test   %ebp,%ebp
jne    1204 <file_exists_with_prefix+0xa4>
mov    (%rsp),%edi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1070 <S_ISREG@plt>
mov    0x2de0(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@ENOMEM = global i32 0
define i32 @file_exists_with_prefix(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca %struct.stat
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%11 = load i8*, i8** %4
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %14
13: ; preds = %2
br label %17
14: ; preds = %2
%15 = load i8*, i8** %4
%16 = call i32 @strlen(i8* %15)
br label %17
17: ; preds = %14, %13
%18 = phi i32 [ 0, %13 ], [ %16, %14 ]
store i32 %18, i32* %9
%19 = load i8*, i8** %5
%20 = call i32 @strlen(i8* %19)
store i32 %20, i32* %10
%21 = load i32, i32* %9
%22 = load i32, i32* %10
%23 = add nsw i32 %21, %22
%24 = add nsw i32 %23, 1
%25 = call i8* @malloc(i32 %24)
store i8* %25, i8** %8
%26 = load i8*, i8** %8
%27 = icmp eq i8* %26, null
br i1 %27, label %28, label %31
28: ; preds = %17
%29 = load i32, i32* @ENOMEM
%30 = sub nsw i32 0, %29
store i32 %30, i32* %3
br label %57
31: ; preds = %17
%32 = load i8*, i8** %8
%33 = load i8*, i8** %4
%34 = load i32, i32* %9
%35 = call i32 @memcpy(i8* %32, i8* %33, i32 %34)
%36 = load i8*, i8** %8
%37 = load i32, i32* %9
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %36, i64 %38
%40 = load i8*, i8** %5
%41 = load i32, i32* %10
%42 = add nsw i32 %41, 1
%43 = call i32 @memcpy(i8* %39, i8* %40, i32 %42)
%44 = load i8*, i8** %8
%45 = call i32 @stat(i8* %44, %struct.stat* %6)
store i32 %45, i32* %7
%46 = load i8*, i8** %8
%47 = call i32 @free(i8* %46)
%48 = load i32, i32* %7
%49 = icmp ne i32 %48, 0
br i1 %49, label %50, label %51
50: ; preds = %31
br label %55
51: ; preds = %31
%52 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%53 = load i32, i32* %52
%54 = call i32 @S_ISREG(i32 %53)
br label %55
55: ; preds = %51, %50
%56 = phi i32 [ 0, %50 ], [ %54, %51 ]
store i32 %56, i32* %3
br label %57
57: ; preds = %55, %28
%58 = load i32, i32* %3
ret i32 %58
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@ENOMEM = global i32 0
define i32 @file_exists_with_prefix(i8* %0, i8* %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = icmp eq i8* %0, null
br i1 %5, label %8, label %6
6: ; preds = %2
%7 = tail call i32 @strlen(i8* %0)
br label %8
8: ; preds = %2, %6
%9 = phi i32 [ %7, %6 ], [ 0, %2 ]
%10 = tail call i32 @strlen(i8* %1)
%11 = add i32 %10, 1
%12 = add i32 %11, %9
%13 = tail call i8* @malloc(i32 %12)
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %18
15: ; preds = %8
%16 = load i32, i32* @ENOMEM
%17 = sub nsw i32 0, %16
br label %30
18: ; preds = %8
%19 = tail call i32 @memcpy(i8* %13, i8* %0, i32 %9)
%20 = sext i32 %9 to i64
%21 = getelementptr inbounds i8, i8* %13, i64 %20
%22 = tail call i32 @memcpy(i8* %21, i8* %1, i32 %11)
%23 = call i32 @stat(i8* %13, %struct.stat* %3)
%24 = tail call i32 @free(i8* %13)
%25 = icmp eq i32 %23, 0
br i1 %25, label %26, label %30
26: ; preds = %18
%27 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%28 = load i32, i32* %27
%29 = tail call i32 @S_ISREG(i32 %28)
br label %30
30: ; preds = %26, %18, %15
%31 = phi i32 [ %17, %15 ], [ %29, %26 ], [ 0, %18 ]
ret i32 %31
}