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

/* Variables and functions */
int EOF;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ printf(char *, char const *);
int stringFromFile(const char *restrict path, char *dest, uint32_t maxWrite) {
  if (!dest || !path || maxWrite < 1)
    return 0;
  FILE *fp = fopen(path, "r");
  if (!fp) {
    printf("error: could not open file \"%s\"\n", path);
    return 0;
  }
  int c, i = 0;
  for (; i < maxWrite; i++) {
    c = fgetc(fp);
    if (c == EOF) {
      dest[i] = '\0';
      break;
    }
    dest[i] = c;
  }
  fclose(fp);
  return i;
}


x86 assembly (clang, -O0):
<stringFromFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1173 <stringFromFile+0x33>
cmpq   $0x0,-0x10(%rbp)
je     1173 <stringFromFile+0x33>
cmpl   $0x1,-0x1c(%rbp)
jge    117f <stringFromFile+0x3f>
movl   $0x0,-0x4(%rbp)
jmp    1229 <stringFromFile+0xe9>
mov    -0x10(%rbp),%rdi
lea    0xe76(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11ba <stringFromFile+0x7a>
mov    -0x10(%rbp),%rsi
lea    0xe59(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1229 <stringFromFile+0xe9>
movl   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    121a <stringFromFile+0xda>
mov    -0x28(%rbp),%rdi
call   1050 <fgetc@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    0x2e05(%rip),%rcx
cmp    (%rcx),%eax
jne    11fc <stringFromFile+0xbc>
mov    -0x18(%rbp),%rax
movslq -0x30(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    121a <stringFromFile+0xda>
mov    -0x2c(%rbp),%eax
mov    %al,%dl
mov    -0x18(%rbp),%rax
movslq -0x30(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    11c1 <stringFromFile+0x81>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<stringFromFile>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
xor    %ebx,%ebx
test   %rdi,%rdi
je     11bd <stringFromFile+0x7d>
mov    %rsi,%r14
test   %rsi,%rsi
je     11bd <stringFromFile+0x7d>
mov    %edx,%ebp
test   %edx,%edx
jle    11bd <stringFromFile+0x7d>
mov    %rdi,%r12
lea    0xe99(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     119f <stringFromFile+0x5f>
mov    %rax,%r15
mov    %ebp,%ebp
xor    %ebx,%ebx
mov    0x2e69(%rip),%r12
nop
mov    %r15,%rdi
call   1050 <fgetc@plt>
cmp    (%r12),%eax
je     11b0 <stringFromFile+0x70>
mov    %al,(%r14,%rbx,1)
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    1180 <stringFromFile+0x40>
mov    %ebp,%ebx
jmp    11b5 <stringFromFile+0x75>
lea    0xe5c(%rip),%rdi
mov    %r12,%rsi
call   1040 <printf@plt>
jmp    11bd <stringFromFile+0x7d>
movb   $0x0,(%r14,%rbx,1)
mov    %r15,%rdi
call   1030 <fclose@plt>
mov    %ebx,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [33 x i8] c"error: could not open file \22%s\22\0A\00"
@EOF = global i32 0
define i32 @stringFromFile(i8* noalias %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%11 = load i8*, i8** %6
%12 = icmp ne i8* %11, null
br i1 %12, label %13, label %19
13: ; preds = %3
%14 = load i8*, i8** %5
%15 = icmp ne i8* %14, null
br i1 %15, label %16, label %19
16: ; preds = %13
%17 = load i32, i32* %7
%18 = icmp slt i32 %17, 1
br i1 %18, label %19, label %20
19: ; preds = %16, %13, %3
store i32 0, i32* %4
br label %58
20: ; preds = %16
%21 = load i8*, i8** %5
%22 = call i32* @fopen(i8* %21, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %22, i32** %8
%23 = load i32*, i32** %8
%24 = icmp ne i32* %23, null
br i1 %24, label %28, label %25
25: ; preds = %20
%26 = load i8*, i8** %5
%27 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %26)
store i32 0, i32* %4
br label %58
28: ; preds = %20
store i32 0, i32* %10
br label %29
29: ; preds = %51, %28
%30 = load i32, i32* %10
%31 = load i32, i32* %7
%32 = icmp slt i32 %30, %31
br i1 %32, label %33, label %54
33: ; preds = %29
%34 = load i32*, i32** %8
%35 = call i32 @fgetc(i32* %34)
store i32 %35, i32* %9
%36 = load i32, i32* %9
%37 = load i32, i32* @EOF
%38 = icmp eq i32 %36, %37
br i1 %38, label %39, label %44
39: ; preds = %33
%40 = load i8*, i8** %6
%41 = load i32, i32* %10
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i8, i8* %40, i64 %42
store i8 0, i8* %43
br label %54
44: ; preds = %33
%45 = load i32, i32* %9
%46 = trunc i32 %45 to i8
%47 = load i8*, i8** %6
%48 = load i32, i32* %10
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds i8, i8* %47, i64 %49
store i8 %46, i8* %50
br label %51
51: ; preds = %44
%52 = load i32, i32* %10
%53 = add nsw i32 %52, 1
store i32 %53, i32* %10
br label %29
54: ; preds = %39, %29
%55 = load i32*, i32** %8
%56 = call i32 @fclose(i32* %55)
%57 = load i32, i32* %10
store i32 %57, i32* %4
br label %58
58: ; preds = %54, %25, %19
%59 = load i32, i32* %4
ret i32 %59
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [33 x i8] c"error: could not open file \22%s\22\0A\00"
@EOF = global i32 0
define i32 @stringFromFile(i8* noalias %0, i8* writeonly %1, i32 %2) {
%4 = icmp eq i8* %1, null
%5 = icmp eq i8* %0, null
%6 = or i1 %5, %4
%7 = icmp slt i32 %2, 1
%8 = or i1 %6, %7
br i1 %8, label %33, label %9
9: ; preds = %3
%10 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%11 = icmp eq i32* %10, null
br i1 %11, label %14, label %12
12: ; preds = %9
%13 = zext i32 %2 to i64
br label %16
14: ; preds = %9
%15 = tail call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %33
16: ; preds = %12, %25
%17 = phi i64 [ 0, %12 ], [ %28, %25 ]
%18 = tail call i32 @fgetc(i32* %10)
%19 = load i32, i32* @EOF
%20 = icmp eq i32 %18, %19
br i1 %20, label %21, label %25
21: ; preds = %16
%22 = trunc i64 %17 to i32
%23 = and i64 %17, 4294967295
%24 = getelementptr inbounds i8, i8* %1, i64 %23
store i8 0, i8* %24
br label %30
25: ; preds = %16
%26 = trunc i32 %18 to i8
%27 = getelementptr inbounds i8, i8* %1, i64 %17
store i8 %26, i8* %27
%28 = add nuw nsw i64 %17, 1
%29 = icmp eq i64 %28, %13
br i1 %29, label %30, label %16
30: ; preds = %25, %21
%31 = phi i32 [ %22, %21 ], [ %2, %25 ]
%32 = tail call i32 @fclose(i32* %10)
br label %33
33: ; preds = %14, %30, %3
%34 = phi i32 [ 0, %3 ], [ %31, %30 ], [ 0, %14 ]
ret i32 %34
}