#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*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int read(int, char *, int);
int /*<<< orphan*/ sprintf(char *, char *, char const *, char const *);
int file2str(const char *directory, const char *what, char *ret, int cap) {
  static char filename[80];
  int fd, num_read;
  sprintf(filename, "%s/%s", directory, what);
  fd = open(filename, O_RDONLY, 0);
  if (fd == -1)
    return -1;
  num_read = read(fd, ret, cap - 1);
  close(fd);
  if (num_read <= 0)
    return -1;
  ret[num_read] = '\0';
  return num_read;
}


x86 assembly (clang, -O0):
<file2str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
lea    0x2eea(%rip),%rdi
lea    0xe93(%rip),%rsi
call   1060 <sprintf@plt>
mov    0x2e6f(%rip),%rax
mov    (%rax),%esi
lea    0x2ece(%rip),%rdi
xor    %edx,%edx
call   1050 <open@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x28(%rbp)
jne    11a2 <file2str+0x62>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e7 <file2str+0xa7>
mov    -0x28(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
sub    $0x1,%edx
call   1040 <read@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%edi
call   1030 <close@plt>
cmpl   $0x0,-0x2c(%rbp)
jg     11d5 <file2str+0x95>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e7 <file2str+0xa7>
mov    -0x20(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file2str>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %ecx,%ebp
mov    %rdx,%r14
mov    %rsi,%rcx
mov    %rdi,%rdx
lea    0x2ef7(%rip),%rbx
lea    0xea0(%rip),%rsi
mov    %rbx,%rdi
call   1060 <sprintf@plt>
mov    0x2e79(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
xor    %edx,%edx
call   1050 <open@plt>
mov    $0xffffffff,%r15d
cmp    $0xffffffff,%eax
je     11ae <file2str+0x6e>
mov    %eax,%ebx
add    $0xffffffff,%ebp
mov    %eax,%edi
mov    %r14,%rsi
mov    %ebp,%edx
call   1040 <read@plt>
mov    %eax,%ebp
mov    %ebx,%edi
call   1030 <close@plt>
test   %ebp,%ebp
jle    11ae <file2str+0x6e>
mov    %ebp,%eax
movb   $0x0,(%r14,%rax,1)
mov    %ebp,%r15d
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@file2str.filename = internal global [80 x i8] zeroinitializer
@.str = [6 x i8] c"%s/%s\00"
@O_RDONLY = global i32 0
define i32 @file2str(i8* %0, i8* %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %6
store i8* %1, i8** %7
store i8* %2, i8** %8
store i32 %3, i32* %9
%12 = load i8*, i8** %6
%13 = load i8*, i8** %7
%14 = call i32 @sprintf(i8* getelementptr inbounds ([80 x i8], [80 x i8]* @file2str.filename, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %12, i8* %13)
%15 = load i32, i32* @O_RDONLY
%16 = call i32 @open(i8* getelementptr inbounds ([80 x i8], [80 x i8]* @file2str.filename, i64 0, i64 0), i32 %15, i32 0)
store i32 %16, i32* %10
%17 = load i32, i32* %10
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %20
19: ; preds = %4
store i32 -1, i32* %5
br label %37
20: ; preds = %4
%21 = load i32, i32* %10
%22 = load i8*, i8** %8
%23 = load i32, i32* %9
%24 = sub nsw i32 %23, 1
%25 = call i32 @read(i32 %21, i8* %22, i32 %24)
store i32 %25, i32* %11
%26 = load i32, i32* %10
%27 = call i32 @close(i32 %26)
%28 = load i32, i32* %11
%29 = icmp sle i32 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %20
store i32 -1, i32* %5
br label %37
31: ; preds = %20
%32 = load i8*, i8** %8
%33 = load i32, i32* %11
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %32, i64 %34
store i8 0, i8* %35
%36 = load i32, i32* %11
store i32 %36, i32* %5
br label %37
37: ; preds = %31, %30, %19
%38 = load i32, i32* %5
ret i32 %38
}

LLVM IR (-O2):
@file2str.filename = internal global [80 x i8] zeroinitializer
@.str = [6 x i8] c"%s/%s\00"
@O_RDONLY = global i32 0
define i32 @file2str(i8* %0, i8* %1, i8* nocapture %2, i32 %3) {
%5 = tail call i32 @sprintf(i8* dereferenceable(1) getelementptr inbounds ([80 x i8], [80 x i8]* @file2str.filename, i64 0, i64 0), i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %1)
%6 = load i32, i32* @O_RDONLY
%7 = tail call i32 @open(i8* getelementptr inbounds ([80 x i8], [80 x i8]* @file2str.filename, i64 0, i64 0), i32 %6, i32 0)
%8 = icmp eq i32 %7, -1
br i1 %8, label %17, label %9
9: ; preds = %4
%10 = add nsw i32 %3, -1
%11 = tail call i32 @read(i32 %7, i8* %2, i32 %10)
%12 = tail call i32 @close(i32 %7)
%13 = icmp slt i32 %11, 1
br i1 %13, label %17, label %14
14: ; preds = %9
%15 = zext i32 %11 to i64
%16 = getelementptr inbounds i8, i8* %2, i64 %15
store i8 0, i8* %16
br label %17
17: ; preds = %9, %4, %14
%18 = phi i32 [ %11, %14 ], [ -1, %4 ], [ -1, %9 ]
ret i32 %18
}