#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*/ uint32_t;
typedef scalar_t__ pid_t;
typedef int /*<<< orphan*/ b;

/* Variables and functions */
int /*<<< orphan*/ LOCK_EX;
int /*<<< orphan*/ LOCK_UN;
int O_APPEND;
int O_CREAT;
int O_DSYNC;
int O_WRONLY;
int /*<<< orphan*/ errno;
int flock(int, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char *);
char *getenv(char *);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int open(char *, int, int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int write(int, int /*<<< orphan*/ *, int);
void write_to_file_of_pids(pid_t pid) {
  char *file_of_pids = getenv("CCTOOLS_RESOURCE_MONITOR_PIDS_FILE");
  if (file_of_pids) {
    int fd = open(file_of_pids, O_WRONLY | O_APPEND | O_CREAT | O_DSYNC, 0660);
    if (fd == -1) {
      if (0)
        fprintf(stderr, "error opening %s: %s\n",
                "CCTOOLS_RESOURCE_MONITOR_PIDS_FILE", strerror(errno));
      return;
    }
    uint32_t b = htonl((uint32_t)pid);
    int ld = flock(fd, LOCK_EX);
    if (ld == -1) {
      if (0)
        fprintf(stderr, "error locking %s: %s\n",
                "CCTOOLS_RESOURCE_MONITOR_PIDS_FILE", strerror(errno));
      return;
    }
    int count = write(fd, &b, sizeof(b));
    flock(fd, LOCK_UN);
    if (count == -1) {
      if (0)
        fprintf(stderr, "error writing to %s: %s\n",
                "CCTOOLS_RESOURCE_MONITOR_PIDS_FILE", strerror(errno));
    }
  }
}


x86 assembly (clang, -O0):
<write_to_file_of_pids>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
lea    0xe9d(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1225 <write_to_file_of_pids+0xd5>
mov    -0x10(%rbp),%rdi
mov    0x2e36(%rip),%rax
mov    (%rax),%esi
mov    0x2e25(%rip),%rax
or     (%rax),%esi
mov    0x2e64(%rip),%rax
or     (%rax),%esi
mov    0x2e33(%rip),%rax
or     (%rax),%esi
mov    $0x1b0,%edx
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11bb <write_to_file_of_pids+0x6b>
jmp    1225 <write_to_file_of_pids+0xd5>
mov    -0x8(%rbp),%rax
mov    %eax,%edi
call   1050 <htonl@plt>
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2e0d(%rip),%rax
mov    (%rax),%esi
call   1070 <flock@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11ec <write_to_file_of_pids+0x9c>
jmp    1225 <write_to_file_of_pids+0xd5>
mov    -0x14(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x4,%edx
call   1040 <write@plt>
mov    %eax,-0x20(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2dbe(%rip),%rax
mov    (%rax),%esi
call   1070 <flock@plt>
cmpl   $0xffffffff,-0x20(%rbp)
jne    1220 <write_to_file_of_pids+0xd0>
jmp    1220 <write_to_file_of_pids+0xd0>
jmp    1225 <write_to_file_of_pids+0xd5>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_to_file_of_pids>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
lea    0xea3(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     11e0 <write_to_file_of_pids+0x90>
mov    0x2e4a(%rip),%rcx
mov    0x2e3b(%rip),%rdx
mov    (%rdx),%esi
or     (%rcx),%esi
mov    0x2e78(%rip),%rcx
or     (%rcx),%esi
mov    0x2e47(%rip),%rcx
or     (%rcx),%esi
mov    %rax,%rdi
mov    $0x1b0,%edx
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     11e0 <write_to_file_of_pids+0x90>
mov    %eax,%ebp
mov    %ebx,%edi
call   1050 <htonl@plt>
mov    %eax,0x4(%rsp)
mov    0x2e2f(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1070 <flock@plt>
cmp    $0xffffffff,%eax
je     11e0 <write_to_file_of_pids+0x90>
lea    0x4(%rsp),%rsi
mov    %ebp,%edi
mov    $0x4,%edx
call   1040 <write@plt>
mov    0x2df1(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1070 <flock@plt>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [35 x i8] c"CCTOOLS_RESOURCE_MONITOR_PIDS_FILE\00"
@O_WRONLY = global i32 0
@O_APPEND = global i32 0
@O_CREAT = global i32 0
@O_DSYNC = global i32 0
@LOCK_EX = global i32 0
@LOCK_UN = global i32 0
@errno = global i32 0
@stderr = global i32 0
define void @write_to_file_of_pids(i64 %0) {
%2 = alloca i64
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i64 %0, i64* %2
%8 = call i8* @getenv(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
store i8* %8, i8** %3
%9 = load i8*, i8** %3
%10 = icmp ne i8* %9, null
br i1 %10, label %11, label %44
11: ; preds = %1
%12 = load i8*, i8** %3
%13 = load i32, i32* @O_WRONLY
%14 = load i32, i32* @O_APPEND
%15 = or i32 %13, %14
%16 = load i32, i32* @O_CREAT
%17 = or i32 %15, %16
%18 = load i32, i32* @O_DSYNC
%19 = or i32 %17, %18
%20 = call i32 @open(i8* %12, i32 %19, i32 432)
store i32 %20, i32* %4
%21 = load i32, i32* %4
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %24
23: ; preds = %11
br label %44
24: ; preds = %11
%25 = load i64, i64* %2
%26 = trunc i64 %25 to i32
%27 = call i32 @htonl(i32 %26)
store i32 %27, i32* %5
%28 = load i32, i32* %4
%29 = load i32, i32* @LOCK_EX
%30 = call i32 @flock(i32 %28, i32 %29)
store i32 %30, i32* %6
%31 = load i32, i32* %6
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %34
33: ; preds = %24
br label %44
34: ; preds = %24
%35 = load i32, i32* %4
%36 = call i32 @write(i32 %35, i32* %5, i32 4)
store i32 %36, i32* %7
%37 = load i32, i32* %4
%38 = load i32, i32* @LOCK_UN
%39 = call i32 @flock(i32 %37, i32 %38)
%40 = load i32, i32* %7
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %43
42: ; preds = %34
br label %43
43: ; preds = %42, %34
br label %44
44: ; preds = %23, %33, %43, %1
ret void
}

LLVM IR (-O2):
@.str = [35 x i8] c"CCTOOLS_RESOURCE_MONITOR_PIDS_FILE\00"
@O_WRONLY = global i32 0
@O_APPEND = global i32 0
@O_CREAT = global i32 0
@O_DSYNC = global i32 0
@LOCK_EX = global i32 0
@LOCK_UN = global i32 0
@errno = global i32 0
@stderr = global i32 0
define void @write_to_file_of_pids(i64 %0) {
%2 = alloca i32
%3 = tail call i8* @getenv(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i8* %3, null
br i1 %4, label %27, label %5
5: ; preds = %1
%6 = load i32, i32* @O_WRONLY
%7 = load i32, i32* @O_APPEND
%8 = or i32 %7, %6
%9 = load i32, i32* @O_CREAT
%10 = or i32 %8, %9
%11 = load i32, i32* @O_DSYNC
%12 = or i32 %10, %11
%13 = tail call i32 @open(i8* %3, i32 %12, i32 432)
%14 = icmp eq i32 %13, -1
br i1 %14, label %27, label %15
15: ; preds = %5
%16 = bitcast i32* %2 to i8*
%17 = trunc i64 %0 to i32
%18 = tail call i32 @htonl(i32 %17)
store i32 %18, i32* %2
%19 = load i32, i32* @LOCK_EX
%20 = tail call i32 @flock(i32 %13, i32 %19)
%21 = icmp eq i32 %20, -1
br i1 %21, label %26, label %22
22: ; preds = %15
%23 = call i32 @write(i32 %13, i32* %2, i32 4)
%24 = load i32, i32* @LOCK_UN
%25 = tail call i32 @flock(i32 %13, i32 %24)
br label %26
26: ; preds = %15, %22
br label %27
27: ; preds = %26, %5, %1
ret void
}