#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_RDWR;
int /*<<< orphan*/ close(int);
int errno;
int open(char *, int);
int write(int, char *, size_t);
int cm_append_entry(char *line, size_t size, char *file) {
  int fd;
  errno = 0;
  if ((fd = open(file, O_RDWR | O_APPEND)) == -1) {
    return -errno;
  }
  if (write(fd, line, size) == -1) {
    close(fd);
    return -errno;
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<cm_append_entry>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    0x2e8d(%rip),%rax
movl   $0x0,(%rax)
mov    -0x20(%rbp),%rdi
mov    0x2e8c(%rip),%rax
mov    (%rax),%esi
mov    0x2e63(%rip),%rax
or     (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x24(%rbp)
cmp    $0xffffffff,%eax
jne    118b <cm_append_entry+0x5b>
xor    %eax,%eax
mov    0x2e57(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11ce <cm_append_entry+0x9e>
mov    -0x24(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    11bf <cm_append_entry+0x8f>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
xor    %eax,%eax
mov    0x2e23(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11ce <cm_append_entry+0x9e>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cm_append_entry>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2e94(%rip),%r15
movl   $0x0,(%r15)
mov    0x2e96(%rip),%rax
mov    0x2e6f(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rdx,%rdi
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     1189 <cm_append_entry+0x59>
mov    %eax,%ebp
mov    %eax,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1030 <write@plt>
mov    %eax,%ebx
mov    %ebp,%edi
call   1040 <close@plt>
xor    %eax,%eax
cmp    $0xffffffff,%ebx
jne    118e <cm_append_entry+0x5e>
xor    %eax,%eax
sub    (%r15),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i32 0
@O_RDWR = global i32 0
@O_APPEND = global i32 0
define i32 @cm_append_entry(i8* %0, i64 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i32
store i8* %0, i8** %5
store i64 %1, i64* %6
store i8* %2, i8** %7
store i32 0, i32* @errno
%9 = load i8*, i8** %7
%10 = load i32, i32* @O_RDWR
%11 = load i32, i32* @O_APPEND
%12 = or i32 %10, %11
%13 = call i32 @open(i8* %9, i32 %12)
store i32 %13, i32* %8
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %18
15: ; preds = %3
%16 = load i32, i32* @errno
%17 = sub nsw i32 0, %16
store i32 %17, i32* %4
br label %32
18: ; preds = %3
%19 = load i32, i32* %8
%20 = load i8*, i8** %5
%21 = load i64, i64* %6
%22 = call i32 @write(i32 %19, i8* %20, i64 %21)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %29
24: ; preds = %18
%25 = load i32, i32* %8
%26 = call i32 @close(i32 %25)
%27 = load i32, i32* @errno
%28 = sub nsw i32 0, %27
store i32 %28, i32* %4
br label %32
29: ; preds = %18
%30 = load i32, i32* %8
%31 = call i32 @close(i32 %30)
store i32 0, i32* %4
br label %32
32: ; preds = %29, %24, %15
%33 = load i32, i32* %4
ret i32 %33
}

LLVM IR (-O2):
@errno = global i32 0
@O_RDWR = global i32 0
@O_APPEND = global i32 0
define i32 @cm_append_entry(i8* nocapture %0, i64 %1, i8* nocapture %2) {
store i32 0, i32* @errno
%4 = load i32, i32* @O_RDWR
%5 = load i32, i32* @O_APPEND
%6 = or i32 %5, %4
%7 = tail call i32 @open(i8* %2, i32 %6)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %12
9: ; preds = %3
%10 = load i32, i32* @errno
%11 = sub nsw i32 0, %10
br label %19
12: ; preds = %3
%13 = tail call i32 @write(i32 %7, i8* %0, i64 %1)
%14 = icmp eq i32 %13, -1
%15 = tail call i32 @close(i32 %7)
br i1 %14, label %16, label %19
16: ; preds = %12
%17 = load i32, i32* @errno
%18 = sub nsw i32 0, %17
br label %19
19: ; preds = %12, %16, %9
%20 = phi i32 [ %11, %9 ], [ %18, %16 ], [ 0, %12 ]
ret i32 %20
}