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

/* Variables and functions */
int /*<<< orphan*/ F_WRLCK;
int WEXITSTATUS(int);
int /*<<< orphan*/ WIFEXITED(int);
scalar_t__ errno;
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ makelock(int /*<<< orphan*/ *, int /*<<< orphan*/, char *);
int /*<<< orphan*/ ohshit(char *);
int /*<<< orphan*/ ohshite(char *, char *, ...);
int system(char *);
int /*<<< orphan*/ unlock(int /*<<< orphan*/ *, char *);
int main(int argc, char **argv) {
  FILE *file;
  int code;
  if (argc != 3)
    ohshit("lockfile needs 2 args");
  file = fopen(argv[1], "r+");
  if (!file)
    ohshite("lockfile: open %s", argv[1]);
  makelock(file, F_WRLCK, argv[1]);
  errno = 0;
  code = system(argv[2]);
  if (!WIFEXITED(code))
    ohshite("lockfile: run %s gave %d", argv[2], code);
  unlock(file, argv[1]);
  return WEXITSTATUS(code);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     11ac <main+0x2c>
lea    0xe59(%rip),%rdi
call   1050 <ohshit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe5b(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11e5 <main+0x65>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe3b(%rip),%rdi
mov    $0x0,%al
call   1060 <ohshite@plt>
mov    -0x18(%rbp),%rdi
mov    0x2de8(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
call   10a0 <makelock@plt>
mov    0x2dda(%rip),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1040 <system@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
call   1090 <WIFEXITED@plt>
cmp    $0x0,%eax
jne    1247 <main+0xc7>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
mov    -0x1c(%rbp),%edx
lea    0xdeb(%rip),%rdi
mov    $0x0,%al
call   1060 <ohshite@plt>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
call   1030 <unlock@plt>
mov    -0x1c(%rbp),%edi
call   1070 <WEXITSTATUS@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%rbx
cmp    $0x3,%edi
je     1198 <main+0x18>
lea    0xe6d(%rip),%rdi
call   1050 <ohshit@plt>
mov    0x8(%rbx),%rdi
lea    0xe73(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,%r14
test   %rax,%rax
jne    11c2 <main+0x42>
mov    0x8(%rbx),%rsi
lea    0xe5e(%rip),%rdi
xor    %eax,%eax
call   1060 <ohshite@plt>
mov    0x2e0f(%rip),%rax
mov    (%rax),%esi
mov    0x8(%rbx),%rdx
mov    %r14,%rdi
call   10a0 <makelock@plt>
mov    0x2e02(%rip),%rax
movq   $0x0,(%rax)
mov    0x10(%rbx),%rdi
call   1040 <system@plt>
mov    %eax,%ebp
mov    %eax,%edi
call   1090 <WIFEXITED@plt>
test   %eax,%eax
jne    120f <main+0x8f>
mov    0x10(%rbx),%rsi
lea    0xe25(%rip),%rdi
mov    %ebp,%edx
xor    %eax,%eax
call   1060 <ohshite@plt>
mov    0x8(%rbx),%rsi
mov    %r14,%rdi
call   1030 <unlock@plt>
mov    %ebp,%edi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1070 <WEXITSTATUS@plt>

LLVM IR (-O0):
@.str = [22 x i8] c"lockfile needs 2 args\00"
@.str.1 = [3 x i8] c"r+\00"
@.str.2 = [18 x i8] c"lockfile: open %s\00"
@F_WRLCK = global i32 0
@errno = global i64 0
@.str.3 = [25 x i8] c"lockfile: run %s gave %d\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* %4
%9 = icmp ne i32 %8, 3
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = call i32 @ohshit(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %12
12: ; preds = %10, %2
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 1
%15 = load i8*, i8** %14
%16 = call i32* @fopen(i8* %15, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %16, i32** %6
%17 = load i32*, i32** %6
%18 = icmp ne i32* %17, null
br i1 %18, label %24, label %19
19: ; preds = %12
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
%23 = call i32 (i8*, i8*, ...) @ohshite(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* %22)
br label %24
24: ; preds = %19, %12
%25 = load i32*, i32** %6
%26 = load i32, i32* @F_WRLCK
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 1
%29 = load i8*, i8** %28
%30 = call i32 @makelock(i32* %25, i32 %26, i8* %29)
store i64 0, i64* @errno
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 2
%33 = load i8*, i8** %32
%34 = call i32 @system(i8* %33)
store i32 %34, i32* %7
%35 = load i32, i32* %7
%36 = call i32 @WIFEXITED(i32 %35)
%37 = icmp ne i32 %36, 0
br i1 %37, label %44, label %38
38: ; preds = %24
%39 = load i8**, i8*** %5
%40 = getelementptr inbounds i8*, i8** %39, i64 2
%41 = load i8*, i8** %40
%42 = load i32, i32* %7
%43 = call i32 (i8*, i8*, ...) @ohshite(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i8* %41, i32 %42)
br label %44
44: ; preds = %38, %24
%45 = load i32*, i32** %6
%46 = load i8**, i8*** %5
%47 = getelementptr inbounds i8*, i8** %46, i64 1
%48 = load i8*, i8** %47
%49 = call i32 @unlock(i32* %45, i8* %48)
%50 = load i32, i32* %7
%51 = call i32 @WEXITSTATUS(i32 %50)
ret i32 %51
}

LLVM IR (-O2):
@.str = [22 x i8] c"lockfile needs 2 args\00"
@.str.1 = [3 x i8] c"r+\00"
@.str.2 = [18 x i8] c"lockfile: open %s\00"
@F_WRLCK = global i32 0
@errno = global i64 0
@.str.3 = [25 x i8] c"lockfile: run %s gave %d\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 3
br i1 %3, label %6, label %4
4: ; preds = %2
%5 = tail call i32 @ohshit(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %6
6: ; preds = %4, %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = tail call i32* @fopen(i8* %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %14
11: ; preds = %6
%12 = load i8*, i8** %7
%13 = tail call i32 (i8*, i8*, ...) @ohshite(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* %12)
br label %14
14: ; preds = %11, %6
%15 = load i32, i32* @F_WRLCK
%16 = load i8*, i8** %7
%17 = tail call i32 @makelock(i32* %9, i32 %15, i8* %16)
store i64 0, i64* @errno
%18 = getelementptr inbounds i8*, i8** %1, i64 2
%19 = load i8*, i8** %18
%20 = tail call i32 @system(i8* %19)
%21 = tail call i32 @WIFEXITED(i32 %20)
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %14
%24 = load i8*, i8** %18
%25 = tail call i32 (i8*, i8*, ...) @ohshite(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i8* %24, i32 %20)
br label %26
26: ; preds = %23, %14
%27 = load i8*, i8** %7
%28 = tail call i32 @unlock(i32* %9, i8* %27)
%29 = tail call i32 @WEXITSTATUS(i32 %20)
ret i32 %29
}