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

/* Variables and functions */
char *cc_fullname(char *, char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
char *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
char *strdup(char *);
int strlen(char *);
char *load_string(char *dir, char *file) {
  char *full = cc_fullname(dir, file);
  char line[4096];
  char *r;
  FILE *f;
  int rlen;
  f = fopen(full, "r");
  free(full);
  if (!f)
    return NULL;
  r = fgets(line, sizeof(line), f);
  fclose(f);
  if (!r)
    return NULL;
  rlen = strlen(r);
  if (r[rlen - 1] == '\n')
    r[rlen - 1] = '\0';
  return strdup(r);
}


x86 assembly (clang, -O0):
<load_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1040,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1070 <cc_fullname@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xe61(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x1030(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
cmpq   $0x0,-0x1030(%rbp)
jne    11cf <load_string+0x5f>
movq   $0x0,-0x8(%rbp)
jmp    126e <load_string+0xfe>
lea    -0x1020(%rbp),%rdi
mov    -0x1030(%rbp),%rdx
mov    $0x1000,%esi
call   1060 <fgets@plt>
mov    %rax,-0x1028(%rbp)
mov    -0x1030(%rbp),%rdi
call   1040 <fclose@plt>
cmpq   $0x0,-0x1028(%rbp)
jne    1215 <load_string+0xa5>
movq   $0x0,-0x8(%rbp)
jmp    126e <load_string+0xfe>
mov    -0x1028(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x1034(%rbp)
mov    -0x1028(%rbp),%rax
mov    -0x1034(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    125e <load_string+0xee>
mov    -0x1028(%rbp),%rax
mov    -0x1034(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x1028(%rbp),%rdi
call   1090 <strdup@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x1040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load_string>:
push   %r15
push   %r14
push   %rbx
sub    $0x1000,%rsp
call   1070 <cc_fullname@plt>
mov    %rax,%rbx
lea    0xe75(%rip),%rsi
mov    %rax,%rdi
call   1080 <fopen@plt>
mov    %rax,%r15
mov    %rbx,%rdi
call   1030 <free@plt>
test   %r15,%r15
je     11e5 <load_string+0x75>
mov    %rsp,%rdi
mov    $0x1000,%esi
mov    %r15,%rdx
call   1060 <fgets@plt>
mov    %rax,%r14
mov    %r15,%rdi
call   1040 <fclose@plt>
test   %r14,%r14
je     11e5 <load_string+0x75>
mov    %r14,%rdi
call   1050 <strlen@plt>
cltq
cmpb   $0xa,-0x1(%rax,%r14,1)
jne    11db <load_string+0x6b>
movb   $0x0,-0x1(%r14,%rax,1)
mov    %r14,%rdi
call   1090 <strdup@plt>
jmp    11e7 <load_string+0x77>
xor    %eax,%eax
add    $0x1000,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
define i8* @load_string(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca [4096 x i8]
%8 = alloca i8*
%9 = alloca i32*
%10 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%11 = load i8*, i8** %4
%12 = load i8*, i8** %5
%13 = call i8* @cc_fullname(i8* %11, i8* %12)
store i8* %13, i8** %6
%14 = load i8*, i8** %6
%15 = call i32* @fopen(i8* %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %15, i32** %9
%16 = load i8*, i8** %6
%17 = call i32 @free(i8* %16)
%18 = load i32*, i32** %9
%19 = icmp ne i32* %18, null
br i1 %19, label %21, label %20
20: ; preds = %2
store i8* null, i8** %3
br label %50
21: ; preds = %2
%22 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%23 = load i32*, i32** %9
%24 = call i8* @fgets(i8* %22, i32 4096, i32* %23)
store i8* %24, i8** %8
%25 = load i32*, i32** %9
%26 = call i32 @fclose(i32* %25)
%27 = load i8*, i8** %8
%28 = icmp ne i8* %27, null
br i1 %28, label %30, label %29
29: ; preds = %21
store i8* null, i8** %3
br label %50
30: ; preds = %21
%31 = load i8*, i8** %8
%32 = call i32 @strlen(i8* %31)
store i32 %32, i32* %10
%33 = load i8*, i8** %8
%34 = load i32, i32* %10
%35 = sub nsw i32 %34, 1
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i8, i8* %33, i64 %36
%38 = load i8, i8* %37
%39 = sext i8 %38 to i32
%40 = icmp eq i32 %39, 10
br i1 %40, label %41, label %47
41: ; preds = %30
%42 = load i8*, i8** %8
%43 = load i32, i32* %10
%44 = sub nsw i32 %43, 1
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds i8, i8* %42, i64 %45
store i8 0, i8* %46
br label %47
47: ; preds = %41, %30
%48 = load i8*, i8** %8
%49 = call i8* @strdup(i8* %48)
store i8* %49, i8** %3
br label %50
50: ; preds = %47, %29, %20
%51 = load i8*, i8** %3
ret i8* %51
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
define noalias i8* @load_string(i8* %0, i8* %1) {
%3 = alloca [4096 x i8]
%4 = tail call i8* @cc_fullname(i8* %0, i8* %1)
%5 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%6 = tail call i32* @fopen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%7 = tail call i32 @free(i8* %4)
%8 = icmp eq i32* %6, null
br i1 %8, label %23, label %9
9: ; preds = %2
%10 = call i8* @fgets(i8* %5, i32 4096, i32* %6)
%11 = call i32 @fclose(i32* %6)
%12 = icmp eq i8* %10, null
br i1 %12, label %23, label %13
13: ; preds = %9
%14 = call i32 @strlen(i8* %10)
%15 = add nsw i32 %14, -1
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds i8, i8* %10, i64 %16
%18 = load i8, i8* %17
%19 = icmp eq i8 %18, 10
br i1 %19, label %20, label %21
20: ; preds = %13
store i8 0, i8* %17
br label %21
21: ; preds = %20, %13
%22 = call i8* @strdup(i8* %10)
br label %23
23: ; preds = %9, %2, %21
%24 = phi i8* [ %22, %21 ], [ null, %2 ], [ null, %9 ]
ret i8* %24
}