#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;

/* Variables and functions */
int /*<<< orphan*/ MySAR_macro_print_msg(char *, int, int, char *, ...);
int O_CREAT;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int getpid();
scalar_t__ link(char *, char *);
int open(char *, int, int);
int sprintf(char *, char *, ...);
int /*<<< orphan*/ unlink(char *);
int write(int, char *, int);
int MySAR_create_lock(char *file) {
  char tmp_file[255], str[20];
  int fd;
  pid_t pid;
  pid = getpid();
  MySAR_macro_print_msg(
      "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
      "2022-01-22/02-19-57/repos/coffnix/mysar-ng/refs/heads/master/bin/"
      "mysar-binary-importer/src/lock.c",
      42, 0x04, "Creating Lock file for PID %d", pid);
  ;
  sprintf(tmp_file, "%s_%d_tmp\n", file, pid);
  if ((fd = open(tmp_file, O_WRONLY | O_CREAT | O_TRUNC, 0644)) < 0) {
    MySAR_macro_print_msg(
        "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
        "2022-01-22/02-19-57/repos/coffnix/mysar-ng/refs/heads/master/bin/"
        "mysar-binary-importer/src/lock.c",
        48, 0x01, "Can't create temp lock file %s", file);
    ;
    MySAR_macro_print_msg(
        "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
        "2022-01-22/02-19-57/repos/coffnix/mysar-ng/refs/heads/master/bin/"
        "mysar-binary-importer/src/lock.c",
        49, 0x02, "No permission to write file!\n");
    ;
  }
  pid = sprintf(str, "%d\n", pid);
  if (write(fd, str, pid) == pid) {
    if (link(tmp_file, file) < 0) {
      MySAR_macro_print_msg(
          "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
          "2022-01-22/02-19-57/repos/coffnix/mysar-ng/refs/heads/master/bin/"
          "mysar-binary-importer/src/lock.c",
          58, 0x02, "Can't link temp lock file %s", tmp_file);
      ;
    }
  } else {
    MySAR_macro_print_msg(
        "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
        "2022-01-22/02-19-57/repos/coffnix/mysar-ng/refs/heads/master/bin/"
        "mysar-binary-importer/src/lock.c",
        62, 0x02, "Can't write to %s", tmp_file);
    ;
  }
  close(fd);
  unlink(tmp_file);
  return 1;
}


