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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
scalar_t__ getenv(char *);
int getpid();
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ stderr;
int CreateLock(char *name, char *progname) {
  FILE *lock_fd;
  int retu = -1;
  pid_t pid;
  char *user;
  pid = getpid();
  user = (char *)getenv("LOGNAME");
  printf("writing new LOCKFILE %s \n", name);
  if ((lock_fd = fopen(name, "w")) != NULL) {
    fprintf(lock_fd, "%.5d %s %s", pid, progname, user);
    fclose(lock_fd);
    retu = 0;
  } else
    fprintf(stderr, "Can't write LOCKFILE %s\n", name);
  return retu;
}


x86 assembly (clang, -O0):
<CreateLock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0x20(%rbp)
lea    0xe78(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rsi
lea    0xe6c(%rip),%rdi
call   1060 <printf@plt>
mov    -0x8(%rbp),%rdi
lea    0xe76(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     11f3 <CreateLock+0x93>
mov    -0x18(%rbp),%rax
mov    %eax,%edi
mov    -0x20(%rbp),%edx
mov    -0x10(%rbp),%rcx
mov    -0x28(%rbp),%r8
lea    0xe4d(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0x0,-0x1c(%rbp)
jmp    120e <CreateLock+0xae>
mov    0x2de6(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe28(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x1c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<CreateLock>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
xor    %r13d,%r13d
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%r15d
lea    0xe7b(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%r12
lea    0xe74(%rip),%rdi
mov    %rbx,%rsi
call   1060 <printf@plt>
lea    0xe7f(%rip),%rsi
mov    %rbx,%rdi
call   1080 <fopen@plt>
test   %rax,%rax
je     11d6 <CreateLock+0x76>
mov    %rax,%rbp
lea    0xe6a(%rip),%rsi
mov    %ebp,%edi
mov    %r15d,%edx
mov    %r14,%rcx
mov    %r12,%r8
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %rbp,%rdi
call   1050 <fclose@plt>
jmp    11f6 <CreateLock+0x96>
mov    0x2e03(%rip),%rax
mov    (%rax),%edi
lea    0xe49(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0xffffffff,%r13d
mov    %r13d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [8 x i8] c"LOGNAME\00"
@.str.1 = [26 x i8] c"writing new LOCKFILE %s \0A\00"
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [11 x i8] c"%.5d %s %s\00"
@stderr = global i32 0
@.str.4 = [25 x i8] c"Can't write LOCKFILE %s\0A\00"
define i32 @CreateLock(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
store i8* %0, i8** %3
store i8* %1, i8** %4
store i32 -1, i32* %6
%9 = call i32 (...) @getpid()
store i32 %9, i32* %7
%10 = call i64 @getenv(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%11 = inttoptr i64 %10 to i8*
store i8* %11, i8** %8
%12 = load i8*, i8** %3
%13 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %12)
%14 = load i8*, i8** %3
%15 = call i32* @fopen(i8* %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %15, i32** %5
%16 = icmp ne i32* %15, null
br i1 %16, label %17, label %26
17: ; preds = %2
%18 = load i32*, i32** %5
%19 = ptrtoint i32* %18 to i32
%20 = load i32, i32* %7
%21 = load i8*, i8** %4
%22 = load i8*, i8** %8
%23 = call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i32 %20, i8* %21, i8* %22)
%24 = load i32*, i32** %5
%25 = call i32 @fclose(i32* %24)
store i32 0, i32* %6
br label %30
26: ; preds = %2
%27 = load i32, i32* @stderr
%28 = load i8*, i8** %3
%29 = call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i8* %28)
br label %30
30: ; preds = %26, %17
%31 = load i32, i32* %6
ret i32 %31
}

LLVM IR (-O2):
@.str = [8 x i8] c"LOGNAME\00"
@.str.1 = [26 x i8] c"writing new LOCKFILE %s \0A\00"
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [11 x i8] c"%.5d %s %s\00"
@stderr = global i32 0
@.str.4 = [25 x i8] c"Can't write LOCKFILE %s\0A\00"
define i32 @CreateLock(i8* %0, i8* %1) {
%3 = tail call i32 (...) @getpid()
%4 = tail call i64 @getenv(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%5 = tail call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%6 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %14, label %8
8: ; preds = %2
%9 = inttoptr i64 %4 to i8*
%10 = ptrtoint i32* %6 to i64
%11 = trunc i64 %10 to i32
%12 = tail call i32 (i32, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i32 %3, i8* %1, i8* %9)
%13 = tail call i32 @fclose(i32* %6)
br label %17
14: ; preds = %2
%15 = load i32, i32* @stderr
%16 = tail call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i8* %0)
br label %17
17: ; preds = %14, %8
%18 = phi i32 [ 0, %8 ], [ -1, %14 ]
ret i32 %18
}