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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ read(int, char *, size_t);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
char *strstr(char *, char *);
int read_sys_node(char *where, char *save, size_t savesz) {
  char *new;
  int fd, ret = 0;
  fd = open(where, O_RDONLY);
  if (fd < 0) {
    fprintf(stderr, "Failed to open %s with errno %s\n", where,
            strerror(errno));
    return 1;
  }
  if (!read(fd, save, savesz - 1))
    ret = 1;
  new = strstr(save, "\n");
  if (new)
    new[0] = '\0';
  close(fd);
  return ret;
}


x86 assembly (clang, -O0):
<read_sys_node>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x0,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e62(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    11de <read_sys_node+0x7e>
mov    0x2e37(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2e1b(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x3c(%rbp),%edi
mov    -0x38(%rbp),%rdx
mov    %rax,%rcx
lea    0xe33(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1236 <read_sys_node+0xd6>
mov    -0x2c(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
sub    $0x1,%rdx
call   1040 <read@plt>
cmp    $0x0,%eax
jne    1202 <read_sys_node+0xa2>
movl   $0x1,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe12(%rip),%rsi
call   1080 <strstr@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1228 <read_sys_node+0xc8>
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x2c(%rbp),%edi
call   1030 <close@plt>
mov    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_sys_node>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%rbp
mov    %rsi,%r14
mov    %rdi,%r15
mov    0x2e71(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
test   %eax,%eax
js     11be <read_sys_node+0x5e>
mov    %eax,%ebx
add    $0xffffffffffffffff,%rbp
mov    %eax,%edi
mov    %r14,%rsi
mov    %rbp,%rdx
call   1050 <read@plt>
xor    %ebp,%ebp
test   %eax,%eax
sete   %r15b
mov    %r14,%rdi
mov    $0xa,%esi
call   1030 <strchr@plt>
test   %rax,%rax
je     11b2 <read_sys_node+0x52>
movb   $0x0,(%rax)
mov    %r15b,%bpl
mov    %ebx,%edi
call   1040 <close@plt>
jmp    11ee <read_sys_node+0x8e>
mov    0x2e13(%rip),%rax
mov    (%rax),%ebx
mov    0x2e02(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe24(%rip),%rsi
mov    %ebx,%edi
mov    %r15,%rdx
mov    %rax,%rcx
call   1060 <fprintf@plt>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str = [33 x i8] c"Failed to open %s with errno %s\0A\00"
@errno = global i32 0
@.str.1 = [2 x i8] c"\0A\00"
define i32 @read_sys_node(i8* %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i32 0, i32* %10
%11 = load i8*, i8** %5
%12 = load i32, i32* @O_RDONLY
%13 = call i32 @open(i8* %11, i32 %12)
store i32 %13, i32* %9
%14 = load i32, i32* %9
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %22
16: ; preds = %3
%17 = load i32, i32* @stderr
%18 = load i8*, i8** %5
%19 = load i32, i32* @errno
%20 = call i8* @strerror(i32 %19)
%21 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i8* %18, i8* %20)
store i32 1, i32* %4
br label %42
22: ; preds = %3
%23 = load i32, i32* %9
%24 = load i8*, i8** %6
%25 = load i64, i64* %7
%26 = sub i64 %25, 1
%27 = call i32 @read(i32 %23, i8* %24, i64 %26)
%28 = icmp ne i32 %27, 0
br i1 %28, label %30, label %29
29: ; preds = %22
store i32 1, i32* %10
br label %30
30: ; preds = %29, %22
%31 = load i8*, i8** %6
%32 = call i8* @strstr(i8* %31, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i8* %32, i8** %8
%33 = load i8*, i8** %8
%34 = icmp ne i8* %33, null
br i1 %34, label %35, label %38
35: ; preds = %30
%36 = load i8*, i8** %8
%37 = getelementptr inbounds i8, i8* %36, i64 0
store i8 0, i8* %37
br label %38
38: ; preds = %35, %30
%39 = load i32, i32* %9
%40 = call i32 @close(i32 %39)
%41 = load i32, i32* %10
store i32 %41, i32* %4
br label %42
42: ; preds = %38, %16
%43 = load i32, i32* %4
ret i32 %43
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str = [33 x i8] c"Failed to open %s with errno %s\0A\00"
@errno = global i32 0
define i32 @read_sys_node(i8* %0, i8* %1, i64 %2) {
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 @open(i8* %0, i32 %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %12
7: ; preds = %3
%8 = load i32, i32* @stderr
%9 = load i32, i32* @errno
%10 = tail call i8* @strerror(i32 %9)
%11 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %10)
br label %22
12: ; preds = %3
%13 = add i64 %2, -1
%14 = tail call i32 @read(i32 %5, i8* %1, i64 %13)
%15 = icmp eq i32 %14, 0
%16 = zext i1 %15 to i32
%17 = tail call i8* @strchr(i8* dereferenceable(1) %1, i32 10)
%18 = icmp eq i8* %17, null
br i1 %18, label %20, label %19
19: ; preds = %12
store i8 0, i8* %17
br label %20
20: ; preds = %19, %12
%21 = tail call i32 @close(i32 %5)
br label %22
22: ; preds = %20, %7
%23 = phi i32 [ 1, %7 ], [ %16, %20 ]
ret i32 %23
}