#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 /*<<< orphan*/ LOCK_SH;
int O_APPEND;
int O_CREAT;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int flock(int, int /*<<< orphan*/);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int open_log(char *pathname) {
  int fd;
  fd = open(pathname, O_WRONLY | O_CREAT | O_APPEND, 0644);
  if (fd < 0) {
    perror("open");
    return -1;
  }
  if (flock(fd, LOCK_SH) == -1) {
    perror("flock");
    close(fd);
    return -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<open_log>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e71(%rip),%rax
mov    (%rax),%esi
mov    0x2e98(%rip),%rax
or     (%rax),%esi
mov    0x2e57(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   1040 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    119a <open_log+0x5a>
lea    0xe77(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11da <open_log+0x9a>
mov    -0x14(%rbp),%edi
mov    0x2e3c(%rip),%rax
mov    (%rax),%esi
call   1060 <flock@plt>
cmp    $0xffffffff,%eax
jne    11d4 <open_log+0x94>
lea    0xe4a(%rip),%rdi
call   1050 <perror@plt>
mov    -0x14(%rbp),%edi
call   1030 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11da <open_log+0x9a>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_log>:
push   %rbx
mov    0x2e80(%rip),%rax
mov    0x2ea9(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e66(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   1040 <open@plt>
test   %eax,%eax
js     1185 <open_log+0x45>
mov    %eax,%ebx
mov    0x2e6d(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1060 <flock@plt>
cmp    $0xffffffff,%eax
je     1193 <open_log+0x53>
mov    %ebx,%eax
pop    %rbx
ret
lea    0xe74(%rip),%rdi
call   1050 <perror@plt>
jmp    11a6 <open_log+0x66>
lea    0xe6b(%rip),%rdi
call   1050 <perror@plt>
mov    %ebx,%edi
call   1030 <close@plt>
mov    $0xffffffff,%ebx
jmp    1181 <open_log+0x41>

LLVM IR (-O0):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_APPEND = global i32 0
@.str = [5 x i8] c"open\00"
@LOCK_SH = global i32 0
@.str.1 = [6 x i8] c"flock\00"
define i32 @open_log(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
store i8* %0, i8** %3
%5 = load i8*, i8** %3
%6 = load i32, i32* @O_WRONLY
%7 = load i32, i32* @O_CREAT
%8 = or i32 %6, %7
%9 = load i32, i32* @O_APPEND
%10 = or i32 %8, %9
%11 = call i32 @open(i8* %5, i32 %10, i32 420)
store i32 %11, i32* %4
%12 = load i32, i32* %4
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %1
%15 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %27
16: ; preds = %1
%17 = load i32, i32* %4
%18 = load i32, i32* @LOCK_SH
%19 = call i32 @flock(i32 %17, i32 %18)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %25
21: ; preds = %16
%22 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%23 = load i32, i32* %4
%24 = call i32 @close(i32 %23)
store i32 -1, i32* %2
br label %27
25: ; preds = %16
%26 = load i32, i32* %4
store i32 %26, i32* %2
br label %27
27: ; preds = %25, %21, %14
%28 = load i32, i32* %2
ret i32 %28
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_APPEND = global i32 0
@.str = [5 x i8] c"open\00"
@LOCK_SH = global i32 0
@.str.1 = [6 x i8] c"flock\00"
define i32 @open_log(i8* nocapture %0) {
%2 = load i32, i32* @O_WRONLY
%3 = load i32, i32* @O_CREAT
%4 = or i32 %3, %2
%5 = load i32, i32* @O_APPEND
%6 = or i32 %4, %5
%7 = tail call i32 @open(i8* %0, i32 %6, i32 420)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %1
%10 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %18
11: ; preds = %1
%12 = load i32, i32* @LOCK_SH
%13 = tail call i32 @flock(i32 %7, i32 %12)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %18
15: ; preds = %11
%16 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%17 = tail call i32 @close(i32 %7)
br label %18
18: ; preds = %11, %15, %9
%19 = phi i32 [ -1, %9 ], [ -1, %15 ], [ %7, %11 ]
ret i32 %19
}