#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 */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
char *smprintf(char *, char *, ...);
char *readfile(char *base, char *file) {
  char *path, line[513];
  FILE *fd;
  memset(line, 0, sizeof(line));
  path = smprintf("%s/%s", base, file);
  fd = fopen(path, "r");
  free(path);
  if (fd == NULL)
    return NULL;
  if (fgets(line, sizeof(line) - 1, fd) == NULL)
    return NULL;
  fclose(fd);
  return smprintf("%s", line);
}


x86 assembly (clang, -O0):
<readfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x240,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x230(%rbp),%rdi
xor    %esi,%esi
mov    $0x201,%edx
call   1060 <memset@plt>
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    0xe6b(%rip),%rdi
mov    $0x0,%al
call   1040 <smprintf@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xe5b(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x238(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
cmpq   $0x0,-0x238(%rbp)
jne    11db <readfile+0x7b>
movq   $0x0,-0x8(%rbp)
jmp    122f <readfile+0xcf>
lea    -0x230(%rbp),%rdi
mov    -0x238(%rbp),%rdx
mov    $0x200,%esi
call   1070 <fgets@plt>
cmp    $0x0,%rax
jne    120a <readfile+0xaa>
movq   $0x0,-0x8(%rbp)
jmp    122f <readfile+0xcf>
mov    -0x238(%rbp),%rdi
call   1050 <fclose@plt>
lea    -0x230(%rbp),%rsi
lea    0xddf(%rip),%rdi
mov    $0x0,%al
call   1040 <smprintf@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x240,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readfile>:
push   %r15
push   %r14
push   %rbx
sub    $0x210,%rsp
mov    %rsi,%r15
mov    %rdi,%rbx
mov    %rsp,%rdi
xor    %r14d,%r14d
xor    %esi,%esi
mov    $0x201,%edx
call   1060 <memset@plt>
lea    0xe75(%rip),%rdi
mov    %rbx,%rsi
mov    %r15,%rdx
xor    %eax,%eax
call   1040 <smprintf@plt>
mov    %rax,%rbx
lea    0xe64(%rip),%rsi
mov    %rax,%rdi
call   1080 <fopen@plt>
mov    %rax,%r15
mov    %rbx,%rdi
call   1030 <free@plt>
test   %r15,%r15
je     11f0 <readfile+0x90>
mov    %rsp,%rdi
mov    $0x200,%esi
mov    %r15,%rdx
call   1070 <fgets@plt>
test   %rax,%rax
je     11ed <readfile+0x8d>
mov    %r15,%rdi
call   1050 <fclose@plt>
lea    0xe25(%rip),%rdi
mov    %rsp,%rsi
xor    %eax,%eax
call   1040 <smprintf@plt>
mov    %rax,%r14
jmp    11f0 <readfile+0x90>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x210,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [6 x i8] c"%s/%s\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [3 x i8] c"%s\00"
define i8* @readfile(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca [513 x i8]
%8 = alloca i32*
store i8* %0, i8** %4
store i8* %1, i8** %5
%9 = getelementptr inbounds [513 x i8], [513 x i8]* %7, i64 0, i64 0
%10 = call i32 @memset(i8* %9, i32 0, i32 513)
%11 = load i8*, i8** %4
%12 = load i8*, i8** %5
%13 = call i8* (i8*, i8*, ...) @smprintf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), 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.1, i64 0, i64 0))
store i32* %15, i32** %8
%16 = load i8*, i8** %6
%17 = call i32 @free(i8* %16)
%18 = load i32*, i32** %8
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %21
20: ; preds = %2
store i8* null, i8** %3
br label %32
21: ; preds = %2
%22 = getelementptr inbounds [513 x i8], [513 x i8]* %7, i64 0, i64 0
%23 = load i32*, i32** %8
%24 = call i32* @fgets(i8* %22, i32 512, i32* %23)
%25 = icmp eq i32* %24, null
br i1 %25, label %26, label %27
26: ; preds = %21
store i8* null, i8** %3
br label %32
27: ; preds = %21
%28 = load i32*, i32** %8
%29 = call i32 @fclose(i32* %28)
%30 = getelementptr inbounds [513 x i8], [513 x i8]* %7, i64 0, i64 0
%31 = call i8* (i8*, i8*, ...) @smprintf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %30)
store i8* %31, i8** %3
br label %32
32: ; preds = %27, %26, %20
%33 = load i8*, i8** %3
ret i8* %33
}

LLVM IR (-O2):
@.str = [6 x i8] c"%s/%s\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [3 x i8] c"%s\00"
define i8* @readfile(i8* %0, i8* %1) {
%3 = alloca [513 x i8]
%4 = getelementptr inbounds [513 x i8], [513 x i8]* %3, i64 0, i64 0
%5 = call i32 @memset(i8* %4, i32 0, i32 513)
%6 = call i8* (i8*, i8*, ...) @smprintf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %1)
%7 = call i32* @fopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%8 = call i32 @free(i8* %6)
%9 = icmp eq i32* %7, null
br i1 %9, label %16, label %10
10: ; preds = %2
%11 = call i32* @fgets(i8* %4, i32 512, i32* %7)
%12 = icmp eq i32* %11, null
br i1 %12, label %16, label %13
13: ; preds = %10
%14 = call i32 @fclose(i32* %7)
%15 = call i8* (i8*, i8*, ...) @smprintf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %4)
br label %16
16: ; preds = %10, %2, %13
%17 = phi i8* [ %15, %13 ], [ null, %2 ], [ null, %10 ]
ret i8* %17
}