#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 ENOMEM;
int errno;
int /*<<< orphan*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ memcpy(char *, char *, int);
int open(char *, int);
int strlen(char *);
int open_file(char *prefix, char *path, int flags) {
  int prefix_len = (prefix == NULL) ? 0 : strlen(prefix);
  int path_len = strlen(path);
  char *fullpath;
  int rv;
  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 = open(fullpath, flags);
  if (rv < 0)
    rv = -errno;
  free(fullpath);
  return rv;
}


x86 assembly (clang, -O0):
<open_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1178 <open_file+0x28>
xor    %eax,%eax
mov    %eax,-0x38(%rbp)
jmp    1184 <open_file+0x34>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x38(%rbp)
mov    -0x38(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%edi
add    -0x24(%rbp),%edi
add    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11c6 <open_file+0x76>
xor    %eax,%eax
mov    0x2e24(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1226 <open_file+0xd6>
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%edx
call   1050 <memcpy@plt>
mov    -0x30(%rbp),%rdi
movslq -0x20(%rbp),%rax
add    %rax,%rdi
mov    -0x18(%rbp),%rsi
mov    -0x24(%rbp),%edx
add    $0x1,%edx
call   1050 <memcpy@plt>
mov    -0x30(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1070 <open@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    1217 <open_file+0xc7>
xor    %eax,%eax
mov    0x2dc6(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x34(%rbp)
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %rsi,%r15
mov    %rdi,%r12
test   %rdi,%rdi
je     1176 <open_file+0x26>
mov    %r12,%rdi
call   1040 <strlen@plt>
mov    %eax,%r13d
jmp    1179 <open_file+0x29>
xor    %r13d,%r13d
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebp
lea    0x1(%rbp,%r13,1),%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     11db <open_file+0x8b>
mov    %rax,%rbx
add    $0x1,%ebp
mov    %rax,%rdi
mov    %r12,%rsi
mov    %r13d,%edx
call   1050 <memcpy@plt>
movslq %r13d,%rdi
add    %rbx,%rdi
mov    %r15,%rsi
mov    %ebp,%edx
call   1050 <memcpy@plt>
mov    %rbx,%rdi
mov    %r14d,%esi
call   1070 <open@plt>
mov    0x2e10(%rip),%rcx
xor    %ebp,%ebp
sub    (%rcx),%ebp
test   %eax,%eax
cmovns %eax,%ebp
mov    %rbx,%rdi
call   1030 <free@plt>
jmp    11e6 <open_file+0x96>
mov    0x2dfe(%rip),%rax
xor    %ebp,%ebp
sub    (%rax),%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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