#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  char *pw_dir;
};

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ F_OK;
int O_CREAT;
int O_RDWR;
int S_IROTH;
int S_IRWXG;
int S_IRWXU;
scalar_t__ access(char *, int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
TYPE_1__ *getpwuid(int /*<<< orphan*/);
int /*<<< orphan*/ getuid();
int /*<<< orphan*/ log_error(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ log_message(char *, int);
int open(char *, int, int);
int /*<<< orphan*/ sprintf(char *, char *, char const *);
int set_lock() {
  char lock_file[1024];
  const char *home_dir = getpwuid(getuid())->pw_dir;
  sprintf(lock_file, "%s/streams.lock", home_dir);
  if (access(lock_file, F_OK) == 0) {
    log_message("Server instance already exists. If not, manually delete the "
                "streams lock file.",
                4);
    exit(EXIT_FAILURE);
  }
  if ((open(lock_file, O_RDWR | O_CREAT, S_IRWXU | S_IRWXG | S_IROTH)) == -1) {
    log_error("Could not create lock file", 1, errno);
    exit(EXIT_FAILURE);
  }
  log_message("Created lock file in home directory", 7);
}


x86 assembly (clang, -O0):
<set_lock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    $0x0,%al
call   1050 <getuid@plt>
mov    %eax,%edi
call   1040 <getpwuid@plt>
mov    (%rax),%rax
mov    %rax,-0x418(%rbp)
lea    -0x410(%rbp),%rdi
mov    -0x418(%rbp),%rdx
lea    0xe48(%rip),%rsi
call   1090 <sprintf@plt>
lea    -0x410(%rbp),%rdi
mov    0x2dd5(%rip),%rax
mov    (%rax),%esi
call   1080 <access@plt>
cmp    $0x0,%rax
jne    11fb <set_lock+0x7b>
lea    0xe2d(%rip),%rdi
mov    $0x4,%esi
call   1060 <log_message@plt>
mov    0x2dbc(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
lea    -0x410(%rbp),%rdi
mov    0x2dcf(%rip),%rax
mov    (%rax),%esi
mov    0x2de6(%rip),%rax
or     (%rax),%esi
mov    0x2dc5(%rip),%rax
mov    (%rax),%edx
mov    0x2dac(%rip),%rax
or     (%rax),%edx
mov    0x2d8b(%rip),%rax
or     (%rax),%edx
call   1070 <open@plt>
cmp    $0xffffffff,%eax
jne    1265 <set_lock+0xe5>
mov    0x2d7c(%rip),%rax
mov    (%rax),%edx
lea    0xe12(%rip),%rdi
mov    $0x1,%esi
call   1030 <log_error@plt>
mov    0x2d52(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
lea    0xe0e(%rip),%rdi
mov    $0x7,%esi
call   1060 <log_message@plt>
mov    -0x4(%rbp),%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_lock>:
push   %rbx
sub    $0x400,%rsp
xor    %eax,%eax
call   1050 <getuid@plt>
mov    %eax,%edi
call   1040 <getpwuid@plt>
mov    (%rax),%rdx
lea    0xe60(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1090 <sprintf@plt>
mov    0x2dee(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1080 <access@plt>
test   %rax,%rax
je     1214 <set_lock+0x94>
mov    0x2e10(%rip),%rax
mov    0x2e29(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e06(%rip),%rax
mov    0x2def(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2dcc(%rip),%rax
or     (%rax),%edx
mov    %rsp,%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     1227 <set_lock+0xa7>
lea    0xe78(%rip),%rdi
mov    $0x7,%esi
add    $0x400,%rsp
pop    %rbx
jmp    1060 <log_message@plt>
lea    0xdf5(%rip),%rdi
mov    $0x4,%esi
call   1060 <log_message@plt>
jmp    1241 <set_lock+0xc1>
mov    0x2d92(%rip),%rax
mov    (%rax),%edx
lea    0xe28(%rip),%rdi
mov    $0x1,%esi
call   1030 <log_error@plt>
mov    0x2d68(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.TYPE_2__ = type { i8* }
@.str = [16 x i8] c"%s/streams.lock\00"
@F_OK = global i32 0
@.str.1 = [79 x i8] c"Server instance already exists. If not, manually delete the streams lock file.\00"
@EXIT_FAILURE = global i32 0
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@S_IRWXU = global i32 0
@S_IRWXG = global i32 0
@S_IROTH = global i32 0
@.str.2 = [27 x i8] c"Could not create lock file\00"
@errno = global i32 0
@.str.3 = [36 x i8] c"Created lock file in home directory\00"
define i32 @set_lock() {
%1 = alloca i32
%2 = alloca [1024 x i8]
%3 = alloca i8*
%4 = call i32 (...) @getuid()
%5 = call %struct.TYPE_2__* @getpwuid(i32 %4)
%6 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %5, i32 0, i32 0
%7 = load i8*, i8** %6
store i8* %7, i8** %3
%8 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%9 = load i8*, i8** %3
%10 = call i32 @sprintf(i8* %8, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* %9)
%11 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%12 = load i32, i32* @F_OK
%13 = call i64 @access(i8* %11, i32 %12)
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %0
%16 = call i32 @log_message(i8* getelementptr inbounds ([79 x i8], [79 x i8]* @.str.1, i64 0, i64 0), i32 4)
%17 = load i32, i32* @EXIT_FAILURE
%18 = call i32 @exit(i32 %17)
unreachable
19: ; preds = %0
%20 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%21 = load i32, i32* @O_RDWR
%22 = load i32, i32* @O_CREAT
%23 = or i32 %21, %22
%24 = load i32, i32* @S_IRWXU
%25 = load i32, i32* @S_IRWXG
%26 = or i32 %24, %25
%27 = load i32, i32* @S_IROTH
%28 = or i32 %26, %27
%29 = call i32 @open(i8* %20, i32 %23, i32 %28)
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %36
31: ; preds = %19
%32 = load i32, i32* @errno
%33 = call i32 @log_error(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i32 1, i32 %32)
%34 = load i32, i32* @EXIT_FAILURE
%35 = call i32 @exit(i32 %34)
unreachable
36: ; preds = %19
%37 = call i32 @log_message(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0), i32 7)
%38 = load i32, i32* %1
ret i32 %38
}

LLVM IR (-O2):
%struct.TYPE_2__ = type { i8* }
@.str = [16 x i8] c"%s/streams.lock\00"
@F_OK = global i32 0
@.str.1 = [79 x i8] c"Server instance already exists. If not, manually delete the streams lock file.\00"
@EXIT_FAILURE = global i32 0
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@S_IRWXU = global i32 0
@S_IRWXG = global i32 0
@S_IROTH = global i32 0
@.str.2 = [27 x i8] c"Could not create lock file\00"
@errno = global i32 0
@.str.3 = [36 x i8] c"Created lock file in home directory\00"
define i32 @set_lock() {
%1 = alloca [1024 x i8]
%2 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%3 = tail call i32 (...) @getuid()
%4 = tail call %struct.TYPE_2__* @getpwuid(i32 %3)
%5 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %4, i64 0, i32 0
%6 = load i8*, i8** %5
%7 = call i32 @sprintf(i8* %2, i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* %6)
%8 = load i32, i32* @F_OK
%9 = call i64 @access(i8* %2, i32 %8)
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %15
11: ; preds = %0
%12 = tail call i32 @log_message(i8* getelementptr inbounds ([79 x i8], [79 x i8]* @.str.1, i64 0, i64 0), i32 4)
%13 = load i32, i32* @EXIT_FAILURE
%14 = tail call i32 @exit(i32 %13)
unreachable
15: ; preds = %0
%16 = load i32, i32* @O_RDWR
%17 = load i32, i32* @O_CREAT
%18 = or i32 %17, %16
%19 = load i32, i32* @S_IRWXU
%20 = load i32, i32* @S_IRWXG
%21 = or i32 %20, %19
%22 = load i32, i32* @S_IROTH
%23 = or i32 %21, %22
%24 = call i32 @open(i8* %2, i32 %18, i32 %23)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %31
26: ; preds = %15
%27 = load i32, i32* @errno
%28 = tail call i32 @log_error(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i32 1, i32 %27)
%29 = load i32, i32* @EXIT_FAILURE
%30 = tail call i32 @exit(i32 %29)
unreachable
31: ; preds = %15
%32 = tail call i32 @log_message(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0), i32 7)
ret i32 undef
}