#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_start;
  int l_len;
  int l_type;
  scalar_t__ l_pid;
  int /*<<< orphan*/ l_whence;
};
typedef int ssize_t;
typedef int /*<<< orphan*/ off_t;

/* Variables and functions */
int /*<<< orphan*/ F_SETLK;
int /*<<< orphan*/ F_SETLKW;
int F_UNLCK;
int F_WRLCK;
int /*<<< orphan*/ SEEK_CUR;
int errno;
scalar_t__ fcntl(int, int /*<<< orphan*/, struct flock *);
int /*<<< orphan*/ fsync(int);
int /*<<< orphan*/ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int read(int, void *, int);
int write(int, void *, int);
void *__utmp_io(int fd, void *ut, ssize_t len, off_t *offset, int type) {
  int e, ret;
  struct flock fl;
  off_t newoffset;
  fl.l_whence = SEEK_CUR;
  fl.l_start = 0;
  fl.l_len = len;
  fl.l_pid = 0;
  fl.l_type = type;
  if (fcntl(fd, F_SETLKW, &fl))
    return 0;
  if (type == F_WRLCK) {
    ret = write(fd, ut, len);
    e = errno;
    fsync(fd);
    newoffset = lseek(fd, 0, SEEK_CUR);
  } else {
    newoffset = lseek(fd, 0, SEEK_CUR);
    ret = read(fd, ut, len);
    e = errno;
  }
  fl.l_start = -(len);
  fl.l_type = F_UNLCK;
  fcntl(fd, F_SETLK, &fl);
  if (offset)
    *offset = newoffset;
  errno = e;
  if (ret != len)
    return (void *)0;
  return ut;
}


