#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ history_add_line(char *);
scalar_t__ isspace(char);
int strlen(char *);
void history_restore(const char *filename) {
  char line[1000];
  FILE *f = fopen(filename, "rt");
  if (f == NULL)
    return;
  for (;;) {
    if (fgets(line, 1000, f) != NULL) {
      int l = strlen(line);
      while (l > 0 && isspace(line[--l]))
        line[l] = 0;
      if (l > 0)
        history_add_line(line);
    } else
      break;
  }
  fclose(f);
}


x86 assembly (clang, -O0):
<history_restore>:
push   %rbp
mov    %rsp,%rbp
sub    $0x400,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe86(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x3f8(%rbp)
cmpq   $0x0,-0x3f8(%rbp)
jne    1199 <history_restore+0x39>
jmp    126d <history_restore+0x10d>
jmp    119e <history_restore+0x3e>
lea    -0x3f0(%rbp),%rdi
mov    -0x3f8(%rbp),%rdx
mov    $0x3e8,%esi
call   1060 <fgets@plt>
cmp    $0x0,%rax
je     1257 <history_restore+0xf7>
lea    -0x3f0(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x3fc(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x3fc(%rbp)
mov    %al,-0x3fd(%rbp)
jle    1212 <history_restore+0xb2>
mov    -0x3fc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x3fc(%rbp)
cltq
movsbl -0x3f0(%rbp,%rax,1),%edi
ff
call   1050 <isspace@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,-0x3fd(%rbp)
mov    -0x3fd(%rbp),%al
test   $0x1,%al
jne    1225 <history_restore+0xc5>
jmp    1239 <history_restore+0xd9>
movslq -0x3fc(%rbp),%rax
movb   $0x0,-0x3f0(%rbp,%rax,1)
jmp    11d2 <history_restore+0x72>
cmpl   $0x0,-0x3fc(%rbp)
jle    1252 <history_restore+0xf2>
lea    -0x3f0(%rbp),%rdi
call   1070 <history_add_line@plt>
jmp    125c <history_restore+0xfc>
jmp    1261 <history_restore+0x101>
jmp    119e <history_restore+0x3e>
mov    -0x3f8(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x400,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<history_restore>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x3e8,%rsp
lea    0xe8b(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     1216 <history_restore+0xb6>
mov    %rax,%r14
mov    %rsp,%rdi
mov    $0x3e8,%esi
mov    %rax,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     120e <history_restore+0xae>
mov    %rsp,%r15
jmp    11b5 <history_restore+0x55>
mov    %r15,%rdi
mov    $0x3e8,%esi
mov    %r14,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     120e <history_restore+0xae>
mov    %r15,%rdi
call   1040 <strlen@plt>
test   %eax,%eax
jle    11a0 <history_restore+0x40>
mov    %eax,%ebx
add    $0xffffffffffffffff,%rbx
nopw   0x0(%rax,%rax,1)
mov    %ebx,%r12d
movsbl (%rsp,%r12,1),%edi
call   1050 <isspace@plt>
test   %rax,%rax
je     1200 <history_restore+0xa0>
movb   $0x0,(%rsp,%r12,1)
lea    -0x1(%rbx),%rax
add    $0x1,%rbx
cmp    $0x1,%rbx
mov    %rax,%rbx
ja     11d0 <history_restore+0x70>
jmp    11a0 <history_restore+0x40>
nopw   0x0(%rax,%rax,1)
test   %ebx,%ebx
je     11a0 <history_restore+0x40>
mov    %r15,%rdi
call   1070 <history_add_line@plt>
jmp    11a0 <history_restore+0x40>
mov    %r14,%rdi
call   1030 <fclose@plt>
add    $0x3e8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rt\00"
define void @history_restore(i8* %0) {
%2 = alloca i8*
%3 = alloca [1000 x i8]
%4 = alloca i32*
%5 = alloca i32
store i8* %0, i8** %2
%6 = load i8*, i8** %2
%7 = call i32* @fopen(i8* %6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %7, i32** %4
%8 = load i32*, i32** %4
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %11
10: ; preds = %1
br label %49
11: ; preds = %1
br label %12
12: ; preds = %45, %11
%13 = getelementptr inbounds [1000 x i8], [1000 x i8]* %3, i64 0, i64 0
%14 = load i32*, i32** %4
%15 = call i32* @fgets(i8* %13, i32 1000, i32* %14)
%16 = icmp ne i32* %15, null
br i1 %16, label %17, label %44
17: ; preds = %12
%18 = getelementptr inbounds [1000 x i8], [1000 x i8]* %3, i64 0, i64 0
%19 = call i32 @strlen(i8* %18)
store i32 %19, i32* %5
br label %20
20: ; preds = %33, %17
%21 = load i32, i32* %5
%22 = icmp sgt i32 %21, 0
br i1 %22, label %23, label %31
23: ; preds = %20
%24 = load i32, i32* %5
%25 = add nsw i32 %24, -1
store i32 %25, i32* %5
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds [1000 x i8], [1000 x i8]* %3, i64 0, i64 %26
%28 = load i8, i8* %27
%29 = call i64 @isspace(i8 signext %28)
%30 = icmp ne i64 %29, 0
br label %31
31: ; preds = %23, %20
%32 = phi i1 [ false, %20 ], [ %30, %23 ]
br i1 %32, label %33, label %37
33: ; preds = %31
%34 = load i32, i32* %5
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds [1000 x i8], [1000 x i8]* %3, i64 0, i64 %35
store i8 0, i8* %36
br label %20
37: ; preds = %31
%38 = load i32, i32* %5
%39 = icmp sgt i32 %38, 0
br i1 %39, label %40, label %43
40: ; preds = %37
%41 = getelementptr inbounds [1000 x i8], [1000 x i8]* %3, i64 0, i64 0
%42 = call i32 @history_add_line(i8* %41)
br label %43
43: ; preds = %40, %37
br label %45
44: ; preds = %12
br label %46
45: ; preds = %43
br label %12
46: ; preds = %44
%47 = load i32*, i32** %4
%48 = call i32 @fclose(i32* %47)
br label %49
49: ; preds = %46, %10
ret void
}

LLVM IR (-O2):
@.str = [3 x i8] c"rt\00"
define void @history_restore(i8* nocapture %0) {
%2 = alloca [1000 x i8]
%3 = getelementptr inbounds [1000 x i8], [1000 x i8]* %2, i64 0, i64 0
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %34, label %6
6: ; preds = %1
%7 = call i32* @fgets(i8* %3, i32 1000, i32* %4)
%8 = icmp eq i32* %7, null
br i1 %8, label %32, label %9
9: ; preds = %6, %29
%10 = call i32 @strlen(i8* %3)
%11 = icmp sgt i32 %10, 0
br i1 %11, label %12, label %29
12: ; preds = %9
%13 = zext i32 %10 to i64
br label %14
14: ; preds = %12, %22
%15 = phi i64 [ %13, %12 ], [ %16, %22 ]
%16 = add nsw i64 %15, -1
%17 = and i64 %16, 4294967295
%18 = getelementptr inbounds [1000 x i8], [1000 x i8]* %2, i64 0, i64 %17
%19 = load i8, i8* %18
%20 = call i64 @isspace(i8 signext %19)
%21 = icmp eq i64 %20, 0
br i1 %21, label %24, label %22
22: ; preds = %14
store i8 0, i8* %18
%23 = icmp ugt i64 %15, 1
br i1 %23, label %14, label %29
24: ; preds = %14
%25 = trunc i64 %16 to i32
%26 = icmp eq i32 %25, 0
br i1 %26, label %29, label %27
27: ; preds = %24
%28 = call i32 @history_add_line(i8* %3)
br label %29
29: ; preds = %22, %9, %27, %24
%30 = call i32* @fgets(i8* %3, i32 1000, i32* %4)
%31 = icmp eq i32* %30, null
br i1 %31, label %32, label %9
32: ; preds = %29, %6
%33 = call i32 @fclose(i32* %4)
br label %34
34: ; preds = %1, %32
ret void
}