#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_CREAT;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int fb_flock(int, int);
int open(char const *, int, int);
int /*<<< orphan*/ strlen(char const *);
int /*<<< orphan*/ write(int, char const *, int /*<<< orphan*/);
int file_append(const char *file, const char *msg) {
  if (file == NULL || msg == NULL)
    return -1;
  int fd = open(file, O_WRONLY | O_CREAT | O_APPEND, 0644);
  if (fd != -1) {
    if (fb_flock(fd, 2) == -1) {
      close(fd);
      return -1;
    }
    write(fd, msg, strlen(msg));
    fb_flock(fd, 8);
    close(fd);
  }
  return 0;
}


x86 assembly (clang, -O0):
<file_append>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1176 <file_append+0x26>
cmpq   $0x0,-0x18(%rbp)
jne    1182 <file_append+0x32>
movl   $0xffffffff,-0x4(%rbp)
jmp    1223 <file_append+0xd3>
mov    -0x10(%rbp),%rdi
mov    0x2e43(%rip),%rax
mov    (%rax),%esi
mov    0x2e62(%rip),%rax
or     (%rax),%esi
mov    0x2e29(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   1070 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
je     121c <file_append+0xcc>
mov    -0x1c(%rbp),%edi
mov    $0x2,%esi
call   1060 <fb_flock@plt>
cmp    $0xffffffff,%eax
jne    11e2 <file_append+0x92>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1223 <file_append+0xd3>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    -0x1c(%rbp),%edi
mov    $0x8,%esi
call   1060 <fb_flock@plt>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_append>:
push   %rbp
push   %r14
push   %rbx
mov    $0xffffffff,%ebx
test   %rdi,%rdi
je     11d7 <file_append+0x87>
mov    %rsi,%r14
test   %rsi,%rsi
je     11d7 <file_append+0x87>
mov    0x2e63(%rip),%rax
mov    0x2e84(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e49(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     11c7 <file_append+0x77>
mov    %eax,%ebp
mov    %eax,%edi
mov    $0x2,%esi
call   1060 <fb_flock@plt>
cmp    $0xffffffff,%eax
je     11cb <file_append+0x7b>
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %ebp,%edi
mov    $0x8,%esi
call   1060 <fb_flock@plt>
xor    %ebx,%ebx
jmp    11d0 <file_append+0x80>
xor    %ebx,%ebx
jmp    11d7 <file_append+0x87>
mov    $0xffffffff,%ebx
mov    %ebp,%edi
call   1050 <close@plt>
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_APPEND = global i32 0
define i32 @file_append(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%7 = load i8*, i8** %4
%8 = icmp eq i8* %7, null
br i1 %8, label %12, label %9
9: ; preds = %2
%10 = load i8*, i8** %5
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %13
12: ; preds = %9, %2
store i32 -1, i32* %3
br label %41
13: ; preds = %9
%14 = load i8*, i8** %4
%15 = load i32, i32* @O_WRONLY
%16 = load i32, i32* @O_CREAT
%17 = or i32 %15, %16
%18 = load i32, i32* @O_APPEND
%19 = or i32 %17, %18
%20 = call i32 @open(i8* %14, i32 %19, i32 420)
store i32 %20, i32* %6
%21 = load i32, i32* %6
%22 = icmp ne i32 %21, -1
br i1 %22, label %23, label %40
23: ; preds = %13
%24 = load i32, i32* %6
%25 = call i32 @fb_flock(i32 %24, i32 2)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %30
27: ; preds = %23
%28 = load i32, i32* %6
%29 = call i32 @close(i32 %28)
store i32 -1, i32* %3
br label %41
30: ; preds = %23
%31 = load i32, i32* %6
%32 = load i8*, i8** %5
%33 = load i8*, i8** %5
%34 = call i32 @strlen(i8* %33)
%35 = call i32 @write(i32 %31, i8* %32, i32 %34)
%36 = load i32, i32* %6
%37 = call i32 @fb_flock(i32 %36, i32 8)
%38 = load i32, i32* %6
%39 = call i32 @close(i32 %38)
br label %40
40: ; preds = %30, %13
store i32 0, i32* %3
br label %41
41: ; preds = %40, %27, %12
%42 = load i32, i32* %3
ret i32 %42
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_APPEND = global i32 0
define i32 @file_append(i8* %0, i8* %1) {
%3 = icmp eq i8* %0, null
%4 = icmp eq i8* %1, null
%5 = or i1 %3, %4
br i1 %5, label %24, label %6
6: ; preds = %2
%7 = load i32, i32* @O_WRONLY
%8 = load i32, i32* @O_CREAT
%9 = or i32 %8, %7
%10 = load i32, i32* @O_APPEND
%11 = or i32 %9, %10
%12 = tail call i32 @open(i8* %0, i32 %11, i32 420)
%13 = icmp eq i32 %12, -1
br i1 %13, label %24, label %14
14: ; preds = %6
%15 = tail call i32 @fb_flock(i32 %12, i32 2)
%16 = icmp eq i32 %15, -1
br i1 %16, label %21, label %17
17: ; preds = %14
%18 = tail call i32 @strlen(i8* %1)
%19 = tail call i32 @write(i32 %12, i8* %1, i32 %18)
%20 = tail call i32 @fb_flock(i32 %12, i32 8)
br label %21
21: ; preds = %14, %17
%22 = phi i32 [ 0, %17 ], [ -1, %14 ]
%23 = tail call i32 @close(i32 %12)
br label %24
24: ; preds = %21, %6, %2
%25 = phi i32 [ -1, %2 ], [ 0, %6 ], [ %22, %21 ]
ret i32 %25
}