#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*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int /*<<< orphan*/ perror(char *);
int do_append(char *filepath, int argc, char **argv) {
  FILE *fp = fopen(filepath, "a");
  if (fp == NULL) {
    perror("1800 Cannot open file for append.");
    return 1;
  }
  int i = 0;
  for (i = 0; i < argc; i++) {
    fprintf(fp, "%s ", argv[i]);
  }
  fprintf(fp, "\n");
  fclose(fp);
  return (0);
}


x86 assembly (clang, -O0):
<do_append>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xea2(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    118a <do_append+0x4a>
lea    0xe89(%rip),%rdi
call   1060 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    11f2 <do_append+0xb2>
movl   $0x0,-0x2c(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11d0 <do_append+0x90>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    (%rax,%rcx,8),%rdx
lea    0xe69(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1198 <do_append+0x58>
mov    -0x28(%rbp),%rdi
lea    0xe4d(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_append>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%r14
mov    %esi,%ebp
lea    0xe9c(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     11ba <do_append+0x6a>
mov    %rax,%r12
test   %ebp,%ebp
jle    119a <do_append+0x4a>
mov    %ebp,%ebp
lea    0xea6(%rip),%r15
xor    %ebx,%ebx
mov    (%r14,%rbx,8),%rdx
mov    %r12,%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    1180 <do_append+0x30>
mov    $0xa,%edi
mov    %r12,%rsi
call   1040 <fputc@plt>
mov    %r12,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe41(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%eax
jmp    11b1 <do_append+0x61>

LLVM IR (-O0):
@.str = [2 x i8] c"a\00"
@.str.1 = [34 x i8] c"1800 Cannot open file for append.\00"
@.str.2 = [4 x i8] c"%s \00"
@.str.3 = [2 x i8] c"\0A\00"
define i32 @do_append(i8* %0, i32 %1, i8** %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8**
%8 = alloca i32*
%9 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i8** %2, i8*** %7
%10 = load i8*, i8** %5
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %8
%12 = load i32*, i32** %8
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %16
14: ; preds = %3
%15 = call i32 @perror(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %4
br label %37
16: ; preds = %3
store i32 0, i32* %9
store i32 0, i32* %9
br label %17
17: ; preds = %29, %16
%18 = load i32, i32* %9
%19 = load i32, i32* %6
%20 = icmp slt i32 %18, %19
br i1 %20, label %21, label %32
21: ; preds = %17
%22 = load i32*, i32** %8
%23 = load i8**, i8*** %7
%24 = load i32, i32* %9
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8*, i8** %23, i64 %25
%27 = load i8*, i8** %26
%28 = call i32 (i32*, i8*, ...) @fprintf(i32* %22, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %27)
br label %29
29: ; preds = %21
%30 = load i32, i32* %9
%31 = add nsw i32 %30, 1
store i32 %31, i32* %9
br label %17
32: ; preds = %17
%33 = load i32*, i32** %8
%34 = call i32 (i32*, i8*, ...) @fprintf(i32* %33, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%35 = load i32*, i32** %8
%36 = call i32 @fclose(i32* %35)
store i32 0, i32* %4
br label %37
37: ; preds = %32, %14
%38 = load i32, i32* %4
ret i32 %38
}

LLVM IR (-O2):
@.str = [2 x i8] c"a\00"
@.str.1 = [34 x i8] c"1800 Cannot open file for append.\00"
@.str.2 = [4 x i8] c"%s \00"
define i32 @do_append(i8* nocapture %0, i32 %1, i8** nocapture %2) {
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %10, label %6
6: ; preds = %3
%7 = icmp sgt i32 %1, 0
br i1 %7, label %8, label %19
8: ; preds = %6
%9 = zext i32 %1 to i64
br label %12
10: ; preds = %3
%11 = tail call i32 @perror(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0))
br label %22
12: ; preds = %8, %12
%13 = phi i64 [ 0, %8 ], [ %17, %12 ]
%14 = getelementptr inbounds i8*, i8** %2, i64 %13
%15 = load i8*, i8** %14
%16 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %4, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %15)
%17 = add nuw nsw i64 %13, 1
%18 = icmp eq i64 %17, %9
br i1 %18, label %19, label %12
19: ; preds = %12, %6
%20 = tail call i32 @fputc(i32 10, i32* %4)
%21 = tail call i32 @fclose(i32* %4)
br label %22
22: ; preds = %19, %10
%23 = phi i32 [ 1, %10 ], [ 0, %19 ]
ret i32 %23
}