#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 */
struct flock {
  int l_type;
  int l_len;
  scalar_t__ l_start;
  int /*<<< orphan*/ l_whence;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
scalar_t__ EINTR;
int F_RDLCK;
int /*<<< orphan*/ F_SETLKW;
int F_WRLCK;
int /*<<< orphan*/ SEEK_SET;
scalar_t__ errno;
int fcntl(int /*<<< orphan*/, int /*<<< orphan*/, struct flock *);
int /*<<< orphan*/ fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ ohshite(char *, char const *, char *);
void makelock(FILE *file, int type, const char *filename) {
  struct flock fl;
  for (;;) {
    fl.l_type = type;
    fl.l_whence = SEEK_SET;
    fl.l_start = 0;
    fl.l_len = 1;
    if (fcntl(fileno(file), F_SETLKW, &fl) != -1)
      return;
    if (errno != EINTR)
      ohshite("Failed to lock %s (%s)", filename,
              type == F_RDLCK   ? "read"
              : type == F_WRLCK ? "write"
                                : "?? ");
  }
}


x86 assembly (clang, -O0):
<makelock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    0x2e80(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movl   $0x1,-0x2c(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <fileno@plt>
mov    %eax,%edi
mov    0x2e72(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
je     1190 <makelock+0x60>
add    $0x40,%rsp
pop    %rbp
ret
mov    0x2e41(%rip),%rax
mov    (%rax),%rax
mov    0x2e27(%rip),%rcx
cmp    (%rcx),%rax
je     120c <makelock+0xdc>
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0xc(%rbp),%eax
mov    0x2df4(%rip),%rcx
cmp    (%rcx),%eax
jne    11d4 <makelock+0xa4>
lea    0xe4c(%rip),%rax
mov    %rax,-0x40(%rbp)
jmp    11f8 <makelock+0xc8>
mov    -0xc(%rbp),%edx
mov    0x2de2(%rip),%rax
mov    (%rax),%esi
lea    0xe3b(%rip),%rax
lea    0xe2e(%rip),%rcx
cmp    %esi,%edx
cmove  %rcx,%rax
mov    %rax,-0x40(%rbp)
mov    -0x38(%rbp),%rsi
mov    -0x40(%rbp),%rdx
lea    0xdf9(%rip),%rdi
call   1040 <ohshite@plt>
jmp    1143 <makelock+0x13>

x86 assembly (clang, -O2):
<makelock>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdx,0x20(%rsp)
mov    %esi,%ebp
mov    %rdi,%rbx
mov    %esi,0x8(%rsp)
mov    0x2e7d(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
movq   $0x0,0x10(%rsp)
movl   $0x1,0xc(%rsp)
call   1050 <fileno@plt>
mov    0x2e72(%rip),%r12
mov    (%r12),%esi
lea    0x8(%rsp),%rdx
mov    %eax,%edi
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
je     119a <makelock+0x6a>
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2e37(%rip),%r14
mov    0x2e20(%rip),%r13
lea    0x8(%rsp),%r15
jmp    1216 <makelock+0xe6>
nop
mov    0x2df9(%rip),%rax
cmp    %ebp,(%rax)
lea    0xe57(%rip),%rax
cmove  %rax,%rdx
lea    0xe35(%rip),%rdi
mov    0x20(%rsp),%rsi
call   1040 <ohshite@plt>
mov    %ebp,0x8(%rsp)
mov    0x2df0(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
movq   $0x0,0x10(%rsp)
movl   $0x1,0xc(%rsp)
mov    %rbx,%rdi
call   1050 <fileno@plt>
mov    (%r12),%esi
mov    %eax,%edi
mov    %r15,%rdx
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    118b <makelock+0x5b>
mov    (%r14),%rax
cmp    0x0(%r13),%rax
je     11d5 <makelock+0xa5>
lea    0xdf6(%rip),%rdx
mov    0x2d93(%rip),%rax
cmp    %ebp,(%rax)
je     11b0 <makelock+0x80>
lea    0xde6(%rip),%rdx
jmp    11b0 <makelock+0x80>

LLVM IR (-O0):
%struct.flock = type { i32, i32, i64, i32 }
@SEEK_SET = global i32 0
@F_SETLKW = global i32 0
@errno = global i64 0
@EINTR = global i64 0
@.str = [23 x i8] c"Failed to lock %s (%s)\00"
@F_RDLCK = global i32 0
@.str.1 = [5 x i8] c"read\00"
@F_WRLCK = global i32 0
@.str.2 = [6 x i8] c"write\00"
@.str.3 = [4 x i8] c"?? \00"
define void @makelock(i32* %0, i32 %1, i8* %2) {
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca %struct.flock
store i32* %0, i32** %4
store i32 %1, i32* %5
store i8* %2, i8** %6
br label %8
8: ; preds = %40, %3
%9 = load i32, i32* %5
%10 = getelementptr inbounds %struct.flock, %struct.flock* %7, i32 0, i32 0
store i32 %9, i32* %10
%11 = load i32, i32* @SEEK_SET
%12 = getelementptr inbounds %struct.flock, %struct.flock* %7, i32 0, i32 3
store i32 %11, i32* %12
%13 = getelementptr inbounds %struct.flock, %struct.flock* %7, i32 0, i32 2
store i64 0, i64* %13
%14 = getelementptr inbounds %struct.flock, %struct.flock* %7, i32 0, i32 1
store i32 1, i32* %14
%15 = load i32*, i32** %4
%16 = call i32 @fileno(i32* %15)
%17 = load i32, i32* @F_SETLKW
%18 = call i32 @fcntl(i32 %16, i32 %17, %struct.flock* %7)
%19 = icmp ne i32 %18, -1
br i1 %19, label %20, label %21
20: ; preds = %8
ret void
21: ; preds = %8
%22 = load i64, i64* @errno
%23 = load i64, i64* @EINTR
%24 = icmp ne i64 %22, %23
br i1 %24, label %25, label %40
25: ; preds = %21
%26 = load i8*, i8** %6
%27 = load i32, i32* %5
%28 = load i32, i32* @F_RDLCK
%29 = icmp eq i32 %27, %28
br i1 %29, label %30, label %31
30: ; preds = %25
br label %37
31: ; preds = %25
%32 = load i32, i32* %5
%33 = load i32, i32* @F_WRLCK
%34 = icmp eq i32 %32, %33
%35 = zext i1 %34 to i64
%36 = select i1 %34, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0)
br label %37
37: ; preds = %31, %30
%38 = phi i8* [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), %30 ], [ %36, %31 ]
%39 = call i32 @ohshite(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %26, i8* %38)
br label %40
40: ; preds = %37, %21
br label %8
}

LLVM IR (-O2):
%struct.flock = type { i32, i32, i64, i32 }
@SEEK_SET = global i32 0
@F_SETLKW = global i32 0
@errno = global i64 0
@EINTR = global i64 0
@.str = [23 x i8] c"Failed to lock %s (%s)\00"
@F_RDLCK = global i32 0
@.str.1 = [5 x i8] c"read\00"
@F_WRLCK = global i32 0
@.str.2 = [6 x i8] c"write\00"
@.str.3 = [4 x i8] c"?? \00"
define void @makelock(i32* nocapture %0, i32 %1, i8* %2) {
%4 = alloca %struct.flock
%5 = bitcast %struct.flock* %4 to i8*
%6 = getelementptr inbounds %struct.flock, %struct.flock* %4, i64 0, i32 0
%7 = getelementptr inbounds %struct.flock, %struct.flock* %4, i64 0, i32 3
%8 = getelementptr inbounds %struct.flock, %struct.flock* %4, i64 0, i32 2
%9 = getelementptr inbounds %struct.flock, %struct.flock* %4, i64 0, i32 1
store i32 %1, i32* %6
%10 = load i32, i32* @SEEK_SET
store i32 %10, i32* %7
store i64 0, i64* %8
store i32 1, i32* %9
%11 = call i32 @fileno(i32* %0)
%12 = load i32, i32* @F_SETLKW
%13 = call i32 @fcntl(i32 %11, i32 %12, %struct.flock* %4)
%14 = icmp eq i32 %13, -1
br i1 %14, label %16, label %15
15: ; preds = %28, %3
ret void
16: ; preds = %3, %28
%17 = load i64, i64* @errno
%18 = load i64, i64* @EINTR
%19 = icmp eq i64 %17, %18
br i1 %19, label %28, label %20
20: ; preds = %16
%21 = load i32, i32* @F_RDLCK
%22 = icmp eq i32 %21, %1
%23 = load i32, i32* @F_WRLCK
%24 = icmp eq i32 %23, %1
%25 = select i1 %24, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0)
%26 = select i1 %22, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %25
%27 = call i32 @ohshite(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %2, i8* %26)
br label %28
28: ; preds = %20, %16
store i32 %1, i32* %6
%29 = load i32, i32* @SEEK_SET
store i32 %29, i32* %7
store i64 0, i64* %8
store i32 1, i32* %9
%30 = call i32 @fileno(i32* %0)
%31 = load i32, i32* @F_SETLKW
%32 = call i32 @fcntl(i32 %30, i32 %31, %struct.flock* %4)
%33 = icmp eq i32 %32, -1
br i1 %33, label %16, label %15
}