#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 */
int /*<<< orphan*/ add_history(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
char *strchr(char *, char);
int read_history(const char *filename) {
  FILE *fp = fopen(filename, "r");
  char buf[4096];
  if (fp == NULL)
    return -1;
  while (fgets(buf, 4096, fp) != NULL) {
    char *p;
    p = strchr(buf, '\r');
    if (!p)
      p = strchr(buf, '\n');
    if (p)
      *p = '\0';
    add_history(buf);
  }
  fclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<read_history>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe96(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    118a <read_history+0x3a>
movl   $0xffffffff,-0x4(%rbp)
jmp    1225 <read_history+0xd5>
jmp    118f <read_history+0x3f>
lea    -0x1020(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x1000,%esi
call   1060 <fgets@plt>
cmp    $0x0,%rax
je     1215 <read_history+0xc5>
lea    -0x1020(%rbp),%rdi
mov    $0xd,%esi
call   1050 <strchr@plt>
mov    %rax,-0x1028(%rbp)
cmpq   $0x0,-0x1028(%rbp)
jne    11ec <read_history+0x9c>
lea    -0x1020(%rbp),%rdi
mov    $0xa,%esi
call   1050 <strchr@plt>
mov    %rax,-0x1028(%rbp)
cmpq   $0x0,-0x1028(%rbp)
je     1204 <read_history+0xb4>
mov    -0x1028(%rbp),%rax
movb   $0x0,(%rax)
lea    -0x1020(%rbp),%rdi
call   1030 <add_history@plt>
jmp    118f <read_history+0x3f>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_history>:
push   %r14
push   %rbx
sub    $0x1008,%rsp
lea    0xe9f(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11e2 <read_history+0x92>
mov    %rax,%r14
mov    %rsp,%rdi
mov    $0x1000,%esi
mov    %rax,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     11d6 <read_history+0x86>
mov    %rsp,%rbx
jmp    11b0 <read_history+0x60>
nopl   0x0(%rax,%rax,1)
movb   $0x0,(%rax)
mov    %rbx,%rdi
call   1030 <add_history@plt>
mov    %rbx,%rdi
mov    $0x1000,%esi
mov    %r14,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     11d6 <read_history+0x86>
mov    %rbx,%rdi
mov    $0xd,%esi
call   1050 <strchr@plt>
test   %rax,%rax
jne    1190 <read_history+0x40>
mov    %rbx,%rdi
mov    $0xa,%esi
call   1050 <strchr@plt>
test   %rax,%rax
jne    1190 <read_history+0x40>
jmp    1193 <read_history+0x43>
mov    %r14,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
jmp    11e7 <read_history+0x97>
mov    $0xffffffff,%eax
add    $0x1008,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
define i32 @read_history(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca [4096 x i8]
%6 = alloca i8*
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = call i32* @fopen(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %8, i32** %4
%9 = load i32*, i32** %4
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %12
11: ; preds = %1
store i32 -1, i32* %2
br label %37
12: ; preds = %1
br label %13
13: ; preds = %31, %12
%14 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%15 = load i32*, i32** %4
%16 = call i32* @fgets(i8* %14, i32 4096, i32* %15)
%17 = icmp ne i32* %16, null
br i1 %17, label %18, label %34
18: ; preds = %13
%19 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%20 = call i8* @strchr(i8* %19, i8 signext 13)
store i8* %20, i8** %6
%21 = load i8*, i8** %6
%22 = icmp ne i8* %21, null
br i1 %22, label %26, label %23
23: ; preds = %18
%24 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%25 = call i8* @strchr(i8* %24, i8 signext 10)
store i8* %25, i8** %6
br label %26
26: ; preds = %23, %18
%27 = load i8*, i8** %6
%28 = icmp ne i8* %27, null
br i1 %28, label %29, label %31
29: ; preds = %26
%30 = load i8*, i8** %6
store i8 0, i8* %30
br label %31
31: ; preds = %29, %26
%32 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%33 = call i32 @add_history(i8* %32)
br label %13
34: ; preds = %13
%35 = load i32*, i32** %4
%36 = call i32 @fclose(i32* %35)
store i32 0, i32* %2
br label %37
37: ; preds = %34, %11
%38 = load i32, i32* %2
ret i32 %38
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
define i32 @read_history(i8* nocapture %0) {
%2 = alloca [4096 x i8]
%3 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%4 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
%5 = icmp eq i32* %3, null
br i1 %5, label %23, label %6
6: ; preds = %1
%7 = call i32* @fgets(i8* %4, i32 4096, i32* %3)
%8 = icmp eq i32* %7, null
br i1 %8, label %21, label %9
9: ; preds = %6, %17
%10 = call i8* @strchr(i8* %4, i8 signext 13)
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %15
12: ; preds = %9
%13 = call i8* @strchr(i8* %4, i8 signext 10)
%14 = icmp eq i8* %13, null
br i1 %14, label %17, label %15
15: ; preds = %9, %12
%16 = phi i8* [ %13, %12 ], [ %10, %9 ]
store i8 0, i8* %16
br label %17
17: ; preds = %15, %12
%18 = call i32 @add_history(i8* %4)
%19 = call i32* @fgets(i8* %4, i32 4096, i32* %3)
%20 = icmp eq i32* %19, null
br i1 %20, label %21, label %9
21: ; preds = %17, %6
%22 = call i32 @fclose(i32* %3)
br label %23
23: ; preds = %1, %21
%24 = phi i32 [ 0, %21 ], [ -1, %1 ]
ret i32 %24
}