#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*/ backup(char const *, int);
int /*<<< orphan*/ close(int);
int creat(char const *, int);
char *getenv(char *);
int perror(char const *);
unsigned int *tlog_cp(char const *, int /*<<< orphan*/);
int write(int, unsigned int *, int);
int tlog_hcp(int bk, const char *arg) {
  static const char *nm = 0;
  if (!nm && !(nm = getenv("LF_TLOG")))
    return -9;
  unsigned int *q = tlog_cp(arg, 0);
  if (*q > 0xfffeffff)
    return -1;
  backup(nm, bk);
  int n = 4 * *q, r, fd = creat(nm, 0600);
  if (fd < 0)
    return -5;
  if ((r = write(fd, q + 2, n) != n))
    return perror(nm), close(fd), -5;
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<tlog_hcp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpq   $0x0,0x2ed9(%rip)
jne    11b6 <tlog_hcp+0x46>
lea    0xe6c(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,0x2ec0(%rip)
cmp    $0x0,%rax
jne    11b6 <tlog_hcp+0x46>
movl   $0xfffffff7,-0x4(%rbp)
jmp    1286 <tlog_hcp+0x116>
mov    -0x10(%rbp),%rdi
xor    %esi,%esi
call   1060 <tlog_cp@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
cmpl   $0xfffeffff,(%rax)
jbe    11e1 <tlog_hcp+0x71>
movl   $0xffffffff,-0x4(%rbp)
jmp    1286 <tlog_hcp+0x116>
mov    0x2e78(%rip),%rdi
mov    -0x8(%rbp),%esi
call   1070 <backup@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
shl    $0x2,%eax
mov    %eax,-0x1c(%rbp)
mov    0x2e5d(%rip),%rdi
mov    $0x180,%esi
call   1090 <creat@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1226 <tlog_hcp+0xb6>
movl   $0xfffffffb,-0x4(%rbp)
jmp    1286 <tlog_hcp+0x116>
mov    -0x24(%rbp),%edi
mov    -0x18(%rbp),%rsi
add    $0x8,%rsi
mov    -0x1c(%rbp),%edx
call   1040 <write@plt>
cmp    -0x1c(%rbp),%eax
setne  %al
mov    %al,%cl
and    $0x1,%cl
movzbl %cl,%ecx
mov    %ecx,-0x20(%rbp)
test   $0x1,%al
jne    1257 <tlog_hcp+0xe7>
jmp    1277 <tlog_hcp+0x107>
mov    0x2e02(%rip),%rdi
call   1080 <perror@plt>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
movl   $0xfffffffb,-0x4(%rbp)
jmp    1286 <tlog_hcp+0x116>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tlog_hcp>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%r14d
cmpq   $0x0,0x2edb(%rip)
jne    119f <tlog_hcp+0x2f>
lea    0xe72(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,0x2ec6(%rip)
test   %rax,%rax
je     1210 <tlog_hcp+0xa0>
mov    %rbx,%rdi
xor    %esi,%esi
call   1060 <tlog_cp@plt>
mov    $0xffffffff,%ebp
cmpl   $0xfffeffff,(%rax)
ja     1215 <tlog_hcp+0xa5>
mov    %rax,%rbx
mov    0x2ea0(%rip),%rdi
mov    %r14d,%esi
call   1070 <backup@plt>
mov    (%rbx),%r15d
mov    0x2e8e(%rip),%rdi
mov    $0x180,%esi
call   1090 <creat@plt>
test   %eax,%eax
js     1209 <tlog_hcp+0x99>
mov    %eax,%r14d
shl    $0x2,%r15d
add    $0x8,%rbx
mov    %eax,%edi
mov    %rbx,%rsi
mov    %r15d,%edx
call   1040 <write@plt>
xor    %ebp,%ebp
cmp    %r15d,%eax
jne    1222 <tlog_hcp+0xb2>
mov    %r14d,%edi
call   1050 <close@plt>
jmp    1215 <tlog_hcp+0xa5>
mov    $0xfffffffb,%ebp
jmp    1215 <tlog_hcp+0xa5>
mov    $0xfffffff7,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2e37(%rip),%rdi
call   1080 <perror@plt>
mov    $0xfffffffb,%ebp
jmp    11ff <tlog_hcp+0x8f>

LLVM IR (-O0):
@tlog_hcp.nm = internal global i8* null
@.str = [8 x i8] c"LF_TLOG\00"
define i32 @tlog_hcp(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
%10 = load i8*, i8** @tlog_hcp.nm
%11 = icmp ne i8* %10, null
br i1 %11, label %16, label %12
12: ; preds = %2
%13 = call i8* @getenv(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
store i8* %13, i8** @tlog_hcp.nm
%14 = icmp ne i8* %13, null
br i1 %14, label %16, label %15
15: ; preds = %12
store i32 -9, i32* %3
br label %52
16: ; preds = %12, %2
%17 = load i8*, i8** %5
%18 = call i32* @tlog_cp(i8* %17, i32 0)
store i32* %18, i32** %6
%19 = load i32*, i32** %6
%20 = load i32, i32* %19
%21 = icmp ugt i32 %20, -65537
br i1 %21, label %22, label %23
22: ; preds = %16
store i32 -1, i32* %3
br label %52
23: ; preds = %16
%24 = load i8*, i8** @tlog_hcp.nm
%25 = load i32, i32* %4
%26 = call i32 @backup(i8* %24, i32 %25)
%27 = load i32*, i32** %6
%28 = load i32, i32* %27
%29 = mul i32 4, %28
store i32 %29, i32* %7
%30 = load i8*, i8** @tlog_hcp.nm
%31 = call i32 @creat(i8* %30, i32 384)
store i32 %31, i32* %9
%32 = load i32, i32* %9
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %23
store i32 -5, i32* %3
br label %52
35: ; preds = %23
%36 = load i32, i32* %9
%37 = load i32*, i32** %6
%38 = getelementptr inbounds i32, i32* %37, i64 2
%39 = load i32, i32* %7
%40 = call i32 @write(i32 %36, i32* %38, i32 %39)
%41 = load i32, i32* %7
%42 = icmp ne i32 %40, %41
%43 = zext i1 %42 to i32
store i32 %43, i32* %8
br i1 %42, label %44, label %49
44: ; preds = %35
%45 = load i8*, i8** @tlog_hcp.nm
%46 = call i32 @perror(i8* %45)
%47 = load i32, i32* %9
%48 = call i32 @close(i32 %47)
store i32 -5, i32* %3
br label %52
49: ; preds = %35
%50 = load i32, i32* %9
%51 = call i32 @close(i32 %50)
store i32 0, i32* %3
br label %52
52: ; preds = %49, %44, %34, %22, %15
%53 = load i32, i32* %3
ret i32 %53
}

LLVM IR (-O2):
@tlog_hcp.nm = internal unnamed_addr global i8* null
@.str = [8 x i8] c"LF_TLOG\00"
define i32 @tlog_hcp(i32 %0, i8* %1) {
%3 = load i8*, i8** @tlog_hcp.nm
%4 = icmp eq i8* %3, null
br i1 %4, label %5, label %8
5: ; preds = %2
%6 = tail call i8* @getenv(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
store i8* %6, i8** @tlog_hcp.nm
%7 = icmp eq i8* %6, null
br i1 %7, label %30, label %8
8: ; preds = %5, %2
%9 = tail call i32* @tlog_cp(i8* %1, i32 0)
%10 = load i32, i32* %9
%11 = icmp ugt i32 %10, -65537
br i1 %11, label %30, label %12
12: ; preds = %8
%13 = load i8*, i8** @tlog_hcp.nm
%14 = tail call i32 @backup(i8* %13, i32 %0)
%15 = load i32, i32* %9
%16 = shl i32 %15, 2
%17 = load i8*, i8** @tlog_hcp.nm
%18 = tail call i32 @creat(i8* %17, i32 384)
%19 = icmp slt i32 %18, 0
br i1 %19, label %30, label %20
20: ; preds = %12
%21 = getelementptr inbounds i32, i32* %9, i64 2
%22 = tail call i32 @write(i32 %18, i32* %21, i32 %16)
%23 = icmp eq i32 %22, %16
br i1 %23, label %27, label %24
24: ; preds = %20
%25 = load i8*, i8** @tlog_hcp.nm
%26 = tail call i32 @perror(i8* %25)
br label %27
27: ; preds = %20, %24
%28 = phi i32 [ -5, %24 ], [ 0, %20 ]
%29 = tail call i32 @close(i32 %18)
br label %30
30: ; preds = %27, %8, %12, %5
%31 = phi i32 [ -9, %5 ], [ -1, %8 ], [ -5, %12 ], [ %28, %27 ]
ret i32 %31
}