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

/* Variables and functions */
int /*<<< orphan*/ F_SETLK;
int /*<<< orphan*/ F_SETLKW;
int F_UNLCK;
int F_WRLCK;
int /*<<< orphan*/ SEEK_CUR;
scalar_t__ fcntl(int, int /*<<< orphan*/, struct flock *);
scalar_t__ read;
scalar_t__ write;
struct utmp *utmp_io(int fd, struct utmp *ut, int type) {
  int ret;
  struct flock fl;
  int (*op)() = (type == F_WRLCK) ? (int (*)())write : (int (*)())read;
  fl.l_whence = SEEK_CUR;
  fl.l_start = 0;
  fl.l_len = (sizeof(struct utmp));
  fl.l_pid = 0;
  fl.l_type = type;
  if (fcntl(fd, F_SETLKW, &fl))
    return 0;
  ret = op(fd, ut, (sizeof(struct utmp)));
  fl.l_start = -(sizeof(struct utmp));
  fl.l_type = F_UNLCK;
  fcntl(fd, F_SETLK, &fl);
  if (ret != (sizeof(struct utmp)))
    return 0;
  return ut;
}


x86 assembly (clang, -O0):
<utmp_io>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    0x2e84(%rip),%rcx
cmp    (%rcx),%eax
jne    1147 <utmp_io+0x37>
mov    0x2ea5(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x50(%rbp)
jmp    1155 <utmp_io+0x45>
mov    0x2e72(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x50(%rbp)
mov    -0x50(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    0x2e84(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
movl   $0x0,-0x40(%rbp)
movl   $0x4,-0x3c(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0xc(%rbp),%edi
mov    0x2e49(%rip),%rax
mov    (%rax),%esi
lea    -0x40(%rbp),%rdx
call   1030 <fcntl@plt>
cmp    $0x0,%rax
je     11b1 <utmp_io+0xa1>
movq   $0x0,-0x8(%rbp)
jmp    1213 <utmp_io+0x103>
mov    -0x48(%rbp),%rcx
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    $0x4,%edx
mov    $0x0,%al
call   *%rcx
mov    %eax,-0x20(%rbp)
movl   $0xfffffffc,-0x40(%rbp)
mov    0x2df2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    -0xc(%rbp),%edi
mov    0x2deb(%rip),%rax
mov    (%rax),%esi
lea    -0x40(%rbp),%rdx
call   1030 <fcntl@plt>
movslq -0x20(%rbp),%rax
cmp    $0x4,%rax
je     120b <utmp_io+0xfb>
movq   $0x0,-0x8(%rbp)
jmp    1213 <utmp_io+0x103>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%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    %edx,%ebx
mov    %rsi,%r14
mov    %edi,%ebp
mov    0x2e84(%rip),%rax
mov    (%rax),%r12d
mov    0x2eaa(%rip),%rax
mov    (%rax),%r13
mov    0x2e80(%rip),%rax
mov    (%rax),%r15
mov    0x2e9e(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
movabs $0x400000000,%rax
mov    %rax,0x8(%rsp)
movq   $0x0,0x18(%rsp)
mov    %edx,0x10(%rsp)
mov    0x2e65(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
call   1030 <fcntl@plt>
test   %rax,%rax
je     1188 <utmp_io+0x78>
xor    %ebx,%ebx
jmp    11d5 <utmp_io+0xc5>
cmp    %ebx,%r12d
cmove  %r13,%r15
xor    %ebx,%ebx
mov    $0x4,%edx
mov    %ebp,%edi
mov    %r14,%rsi
xor    %eax,%eax
call   *%r15
mov    %eax,%r15d
movl   $0xfffffffc,0x8(%rsp)
ff
mov    0x2e16(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    0x2e11(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebp,%edi
call   1030 <fcntl@plt>
cmp    $0x4,%r15d
cmove  %r14,%rbx
mov    %rbx,%rax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.utmp = type { i32 }
%struct.flock = type { i32, i32, i32, i64, i32 }
@F_WRLCK = global i32 0
@write = global i64 0
@read = global i64 0
@SEEK_CUR = global i32 0
@F_SETLKW = global i32 0
@F_UNLCK = global i32 0
@F_SETLK = global i32 0
define %struct.utmp* @utmp_io(i32 %0, %struct.utmp* %1, i32 %2) {
%4 = alloca %struct.utmp*
%5 = alloca i32
%6 = alloca %struct.utmp*
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.flock
%10 = alloca i32 (...)*
store i32 %0, i32* %5
store %struct.utmp* %1, %struct.utmp** %6
store i32 %2, i32* %7
%11 = load i32, i32* %7
%12 = load i32, i32* @F_WRLCK
%13 = icmp eq i32 %11, %12
br i1 %13, label %14, label %17
14: ; preds = %3
%15 = load i64, i64* @write
%16 = inttoptr i64 %15 to i32 (...)*
br label %20
17: ; preds = %3
%18 = load i64, i64* @read
%19 = inttoptr i64 %18 to i32 (...)*
br label %20
20: ; preds = %17, %14
%21 = phi i32 (...)* [ %16, %14 ], [ %19, %17 ]
store i32 (...)* %21, i32 (...)** %10
%22 = load i32, i32* @SEEK_CUR
%23 = getelementptr inbounds %struct.flock, %struct.flock* %9, i32 0, i32 4
store i32 %22, i32* %23
%24 = getelementptr inbounds %struct.flock, %struct.flock* %9, i32 0, i32 0
store i32 0, i32* %24
%25 = getelementptr inbounds %struct.flock, %struct.flock* %9, i32 0, i32 1
store i32 4, i32* %25
%26 = getelementptr inbounds %struct.flock, %struct.flock* %9, i32 0, i32 3
store i64 0, i64* %26
%27 = load i32, i32* %7
%28 = getelementptr inbounds %struct.flock, %struct.flock* %9, i32 0, i32 2
store i32 %27, i32* %28
%29 = load i32, i32* %5
%30 = load i32, i32* @F_SETLKW
%31 = call i64 @fcntl(i32 %29, i32 %30, %struct.flock* %9)
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %20
store %struct.utmp* null, %struct.utmp** %4
br label %52
34: ; preds = %20
%35 = load i32 (...)*, i32 (...)** %10
%36 = load i32, i32* %5
%37 = load %struct.utmp*, %struct.utmp** %6
%38 = bitcast i32 (...)* %35 to i32 (i32, %struct.utmp*, i64, ...)*
%39 = call i32 (i32, %struct.utmp*, i64, ...) %38(i32 %36, %struct.utmp* %37, i64 4)
store i32 %39, i32* %8
%40 = getelementptr inbounds %struct.flock, %struct.flock* %9, i32 0, i32 0
store i32 -4, i32* %40
%41 = load i32, i32* @F_UNLCK
%42 = getelementptr inbounds %struct.flock, %struct.flock* %9, i32 0, i32 2
store i32 %41, i32* %42
%43 = load i32, i32* %5
%44 = load i32, i32* @F_SETLK
%45 = call i64 @fcntl(i32 %43, i32 %44, %struct.flock* %9)
%46 = load i32, i32* %8
%47 = sext i32 %46 to i64
%48 = icmp ne i64 %47, 4
br i1 %48, label %49, label %50
49: ; preds = %34
store %struct.utmp* null, %struct.utmp** %4
br label %52
50: ; preds = %34
%51 = load %struct.utmp*, %struct.utmp** %6
store %struct.utmp* %51, %struct.utmp** %4
br label %52
52: ; preds = %50, %49, %33
%53 = load %struct.utmp*, %struct.utmp** %4
ret %struct.utmp* %53
}

LLVM IR (-O2):
%struct.utmp = type { i32 }
%struct.flock = type { i32, i32, i32, i64, i32 }
@F_WRLCK = global i32 0
@write = global i64 0
@read = global i64 0
@SEEK_CUR = global i32 0
@F_SETLKW = global i32 0
@F_UNLCK = global i32 0
@F_SETLK = global i32 0
define %struct.utmp* @utmp_io(i32 %0, %struct.utmp* %1, i32 %2) {
%4 = alloca %struct.flock
%5 = bitcast %struct.flock* %4 to i8*
%6 = load i32, i32* @F_WRLCK
%7 = load i64, i64* @write
%8 = load i64, i64* @read
%9 = load i32, i32* @SEEK_CUR
%10 = getelementptr inbounds %struct.flock, %struct.flock* %4, i64 0, i32 4
store i32 %9, i32* %10
%11 = getelementptr inbounds %struct.flock, %struct.flock* %4, i64 0, i32 0
store i32 0, i32* %11
%12 = getelementptr inbounds %struct.flock, %struct.flock* %4, i64 0, i32 1
store i32 4, i32* %12
%13 = getelementptr inbounds %struct.flock, %struct.flock* %4, i64 0, i32 3
store i64 0, i64* %13
%14 = getelementptr inbounds %struct.flock, %struct.flock* %4, i64 0, i32 2
store i32 %2, i32* %14
%15 = load i32, i32* @F_SETLKW
%16 = call i64 @fcntl(i32 %0, i32 %15, %struct.flock* %4)
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %28
18: ; preds = %3
%19 = icmp eq i32 %6, %2
%20 = select i1 %19, i64 %7, i64 %8
%21 = inttoptr i64 %20 to i32 (i32, %struct.utmp*, i64, ...)*
%22 = call i32 (i32, %struct.utmp*, i64, ...) %21(i32 %0, %struct.utmp* %1, i64 4)
store i32 -4, i32* %11
%23 = load i32, i32* @F_UNLCK
store i32 %23, i32* %14
%24 = load i32, i32* @F_SETLK
%25 = call i64 @fcntl(i32 %0, i32 %24, %struct.flock* %4)
%26 = icmp eq i32 %22, 4
%27 = select i1 %26, %struct.utmp* %1, %struct.utmp* null
br label %28
28: ; preds = %18, %3
%29 = phi %struct.utmp* [ null, %3 ], [ %27, %18 ]
ret %struct.utmp* %29
}