#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 */
char *concat(char *, char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fprintf(int /*<<< orphan*/ *, char *, char *);
int /*<<< orphan*/ free(char *);
char *getenv(char *);
unsigned int add_line(char *line, char *filename) {
  char *home = getenv("HOME");
  char *p = concat(home, filename);
  FILE *fp = fopen(p, "a");
  if (fp == NULL) {
    free(p);
    return 0;
  }
  int res = fprintf(fp, "%s\n", line);
  fclose(fp);
  free(p);
  return res >= 0;
}


x86 assembly (clang, -O0):
<add_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    0xe89(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <concat@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
lea    0xe69(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11c5 <add_line+0x65>
mov    -0x28(%rbp),%rdi
call   1040 <free@plt>
movl   $0x0,-0x4(%rbp)
jmp    11fd <add_line+0x9d>
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xe33(%rip),%rsi
call   1070 <fprintf@plt>
mov    %eax,-0x34(%rbp)
mov    -0x30(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x28(%rbp),%rdi
call   1040 <free@plt>
cmpl   $0x0,-0x34(%rbp)
setge  %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<add_line>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r14
lea    0xe8c(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rdi
mov    %rbx,%rsi
call   1060 <concat@plt>
mov    %rax,%r15
lea    0xe77(%rip),%rsi
mov    %rax,%rdi
call   1080 <fopen@plt>
test   %rax,%rax
je     11c9 <add_line+0x69>
mov    %rax,%rbx
lea    0xe62(%rip),%rsi
mov    %rax,%rdi
mov    %r14,%rdx
call   1070 <fprintf@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    %r15,%rdi
call   1040 <free@plt>
not    %ebp
shr    $0x1f,%ebp
jmp    11d3 <add_line+0x73>
mov    %r15,%rdi
call   1040 <free@plt>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"HOME\00"
@.str.1 = [2 x i8] c"a\00"
@.str.2 = [4 x i8] c"%s\0A\00"
define i32 @add_line(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32*
%9 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%10 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i8* %10, i8** %6
%11 = load i8*, i8** %6
%12 = load i8*, i8** %5
%13 = call i8* @concat(i8* %11, i8* %12)
store i8* %13, i8** %7
%14 = load i8*, i8** %7
%15 = call i32* @fopen(i8* %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %15, i32** %8
%16 = load i32*, i32** %8
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %21
18: ; preds = %2
%19 = load i8*, i8** %7
%20 = call i32 @free(i8* %19)
store i32 0, i32* %3
br label %32
21: ; preds = %2
%22 = load i32*, i32** %8
%23 = load i8*, i8** %4
%24 = call i32 @fprintf(i32* %22, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %23)
store i32 %24, i32* %9
%25 = load i32*, i32** %8
%26 = call i32 @fclose(i32* %25)
%27 = load i8*, i8** %7
%28 = call i32 @free(i8* %27)
%29 = load i32, i32* %9
%30 = icmp sge i32 %29, 0
%31 = zext i1 %30 to i32
store i32 %31, i32* %3
br label %32
32: ; preds = %21, %18
%33 = load i32, i32* %3
ret i32 %33
}

LLVM IR (-O2):
@.str = [5 x i8] c"HOME\00"
@.str.1 = [2 x i8] c"a\00"
@.str.2 = [4 x i8] c"%s\0A\00"
define i32 @add_line(i8* %0, i8* %1) {
%3 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%4 = tail call i8* @concat(i8* %3, i8* %1)
%5 = tail call i32* @fopen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = tail call i32 @free(i8* %4)
br label %15
9: ; preds = %2
%10 = tail call i32 @fprintf(i32* %5, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %0)
%11 = tail call i32 @fclose(i32* %5)
%12 = tail call i32 @free(i8* %4)
%13 = xor i32 %10, -1
%14 = lshr i32 %13, 31
br label %15
15: ; preds = %9, %7
%16 = phi i32 [ 0, %7 ], [ %14, %9 ]
ret i32 %16
}