#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*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stderr;
scalar_t__ strlen(char *);
int /*<<< orphan*/ strncat(char *, char *, int);
int /*<<< orphan*/ strncpy(char *, char *, int);
FILE *open_file(char *base_path, char *suffix, char *mode) {
  char ts[1000] = "";
  int left = 999;
  strncpy(ts, base_path, left);
  left -= strlen(base_path);
  strncat(ts, suffix, left);
  left -= strlen(suffix);
  printf("Opening file: \"%s\" (mode: \"%s\")\n", ts, mode);
  FILE *f = fopen(ts, mode);
  if (f == NULL)
    fprintf(stderr, "Failed to open file \"%s\"!\n", ts);
  else
    printf("File opened.\n");
  return f;
}


x86 assembly (clang, -O0):
<open_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x410,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
lea    -0x400(%rbp),%rdi
xor    %esi,%esi
mov    $0x3e8,%edx
call   1060 <memset@plt>
movl   $0x3e7,-0x404(%rbp)
lea    -0x400(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x404(%rbp),%edx
call   1030 <strncpy@plt>
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
movslq -0x404(%rbp),%rax
sub    %rcx,%rax
mov    %eax,-0x404(%rbp)
lea    -0x400(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x404(%rbp),%edx
call   1070 <strncat@plt>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
movslq -0x404(%rbp),%rax
sub    %rcx,%rax
mov    %eax,-0x404(%rbp)
lea    -0x400(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    0xde6(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x400(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x410(%rbp)
cmpq   $0x0,-0x410(%rbp)
jne    1267 <open_file+0xf7>
mov    0x2d93(%rip),%rax
mov    (%rax),%edi
lea    -0x400(%rbp),%rdx
lea    0xdc4(%rip),%rsi
call   1080 <fprintf@plt>
jmp    1275 <open_file+0x105>
lea    0xdce(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x410(%rbp),%rax
add    $0x410,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_file>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x3f0,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
mov    %rsp,%r12
mov    $0x3e8,%edx
mov    %r12,%rdi
xor    %esi,%esi
call   1070 <memset@plt>
mov    $0x3e7,%ebp
mov    %r12,%rdi
mov    %rbx,%rsi
mov    $0x3e7,%edx
call   1030 <strncpy@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
sub    %eax,%ebp
mov    %r12,%rdi
mov    %r15,%rsi
mov    %ebp,%edx
call   1080 <strncat@plt>
lea    0xe23(%rip),%rdi
mov    %r12,%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r12,%rdi
mov    %r14,%rsi
call   10a0 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
je     120b <open_file+0x8b>
lea    0xe38(%rip),%rdi
call   1040 <puts@plt>
jmp    1223 <open_file+0xa3>
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
lea    0xe06(%rip),%rsi
mov    %rsp,%rdx
call   1090 <fprintf@plt>
mov    %rbx,%rax
add    $0x3f0,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [33 x i8] c"Opening file: \22%s\22 (mode: \22%s\22)\0A\00"
@stderr = global i32 0
@.str.1 = [27 x i8] c"Failed to open file \22%s\22!\0A\00"
@.str.2 = [14 x i8] c"File opened.\0A\00"
define i32* @open_file(i8* %0, i8* %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca [1000 x i8]
%8 = alloca i32
%9 = alloca i32*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i8* %2, i8** %6
%10 = bitcast [1000 x i8]* %7 to i8*
store i32 999, i32* %8
%11 = getelementptr inbounds [1000 x i8], [1000 x i8]* %7, i64 0, i64 0
%12 = load i8*, i8** %4
%13 = load i32, i32* %8
%14 = call i32 @strncpy(i8* %11, i8* %12, i32 %13)
%15 = load i8*, i8** %4
%16 = call i64 @strlen(i8* %15)
%17 = load i32, i32* %8
%18 = sext i32 %17 to i64
%19 = sub nsw i64 %18, %16
%20 = trunc i64 %19 to i32
store i32 %20, i32* %8
%21 = getelementptr inbounds [1000 x i8], [1000 x i8]* %7, i64 0, i64 0
%22 = load i8*, i8** %5
%23 = load i32, i32* %8
%24 = call i32 @strncat(i8* %21, i8* %22, i32 %23)
%25 = load i8*, i8** %5
%26 = call i64 @strlen(i8* %25)
%27 = load i32, i32* %8
%28 = sext i32 %27 to i64
%29 = sub nsw i64 %28, %26
%30 = trunc i64 %29 to i32
store i32 %30, i32* %8
%31 = getelementptr inbounds [1000 x i8], [1000 x i8]* %7, i64 0, i64 0
%32 = load i8*, i8** %6
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i8* %31, i8* %32)
%34 = getelementptr inbounds [1000 x i8], [1000 x i8]* %7, i64 0, i64 0
%35 = load i8*, i8** %6
%36 = call i32* @fopen(i8* %34, i8* %35)
store i32* %36, i32** %9
%37 = load i32*, i32** %9
%38 = icmp eq i32* %37, null
br i1 %38, label %39, label %43
39: ; preds = %3
%40 = load i32, i32* @stderr
%41 = getelementptr inbounds [1000 x i8], [1000 x i8]* %7, i64 0, i64 0
%42 = call i32 @fprintf(i32 %40, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %41)
br label %45
43: ; preds = %3
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %45
45: ; preds = %43, %39
%46 = load i32*, i32** %9
ret i32* %46
}

LLVM IR (-O2):
@.str = [33 x i8] c"Opening file: \22%s\22 (mode: \22%s\22)\0A\00"
@stderr = global i32 0
@.str.1 = [27 x i8] c"Failed to open file \22%s\22!\0A\00"
@str = [13 x i8] c"File opened.\00"
define noalias i32* @open_file(i8* %0, i8* %1, i8* %2) {
%4 = alloca [1000 x i8]
%5 = getelementptr inbounds [1000 x i8], [1000 x i8]* %4, i64 0, i64 0
%6 = call i32 @strncpy(i8* %5, i8* %0, i32 999)
%7 = call i64 @strlen(i8* dereferenceable(1) %0)
%8 = trunc i64 %7 to i32
%9 = sub i32 999, %8
%10 = call i32 @strncat(i8* %5, i8* %1, i32 %9)
%11 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i8* %5, i8* %2)
%12 = call i32* @fopen(i8* %5, i8* %2)
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %17
14: ; preds = %3
%15 = load i32, i32* @stderr
%16 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %5)
br label %19
17: ; preds = %3
%18 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
br label %19
19: ; preds = %17, %14
ret i32* %12
}