x86 assembly (clang, -O0):
<__utmp_io>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8d,-0x2c(%rbp)
mov    0x2e77(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
movl   $0x0,-0x58(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x54(%rbp)
movq   $0x0,-0x48(%rbp)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x50(%rbp)
mov    -0xc(%rbp),%edi
mov    0x2e45(%rip),%rax
mov    (%rax),%esi
lea    -0x58(%rbp),%rdx
call   1030 <fcntl@plt>
cmp    $0x0,%rax
je     11bd <__utmp_io+0x6d>
movq   $0x0,-0x8(%rbp)
jmp    12ae <__utmp_io+0x15e>
mov    -0x2c(%rbp),%eax
mov    0x2df1(%rip),%rcx
cmp    (%rcx),%eax
jne    1210 <__utmp_io+0xc0>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1040 <write@plt>
mov    %eax,-0x34(%rbp)
mov    0x2dd8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0xc(%rbp),%edi
call   1070 <fsync@plt>
mov    -0xc(%rbp),%edi
mov    0x2de9(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1050 <lseek@plt>
mov    %eax,-0x5c(%rbp)
jmp    1244 <__utmp_io+0xf4>
mov    -0xc(%rbp),%edi
mov    0x2dce(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1050 <lseek@plt>
mov    %eax,-0x5c(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1060 <read@plt>
mov    %eax,-0x34(%rbp)
mov    0x2d81(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
xor    %eax,%eax
sub    -0x1c(%rbp),%eax
mov    %eax,-0x58(%rbp)
mov    0x2d7d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x50(%rbp)
mov    -0xc(%rbp),%edi
mov    0x2d76(%rip),%rax
mov    (%rax),%esi
lea    -0x58(%rbp),%rdx
call   1030 <fcntl@plt>
cmpq   $0x0,-0x28(%rbp)
je     1281 <__utmp_io+0x131>
mov    -0x5c(%rbp),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x30(%rbp),%ecx
mov    0x2d35(%rip),%rax
mov    %ecx,(%rax)
mov    -0x34(%rbp),%eax
cmp    -0x1c(%rbp),%eax
je     12a6 <__utmp_io+0x156>
movq   $0x0,-0x8(%rbp)
jmp    12ae <__utmp_io+0x15e>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__utmp_io>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %r8d,%ebp
mov    %rcx,%r14
mov    %edx,%r13d
mov    %rsi,%r15
mov    %edi,%ebx
mov    0x2e75(%rip),%r12
mov    (%r12),%eax
mov    %eax,0x20(%rsp)
movl   $0x0,0x8(%rsp)
mov    %edx,0xc(%rsp)
movq   $0x0,0x18(%rsp)
mov    %r8d,0x10(%rsp)
mov    0x2e44(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
call   1030 <fcntl@plt>
test   %rax,%rax
je     11b4 <__utmp_io+0x64>
xor    %eax,%eax
jmp    126b <__utmp_io+0x11b>
mov    0x2dfd(%rip),%rax
cmp    %ebp,(%rax)
jne    11f9 <__utmp_io+0xa9>
mov    %ebx,%edi
mov    %r15,%rsi
mov    %r13d,%edx
call   1040 <write@plt>
mov    %eax,0x4(%rsp)
mov    0x2de9(%rip),%rax
mov    (%rax),%ebp
mov    %ebx,%edi
call   1070 <fsync@plt>
mov    (%r12),%edx
mov    %ebp,%r12d
mov    0x4(%rsp),%ebp
mov    %ebx,%edi
xor    %esi,%esi
call   1050 <lseek@plt>
mov    %eax,(%rsp)
jmp    1222 <__utmp_io+0xd2>
mov    (%r12),%edx
mov    %ebx,%edi
xor    %esi,%esi
call   1050 <lseek@plt>
mov    %eax,(%rsp)
mov    %ebx,%edi
mov    %r15,%rsi
mov    %r13d,%edx
call   1060 <read@plt>
mov    %eax,%ebp
mov    0x2da1(%rip),%rax
mov    (%rax),%r12d
mov    %r13d,%eax
neg    %eax
mov    %eax,0x8(%rsp)
mov    0x2d9e(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    0x2d99(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1030 <fcntl@plt>
test   %r14,%r14
je     1258 <__utmp_io+0x108>
mov    (%rsp),%eax
mov    %eax,(%r14)
mov    0x2d61(%rip),%rax
mov    %r12d,(%rax)
xor    %eax,%eax
cmp    %r13d,%ebp
cmove  %r15,%rax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.flock = type { i32, i32, i32, i64, i32 }
@SEEK_CUR = global i32 0
@F_SETLKW = global i32 0
@F_WRLCK = global i32 0
@errno = global i32 0
@F_UNLCK = global i32 0
@F_SETLK = global i32 0
define i8* @__utmp_io(i32 %0, i8* %1, i32 %2, i32* %3, i32 %4) {
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32*
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca %struct.flock
%15 = alloca i32
store i32 %0, i32* %7
store i8* %1, i8** %8
store i32 %2, i32* %9
store i32* %3, i32** %10
store i32 %4, i32* %11
%16 = load i32, i32* @SEEK_CUR
%17 = getelementptr inbounds %struct.flock, %struct.flock* %14, i32 0, i32 4
store i32 %16, i32* %17
%18 = getelementptr inbounds %struct.flock, %struct.flock* %14, i32 0, i32 0
store i32 0, i32* %18
%19 = load i32, i32* %9
%20 = getelementptr inbounds %struct.flock, %struct.flock* %14, i32 0, i32 1
store i32 %19, i32* %20
%21 = getelementptr inbounds %struct.flock, %struct.flock* %14, i32 0, i32 3
store i64 0, i64* %21
%22 = load i32, i32* %11
%23 = getelementptr inbounds %struct.flock, %struct.flock* %14, i32 0, i32 2
store i32 %22, i32* %23
%24 = load i32, i32* %7
%25 = load i32, i32* @F_SETLKW
%26 = call i64 @fcntl(i32 %24, i32 %25, %struct.flock* %14)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %5
store i8* null, i8** %6
br label %75
29: ; preds = %5
%30 = load i32, i32* %11
%31 = load i32, i32* @F_WRLCK
%32 = icmp eq i32 %30, %31
br i1 %32, label %33, label %44
33: ; preds = %29
%34 = load i32, i32* %7
%35 = load i8*, i8** %8
%36 = load i32, i32* %9
%37 = call i32 @write(i32 %34, i8* %35, i32 %36)
store i32 %37, i32* %13
%38 = load i32, i32* @errno
store i32 %38, i32* %12
%39 = load i32, i32* %7
%40 = call i32 @fsync(i32 %39)
%41 = load i32, i32* %7
%42 = load i32, i32* @SEEK_CUR
%43 = call i32 @lseek(i32 %41, i32 0, i32 %42)
store i32 %43, i32* %15
br label %53
44: ; preds = %29
%45 = load i32, i32* %7
%46 = load i32, i32* @SEEK_CUR
%47 = call i32 @lseek(i32 %45, i32 0, i32 %46)
store i32 %47, i32* %15
%48 = load i32, i32* %7
%49 = load i8*, i8** %8
%50 = load i32, i32* %9
%51 = call i32 @read(i32 %48, i8* %49, i32 %50)
store i32 %51, i32* %13
%52 = load i32, i32* @errno
store i32 %52, i32* %12
br label %53
53: ; preds = %44, %33
%54 = load i32, i32* %9
%55 = sub nsw i32 0, %54
%56 = getelementptr inbounds %struct.flock, %struct.flock* %14, i32 0, i32 0
store i32 %55, i32* %56
%57 = load i32, i32* @F_UNLCK
%58 = getelementptr inbounds %struct.flock, %struct.flock* %14, i32 0, i32 2
store i32 %57, i32* %58
%59 = load i32, i32* %7
%60 = load i32, i32* @F_SETLK
%61 = call i64 @fcntl(i32 %59, i32 %60, %struct.flock* %14)
%62 = load i32*, i32** %10
%63 = icmp ne i32* %62, null
br i1 %63, label %64, label %67
64: ; preds = %53
%65 = load i32, i32* %15
%66 = load i32*, i32** %10
store i32 %65, i32* %66
br label %67
67: ; preds = %64, %53
%68 = load i32, i32* %12
store i32 %68, i32* @errno
%69 = load i32, i32* %13
%70 = load i32, i32* %9
%71 = icmp ne i32 %69, %70
br i1 %71, label %72, label %73
72: ; preds = %67
store i8* null, i8** %6
br label %75
73: ; preds = %67
%74 = load i8*, i8** %8
store i8* %74, i8** %6
br label %75
75: ; preds = %73, %72, %28
%76 = load i8*, i8** %6
ret i8* %76
}

LLVM IR (-O2):
%struct.flock = type { i32, i32, i32, i64, i32 }
@SEEK_CUR = global i32 0
@F_SETLKW = global i32 0
@F_WRLCK = global i32 0
@errno = global i32 0
@F_UNLCK = global i32 0
@F_SETLK = global i32 0
define i8* @__utmp_io(i32 %0, i8* %1, i32 %2, i32* writeonly %3, i32 %4) {
%6 = alloca %struct.flock
%7 = bitcast %struct.flock* %6 to i8*
%8 = load i32, i32* @SEEK_CUR
%9 = getelementptr inbounds %struct.flock, %struct.flock* %6, i64 0, i32 4
store i32 %8, i32* %9
%10 = getelementptr inbounds %struct.flock, %struct.flock* %6, i64 0, i32 0
store i32 0, i32* %10
%11 = getelementptr inbounds %struct.flock, %struct.flock* %6, i64 0, i32 1
store i32 %2, i32* %11
%12 = getelementptr inbounds %struct.flock, %struct.flock* %6, i64 0, i32 3
store i64 0, i64* %12
%13 = getelementptr inbounds %struct.flock, %struct.flock* %6, i64 0, i32 2
store i32 %4, i32* %13
%14 = load i32, i32* @F_SETLKW
%15 = call i64 @fcntl(i32 %0, i32 %14, %struct.flock* %6)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %44
17: ; preds = %5
%18 = load i32, i32* @F_WRLCK
%19 = icmp eq i32 %18, %4
br i1 %19, label %20, label %26
20: ; preds = %17
%21 = call i32 @write(i32 %0, i8* %1, i32 %2)
%22 = load i32, i32* @errno
%23 = call i32 @fsync(i32 %0)
%24 = load i32, i32* @SEEK_CUR
%25 = call i32 @lseek(i32 %0, i32 0, i32 %24)
br label %31
26: ; preds = %17
%27 = load i32, i32* @SEEK_CUR
%28 = call i32 @lseek(i32 %0, i32 0, i32 %27)
%29 = call i32 @read(i32 %0, i8* %1, i32 %2)
%30 = load i32, i32* @errno
br label %31
31: ; preds = %26, %20
%32 = phi i32 [ %22, %20 ], [ %30, %26 ]
%33 = phi i32 [ %21, %20 ], [ %29, %26 ]
%34 = phi i32 [ %25, %20 ], [ %28, %26 ]
%35 = sub nsw i32 0, %2
store i32 %35, i32* %10
%36 = load i32, i32* @F_UNLCK
store i32 %36, i32* %13
%37 = load i32, i32* @F_SETLK
%38 = call i64 @fcntl(i32 %0, i32 %37, %struct.flock* %6)
%39 = icmp eq i32* %3, null
br i1 %39, label %41, label %40
40: ; preds = %31
store i32 %34, i32* %3
br label %41
41: ; preds = %40, %31
store i32 %32, i32* @errno
%42 = icmp eq i32 %33, %2
%43 = select i1 %42, i8* %1, i8* null
br label %44
44: ; preds = %41, %5
%45 = phi i8* [ null, %5 ], [ %43, %41 ]
ret i8* %45
}