#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");
  if (fd == NULL) {
    free(path);
    return NULL;
  }
  free(path);
  if (fgets(line, sizeof(line) - 1, fd) == NULL) {
    fclose(fd);
    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)
cmpq   $0x0,-0x238(%rbp)
jne    11db <readfile+0x7b>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1244 <readfile+0xe4>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
lea    -0x230(%rbp),%rdi
mov    -0x238(%rbp),%rdx
mov    $0x200,%esi
call   1070 <fgets@plt>
cmp    $0x0,%rax
jne    121f <readfile+0xbf>
mov    -0x238(%rbp),%rdi
call   1050 <fclose@plt>
movq   $0x0,-0x8(%rbp)
jmp    1244 <readfile+0xe4>
mov    -0x238(%rbp),%rdi
call   1050 <fclose@plt>
lea    -0x230(%rbp),%rsi
lea    0xdca(%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     11f3 <readfile+0x93>
mov    %rsp,%rdi
mov    $0x200,%esi
mov    %r15,%rdx
call   1070 <fgets@plt>
mov    %rax,%rbx
mov    %r15,%rdi
call   1050 <fclose@plt>
test   %rbx,%rbx
je     11f0 <readfile+0x90>
lea    0xe22(%rip),%rdi
mov    %rsp,%rsi
xor    %eax,%eax
call   1040 <smprintf@plt>
mov    %rax,%r14
jmp    11f3 <readfile+0x93>
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 i32*, i32** %8
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %21
18: ; preds = %2
%19 = load i8*, i8** %6
%20 = call i32 @free(i8* %19)
store i8* null, i8** %3
br label %36
21: ; preds = %2
%22 = load i8*, i8** %6
%23 = call i32 @free(i8* %22)
%24 = getelementptr inbounds [513 x i8], [513 x i8]* %7, i64 0, i64 0
%25 = load i32*, i32** %8
%26 = call i32* @fgets(i8* %24, i32 512, i32* %25)
%27 = icmp eq i32* %26, null
br i1 %27, label %28, label %31
28: ; preds = %21
%29 = load i32*, i32** %8
%30 = call i32 @fclose(i32* %29)
store i8* null, i8** %3
br label %36
31: ; preds = %21
%32 = load i32*, i32** %8
%33 = call i32 @fclose(i32* %32)
%34 = getelementptr inbounds [513 x i8], [513 x i8]* %7, i64 0, i64 0
%35 = call i8* (i8*, i8*, ...) @smprintf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %34)
store i8* %35, i8** %3
br label %36
36: ; preds = %31, %28, %18
%37 = load i8*, i8** %3
ret i8* %37
}

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 = icmp eq i32* %7, null
%9 = call i32 @free(i8* %6)
br i1 %8, label %16, label %10
10: ; preds = %2
%11 = call i32* @fgets(i8* %4, i32 512, i32* %7)
%12 = icmp eq i32* %11, null
%13 = call i32 @fclose(i32* %7)
br i1 %12, label %16, label %14
14: ; preds = %10
%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, %14
%17 = phi i8* [ %15, %14 ], [ null, %2 ], [ null, %10 ]
ret i8* %17
}