#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*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
size_t getline(char **, size_t *, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, char *);
int /*<<< orphan*/ *psh_open_readable_history();
int /*<<< orphan*/ stderr;
int psh_history(char **args) {
  FILE *hist;
  char *line = NULL;
  size_t line_size = 0;
  size_t read;
  int count = 1;
  hist = psh_open_readable_history();
  if (!hist) {
    fprintf(stderr, "Unable to open history file\n");
    return 1;
  }
  while ((read = getline(&line, &line_size, hist)) != -1) {
    printf("[%d] %s", count, line);
    count++;
  }
  if (fclose(hist)) {
    perror("history");
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<psh_history>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movl   $0x1,-0x34(%rbp)
mov    $0x0,%al
call   1050 <psh_open_readable_history@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11ba <psh_history+0x5a>
mov    0x2e40(%rip),%rax
mov    (%rax),%edi
lea    0xe57(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1231 <psh_history+0xd1>
jmp    11bf <psh_history+0x5f>
mov    -0x18(%rbp),%rdx
lea    -0x20(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1080 <getline@plt>
mov    %rax,-0x30(%rbp)
cmp    $0xffffffffffffffff,%rax
je     11ff <psh_history+0x9f>
mov    -0x34(%rbp),%esi
mov    -0x20(%rbp),%rdx
lea    0xe31(%rip),%rdi
call   1040 <printf@plt>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    11bf <psh_history+0x5f>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
cmp    $0x0,%rax
je     122a <psh_history+0xca>
lea    0xe0c(%rip),%rdi
call   1070 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1231 <psh_history+0xd1>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<psh_history>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
movq   $0x0,(%rsp)
movq   $0x0,0x8(%rsp)
xor    %eax,%eax
call   1050 <psh_open_readable_history@plt>
test   %rax,%rax
je     1205 <psh_history+0xa5>
mov    %rax,%rbx
mov    %rsp,%rdi
lea    0x8(%rsp),%rsi
mov    %rax,%rdx
call   1080 <getline@plt>
cmp    $0xffffffffffffffff,%rax
je     11e5 <psh_history+0x85>
mov    $0x1,%ebp
lea    0xe6f(%rip),%r14
mov    %rsp,%r15
lea    0x8(%rsp),%r12
cs nopw 0x0(%rax,%rax,1)
mov    (%rsp),%rdx
mov    %r14,%rdi
mov    %ebp,%esi
call   1040 <printf@plt>
add    $0x1,%ebp
mov    %r15,%rdi
mov    %r12,%rsi
mov    %rbx,%rdx
call   1080 <getline@plt>
cmp    $0xffffffffffffffff,%rax
jne    11c0 <psh_history+0x60>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
je     121f <psh_history+0xbf>
lea    0xe27(%rip),%rdi
call   1070 <perror@plt>
jmp    121a <psh_history+0xba>
mov    0x2dd4(%rip),%rax
mov    (%rax),%edi
lea    0xdeb(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [29 x i8] c"Unable to open history file\0A\00"
@.str.1 = [8 x i8] c"[%d] %s\00"
@.str.2 = [8 x i8] c"history\00"
define i32 @psh_history(i8** %0) {
%2 = alloca i32
%3 = alloca i8**
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i32
store i8** %0, i8*** %3
store i8* null, i8** %5
store i64 0, i64* %6
store i32 1, i32* %8
%9 = call i32* (...) @psh_open_readable_history()
store i32* %9, i32** %4
%10 = load i32*, i32** %4
%11 = icmp ne i32* %10, null
br i1 %11, label %15, label %12
12: ; preds = %1
%13 = load i32, i32* @stderr
%14 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %2
br label %33
15: ; preds = %1
br label %16
16: ; preds = %20, %15
%17 = load i32*, i32** %4
%18 = call i64 @getline(i8** %5, i64* %6, i32* %17)
store i64 %18, i64* %7
%19 = icmp ne i64 %18, -1
br i1 %19, label %20, label %26
20: ; preds = %16
%21 = load i32, i32* %8
%22 = load i8*, i8** %5
%23 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 %21, i8* %22)
%24 = load i32, i32* %8
%25 = add nsw i32 %24, 1
store i32 %25, i32* %8
br label %16
26: ; preds = %16
%27 = load i32*, i32** %4
%28 = call i64 @fclose(i32* %27)
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %26
%31 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %2
br label %33
32: ; preds = %26
store i32 0, i32* %2
br label %33
33: ; preds = %32, %30, %12
%34 = load i32, i32* %2
ret i32 %34
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [29 x i8] c"Unable to open history file\0A\00"
@.str.1 = [8 x i8] c"[%d] %s\00"
@.str.2 = [8 x i8] c"history\00"
define i32 @psh_history(i8** nocapture readnone %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = bitcast i8** %2 to i8*
store i8* null, i8** %2
%5 = bitcast i64* %3 to i8*
store i64 0, i64* %3
%6 = tail call i32* (...) @psh_open_readable_history()
%7 = icmp eq i32* %6, null
br i1 %7, label %11, label %8
8: ; preds = %1
%9 = call i64 @getline(i8** %2, i64* %3, i32* %6)
%10 = icmp eq i64 %9, -1
br i1 %10, label %21, label %14
11: ; preds = %1
%12 = load i32, i32* @stderr
%13 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
br label %26
14: ; preds = %8, %14
%15 = phi i32 [ %18, %14 ], [ 1, %8 ]
%16 = load i8*, i8** %2
%17 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 %15, i8* %16)
%18 = add nuw nsw i32 %15, 1
%19 = call i64 @getline(i8** %2, i64* %3, i32* %6)
%20 = icmp eq i64 %19, -1
br i1 %20, label %21, label %14
21: ; preds = %14, %8
%22 = call i64 @fclose(i32* %6)
%23 = icmp eq i64 %22, 0
br i1 %23, label %26, label %24
24: ; preds = %21
%25 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %26
26: ; preds = %21, %24, %11
%27 = phi i32 [ 1, %24 ], [ 1, %11 ], [ 0, %21 ]
ret i32 %27
}