#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 */
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int strlen(char const *);
int /*<<< orphan*/ strncpy(char *, char const *, int);
int syscall(int, int, long, size_t, int, int);
int open(const char *fn, int flags, int mode) {
  static char buf[128] __attribute__((aligned(64)));
  size_t fn_size = strlen(fn) + 1;
  if (fn_size > sizeof(buf)) {
    return -1;
  }
  strncpy(buf, fn, sizeof(buf));
  return syscall(56, -100, (long)buf, fn_size, flags, mode);
}


x86 assembly (clang, -O0):
<open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
cltq
mov    %rax,-0x20(%rbp)
cmpq   $0x80,-0x20(%rbp)
jbe    116e <open+0x3e>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a8 <open+0x78>
mov    -0x10(%rbp),%rsi
lea    0x2f07(%rip),%rdi
mov    $0x80,%edx
call   1030 <strncpy@plt>
mov    -0x20(%rbp),%rcx
mov    -0x14(%rbp),%r8d
mov    -0x18(%rbp),%r9d
mov    $0x38,%edi
mov    $0xffffff9c,%esi
lea    0x2ee0(%rip),%rdx
call   1050 <syscall@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%r14d
mov    %esi,%ebp
mov    %rdi,%rbx
call   1040 <strlen@plt>
add    $0x1,%eax
cmp    $0x80,%eax
jbe    115d <open+0x2d>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %eax,%r15d
lea    0x2f19(%rip),%r12
mov    %r12,%rdi
mov    %rbx,%rsi
mov    $0x80,%edx
call   1030 <strncpy@plt>
mov    $0x38,%edi
mov    $0xffffff9c,%esi
mov    %r12,%rdx
mov    %r15,%rcx
mov    %ebp,%r8d
mov    %r14d,%r9d
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <syscall@plt>

LLVM IR (-O0):
@open.buf = internal global [128 x i8] zeroinitializer
define i32 @open(i8* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i64
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%9 = load i8*, i8** %5
%10 = call i32 @strlen(i8* %9)
%11 = add nsw i32 %10, 1
%12 = sext i32 %11 to i64
store i64 %12, i64* %8
%13 = load i64, i64* %8
%14 = icmp ugt i64 %13, 128
br i1 %14, label %15, label %16
15: ; preds = %3
store i32 -1, i32* %4
br label %23
16: ; preds = %3
%17 = load i8*, i8** %5
%18 = call i32 @strncpy(i8* getelementptr inbounds ([128 x i8], [128 x i8]* @open.buf, i64 0, i64 0), i8* %17, i32 128)
%19 = load i64, i64* %8
%20 = load i32, i32* %6
%21 = load i32, i32* %7
%22 = call i32 @syscall(i32 56, i32 -100, i64 ptrtoint ([128 x i8]* @open.buf to i64), i64 %19, i32 %20, i32 %21)
store i32 %22, i32* %4
br label %23
23: ; preds = %16, %15
%24 = load i32, i32* %4
ret i32 %24
}

LLVM IR (-O2):
@open.buf = internal global [128 x i8] zeroinitializer
define i32 @open(i8* %0, i32 %1, i32 %2) {
%4 = tail call i32 @strlen(i8* %0)
%5 = add nsw i32 %4, 1
%6 = icmp ugt i32 %5, 128
br i1 %6, label %11, label %7
7: ; preds = %3
%8 = zext i32 %5 to i64
%9 = tail call i32 @strncpy(i8* getelementptr inbounds ([128 x i8], [128 x i8]* @open.buf, i64 0, i64 0), i8* %0, i32 128)
%10 = tail call i32 @syscall(i32 56, i32 -100, i64 ptrtoint ([128 x i8]* @open.buf to i64), i64 %8, i32 %1, i32 %2)
br label %11
11: ; preds = %3, %7
%12 = phi i32 [ %10, %7 ], [ -1, %3 ]
ret i32 %12
}