#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 *, ...);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ *psh_open_writable_history();
int /*<<< orphan*/ *stderr;
int psh_append_history(char **cmd) {
  int i = 0;
  FILE *history = psh_open_writable_history();
  if (!history) {
    fprintf(stderr, "Couldn't open history file :(\n");
    return 1;
  }
  while (cmd[i]) {
    fprintf(history, "%s", cmd[i]);
    if (cmd[++i])
      fprintf(history, " ");
  }
  fprintf(history, "\n");
  if (fclose(history)) {
    perror("psh");
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<psh_append_history>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    $0x0,%al
call   1060 <psh_open_writable_history@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    118d <psh_append_history+0x4d>
mov    0x2e70(%rip),%rax
mov    (%rax),%rdi
lea    0xe86(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1239 <psh_append_history+0xf9>
jmp    1192 <psh_append_history+0x52>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
cmpq   $0x0,(%rax,%rcx,8)
je     11f5 <psh_append_history+0xb5>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,8),%rdx
lea    0xe63(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
add    $0x1,%ecx
mov    %ecx,-0x14(%rbp)
movslq %ecx,%rcx
cmpq   $0x0,(%rax,%rcx,8)
je     11f0 <psh_append_history+0xb0>
mov    -0x20(%rbp),%rdi
lea    0xe39(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
jmp    1192 <psh_append_history+0x52>
mov    -0x20(%rbp),%rdi
lea    0xe1d(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
cmp    $0x0,%rax
je     1232 <psh_append_history+0xf2>
lea    0xe03(%rip),%rdi
call   1050 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1239 <psh_append_history+0xf9>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<psh_append_history>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
xor    %eax,%eax
call   1070 <psh_open_writable_history@plt>
test   %rax,%rax
je     11e4 <psh_append_history+0x84>
mov    %rax,%r14
mov    (%rbx),%rdi
test   %rdi,%rdi
je     11b7 <psh_append_history+0x57>
add    $0x8,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r14,%rsi
call   1040 <fputs@plt>
cmpq   $0x0,(%rbx)
je     11b7 <psh_append_history+0x57>
mov    $0x20,%edi
mov    %r14,%rsi
call   1050 <fputc@plt>
mov    (%rbx),%rdi
add    $0x8,%rbx
test   %rdi,%rdi
jne    1190 <psh_append_history+0x30>
mov    $0xa,%edi
mov    %r14,%rsi
call   1050 <fputc@plt>
mov    %r14,%rdi
call   1030 <fclose@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
je     1209 <psh_append_history+0xa9>
lea    0xe42(%rip),%rdi
call   1060 <perror@plt>
jmp    1204 <psh_append_history+0xa4>
mov    0x2df5(%rip),%rax
mov    (%rax),%rcx
lea    0xe0b(%rip),%rdi
mov    $0x1e,%esi
mov    $0x1,%edx
call   1080 <fwrite@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@stderr = global i32* null
@.str = [31 x i8] c"Couldn't open history file :(\0A\00"
@.str.1 = [3 x i8] c"%s\00"
@.str.2 = [2 x i8] c" \00"
@.str.3 = [2 x i8] c"\0A\00"
@.str.4 = [4 x i8] c"psh\00"
define i32 @psh_append_history(i8** %0) {
%2 = alloca i32
%3 = alloca i8**
%4 = alloca i32
%5 = alloca i32*
store i8** %0, i8*** %3
store i32 0, i32* %4
%6 = call i32* (...) @psh_open_writable_history()
store i32* %6, i32** %5
%7 = load i32*, i32** %5
%8 = icmp ne i32* %7, null
br i1 %8, label %12, label %9
9: ; preds = %1
%10 = load i32*, i32** @stderr
%11 = call i32 (i32*, i8*, ...) @fprintf(i32* %10, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %2
br label %48
12: ; preds = %1
br label %13
13: ; preds = %38, %12
%14 = load i8**, i8*** %3
%15 = load i32, i32* %4
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds i8*, i8** %14, i64 %16
%18 = load i8*, i8** %17
%19 = icmp ne i8* %18, null
br i1 %19, label %20, label %39
20: ; preds = %13
%21 = load i32*, i32** %5
%22 = load i8**, i8*** %3
%23 = load i32, i32* %4
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8*, i8** %22, i64 %24
%26 = load i8*, i8** %25
%27 = call i32 (i32*, i8*, ...) @fprintf(i32* %21, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %26)
%28 = load i8**, i8*** %3
%29 = load i32, i32* %4
%30 = add nsw i32 %29, 1
store i32 %30, i32* %4
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8*, i8** %28, i64 %31
%33 = load i8*, i8** %32
%34 = icmp ne i8* %33, null
br i1 %34, label %35, label %38
35: ; preds = %20
%36 = load i32*, i32** %5
%37 = call i32 (i32*, i8*, ...) @fprintf(i32* %36, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %38
38: ; preds = %35, %20
br label %13
39: ; preds = %13
%40 = load i32*, i32** %5
%41 = call i32 (i32*, i8*, ...) @fprintf(i32* %40, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%42 = load i32*, i32** %5
%43 = call i64 @fclose(i32* %42)
%44 = icmp ne i64 %43, 0
br i1 %44, label %45, label %47
45: ; preds = %39
%46 = call i32 @perror(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %2
br label %48
47: ; preds = %39
store i32 0, i32* %2
br label %48
48: ; preds = %47, %45, %9
%49 = load i32, i32* %2
ret i32 %49
}

LLVM IR (-O2):
@stderr = global i32* null
@.str = [31 x i8] c"Couldn't open history file :(\0A\00"
@.str.4 = [4 x i8] c"psh\00"
define i32 @psh_append_history(i8** nocapture %0) {
%2 = tail call i32* (...) @psh_open_writable_history()
%3 = icmp eq i32* %2, null
br i1 %3, label %7, label %4
4: ; preds = %1
%5 = load i8*, i8** %0
%6 = icmp eq i8* %5, null
br i1 %6, label %22, label %10
7: ; preds = %1
%8 = load i32*, i32** @stderr
%9 = tail call i64 @fwrite(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i64 30, i64 1, i32* %8)
br label %28
10: ; preds = %4, %18
%11 = phi i64 [ %14, %18 ], [ 0, %4 ]
%12 = phi i8* [ %20, %18 ], [ %5, %4 ]
%13 = tail call i32 @fputs(i8* %12, i32* %2)
%14 = add nuw i64 %11, 1
%15 = getelementptr inbounds i8*, i8** %0, i64 %14
%16 = load i8*, i8** %15
%17 = icmp eq i8* %16, null
br i1 %17, label %22, label %18
18: ; preds = %10
%19 = tail call i32 @fputc(i32 32, i32* %2)
%20 = load i8*, i8** %15
%21 = icmp eq i8* %20, null
br i1 %21, label %22, label %10
22: ; preds = %10, %18, %4
%23 = tail call i32 @fputc(i32 10, i32* %2)
%24 = tail call i64 @fclose(i32* %2)
%25 = icmp eq i64 %24, 0
br i1 %25, label %28, label %26
26: ; preds = %22
%27 = tail call i32 @perror(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0))
br label %28
28: ; preds = %22, %26, %7
%29 = phi i32 [ 1, %26 ], [ 1, %7 ], [ 0, %22 ]
ret i32 %29
}