#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 */

/* Variables and functions */
int O_APPEND;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int open(char const *, int);
int write(int, char *, int);
int append_text_to_file(const char *filename, char *text_content) {
  int c_open;
  int c_write;
  int i;
  if (filename == NULL)
    return (-1);
  c_open = open(filename, O_WRONLY | O_APPEND);
  if (c_open == -1) {
    return (-1);
  }
  if (text_content == NULL)
    text_content = "";
  i = 0;
  while (text_content[i] != '\0') {
    i++;
  }
  c_write = write(c_open, text_content, i);
  if (c_write == -1)
    return (-1);
  close(c_open);
  return (1);
}


x86 assembly (clang, -O0):
<append_text_to_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1157 <append_text_to_file+0x27>
movl   $0xffffffff,-0x4(%rbp)
jmp    1202 <append_text_to_file+0xd2>
mov    -0x10(%rbp),%rdi
mov    0x2e76(%rip),%rax
mov    (%rax),%esi
mov    0x2e65(%rip),%rax
or     (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    118b <append_text_to_file+0x5b>
movl   $0xffffffff,-0x4(%rbp)
jmp    1202 <append_text_to_file+0xd2>
cmpq   $0x0,-0x18(%rbp)
jne    11a1 <append_text_to_file+0x71>
lea    0xe63(%rip),%rax
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x18(%rbp),%rax
movslq -0x24(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
je     11cb <append_text_to_file+0x9b>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11a8 <append_text_to_file+0x78>
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    11f3 <append_text_to_file+0xc3>
movl   $0xffffffff,-0x4(%rbp)
jmp    1202 <append_text_to_file+0xd2>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<append_text_to_file>:
push   %rbp
push   %r14
push   %rbx
mov    $0xffffffff,%r14d
test   %rdi,%rdi
je     1196 <append_text_to_file+0x66>
mov    %rsi,%rbx
mov    0x2e8f(%rip),%rax
mov    0x2e80(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     1196 <append_text_to_file+0x66>
mov    %eax,%ebp
test   %rbx,%rbx
lea    0xe96(%rip),%rsi
cmovne %rbx,%rsi
xor    %edx,%edx
cmpb   $0x0,(%rsi,%rdx,1)
lea    0x1(%rdx),%rdx
jne    1170 <append_text_to_file+0x40>
add    $0xffffffff,%edx
mov    %ebp,%edi
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     1196 <append_text_to_file+0x66>
mov    %ebp,%edi
call   1040 <close@plt>
mov    $0x1,%r14d
mov    %r14d,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@O_WRONLY = global i32 0
@O_APPEND = global i32 0
@.str = [1 x i8] zeroinitializer
define i32 @append_text_to_file(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%9 = load i8*, i8** %4
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %12
11: ; preds = %2
store i32 -1, i32* %3
br label %48
12: ; preds = %2
%13 = load i8*, i8** %4
%14 = load i32, i32* @O_WRONLY
%15 = load i32, i32* @O_APPEND
%16 = or i32 %14, %15
%17 = call i32 @open(i8* %13, i32 %16)
store i32 %17, i32* %6
%18 = load i32, i32* %6
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %21
20: ; preds = %12
store i32 -1, i32* %3
br label %48
21: ; preds = %12
%22 = load i8*, i8** %5
%23 = icmp eq i8* %22, null
br i1 %23, label %24, label %25
24: ; preds = %21
store i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), i8** %5
br label %25
25: ; preds = %24, %21
store i32 0, i32* %8
br label %26
26: ; preds = %34, %25
%27 = load i8*, i8** %5
%28 = load i32, i32* %8
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %27, i64 %29
%31 = load i8, i8* %30
%32 = sext i8 %31 to i32
%33 = icmp ne i32 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %26
%35 = load i32, i32* %8
%36 = add nsw i32 %35, 1
store i32 %36, i32* %8
br label %26
37: ; preds = %26
%38 = load i32, i32* %6
%39 = load i8*, i8** %5
%40 = load i32, i32* %8
%41 = call i32 @write(i32 %38, i8* %39, i32 %40)
store i32 %41, i32* %7
%42 = load i32, i32* %7
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %45
44: ; preds = %37
store i32 -1, i32* %3
br label %48
45: ; preds = %37
%46 = load i32, i32* %6
%47 = call i32 @close(i32 %46)
store i32 1, i32* %3
br label %48
48: ; preds = %45, %44, %20, %11
%49 = load i32, i32* %3
ret i32 %49
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
@O_APPEND = global i32 0
@.str = [1 x i8] zeroinitializer
define i32 @append_text_to_file(i8* %0, i8* %1) {
%3 = icmp eq i8* %0, null
br i1 %3, label %25, label %4
4: ; preds = %2
%5 = load i32, i32* @O_WRONLY
%6 = load i32, i32* @O_APPEND
%7 = or i32 %6, %5
%8 = tail call i32 @open(i8* %0, i32 %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %25, label %10
10: ; preds = %4
%11 = icmp eq i8* %1, null
%12 = select i1 %11, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), i8* %1
br label %13
13: ; preds = %13, %10
%14 = phi i64 [ %18, %13 ], [ 0, %10 ]
%15 = getelementptr inbounds i8, i8* %12, i64 %14
%16 = load i8, i8* %15
%17 = icmp eq i8 %16, 0
%18 = add nuw i64 %14, 1
br i1 %17, label %19, label %13
19: ; preds = %13
%20 = trunc i64 %14 to i32
%21 = tail call i32 @write(i32 %8, i8* %12, i32 %20)
%22 = icmp eq i32 %21, -1
br i1 %22, label %25, label %23
23: ; preds = %19
%24 = tail call i32 @close(i32 %8)
br label %25
25: ; preds = %19, %4, %2, %23
%26 = phi i32 [ 1, %23 ], [ -1, %2 ], [ -1, %4 ], [ -1, %19 ]
ret i32 %26
}