#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*/ free(char *);
scalar_t__ malloc(scalar_t__);
int open(char *, int);
int /*<<< orphan*/ sprintf(char *, char *, char const *, char const *);
scalar_t__ strlen(char const *);
int file_open(const char *root, const char *path, int oflag) {
  char *file = NULL;
  int fd = -1;
  file = (char *)malloc(strlen(root) + strlen(path) + 2);
  if (file == NULL)
    return -1;
  sprintf(file, "%s/%s", root, path);
  if ((fd = open(file, oflag)) < 0)
    goto error;
  free(file);
  return fd;
error:
  if (file != NULL)
    free(file);
  return -1;
}


x86 assembly (clang, -O0):
<file_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movq   $0x0,-0x28(%rbp)
movl   $0xffffffff,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x38(%rbp),%rdi
add    %rax,%rdi
add    $0x2,%rdi
call   1050 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11b3 <file_open+0x63>
movl   $0xffffffff,-0x4(%rbp)
jmp    1217 <file_open+0xc7>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
lea    0xe3a(%rip),%rsi
call   1070 <sprintf@plt>
mov    -0x28(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1060 <open@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
jge    11e8 <file_open+0x98>
jmp    11fc <file_open+0xac>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1217 <file_open+0xc7>
cmpq   $0x0,-0x28(%rbp)
je     1210 <file_open+0xc0>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_open>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %rsi,%r15
mov    %rdi,%rbp
call   1040 <strlen@plt>
mov    %rax,%rbx
mov    %r15,%rdi
call   1040 <strlen@plt>
lea    (%rbx,%rax,1),%rdi
add    $0x2,%rdi
call   1050 <malloc@plt>
test   %rax,%rax
je     11bb <file_open+0x6b>
mov    %rax,%rbx
lea    0xe74(%rip),%rsi
mov    %rax,%rdi
mov    %rbp,%rdx
mov    %r15,%rcx
call   1070 <sprintf@plt>
mov    %rbx,%rdi
mov    %r14d,%esi
call   1060 <open@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1030 <free@plt>
test   %ebp,%ebp
mov    $0xffffffff,%eax
cmovns %ebp,%eax
jmp    11c0 <file_open+0x70>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [6 x i8] c"%s/%s\00"
define i32 @file_open(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i8* null, i8** %8
store i32 -1, i32* %9
%10 = load i8*, i8** %5
%11 = call i64 @strlen(i8* %10)
%12 = load i8*, i8** %6
%13 = call i64 @strlen(i8* %12)
%14 = add nsw i64 %11, %13
%15 = add nsw i64 %14, 2
%16 = call i64 @malloc(i64 %15)
%17 = inttoptr i64 %16 to i8*
store i8* %17, i8** %8
%18 = load i8*, i8** %8
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %21
20: ; preds = %3
store i32 -1, i32* %4
br label %42
21: ; preds = %3
%22 = load i8*, i8** %8
%23 = load i8*, i8** %5
%24 = load i8*, i8** %6
%25 = call i32 @sprintf(i8* %22, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %23, i8* %24)
%26 = load i8*, i8** %8
%27 = load i32, i32* %7
%28 = call i32 @open(i8* %26, i32 %27)
store i32 %28, i32* %9
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %21
br label %35
31: ; preds = %21
%32 = load i8*, i8** %8
%33 = call i32 @free(i8* %32)
%34 = load i32, i32* %9
store i32 %34, i32* %4
br label %42
35: ; preds = %30
%36 = load i8*, i8** %8
%37 = icmp ne i8* %36, null
br i1 %37, label %38, label %41
38: ; preds = %35
%39 = load i8*, i8** %8
%40 = call i32 @free(i8* %39)
br label %41
41: ; preds = %38, %35
store i32 -1, i32* %4
br label %42
42: ; preds = %41, %31, %20
%43 = load i32, i32* %4
ret i32 %43
}

LLVM IR (-O2):
@.str = [6 x i8] c"%s/%s\00"
define i32 @file_open(i8* %0, i8* %1, i32 %2) {
%4 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%5 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%6 = add i64 %4, 2
%7 = add i64 %6, %5
%8 = tail call i64 @malloc(i64 %7)
%9 = inttoptr i64 %8 to i8*
%10 = icmp eq i64 %8, 0
br i1 %10, label %17, label %11
11: ; preds = %3
%12 = tail call i32 @sprintf(i8* %9, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %1)
%13 = tail call i32 @open(i8* %9, i32 %2)
%14 = tail call i32 @free(i8* %9)
%15 = icmp sgt i32 %13, -1
%16 = select i1 %15, i32 %13, i32 -1
br label %17
17: ; preds = %11, %3
%18 = phi i32 [ -1, %3 ], [ %16, %11 ]
ret i32 %18
}