int xopen(char *filename, int flags, int mode) {
  int ret;
  printf("[+] Opening file '%s' with flags 0x%x and mode 0x%x\n", filename,
         flags, mode);
  ret = open(filename, flags, mode);
  if (ret <= 0) {
    printf("[-] Unable to open file\n");
    perror(__func__);
    exit(1);
  }
  printf("[+] open returned: %d\n", ret);
  return ret;
}


x86 assembly (clang, -O0):
<xopen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    -0x8(%rbp),%rsi
mov    -0xc(%rbp),%edx
mov    -0x10(%rbp),%ecx
lea    0xe9d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
mov    $0x0,%al
call   1040 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jg     11ae <xopen+0x6e>
lea    0xea6(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xeb1(%rip),%rdi
mov    $0x0,%al
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x14(%rbp),%esi
lea    0xe9c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xopen>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%r14d
mov    %esi,%ebp
mov    %rdi,%rbx
lea    0xe9d(%rip),%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
mov    %r14d,%ecx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
mov    %r14d,%edx
xor    %eax,%eax
call   1050 <open@plt>
test   %eax,%eax
jle    119e <xopen+0x4e>
mov    %eax,%ebx
lea    0xead(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xead(%rip),%rdi
call   1030 <puts@plt>
lea    0xe84(%rip),%rdi
xor    %eax,%eax
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [53 x i8] c"[+] Opening file '%s' with flags 0x%x and mode 0x%x\0A\00"
@.str.1 = [25 x i8] c"[-] Unable to open file\0A\00"
@__func__.xopen = [6 x i8] c"xopen\00"
@.str.2 = [23 x i8] c"[+] open returned: %d\0A\00"
define i32 @xopen(i8* %0, i32 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%8 = load i8*, i8** %4
%9 = load i32, i32* %5
%10 = load i32, i32* %6
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str, i64 0, i64 0), i8* %8, i32 %9, i32 %10)
%12 = load i8*, i8** %4
%13 = load i32, i32* %5
%14 = load i32, i32* %6
%15 = call i32 (i8*, i32, i32, ...) bitcast (i32 (...)* @open to i32 (i8*, i32, i32, ...)*)(i8* %12, i32 %13, i32 %14)
store i32 %15, i32* %7
%16 = load i32, i32* %7
%17 = icmp sle i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %3
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
%20 = call i32 (i8*, ...) bitcast (i32 (...)* @perror to i32 (i8*, ...)*)(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @__func__.xopen, i64 0, i64 0))
unreachable
21: ; preds = %3
%22 = load i32, i32* %7
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i32 %22)
%24 = load i32, i32* %7
ret i32 %24
}

LLVM IR (-O2):
@.str = [53 x i8] c"[+] Opening file '%s' with flags 0x%x and mode 0x%x\0A\00"
@__func__.xopen = [6 x i8] c"xopen\00"
@.str.2 = [23 x i8] c"[+] open returned: %d\0A\00"
@str = [24 x i8] c"[-] Unable to open file\00"
define i32 @xopen(i8* %0, i32 %1, i32 %2) {
%4 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %1, i32 %2)
%5 = tail call i32 (i8*, i32, i32, ...) bitcast (i32 (...)* @open to i32 (i8*, i32, i32, ...)*)(i8* %0, i32 %1, i32 %2)
%6 = icmp slt i32 %5, 1
br i1 %6, label %7, label %10
7: ; preds = %3
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str, i64 0, i64 0))
%9 = tail call i32 (i8*, ...) bitcast (i32 (...)* @perror to i32 (i8*, ...)*)(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @__func__.xopen, i64 0, i64 0))
tail call void @exit(i32 1)
unreachable
10: ; preds = %3
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i32 %5)
ret i32 %5
}