#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*/ buf;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int read(int, char *, int);
int /*<<< orphan*/ sprintf(char *, char *, char const *, char const *);
char *strdup(char *);
char *getstring(const char *dir, const char *file) {
  char fn[512];
  char buf[256];
  int fd, n;
  sprintf(fn, "%s/%s", dir, file);
  fd = open(fn, O_RDONLY);
  if (fd == -1)
    return NULL;
  n = read(fd, buf, sizeof(buf) - 1);
  if (n >= 0) {
    buf[n] = 0;
    if (n > 0) {
      if (buf[n - 1] == '\n')
        buf[n - 1] = 0;
    }
  } else
    buf[0] = 0;
  buf[sizeof(buf) - 1] = 0;
  close(fd);
  return strdup(buf);
}


x86 assembly (clang, -O0):
<getstring>:
push   %rbp
mov    %rsp,%rbp
sub    $0x330,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x220(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
lea    0xe87(%rip),%rsi
call   1060 <sprintf@plt>
lea    -0x220(%rbp),%rdi
mov    0x2e5c(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x324(%rbp)
cmpl   $0xffffffff,-0x324(%rbp)
jne    11b3 <getstring+0x63>
movq   $0x0,-0x8(%rbp)
jmp    125b <getstring+0x10b>
mov    -0x324(%rbp),%edi
lea    -0x320(%rbp),%rsi
mov    $0xff,%edx
call   1040 <read@plt>
mov    %eax,-0x328(%rbp)
cmpl   $0x0,-0x328(%rbp)
jl     1232 <getstring+0xe2>
movslq -0x328(%rbp),%rax
movb   $0x0,-0x320(%rbp,%rax,1)
cmpl   $0x0,-0x328(%rbp)
jle    122d <getstring+0xdd>
mov    -0x328(%rbp),%eax
sub    $0x1,%eax
cltq
movsbl -0x320(%rbp,%rax,1),%eax
ff
cmp    $0xa,%eax
jne    1228 <getstring+0xd8>
mov    -0x328(%rbp),%eax
sub    $0x1,%eax
cltq
movb   $0x0,-0x320(%rbp,%rax,1)
jmp    122d <getstring+0xdd>
jmp    1239 <getstring+0xe9>
movb   $0x0,-0x320(%rbp)
movb   $0x0,-0x221(%rbp)
mov    -0x324(%rbp),%edi
call   1030 <close@plt>
lea    -0x320(%rbp),%rdi
call   1070 <strdup@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x330,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getstring>:
push   %rbx
sub    $0x300,%rsp
mov    %rsi,%rcx
mov    %rdi,%rdx
lea    0xe9b(%rip),%rsi
lea    0x100(%rsp),%rbx
mov    %rbx,%rdi
call   1060 <sprintf@plt>
mov    0x2e6c(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     11bc <getstring+0x6c>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0xff,%edx
call   1040 <read@plt>
test   %eax,%eax
js     11c0 <getstring+0x70>
mov    %eax,%ecx
movb   $0x0,(%rsp,%rcx,1)
je     11c4 <getstring+0x74>
add    $0xffffffff,%eax
movzbl (%rsp,%rax,1),%ecx
xor    %edx,%edx
cmp    $0xa,%cl
cmovne %ecx,%edx
mov    %dl,(%rsp,%rax,1)
jmp    11c4 <getstring+0x74>
xor    %eax,%eax
jmp    11db <getstring+0x8b>
movb   $0x0,(%rsp)
movb   $0x0,0xff(%rsp)
mov    %ebx,%edi
call   1030 <close@plt>
mov    %rsp,%rdi
call   1070 <strdup@plt>
add    $0x300,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [6 x i8] c"%s/%s\00"
@O_RDONLY = global i32 0
define i8* @getstring(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca [512 x i8]
%7 = alloca [256 x i8]
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%10 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%11 = load i8*, i8** %4
%12 = load i8*, i8** %5
%13 = call i32 @sprintf(i8* %10, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %11, i8* %12)
%14 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%15 = load i32, i32* @O_RDONLY
%16 = call i32 @open(i8* %14, i32 %15)
store i32 %16, i32* %8
%17 = load i32, i32* %8
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %20
19: ; preds = %2
store i8* null, i8** %3
br label %55
20: ; preds = %2
%21 = load i32, i32* %8
%22 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%23 = call i32 @read(i32 %21, i8* %22, i32 255)
store i32 %23, i32* %9
%24 = load i32, i32* %9
%25 = icmp sge i32 %24, 0
br i1 %25, label %26, label %47
26: ; preds = %20
%27 = load i32, i32* %9
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 %28
store i8 0, i8* %29
%30 = load i32, i32* %9
%31 = icmp sgt i32 %30, 0
br i1 %31, label %32, label %46
32: ; preds = %26
%33 = load i32, i32* %9
%34 = sub nsw i32 %33, 1
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 %35
%37 = load i8, i8* %36
%38 = sext i8 %37 to i32
%39 = icmp eq i32 %38, 10
br i1 %39, label %40, label %45
40: ; preds = %32
%41 = load i32, i32* %9
%42 = sub nsw i32 %41, 1
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 %43
store i8 0, i8* %44
br label %45
45: ; preds = %40, %32
br label %46
46: ; preds = %45, %26
br label %49
47: ; preds = %20
%48 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
store i8 0, i8* %48
br label %49
49: ; preds = %47, %46
%50 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 255
store i8 0, i8* %50
%51 = load i32, i32* %8
%52 = call i32 @close(i32 %51)
%53 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%54 = call i8* @strdup(i8* %53)
store i8* %54, i8** %3
br label %55
55: ; preds = %49, %19
%56 = load i8*, i8** %3
ret i8* %56
}

LLVM IR (-O2):
@.str = [6 x i8] c"%s/%s\00"
@O_RDONLY = global i32 0
define noalias i8* @getstring(i8* %0, i8* %1) {
%3 = alloca [512 x i8]
%4 = alloca [256 x i8]
%5 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%6 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%7 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %1)
%8 = load i32, i32* @O_RDONLY
%9 = call i32 @open(i8* %5, i32 %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %30, label %11
11: ; preds = %2
%12 = call i32 @read(i32 %9, i8* %6, i32 255)
%13 = icmp sgt i32 %12, -1
br i1 %13, label %14, label %25
14: ; preds = %11
%15 = zext i32 %12 to i64
%16 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 %15
store i8 0, i8* %16
%17 = icmp eq i32 %12, 0
br i1 %17, label %26, label %18
18: ; preds = %14
%19 = add nsw i32 %12, -1
%20 = zext i32 %19 to i64
%21 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 %20
%22 = load i8, i8* %21
%23 = icmp eq i8 %22, 10
%24 = select i1 %23, i8 0, i8 %22
store i8 %24, i8* %21
br label %26
25: ; preds = %11
store i8 0, i8* %6
br label %26
26: ; preds = %18, %14, %25
%27 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 255
store i8 0, i8* %27
%28 = tail call i32 @close(i32 %9)
%29 = call i8* @strdup(i8* %6)
br label %30
30: ; preds = %2, %26
%31 = phi i8* [ %29, %26 ], [ null, %2 ]
ret i8* %31
}