#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 loop_info {
  int /*<<< orphan*/ lo_name;
  int /*<<< orphan*/ member_0;
};

/* Variables and functions */
scalar_t__ ENXIO;
int EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ LOOP_CLR_FD;
int /*<<< orphan*/ LOOP_GET_STATUS;
int /*<<< orphan*/ LOOP_SET_FD;
int /*<<< orphan*/ LOOP_SET_STATUS;
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int ioctl(int, int /*<<< orphan*/, ...);
int open(char const *, int const);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char const *);
int _associate(const char *device_path, const char *file_path,
               const int flags) {
  struct loop_info info = {0};
  int device = (-1);
  int fd = (-1);
  int result = EXIT_FAILURE;
  assert(NULL != device_path);
  assert(NULL != file_path);
  fd = open(file_path, flags);
  if (-1 == fd) {
    goto end;
  }
  device = open(device_path, flags);
  if (-1 == device) {
    goto close_file;
  }
  if (-1 != ioctl(device, LOOP_GET_STATUS, &info)) {
    goto close_device;
  }
  if (ENXIO != errno) {
    goto close_device;
  }
  (void)strcpy(info.lo_name, file_path);
  if (0 != ioctl(device, LOOP_SET_FD, fd)) {
    goto close_device;
  }
  if (0 != ioctl(device, LOOP_SET_STATUS, &info)) {
    (void)ioctl(device, LOOP_CLR_FD, 0);
    goto close_device;
  }
  result = EXIT_SUCCESS;
close_device:
  (void)close(device);
close_file:
  (void)close(fd);
end:
  return result;
}


