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

/* Variables and functions */
scalar_t__ fclose(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
size_t fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ memcpy(char *, char *, int);
int libname(char *name, char *buf) {
  static char read_buf[1024];
  FILE *fp;
  char *s;
  size_t r;
  int ret = 1;
  int clean;
  fp = fopen(name, "rb");
  if (fp == NULL)
    return 0;
  r = fread(read_buf, 1, 1024, fp);
  if (r < 1024) {
    if (ferror(fp)) {
      ret = 0;
      goto END;
    }
  }
  s = read_buf;
  clean = 0;
  while (r) {
    switch (*s) {
    case ' ':
    case '\t':
    case '\n':
    case '\r':
      s[0] = 0;
      clean = 1;
      break;
    default:
      break;
    }
    if (clean)
      break;
    --r;
    ++s;
  }
  memcpy(buf, read_buf, s - read_buf);
  buf[s - read_buf] = 0;
END:
  if (fclose(fp) != 0)
    return 0;
  return ret;
}


x86 assembly (clang, -O0):
<libname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x1,-0x34(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe8e(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1192 <libname+0x42>
movl   $0x0,-0x4(%rbp)
jmp    12db <libname+0x18b>
mov    -0x20(%rbp),%rcx
lea    0x2ec3(%rip),%rdi
mov    $0x1,%esi
mov    $0x400,%edx
call   1040 <fread@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x400,-0x30(%rbp)
jae    11e2 <libname+0x92>
mov    -0x20(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     11dd <libname+0x8d>
movl   $0x0,-0x34(%rbp)
jmp    12b6 <libname+0x166>
jmp    11e2 <libname+0x92>
lea    0x2e77(%rip),%rax
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x38(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     1280 <libname+0x130>
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
mov    %eax,-0x3c(%rbp)
add    $0xfffffff7,%eax
sub    $0x2,%eax
jb     123c <libname+0xec>
jmp    121a <libname+0xca>
mov    -0x3c(%rbp),%eax
sub    $0xd,%eax
je     123c <libname+0xec>
jmp    122b <libname+0xdb>
mov    -0x3c(%rbp),%eax
sub    $0x20,%eax
jne    124f <libname+0xff>
jmp    123c <libname+0xec>
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
movl   $0x1,-0x38(%rbp)
jmp    1254 <libname+0x104>
jmp    1254 <libname+0x104>
cmpl   $0x0,-0x38(%rbp)
je     1263 <libname+0x113>
jmp    1280 <libname+0x130>
mov    -0x30(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    11f4 <libname+0xa4>
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rax
lea    0x2dd1(%rip),%rcx
sub    %rcx,%rax
mov    %eax,%edx
lea    0x2dc5(%rip),%rsi
call   1060 <memcpy@plt>
mov    -0x18(%rbp),%rax
mov    -0x28(%rbp),%rcx
lea    0x2db1(%rip),%rdx
sub    %rdx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
cmp    $0x0,%rax
je     12d5 <libname+0x185>
movl   $0x0,-0x4(%rbp)
jmp    12db <libname+0x18b>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<libname>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r14
lea    0xe9e(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     120e <libname+0xbe>
mov    %rax,%r15
lea    0x2ee6(%rip),%rbx
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x400,%edx
mov    %rax,%rcx
call   1040 <fread@plt>
mov    %rax,%r12
cmp    $0x3ff,%rax
ja     11ae <libname+0x5e>
mov    %r15,%rdi
call   1030 <ferror@plt>
xor    %ebp,%ebp
test   %rax,%rax
jne    11f9 <libname+0xa9>
test   %r12,%r12
je     121e <libname+0xce>
mov    %r12,%rax
add    %rbx,%r12
movabs $0x100002600,%rcx
jmp    11ca <libname+0x7a>
add    $0x1,%rbx
add    $0xffffffffffffffff,%rax
je     1219 <libname+0xc9>
movsbl (%rbx),%edx
cmp    $0x20,%edx
ja     11c0 <libname+0x70>
bt     %rdx,%rcx
jae    11c0 <libname+0x70>
movb   $0x0,(%rbx)
lea    0x2e7e(%rip),%rsi
sub    %rsi,%rbx
mov    %r14,%rdi
mov    %ebx,%edx
call   1060 <memcpy@plt>
movb   $0x0,(%r14,%rbx,1)
mov    $0x1,%ebp
mov    %r15,%rdi
call   1050 <fclose@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
cmove  %ebp,%eax
jmp    1210 <libname+0xc0>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r12,%rbx
jmp    11db <libname+0x8b>
lea    0x2e3b(%rip),%rbx
jmp    11db <libname+0x8b>

LLVM IR (-O0):
@libname.read_buf = internal global [1024 x i8] zeroinitializer
@.str = [3 x i8] c"rb\00"
define i32 @libname(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 1, i32* %9
%11 = load i8*, i8** %4
%12 = call i32* @fopen(i8* %11, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %12, i32** %6
%13 = load i32*, i32** %6
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %16
15: ; preds = %2
store i32 0, i32* %3
br label %67
16: ; preds = %2
%17 = load i32*, i32** %6
%18 = call i64 @fread(i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @libname.read_buf, i64 0, i64 0), i32 1, i32 1024, i32* %17)
store i64 %18, i64* %8
%19 = load i64, i64* %8
%20 = icmp ult i64 %19, 1024
br i1 %20, label %21, label %27
21: ; preds = %16
%22 = load i32*, i32** %6
%23 = call i64 @ferror(i32* %22)
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %21
store i32 0, i32* %9
br label %60
26: ; preds = %21
br label %27
27: ; preds = %26, %16
store i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @libname.read_buf, i64 0, i64 0), i8** %7
store i32 0, i32* %10
br label %28
28: ; preds = %43, %27
%29 = load i64, i64* %8
%30 = icmp ne i64 %29, 0
br i1 %30, label %31, label %48
31: ; preds = %28
%32 = load i8*, i8** %7
%33 = load i8, i8* %32
%34 = sext i8 %33 to i32
switch i32 %34, label %38 [
i32 32, label %35
i32 9, label %35
i32 10, label %35
i32 13, label %35
]
35: ; preds = %31, %31, %31, %31
%36 = load i8*, i8** %7
%37 = getelementptr inbounds i8, i8* %36, i64 0
store i8 0, i8* %37
store i32 1, i32* %10
br label %39
38: ; preds = %31
br label %39
39: ; preds = %38, %35
%40 = load i32, i32* %10
%41 = icmp ne i32 %40, 0
br i1 %41, label %42, label %43
42: ; preds = %39
br label %48
43: ; preds = %39
%44 = load i64, i64* %8
%45 = add i64 %44, -1
store i64 %45, i64* %8
%46 = load i8*, i8** %7
%47 = getelementptr inbounds i8, i8* %46, i32 1
store i8* %47, i8** %7
br label %28
48: ; preds = %42, %28
%49 = load i8*, i8** %5
%50 = load i8*, i8** %7
%51 = ptrtoint i8* %50 to i64
%52 = sub i64 %51, ptrtoint ([1024 x i8]* @libname.read_buf to i64)
%53 = trunc i64 %52 to i32
%54 = call i32 @memcpy(i8* %49, i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @libname.read_buf, i64 0, i64 0), i32 %53)
%55 = load i8*, i8** %5
%56 = load i8*, i8** %7
%57 = ptrtoint i8* %56 to i64
%58 = sub i64 %57, ptrtoint ([1024 x i8]* @libname.read_buf to i64)
%59 = getelementptr inbounds i8, i8* %55, i64 %58
store i8 0, i8* %59
br label %60
60: ; preds = %48, %25
%61 = load i32*, i32** %6
%62 = call i64 @fclose(i32* %61)
%63 = icmp ne i64 %62, 0
br i1 %63, label %64, label %65
64: ; preds = %60
store i32 0, i32* %3
br label %67
65: ; preds = %60
%66 = load i32, i32* %9
store i32 %66, i32* %3
br label %67
67: ; preds = %65, %64, %15
%68 = load i32, i32* %3
ret i32 %68
}

LLVM IR (-O2):
@libname.read_buf = internal global [1024 x i8] zeroinitializer
@.str = [3 x i8] c"rb\00"
define i32 @libname(i8* nocapture %0, i8* %1) {
%3 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %37, label %5
5: ; preds = %2
%6 = tail call i64 @fread(i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @libname.read_buf, i64 0, i64 0), i32 1, i32 1024, i32* %3)
%7 = icmp ult i64 %6, 1024
br i1 %7, label %8, label %13
8: ; preds = %5
%9 = tail call i64 @ferror(i32* %3)
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %32
11: ; preds = %8
%12 = icmp eq i64 %6, 0
br i1 %12, label %25, label %13
13: ; preds = %5, %11
%14 = getelementptr [1024 x i8], [1024 x i8]* @libname.read_buf, i64 0, i64 %6
br label %15
15: ; preds = %13, %21
%16 = phi i64 [ %22, %21 ], [ %6, %13 ]
%17 = phi i8* [ %23, %21 ], [ getelementptr inbounds ([1024 x i8], [1024 x i8]* @libname.read_buf, i64 0, i64 0), %13 ]
%18 = load i8, i8* %17
%19 = sext i8 %18 to i32
switch i32 %19, label %21 [
i32 32, label %20
i32 9, label %20
i32 10, label %20
i32 13, label %20
]
20: ; preds = %15, %15, %15, %15
store i8 0, i8* %17
br label %25
21: ; preds = %15
%22 = add i64 %16, -1
%23 = getelementptr inbounds i8, i8* %17, i64 1
%24 = icmp eq i64 %22, 0
br i1 %24, label %25, label %15
25: ; preds = %21, %11, %20
%26 = phi i8* [ %17, %20 ], [ getelementptr inbounds ([1024 x i8], [1024 x i8]* @libname.read_buf, i64 0, i64 0), %11 ], [ %14, %21 ]
%27 = ptrtoint i8* %26 to i64
%28 = sub i64 %27, ptrtoint ([1024 x i8]* @libname.read_buf to i64)
%29 = trunc i64 %28 to i32
%30 = tail call i32 @memcpy(i8* %1, i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @libname.read_buf, i64 0, i64 0), i32 %29)
%31 = getelementptr inbounds i8, i8* %1, i64 %28
store i8 0, i8* %31
br label %32
32: ; preds = %8, %25
%33 = phi i32 [ 1, %25 ], [ 0, %8 ]
%34 = tail call i64 @fclose(i32* %3)
%35 = icmp eq i64 %34, 0
%36 = select i1 %35, i32 %33, i32 0
br label %37
37: ; preds = %32, %2
%38 = phi i32 [ 0, %2 ], [ %36, %32 ]
ret i32 %38
}