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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int errno;
int open(char *, int /*<<< orphan*/);
int read(int, char *, int);
int ret_code();
int /*<<< orphan*/ snprintf(char *, int, char *, char *, char *);
char *strrchr(char *, char);
int sys_read_string(char *dir_name, char *file_name, char *str, int max_len) {
  char path[256], *s;
  int fd, r;
  snprintf(path, sizeof(path), "%s/%s", dir_name, file_name);
  if ((fd = open(path, O_RDONLY)) < 0)
    return ret_code();
  if ((r = read(fd, str, max_len)) < 0) {
    int e = errno;
    close(fd);
    errno = e;
    return ret_code();
  }
  str[(r < max_len) ? r : max_len - 1] = 0;
  if ((s = strrchr(str, '\n')))
    *s = 0;
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<sys_read_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x160,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
lea    -0x130(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%r8
mov    $0x100,%esi
lea    0xe6b(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x130(%rbp),%rdi
mov    0x2e40(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x13c(%rbp)
cmp    $0x0,%eax
jge    11cd <sys_read_string+0x6d>
mov    $0x0,%al
call   1030 <ret_code@plt>
mov    %eax,-0x4(%rbp)
jmp    12ac <sys_read_string+0x14c>
mov    -0x13c(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1070 <read@plt>
mov    %eax,-0x140(%rbp)
cmp    $0x0,%eax
jge    1226 <sys_read_string+0xc6>
mov    0x2de3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x144(%rbp)
mov    -0x13c(%rbp),%edi
call   1060 <close@plt>
mov    -0x144(%rbp),%ecx
mov    0x2dc3(%rip),%rax
mov    %ecx,(%rax)
mov    $0x0,%al
call   1030 <ret_code@plt>
mov    %eax,-0x4(%rbp)
jmp    12ac <sys_read_string+0x14c>
mov    -0x20(%rbp),%rax
mov    %rax,-0x150(%rbp)
mov    -0x140(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    1251 <sys_read_string+0xf1>
mov    -0x140(%rbp),%eax
mov    %eax,-0x154(%rbp)
jmp    125d <sys_read_string+0xfd>
mov    -0x24(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x154(%rbp)
mov    -0x150(%rbp),%rax
mov    -0x154(%rbp),%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rdi
mov    $0xa,%esi
call   1050 <strrchr@plt>
mov    %rax,-0x138(%rbp)
cmp    $0x0,%rax
je     129a <sys_read_string+0x13a>
mov    -0x138(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x13c(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x160,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sys_read_string>:
push   %rbp
push   %r14
push   %rbx
sub    $0x100,%rsp
mov    %ecx,%ebx
mov    %rdx,%r14
mov    %rsi,%r8
mov    %rdi,%rcx
lea    0xe83(%rip),%rdx
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    $0x100,%esi
call   1040 <snprintf@plt>
mov    0x2e54(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   1080 <open@plt>
test   %eax,%eax
js     11f8 <sys_read_string+0x98>
mov    %eax,%ebp
mov    %eax,%edi
mov    %r14,%rsi
mov    %ebx,%edx
call   1070 <read@plt>
test   %eax,%eax
js     11e4 <sys_read_string+0x84>
lea    -0x1(%rbx),%ecx
cmp    %ebx,%eax
cmovl  %eax,%ecx
movslq %ecx,%rax
movb   $0x0,(%r14,%rax,1)
mov    %r14,%rdi
mov    $0xa,%esi
call   1050 <strrchr@plt>
test   %rax,%rax
je     11d9 <sys_read_string+0x79>
movb   $0x0,(%rax)
mov    %ebp,%edi
call   1060 <close@plt>
xor    %eax,%eax
jmp    11ff <sys_read_string+0x9f>
mov    0x2ded(%rip),%r14
mov    (%r14),%ebx
mov    %ebp,%edi
call   1060 <close@plt>
mov    %ebx,(%r14)
xor    %eax,%eax
call   1030 <ret_code@plt>
add    $0x100,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [6 x i8] c"%s/%s\00"
@O_RDONLY = global i32 0
@errno = global i32 0
define i32 @sys_read_string(i8* %0, i8* %1, i8* %2, i32 %3) {
%5 = alloca [256 x i8]
%6 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%7 = call i32 @snprintf(i8* %6, i32 256, i8* 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* %6, i32 %8)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %4
%12 = call i32 (...) @ret_code()
br label %31
13: ; preds = %4
%14 = call i32 @read(i32 %9, i8* %2, i32 %3)
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %20
16: ; preds = %13
%17 = load i32, i32* @errno
%18 = call i32 @close(i32 %9)
store i32 %17, i32* @errno
%19 = call i32 (...) @ret_code()
br label %31
20: ; preds = %13
%21 = icmp slt i32 %14, %3
%22 = add nsw i32 %3, -1
%23 = select i1 %21, i32 %14, i32 %22
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %2, i64 %24
store i8 0, i8* %25
%26 = call i8* @strrchr(i8* dereferenceable(1) %2, i8 signext 10)
%27 = icmp eq i8* %26, null
br i1 %27, label %29, label %28
28: ; preds = %20
store i8 0, i8* %26
br label %29
29: ; preds = %28, %20
%30 = call i32 @close(i32 %9)
br label %31
31: ; preds = %29, %16, %11
%32 = phi i32 [ %12, %11 ], [ %19, %16 ], [ 0, %29 ]
ret i32 %32
}