x86 assembly (clang, -O0):
<MySAR_create_lock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x0,%al
call   1050 <getpid@plt>
mov    %eax,-0x138(%rbp)
mov    -0x138(%rbp),%r8d
lea    0xe56(%rip),%rdi
mov    $0x2a,%esi
mov    $0x4,%edx
lea    0xee7(%rip),%rcx
mov    $0x0,%al
call   1080 <MySAR_macro_print_msg@plt>
lea    -0x110(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    -0x138(%rbp),%ecx
lea    0xee6(%rip),%rsi
mov    $0x0,%al
call   1090 <sprintf@plt>
lea    -0x110(%rbp),%rdi
mov    0x2dd9(%rip),%rax
mov    (%rax),%esi
mov    0x2e00(%rip),%rax
or     (%rax),%esi
mov    0x2def(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   1070 <open@plt>
mov    %eax,-0x134(%rbp)
cmp    $0x0,%eax
jge    125e <MySAR_create_lock+0xde>
mov    -0x8(%rbp),%r8
lea    0xdd9(%rip),%rdi
mov    $0x30,%esi
mov    $0x1,%edx
lea    0xe93(%rip),%rcx
mov    $0x0,%al
call   1080 <MySAR_macro_print_msg@plt>
lea    0xdba(%rip),%rdi
mov    $0x31,%esi
mov    $0x2,%edx
lea    0xe93(%rip),%rcx
mov    $0x0,%al
call   1080 <MySAR_macro_print_msg@plt>
lea    -0x130(%rbp),%rdi
mov    -0x138(%rbp),%edx
lea    0xe96(%rip),%rsi
mov    $0x0,%al
call   1090 <sprintf@plt>
mov    %eax,-0x138(%rbp)
mov    -0x134(%rbp),%edi
lea    -0x130(%rbp),%rsi
mov    -0x138(%rbp),%edx
call   1040 <write@plt>
cmp    -0x138(%rbp),%eax
jne    12e8 <MySAR_create_lock+0x168>
lea    -0x110(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   10a0 <link@plt>
cmp    $0x0,%rax
jge    12e3 <MySAR_create_lock+0x163>
lea    -0x110(%rbp),%r8
lea    0xd35(%rip),%rdi
mov    $0x3a,%esi
mov    $0x2,%edx
lea    0xe30(%rip),%rcx
mov    $0x0,%al
call   1080 <MySAR_macro_print_msg@plt>
jmp    130e <MySAR_create_lock+0x18e>
lea    -0x110(%rbp),%r8
lea    0xd0a(%rip),%rdi
mov    $0x3e,%esi
mov    $0x2,%edx
lea    0xe22(%rip),%rcx
mov    $0x0,%al
call   1080 <MySAR_macro_print_msg@plt>
mov    -0x134(%rbp),%edi
call   1060 <close@plt>
lea    -0x110(%rbp),%rdi
call   1030 <unlink@plt>
mov    $0x1,%eax
add    $0x140,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<MySAR_create_lock>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x120,%rsp
mov    %rdi,%r14
xor    %eax,%eax
call   1050 <getpid@plt>
mov    %eax,%ebx
lea    0xe5e(%rip),%rdi
lea    0xef9(%rip),%rcx
mov    $0x2a,%esi
mov    $0x4,%edx
mov    %eax,%r8d
xor    %eax,%eax
call   1080 <MySAR_macro_print_msg@plt>
lea    0xefc(%rip),%rsi
lea    0x20(%rsp),%rbp
mov    %rbp,%rdi
mov    %r14,%rdx
mov    %ebx,%ecx
xor    %eax,%eax
call   1090 <sprintf@plt>
mov    0x2de9(%rip),%rax
mov    0x2e12(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2dff(%rip),%rax
or     (%rax),%esi
mov    %rbp,%rdi
mov    $0x1a4,%edx
call   1070 <open@plt>
mov    %eax,%r12d
test   %eax,%eax
jns    1247 <MySAR_create_lock+0xc7>
lea    0xdf2(%rip),%r15
lea    0xeb6(%rip),%rcx
mov    %r15,%rdi
mov    $0x30,%esi
mov    $0x1,%edx
mov    %r14,%r8
xor    %eax,%eax
call   1080 <MySAR_macro_print_msg@plt>
lea    0xeb7(%rip),%rcx
mov    %r15,%rdi
mov    $0x31,%esi
mov    $0x2,%edx
xor    %eax,%eax
call   1080 <MySAR_macro_print_msg@plt>
lea    0xeba(%rip),%rsi
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    %ebx,%edx
xor    %eax,%eax
call   1090 <sprintf@plt>
mov    %eax,%ebx
mov    %r12d,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1040 <write@plt>
cmp    %ebx,%eax
jne    129c <MySAR_create_lock+0x11c>
lea    0x20(%rsp),%rdi
mov    %r14,%rsi
call   10a0 <link@plt>
test   %rax,%rax
jns    12c0 <MySAR_create_lock+0x140>
lea    0xd77(%rip),%rdi
lea    0xe7c(%rip),%rcx
lea    0x20(%rsp),%r8
mov    $0x3a,%esi
jmp    12b4 <MySAR_create_lock+0x134>
lea    0xd5d(%rip),%rdi
lea    0xe7f(%rip),%rcx
lea    0x20(%rsp),%r8
mov    $0x3e,%esi
mov    $0x2,%edx
xor    %eax,%eax
call   1080 <MySAR_macro_print_msg@plt>
mov    %r12d,%edi
call   1060 <close@plt>
lea    0x20(%rsp),%rdi
call   1030 <unlink@plt>
mov    $0x1,%eax
add    $0x120,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [162 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/coffnix/mysar-ng/refs/heads/master/bin/mysar-binary-importer/src/lock.c\00"
@.str.1 = [30 x i8] c"Creating Lock file for PID %d\00"
@.str.2 = [11 x i8] c"%s_%d_tmp\0A\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.3 = [31 x i8] c"Can't create temp lock file %s\00"
@.str.4 = [30 x i8] c"No permission to write file!\0A\00"
@.str.5 = [4 x i8] c"%d\0A\00"
@.str.6 = [29 x i8] c"Can't link temp lock file %s\00"
@.str.7 = [18 x i8] c"Can't write to %s\00"
define i32 @MySAR_create_lock(i8* %0) {
%2 = alloca i8*
%3 = alloca [255 x i8]
%4 = alloca [20 x i8]
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %2
%7 = call i32 (...) @getpid()
store i32 %7, i32* %6
%8 = load i32, i32* %6
%9 = call i32 (i8*, i32, i32, i8*, ...) @MySAR_macro_print_msg(i8* getelementptr inbounds ([162 x i8], [162 x i8]* @.str, i64 0, i64 0), i32 42, i32 4, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i32 %8)
%10 = getelementptr inbounds [255 x i8], [255 x i8]* %3, i64 0, i64 0
%11 = load i8*, i8** %2
%12 = load i32, i32* %6
%13 = call i32 (i8*, i8*, ...) @sprintf(i8* %10, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8* %11, i32 %12)
%14 = getelementptr inbounds [255 x i8], [255 x i8]* %3, i64 0, i64 0
%15 = load i32, i32* @O_WRONLY
%16 = load i32, i32* @O_CREAT
%17 = or i32 %15, %16
%18 = load i32, i32* @O_TRUNC
%19 = or i32 %17, %18
%20 = call i32 @open(i8* %14, i32 %19, i32 420)
store i32 %20, i32* %5
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %26
22: ; preds = %1
%23 = load i8*, i8** %2
%24 = call i32 (i8*, i32, i32, i8*, ...) @MySAR_macro_print_msg(i8* getelementptr inbounds ([162 x i8], [162 x i8]* @.str, i64 0, i64 0), i32 48, i32 1, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i8* %23)
%25 = call i32 (i8*, i32, i32, i8*, ...) @MySAR_macro_print_msg(i8* getelementptr inbounds ([162 x i8], [162 x i8]* @.str, i64 0, i64 0), i32 49, i32 2, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0))
br label %26
26: ; preds = %22, %1
%27 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
%28 = load i32, i32* %6
%29 = call i32 (i8*, i8*, ...) @sprintf(i8* %27, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i32 %28)
store i32 %29, i32* %6
%30 = load i32, i32* %5
%31 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
%32 = load i32, i32* %6
%33 = call i32 @write(i32 %30, i8* %31, i32 %32)
%34 = load i32, i32* %6
%35 = icmp eq i32 %33, %34
br i1 %35, label %36, label %45
36: ; preds = %26
%37 = getelementptr inbounds [255 x i8], [255 x i8]* %3, i64 0, i64 0
%38 = load i8*, i8** %2
%39 = call i64 @link(i8* %37, i8* %38)
%40 = icmp slt i64 %39, 0
br i1 %40, label %41, label %44
41: ; preds = %36
%42 = getelementptr inbounds [255 x i8], [255 x i8]* %3, i64 0, i64 0
%43 = call i32 (i8*, i32, i32, i8*, ...) @MySAR_macro_print_msg(i8* getelementptr inbounds ([162 x i8], [162 x i8]* @.str, i64 0, i64 0), i32 58, i32 2, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.6, i64 0, i64 0), i8* %42)
br label %44
44: ; preds = %41, %36
br label %48
45: ; preds = %26
%46 = getelementptr inbounds [255 x i8], [255 x i8]* %3, i64 0, i64 0
%47 = call i32 (i8*, i32, i32, i8*, ...) @MySAR_macro_print_msg(i8* getelementptr inbounds ([162 x i8], [162 x i8]* @.str, i64 0, i64 0), i32 62, i32 2, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.7, i64 0, i64 0), i8* %46)
br label %48
48: ; preds = %45, %44
%49 = load i32, i32* %5
%50 = call i32 @close(i32 %49)
%51 = getelementptr inbounds [255 x i8], [255 x i8]* %3, i64 0, i64 0
%52 = call i32 @unlink(i8* %51)
ret i32 1
}

LLVM IR (-O2):
@.str = [162 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/coffnix/mysar-ng/refs/heads/master/bin/mysar-binary-importer/src/lock.c\00"
@.str.1 = [30 x i8] c"Creating Lock file for PID %d\00"
@.str.2 = [11 x i8] c"%s_%d_tmp\0A\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.3 = [31 x i8] c"Can't create temp lock file %s\00"
@.str.4 = [30 x i8] c"No permission to write file!\0A\00"
@.str.5 = [4 x i8] c"%d\0A\00"
@.str.6 = [29 x i8] c"Can't link temp lock file %s\00"
@.str.7 = [18 x i8] c"Can't write to %s\00"
define i32 @MySAR_create_lock(i8* %0) {
%2 = alloca [255 x i8]
%3 = alloca [20 x i8]
%4 = getelementptr inbounds [255 x i8], [255 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds [20 x i8], [20 x i8]* %3, i64 0, i64 0
%6 = tail call i32 (...) @getpid()
%7 = tail call i32 (i8*, i32, i32, i8*, ...) @MySAR_macro_print_msg(i8* getelementptr inbounds ([162 x i8], [162 x i8]* @.str, i64 0, i64 0), i32 42, i32 4, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i32 %6)
%8 = call i32 (i8*, i8*, ...) @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8* %0, i32 %6)
%9 = load i32, i32* @O_WRONLY
%10 = load i32, i32* @O_CREAT
%11 = or i32 %10, %9
%12 = load i32, i32* @O_TRUNC
%13 = or i32 %11, %12
%14 = call i32 @open(i8* %4, i32 %13, i32 420)
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %1
%17 = tail call i32 (i8*, i32, i32, i8*, ...) @MySAR_macro_print_msg(i8* getelementptr inbounds ([162 x i8], [162 x i8]* @.str, i64 0, i64 0), i32 48, i32 1, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i8* %0)
%18 = tail call i32 (i8*, i32, i32, i8*, ...) @MySAR_macro_print_msg(i8* getelementptr inbounds ([162 x i8], [162 x i8]* @.str, i64 0, i64 0), i32 49, i32 2, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0))
br label %19
19: ; preds = %16, %1
%20 = call i32 (i8*, i8*, ...) @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i32 %6)
%21 = call i32 @write(i32 %14, i8* %5, i32 %20)
%22 = icmp eq i32 %21, %20
br i1 %22, label %23, label %28
23: ; preds = %19
%24 = call i64 @link(i8* %4, i8* %0)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %30
26: ; preds = %23
%27 = call i32 (i8*, i32, i32, i8*, ...) @MySAR_macro_print_msg(i8* getelementptr inbounds ([162 x i8], [162 x i8]* @.str, i64 0, i64 0), i32 58, i32 2, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.6, i64 0, i64 0), i8* %4)
br label %30
28: ; preds = %19
%29 = call i32 (i8*, i32, i32, i8*, ...) @MySAR_macro_print_msg(i8* getelementptr inbounds ([162 x i8], [162 x i8]* @.str, i64 0, i64 0), i32 62, i32 2, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.7, i64 0, i64 0), i8* %4)
br label %30
30: ; preds = %23, %26, %28
%31 = call i32 @close(i32 %14)
%32 = call i32 @unlink(i8* %4)
ret i32 1
}