#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 */
struct str {
  int dummy;
};
typedef void FILE;

/* Variables and functions */
int /*<<< orphan*/ errx(int, char *);
void *fopen(char *, char *);
int /*<<< orphan*/ str_copy(struct str const *, char *, int /*<<< orphan*/,
                            unsigned int);
unsigned int str_len(struct str const *);
void *stdio_open(void *fctxt, const struct str *path_str) {
  char path[1024];
  unsigned len = str_len(path_str);
  FILE *f;
  if (len >= sizeof path - 1)
    errx(1, "path too long");
  str_copy(path_str, path, 0, len);
  path[len] = '\0';
  f = fopen(path, "rb");
  return f;
}


x86 assembly (clang, -O0):
<stdio_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <str_len@plt>
mov    %eax,-0x414(%rbp)
mov    -0x414(%rbp),%eax
cmp    $0x3ff,%rax
jb     1185 <stdio_open+0x45>
mov    $0x1,%edi
lea    0xe80(%rip),%rsi
call   1030 <errx@plt>
mov    -0x10(%rbp),%rdi
lea    -0x410(%rbp),%rsi
mov    -0x414(%rbp),%ecx
xor    %edx,%edx
call   1050 <str_copy@plt>
mov    -0x414(%rbp),%eax
movb   $0x0,-0x410(%rbp,%rax,1)
lea    -0x410(%rbp),%rdi
lea    0xe55(%rip),%rsi
call   1040 <fopen@plt>
mov    %rax,-0x420(%rbp)
mov    -0x420(%rbp),%rax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<stdio_open>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x408,%rsp
mov    %rsi,%rbx
mov    %rsi,%rdi
call   1060 <str_len@plt>
mov    %eax,%ebp
cmp    $0x3ff,%eax
jb     1172 <stdio_open+0x32>
lea    0xe98(%rip),%rsi
mov    $0x1,%edi
call   1030 <errx@plt>
mov    %ebp,%r15d
mov    %rsp,%r14
mov    %rbx,%rdi
mov    %r14,%rsi
xor    %edx,%edx
mov    %ebp,%ecx
call   1050 <str_copy@plt>
movb   $0x0,(%rsp,%r15,1)
lea    0xe7b(%rip),%rsi
mov    %r14,%rdi
call   1040 <fopen@plt>
add    $0x408,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.str = type { i32 }
@.str = [14 x i8] c"path too long\00"
@.str.1 = [3 x i8] c"rb\00"
define i8* @stdio_open(i8* %0, %struct.str* %1) {
%3 = alloca i8*
%4 = alloca %struct.str*
%5 = alloca [1024 x i8]
%6 = alloca i32
%7 = alloca i8*
store i8* %0, i8** %3
store %struct.str* %1, %struct.str** %4
%8 = load %struct.str*, %struct.str** %4
%9 = call i32 @str_len(%struct.str* %8)
store i32 %9, i32* %6
%10 = load i32, i32* %6
%11 = zext i32 %10 to i64
%12 = icmp uge i64 %11, 1023
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i32 @errx(i32 1, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %15
15: ; preds = %13, %2
%16 = load %struct.str*, %struct.str** %4
%17 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%18 = load i32, i32* %6
%19 = call i32 @str_copy(%struct.str* %16, i8* %17, i32 0, i32 %18)
%20 = load i32, i32* %6
%21 = zext i32 %20 to i64
%22 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 %21
store i8 0, i8* %22
%23 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%24 = call i8* @fopen(i8* %23, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i8* %24, i8** %7
%25 = load i8*, i8** %7
ret i8* %25
}

LLVM IR (-O2):
%struct.str = type { i32 }
@.str = [14 x i8] c"path too long\00"
@.str.1 = [3 x i8] c"rb\00"
define noalias i8* @stdio_open(i8* nocapture readnone %0, %struct.str* %1) {
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%5 = tail call i32 @str_len(%struct.str* %1)
%6 = icmp ugt i32 %5, 1022
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = tail call i32 @errx(i32 1, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %9
9: ; preds = %7, %2
%10 = zext i32 %5 to i64
%11 = call i32 @str_copy(%struct.str* %1, i8* %4, i32 0, i32 %5)
%12 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 %10
store i8 0, i8* %12
%13 = call i8* @fopen(i8* %4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
ret i8* %13
}