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

/* Variables and functions */
int O_CREAT;
int O_TRUNC;
int O_WRONLY;
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int /*<<< orphan*/ close(int);
int getpid();
int open(char const *, int, int);
int snprintf(char *, int, char *, int);
int write(int, char *, int);
int lock_create(const char *lockfile) {
  int fd;
  int len = 0;
  char pidb[21];
  fd =
      open(lockfile, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IRGRP | S_IROTH);
  if (fd >= 0) {
    len = snprintf(pidb, sizeof(pidb), "%d", getpid());
    len = write(fd, pidb, len);
    close(fd);
  }
  return len;
}


x86 assembly (clang, -O0):
<lock_create>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e42(%rip),%rax
mov    (%rax),%esi
mov    0x2e81(%rip),%rax
or     (%rax),%esi
mov    0x2e70(%rip),%rax
or     (%rax),%esi
mov    0x2e37(%rip),%rax
mov    (%rax),%edx
mov    0x2e3e(%rip),%rax
or     (%rax),%edx
mov    0x2e2d(%rip),%rax
or     (%rax),%edx
call   1070 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jl     11f2 <lock_create+0xa2>
lea    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x38(%rbp),%rdi
mov    %eax,%ecx
mov    $0x15,%esi
lea    0xe30(%rip),%rdx
call   1050 <snprintf@plt>
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    -0x10(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
mov    -0x10(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lock_create>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    0x2e53(%rip),%rax
mov    0x2e94(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e81(%rip),%rax
or     (%rax),%esi
mov    0x2e48(%rip),%rax
mov    0x2e51(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e3e(%rip),%rax
or     (%rax),%edx
call   1070 <open@plt>
test   %eax,%eax
js     11ce <lock_create+0x7e>
mov    %eax,%ebx
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe5b(%rip),%rdx
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    $0x15,%esi
mov    %eax,%ecx
call   1050 <snprintf@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %eax,%ebp
mov    %ebx,%edi
call   1060 <close@plt>
jmp    11d0 <lock_create+0x80>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@.str = [3 x i8] c"%d\00"
define i32 @lock_create(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca [21 x i8]
store i8* %0, i8** %2
store i32 0, i32* %4
%6 = load i8*, i8** %2
%7 = load i32, i32* @O_WRONLY
%8 = load i32, i32* @O_CREAT
%9 = or i32 %7, %8
%10 = load i32, i32* @O_TRUNC
%11 = or i32 %9, %10
%12 = load i32, i32* @S_IRUSR
%13 = load i32, i32* @S_IRGRP
%14 = or i32 %12, %13
%15 = load i32, i32* @S_IROTH
%16 = or i32 %14, %15
%17 = call i32 @open(i8* %6, i32 %11, i32 %16)
store i32 %17, i32* %3
%18 = load i32, i32* %3
%19 = icmp sge i32 %18, 0
br i1 %19, label %20, label %30
20: ; preds = %1
%21 = getelementptr inbounds [21 x i8], [21 x i8]* %5, i64 0, i64 0
%22 = call i32 (...) @getpid()
%23 = call i32 @snprintf(i8* %21, i32 21, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %22)
store i32 %23, i32* %4
%24 = load i32, i32* %3
%25 = getelementptr inbounds [21 x i8], [21 x i8]* %5, i64 0, i64 0
%26 = load i32, i32* %4
%27 = call i32 @write(i32 %24, i8* %25, i32 %26)
store i32 %27, i32* %4
%28 = load i32, i32* %3
%29 = call i32 @close(i32 %28)
br label %30
30: ; preds = %20, %1
%31 = load i32, i32* %4
ret i32 %31
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@.str = [3 x i8] c"%d\00"
define i32 @lock_create(i8* nocapture %0) {
%2 = alloca [21 x i8]
%3 = getelementptr inbounds [21 x i8], [21 x i8]* %2, i64 0, i64 0
%4 = load i32, i32* @O_WRONLY
%5 = load i32, i32* @O_CREAT
%6 = or i32 %5, %4
%7 = load i32, i32* @O_TRUNC
%8 = or i32 %6, %7
%9 = load i32, i32* @S_IRUSR
%10 = load i32, i32* @S_IRGRP
%11 = or i32 %10, %9
%12 = load i32, i32* @S_IROTH
%13 = or i32 %11, %12
%14 = tail call i32 @open(i8* %0, i32 %8, i32 %13)
%15 = icmp sgt i32 %14, -1
br i1 %15, label %16, label %21
16: ; preds = %1
%17 = tail call i32 (...) @getpid()
%18 = call i32 @snprintf(i8* %3, i32 21, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %17)
%19 = call i32 @write(i32 %14, i8* %3, i32 %18)
%20 = call i32 @close(i32 %14)
br label %21
21: ; preds = %16, %1
%22 = phi i32 [ %19, %16 ], [ 0, %1 ]
ret i32 %22
}