x86 assembly (clang, -O0):
<_associate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
movl   $0xffffffff,-0x24(%rbp)
movl   $0xffffffff,-0x28(%rbp)
mov    0x2e20(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
xor    %eax,%eax
cmp    -0x8(%rbp),%rax
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
xor    %eax,%eax
cmp    -0x10(%rbp),%rax
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1080 <open@plt>
mov    %eax,-0x28(%rbp)
mov    $0xffffffff,%eax
cmp    -0x28(%rbp),%eax
jne    11e5 <_associate+0x85>
jmp    12dd <_associate+0x17d>
mov    -0x8(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1080 <open@plt>
mov    %eax,-0x24(%rbp)
mov    $0xffffffff,%eax
cmp    -0x24(%rbp),%eax
jne    1207 <_associate+0xa7>
jmp    12d5 <_associate+0x175>
mov    -0x24(%rbp),%edi
mov    0x2dd7(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
mov    $0x0,%al
call   1050 <ioctl@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
je     1232 <_associate+0xd2>
jmp    12cd <_associate+0x16d>
mov    0x2d87(%rip),%rax
mov    (%rax),%rax
mov    0x2d85(%rip),%rcx
cmp    (%rcx),%rax
je     1251 <_associate+0xf1>
jmp    12cd <_associate+0x16d>
mov    -0x20(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x24(%rbp),%edi
mov    0x2d39(%rip),%rax
mov    (%rax),%esi
mov    -0x28(%rbp),%edx
mov    $0x0,%al
call   1050 <ioctl@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    %ecx,%eax
je     1284 <_associate+0x124>
jmp    12cd <_associate+0x16d>
mov    -0x24(%rbp),%edi
mov    0x2d52(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
mov    $0x0,%al
call   1050 <ioctl@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    %ecx,%eax
je     12c1 <_associate+0x161>
mov    -0x24(%rbp),%edi
mov    0x2d27(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
mov    $0x0,%al
call   1050 <ioctl@plt>
jmp    12cd <_associate+0x16d>
mov    0x2ce8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
mov    -0x28(%rbp),%edi
call   1060 <close@plt>
mov    -0x2c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_associate>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %rsi,%r13
mov    %rdi,%rbx
movq   $0x0,(%rsp)
mov    0x2e46(%rip),%rax
mov    (%rax),%r14d
xor    %edi,%edi
test   %rbx,%rbx
setne  %dil
call   1060 <assert@plt>
xor    %edi,%edi
test   %r13,%r13
setne  %dil
call   1060 <assert@plt>
mov    %r13,%rdi
mov    %ebp,%esi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     125e <_associate+0x10e>
mov    %eax,%r12d
mov    %rbx,%rdi
mov    %ebp,%esi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     1256 <_associate+0x106>
mov    %eax,%r15d
mov    0x2e24(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %r15d,%edi
xor    %eax,%eax
call   1040 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    124e <_associate+0xfe>
mov    0x2de1(%rip),%rax
mov    (%rax),%rax
mov    0x2ddf(%rip),%rcx
cmp    (%rcx),%rax
jne    124e <_associate+0xfe>
mov    (%rsp),%edi
mov    %r13,%rsi
call   1030 <strcpy@plt>
mov    0x2da0(%rip),%rax
mov    (%rax),%esi
mov    %r15d,%edi
mov    %r12d,%edx
xor    %eax,%eax
call   1040 <ioctl@plt>
test   %eax,%eax
jne    124e <_associate+0xfe>
mov    0x2dc6(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %r15d,%edi
xor    %eax,%eax
call   1040 <ioctl@plt>
test   %eax,%eax
je     1244 <_associate+0xf4>
mov    0x2da4(%rip),%rax
mov    (%rax),%esi
mov    %r15d,%edi
xor    %edx,%edx
xor    %eax,%eax
call   1040 <ioctl@plt>
jmp    124e <_associate+0xfe>
mov    0x2d65(%rip),%rax
mov    (%rax),%r14d
mov    %r15d,%edi
call   1050 <close@plt>
mov    %r12d,%edi
call   1050 <close@plt>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.loop_info = type { i32, i32 }
@EXIT_FAILURE = global i32 0
@LOOP_GET_STATUS = global i32 0
@ENXIO = global i64 0
@errno = global i64 0
@LOOP_SET_FD = global i32 0
@LOOP_SET_STATUS = global i32 0
@LOOP_CLR_FD = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @_associate(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca %struct.loop_info
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%11 = bitcast %struct.loop_info* %7 to i8*
store i32 -1, i32* %8
store i32 -1, i32* %9
%12 = load i32, i32* @EXIT_FAILURE
store i32 %12, i32* %10
%13 = load i8*, i8** %4
%14 = icmp ne i8* null, %13
%15 = zext i1 %14 to i32
%16 = call i32 @assert(i32 %15)
%17 = load i8*, i8** %5
%18 = icmp ne i8* null, %17
%19 = zext i1 %18 to i32
%20 = call i32 @assert(i32 %19)
%21 = load i8*, i8** %5
%22 = load i32, i32* %6
%23 = call i32 @open(i8* %21, i32 %22)
store i32 %23, i32* %9
%24 = load i32, i32* %9
%25 = icmp eq i32 -1, %24
br i1 %25, label %26, label %27
26: ; preds = %3
br label %73
27: ; preds = %3
%28 = load i8*, i8** %4
%29 = load i32, i32* %6
%30 = call i32 @open(i8* %28, i32 %29)
store i32 %30, i32* %8
%31 = load i32, i32* %8
%32 = icmp eq i32 -1, %31
br i1 %32, label %33, label %34
33: ; preds = %27
br label %70
34: ; preds = %27
%35 = load i32, i32* %8
%36 = load i32, i32* @LOOP_GET_STATUS
%37 = call i32 (i32, i32, ...) @ioctl(i32 %35, i32 %36, %struct.loop_info* %7)
%38 = icmp ne i32 -1, %37
br i1 %38, label %39, label %40
39: ; preds = %34
br label %67
40: ; preds = %34
%41 = load i64, i64* @ENXIO
%42 = load i64, i64* @errno
%43 = icmp ne i64 %41, %42
br i1 %43, label %44, label %45
44: ; preds = %40
br label %67
45: ; preds = %40
%46 = getelementptr inbounds %struct.loop_info, %struct.loop_info* %7, i32 0, i32 0
%47 = load i32, i32* %46
%48 = load i8*, i8** %5
%49 = call i32 @strcpy(i32 %47, i8* %48)
%50 = load i32, i32* %8
%51 = load i32, i32* @LOOP_SET_FD
%52 = load i32, i32* %9
%53 = call i32 (i32, i32, ...) @ioctl(i32 %50, i32 %51, i32 %52)
%54 = icmp ne i32 0, %53
br i1 %54, label %55, label %56
55: ; preds = %45
br label %67
56: ; preds = %45
%57 = load i32, i32* %8
%58 = load i32, i32* @LOOP_SET_STATUS
%59 = call i32 (i32, i32, ...) @ioctl(i32 %57, i32 %58, %struct.loop_info* %7)
%60 = icmp ne i32 0, %59
br i1 %60, label %61, label %65
61: ; preds = %56
%62 = load i32, i32* %8
%63 = load i32, i32* @LOOP_CLR_FD
%64 = call i32 (i32, i32, ...) @ioctl(i32 %62, i32 %63, i32 0)
br label %67
65: ; preds = %56
%66 = load i32, i32* @EXIT_SUCCESS
store i32 %66, i32* %10
br label %67
67: ; preds = %65, %61, %55, %44, %39
%68 = load i32, i32* %8
%69 = call i32 @close(i32 %68)
br label %70
70: ; preds = %67, %33
%71 = load i32, i32* %9
%72 = call i32 @close(i32 %71)
br label %73
73: ; preds = %70, %26
%74 = load i32, i32* %10
ret i32 %74
}

LLVM IR (-O2):
@EXIT_FAILURE = global i32 0
@LOOP_GET_STATUS = global i32 0
@ENXIO = global i64 0
@errno = global i64 0
@LOOP_SET_FD = global i32 0
@LOOP_SET_STATUS = global i32 0
@LOOP_CLR_FD = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @_associate(i8* %0, i8* %1, i32 %2) {
%4 = alloca i64
%5 = bitcast i64* %4 to i8*
store i64 0, i64* %4
%6 = load i32, i32* @EXIT_FAILURE
%7 = icmp ne i8* %0, null
%8 = zext i1 %7 to i32
%9 = tail call i32 @assert(i32 %8)
%10 = icmp ne i8* %1, null
%11 = zext i1 %10 to i32
%12 = tail call i32 @assert(i32 %11)
%13 = tail call i32 @open(i8* %1, i32 %2)
%14 = icmp eq i32 %13, -1
br i1 %14, label %48, label %15
15: ; preds = %3
%16 = tail call i32 @open(i8* %0, i32 %2)
%17 = icmp eq i32 %16, -1
br i1 %17, label %45, label %18
18: ; preds = %15
%19 = load i32, i32* @LOOP_GET_STATUS
%20 = call i32 (i32, i32, ...) @ioctl(i32 %16, i32 %19, i64* %4)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %42
22: ; preds = %18
%23 = load i64, i64* @ENXIO
%24 = load i64, i64* @errno
%25 = icmp eq i64 %23, %24
br i1 %25, label %26, label %42
26: ; preds = %22
%27 = bitcast i64* %4 to i32*
%28 = load i32, i32* %27
%29 = call i32 @strcpy(i32 %28, i8* %1)
%30 = load i32, i32* @LOOP_SET_FD
%31 = call i32 (i32, i32, ...) @ioctl(i32 %16, i32 %30, i32 %13)
%32 = icmp eq i32 %31, 0
br i1 %32, label %33, label %42
33: ; preds = %26
%34 = load i32, i32* @LOOP_SET_STATUS
%35 = call i32 (i32, i32, ...) @ioctl(i32 %16, i32 %34, i64* %4)
%36 = icmp eq i32 %35, 0
br i1 %36, label %40, label %37
37: ; preds = %33
%38 = load i32, i32* @LOOP_CLR_FD
%39 = call i32 (i32, i32, ...) @ioctl(i32 %16, i32 %38, i32 0)
br label %42
40: ; preds = %33
%41 = load i32, i32* @EXIT_SUCCESS
br label %42
42: ; preds = %26, %22, %18, %40, %37
%43 = phi i32 [ %6, %18 ], [ %6, %22 ], [ %6, %26 ], [ %6, %37 ], [ %41, %40 ]
%44 = call i32 @close(i32 %16)
br label %45
45: ; preds = %15, %42
%46 = phi i32 [ %6, %15 ], [ %43, %42 ]
%47 = call i32 @close(i32 %13)
br label %48
48: ; preds = %3, %45
%49 = phi i32 [ %6, %3 ], [ %46, %45 ]
ret i32 %49
}