#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*/ pid_str;

/* Variables and functions */
int /*<<< orphan*/ F_TLOCK;
int O_CREAT;
int O_RDWR;
int O_TRUNC;
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_IWGRP;
int S_IWOTH;
int S_IWUSR;
int /*<<< orphan*/ close(int);
scalar_t__ getpid();
scalar_t__ lockf(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int open(char *, int, int);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ strlen(char *);
scalar_t__ write(int, char *, int /*<<< orphan*/);
int create_pid_file() {
  char pid_str[10];
  int lock_file;
  lock_file = open("/tmp/stressgen.lock", O_RDWR | O_CREAT | O_TRUNC,
                   S_IRUSR | S_IWUSR | S_IROTH | S_IWOTH | S_IRGRP | S_IWGRP);
  if (0 == lockf(lock_file, F_TLOCK, 0)) {
    memset(pid_str, 0, sizeof(pid_str));
    sprintf(pid_str, "%d", (int)getpid());
    if (0 < write(lock_file, pid_str, strlen(pid_str)))
      return lock_file;
  }
  close(lock_file);
  return 0;
}


x86 assembly (clang, -O0):
<create_pid_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    0x2e41(%rip),%rax
mov    (%rax),%esi
mov    0x2e60(%rip),%rax
or     (%rax),%esi
mov    0x2e4f(%rip),%rax
or     (%rax),%esi
mov    0x2df6(%rip),%rax
mov    (%rax),%edx
mov    0x2e0d(%rip),%rax
or     (%rax),%edx
mov    0x2dec(%rip),%rax
or     (%rax),%edx
mov    0x2dcb(%rip),%rax
or     (%rax),%edx
mov    0x2de2(%rip),%rax
or     (%rax),%edx
mov    0x2df1(%rip),%rax
or     (%rax),%edx
lea    0xe20(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2dee(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <lockf@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    127d <create_pid_file+0xfd>
lea    -0xe(%rbp),%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1070 <memset@plt>
lea    -0xe(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x30(%rbp),%rdi
mov    %eax,%edx
lea    0xddf(%rip),%rsi
call   10a0 <sprintf@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x24(%rbp)
lea    -0xe(%rbp),%rax
mov    %rax,-0x20(%rbp)
lea    -0xe(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jge    1278 <create_pid_file+0xf8>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    128c <create_pid_file+0x10c>
jmp    127d <create_pid_file+0xfd>
mov    -0x14(%rbp),%edi
call   1080 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_pid_file>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2e41(%rip),%rax
mov    0x2e62(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e4f(%rip),%rax
or     (%rax),%esi
mov    0x2df6(%rip),%rax
mov    0x2e0f(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2dec(%rip),%rax
or     (%rax),%edx
mov    0x2dcb(%rip),%rax
or     (%rax),%edx
mov    0x2de2(%rip),%rax
or     (%rax),%edx
mov    0x2df1(%rip),%rax
or     (%rax),%edx
lea    0xe20(%rip),%rdi
call   1090 <open@plt>
mov    %eax,%ebp
mov    0x2df2(%rip),%rax
mov    (%rax),%esi
xor    %ebx,%ebx
mov    %ebp,%edi
xor    %edx,%edx
call   1060 <lockf@plt>
test   %rax,%rax
je     1209 <create_pid_file+0x89>
mov    %ebp,%edi
call   1080 <close@plt>
jmp    1250 <create_pid_file+0xd0>
lea    0x6(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1070 <memset@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xde9(%rip),%rsi
mov    %r14,%rdi
mov    %eax,%edx
call   10a0 <sprintf@plt>
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1030 <write@plt>
test   %rax,%rax
jle    1200 <create_pid_file+0x80>
mov    %ebp,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [20 x i8] c"/tmp/stressgen.lock\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IROTH = global i32 0
@S_IWOTH = global i32 0
@S_IRGRP = global i32 0
@S_IWGRP = global i32 0
@F_TLOCK = global i32 0
@.str.1 = [3 x i8] c"%d\00"
define i32 @create_pid_file() {
%1 = alloca i32
%2 = alloca [10 x i8]
%3 = alloca i32
%4 = load i32, i32* @O_RDWR
%5 = load i32, i32* @O_CREAT
%6 = or i32 %4, %5
%7 = load i32, i32* @O_TRUNC
%8 = or i32 %6, %7
%9 = load i32, i32* @S_IRUSR
%10 = load i32, i32* @S_IWUSR
%11 = or i32 %9, %10
%12 = load i32, i32* @S_IROTH
%13 = or i32 %11, %12
%14 = load i32, i32* @S_IWOTH
%15 = or i32 %13, %14
%16 = load i32, i32* @S_IRGRP
%17 = or i32 %15, %16
%18 = load i32, i32* @S_IWGRP
%19 = or i32 %17, %18
%20 = call i32 @open(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %8, i32 %19)
store i32 %20, i32* %3
%21 = load i32, i32* %3
%22 = load i32, i32* @F_TLOCK
%23 = call i64 @lockf(i32 %21, i32 %22, i32 0)
%24 = icmp eq i64 0, %23
br i1 %24, label %25, label %41
25: ; preds = %0
%26 = getelementptr inbounds [10 x i8], [10 x i8]* %2, i64 0, i64 0
%27 = call i32 @memset(i8* %26, i32 0, i32 10)
%28 = getelementptr inbounds [10 x i8], [10 x i8]* %2, i64 0, i64 0
%29 = call i64 (...) @getpid()
%30 = trunc i64 %29 to i32
%31 = call i32 @sprintf(i8* %28, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %30)
%32 = load i32, i32* %3
%33 = getelementptr inbounds [10 x i8], [10 x i8]* %2, i64 0, i64 0
%34 = getelementptr inbounds [10 x i8], [10 x i8]* %2, i64 0, i64 0
%35 = call i32 @strlen(i8* %34)
%36 = call i64 @write(i32 %32, i8* %33, i32 %35)
%37 = icmp slt i64 0, %36
br i1 %37, label %38, label %40
38: ; preds = %25
%39 = load i32, i32* %3
store i32 %39, i32* %1
br label %44
40: ; preds = %25
br label %41
41: ; preds = %40, %0
%42 = load i32, i32* %3
%43 = call i32 @close(i32 %42)
store i32 0, i32* %1
br label %44
44: ; preds = %41, %38
%45 = load i32, i32* %1
ret i32 %45
}

LLVM IR (-O2):
@.str = [20 x i8] c"/tmp/stressgen.lock\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IROTH = global i32 0
@S_IWOTH = global i32 0
@S_IRGRP = global i32 0
@S_IWGRP = global i32 0
@F_TLOCK = global i32 0
@.str.1 = [3 x i8] c"%d\00"
define i32 @create_pid_file() {
%1 = alloca [10 x i8]
%2 = getelementptr inbounds [10 x i8], [10 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @O_RDWR
%4 = load i32, i32* @O_CREAT
%5 = or i32 %4, %3
%6 = load i32, i32* @O_TRUNC
%7 = or i32 %5, %6
%8 = load i32, i32* @S_IRUSR
%9 = load i32, i32* @S_IWUSR
%10 = or i32 %9, %8
%11 = load i32, i32* @S_IROTH
%12 = or i32 %10, %11
%13 = load i32, i32* @S_IWOTH
%14 = or i32 %12, %13
%15 = load i32, i32* @S_IRGRP
%16 = or i32 %14, %15
%17 = load i32, i32* @S_IWGRP
%18 = or i32 %16, %17
%19 = tail call i32 @open(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %7, i32 %18)
%20 = load i32, i32* @F_TLOCK
%21 = tail call i64 @lockf(i32 %19, i32 %20, i32 0)
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %31
23: ; preds = %0
%24 = call i32 @memset(i8* %2, i32 0, i32 10)
%25 = call i64 (...) @getpid()
%26 = trunc i64 %25 to i32
%27 = call i32 @sprintf(i8* %2, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %26)
%28 = call i32 @strlen(i8* %2)
%29 = call i64 @write(i32 %19, i8* %2, i32 %28)
%30 = icmp sgt i64 %29, 0
br i1 %30, label %33, label %31
31: ; preds = %23, %0
%32 = call i32 @close(i32 %19)
br label %33
33: ; preds = %23, %31
%34 = phi i32 [ 0, %31 ], [ %19, %23 ]
ret i32 %34
}