#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 */
int O_CREAT;
int O_RDWR;
int S_IRUSR;
int S_IWUSR;
int aquireLock(char const *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fdopen(int, char *);
int /*<<< orphan*/ freeLock(int);
char *getenv(char *);
int /*<<< orphan*/ logProgramArgs(int /*<<< orphan*/ *, char const *, int,
                                  char const **);
int open(char const *, int, int);
int logExec(int argc_, char const *argv_[]) {
  int lockFd;
  int logFd;
  FILE *stream;
  char const *logFileEnv;
  if (argc_ < 2) {
    return -1;
  }
  logFileEnv = getenv("CC_LOGGER_FILE");
  if (!logFileEnv) {
    return -3;
  }
  lockFd = aquireLock(logFileEnv);
  if (lockFd == -1) {
    return -5;
  }
  logFd = open(logFileEnv, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
  if (logFd == -1) {
    freeLock(lockFd);
    return -7;
  }
  stream = fdopen(logFd, "w+");
  if (!stream) {
    close(logFd);
    freeLock(lockFd);
    return -9;
  }
  logProgramArgs(stream, argv_[0], argc_ - 1, argv_ + 1);
  fclose(stream);
  freeLock(lockFd);
  return 0;
}


x86 assembly (clang, -O0):
<logExec>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11a5 <logExec+0x25>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ac <logExec+0x12c>
lea    0xe54(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11cc <logExec+0x4c>
movl   $0xfffffffd,-0x4(%rbp)
jmp    12ac <logExec+0x12c>
mov    -0x28(%rbp),%rdi
call   1090 <aquireLock@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11ee <logExec+0x6e>
movl   $0xfffffffb,-0x4(%rbp)
jmp    12ac <logExec+0x12c>
mov    -0x28(%rbp),%rdi
mov    0x2dff(%rip),%rax
mov    (%rax),%esi
mov    0x2dde(%rip),%rax
or     (%rax),%esi
mov    0x2dbd(%rip),%rax
mov    (%rax),%edx
mov    0x2dc4(%rip),%rax
or     (%rax),%edx
call   1070 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    123c <logExec+0xbc>
mov    -0x14(%rbp),%edi
call   1080 <freeLock@plt>
movl   $0xfffffff9,-0x4(%rbp)
jmp    12ac <logExec+0x12c>
mov    -0x18(%rbp),%edi
lea    0xdc9(%rip),%rsi
call   1060 <fdopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1276 <logExec+0xf6>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
mov    -0x14(%rbp),%edi
call   1080 <freeLock@plt>
movl   $0xfffffff7,-0x4(%rbp)
jmp    12ac <logExec+0x12c>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
mov    -0x8(%rbp),%edx
sub    $0x1,%edx
mov    -0x10(%rbp),%rcx
add    $0x8,%rcx
call   10a0 <logProgramArgs@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x14(%rbp),%edi
call   1080 <freeLock@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<logExec>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    $0xffffffff,%ebx
cmp    $0x2,%edi
jl     1254 <logExec+0xd4>
mov    %rsi,%r15
mov    %edi,%ebp
lea    0xe5e(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     122b <logExec+0xab>
mov    %rax,%rbx
mov    %rax,%rdi
call   1090 <aquireLock@plt>
cmp    $0xffffffff,%eax
je     1232 <logExec+0xb2>
mov    %eax,%r14d
mov    0x2e32(%rip),%rax
mov    0x2e13(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2df0(%rip),%rax
mov    0x2df9(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    %rbx,%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     1239 <logExec+0xb9>
mov    %eax,%ebx
lea    0xe16(%rip),%rsi
mov    %eax,%edi
call   1060 <fdopen@plt>
test   %rax,%rax
je     1240 <logExec+0xc0>
mov    %rax,%r12
mov    (%r15),%rsi
add    $0xffffffff,%ebp
add    $0x8,%r15
mov    %rax,%rdi
mov    %ebp,%edx
mov    %r15,%rcx
call   10a0 <logProgramArgs@plt>
mov    %r12,%rdi
call   1040 <fclose@plt>
xor    %ebx,%ebx
jmp    124c <logExec+0xcc>
mov    $0xfffffffd,%ebx
jmp    1254 <logExec+0xd4>
mov    $0xfffffffb,%ebx
jmp    1254 <logExec+0xd4>
mov    $0xfffffff9,%ebx
jmp    124c <logExec+0xcc>
mov    %ebx,%edi
call   1050 <close@plt>
mov    $0xfffffff7,%ebx
mov    %r14d,%edi
call   1080 <freeLock@plt>
mov    %ebx,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [15 x i8] c"CC_LOGGER_FILE\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.1 = [3 x i8] c"w+\00"
define i32 @logExec(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i8*
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp slt i32 %10, 2
br i1 %11, label %12, label %13
12: ; preds = %2
store i32 -1, i32* %3
br label %62
13: ; preds = %2
%14 = call i8* @getenv(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
store i8* %14, i8** %9
%15 = load i8*, i8** %9
%16 = icmp ne i8* %15, null
br i1 %16, label %18, label %17
17: ; preds = %13
store i32 -3, i32* %3
br label %62
18: ; preds = %13
%19 = load i8*, i8** %9
%20 = call i32 @aquireLock(i8* %19)
store i32 %20, i32* %6
%21 = load i32, i32* %6
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %24
23: ; preds = %18
store i32 -5, i32* %3
br label %62
24: ; preds = %18
%25 = load i8*, i8** %9
%26 = load i32, i32* @O_CREAT
%27 = load i32, i32* @O_RDWR
%28 = or i32 %26, %27
%29 = load i32, i32* @S_IRUSR
%30 = load i32, i32* @S_IWUSR
%31 = or i32 %29, %30
%32 = call i32 @open(i8* %25, i32 %28, i32 %31)
store i32 %32, i32* %7
%33 = load i32, i32* %7
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %38
35: ; preds = %24
%36 = load i32, i32* %6
%37 = call i32 @freeLock(i32 %36)
store i32 -7, i32* %3
br label %62
38: ; preds = %24
%39 = load i32, i32* %7
%40 = call i32* @fdopen(i32 %39, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %40, i32** %8
%41 = load i32*, i32** %8
%42 = icmp ne i32* %41, null
br i1 %42, label %48, label %43
43: ; preds = %38
%44 = load i32, i32* %7
%45 = call i32 @close(i32 %44)
%46 = load i32, i32* %6
%47 = call i32 @freeLock(i32 %46)
store i32 -9, i32* %3
br label %62
48: ; preds = %38
%49 = load i32*, i32** %8
%50 = load i8**, i8*** %5
%51 = getelementptr inbounds i8*, i8** %50, i64 0
%52 = load i8*, i8** %51
%53 = load i32, i32* %4
%54 = sub nsw i32 %53, 1
%55 = load i8**, i8*** %5
%56 = getelementptr inbounds i8*, i8** %55, i64 1
%57 = call i32 @logProgramArgs(i32* %49, i8* %52, i32 %54, i8** %56)
%58 = load i32*, i32** %8
%59 = call i32 @fclose(i32* %58)
%60 = load i32, i32* %6
%61 = call i32 @freeLock(i32 %60)
store i32 0, i32* %3
br label %62
62: ; preds = %48, %43, %35, %23, %17, %12
%63 = load i32, i32* %3
ret i32 %63
}

LLVM IR (-O2):
@.str = [15 x i8] c"CC_LOGGER_FILE\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.1 = [3 x i8] c"w+\00"
define i32 @logExec(i32 %0, i8** %1) {
%3 = icmp slt i32 %0, 2
br i1 %3, label %33, label %4
4: ; preds = %2
%5 = tail call i8* @getenv(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i8* %5, null
br i1 %6, label %33, label %7
7: ; preds = %4
%8 = tail call i32 @aquireLock(i8* %5)
%9 = icmp eq i32 %8, -1
br i1 %9, label %33, label %10
10: ; preds = %7
%11 = load i32, i32* @O_CREAT
%12 = load i32, i32* @O_RDWR
%13 = or i32 %12, %11
%14 = load i32, i32* @S_IRUSR
%15 = load i32, i32* @S_IWUSR
%16 = or i32 %15, %14
%17 = tail call i32 @open(i8* %5, i32 %13, i32 %16)
%18 = icmp eq i32 %17, -1
br i1 %18, label %30, label %19
19: ; preds = %10
%20 = tail call i32* @fdopen(i32 %17, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%21 = icmp eq i32* %20, null
br i1 %21, label %22, label %24
22: ; preds = %19
%23 = tail call i32 @close(i32 %17)
br label %30
24: ; preds = %19
%25 = load i8*, i8** %1
%26 = add nsw i32 %0, -1
%27 = getelementptr inbounds i8*, i8** %1, i64 1
%28 = tail call i32 @logProgramArgs(i32* %20, i8* %25, i32 %26, i8** %27)
%29 = tail call i32 @fclose(i32* %20)
br label %30
30: ; preds = %10, %22, %24
%31 = phi i32 [ -9, %22 ], [ 0, %24 ], [ -7, %10 ]
%32 = tail call i32 @freeLock(i32 %8)
br label %33
33: ; preds = %30, %7, %4, %2
%34 = phi i32 [ -1, %2 ], [ -3, %4 ], [ -5, %7 ], [ %31, %30 ]
ret i32 %34
}