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

/* Variables and functions */
int EXIT_FAILURE;
int O_RDONLY;
int O_RDWR;
int /*<<< orphan*/ STDOUT_FILENO;
int _associate(char *, char *, int);
int _unassociate(char *);
int getopt(int, char **, char *);
int optind;
int /*<<< orphan*/ write(int /*<<< orphan*/, char *, int);
int main(int argc, char *argv[]) {
  int exit_code = EXIT_FAILURE;
  int flags = O_RDWR;
  int option = 0;
  char mode = 'a';
  int required_argc = 2;
  do {
    option = getopt(argc, argv, "dr");
    if (-1 == option) {
      break;
    }
    switch (option) {
    case 'd':
      mode = option;
      required_argc = 1;
      break;
    case 'r':
      flags = O_RDONLY;
      break;
    default:
      (void)write(STDOUT_FILENO, "Usage: losetup [-d] DEVICE [FILE]\n",
                  sizeof("Usage: losetup [-d] DEVICE [FILE]\n"));
      goto end;
    }
  } while (1);
  if ((argc - optind) != required_argc) {
    (void)write(STDOUT_FILENO, "Usage: losetup [-d] DEVICE [FILE]\n",
                sizeof("Usage: losetup [-d] DEVICE [FILE]\n"));
    goto end;
  }
  if ('d' == mode) {
    exit_code = _unassociate(argv[optind]);
  } else {
    exit_code = _associate(argv[optind], argv[1 + optind], flags);
  }
end:
  return exit_code;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e63(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    0x2e77(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movb   $0x61,-0x1d(%rbp)
movl   $0x2,-0x24(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    0xe72(%rip),%rdx
call   1060 <getopt@plt>
mov    %eax,-0x1c(%rbp)
mov    $0xffffffff,%eax
cmp    -0x1c(%rbp),%eax
jne    11a9 <main+0x69>
jmp    1224 <main+0xe4>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x28(%rbp)
sub    $0x64,%eax
je     11ce <main+0x8e>
jmp    11bd <main+0x7d>
mov    -0x28(%rbp),%eax
sub    $0x72,%eax
je     11e0 <main+0xa0>
jmp    11f1 <main+0xb1>
mov    -0x1c(%rbp),%eax
mov    %al,-0x1d(%rbp)
movl   $0x1,-0x24(%rbp)
jmp    1210 <main+0xd0>
mov    0x2df1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
jmp    1210 <main+0xd0>
mov    0x2df8(%rip),%rax
mov    (%rax),%edi
lea    0xe02(%rip),%rsi
mov    $0x23,%edx
call   1040 <write@plt>
jmp    12c1 <main+0x181>
jmp    1215 <main+0xd5>
mov    $0x1,%al
test   $0x1,%al
jne    1180 <main+0x40>
jmp    1224 <main+0xe4>
mov    -0x8(%rbp),%eax
mov    0x2da2(%rip),%rcx
sub    (%rcx),%eax
cmp    -0x24(%rbp),%eax
je     1258 <main+0x118>
mov    0x2db0(%rip),%rax
mov    (%rax),%edi
lea    0xdba(%rip),%rsi
mov    $0x23,%edx
call   1040 <write@plt>
jmp    12c1 <main+0x181>
movsbl -0x1d(%rbp),%ecx
mov    $0x64,%eax
cmp    %ecx,%eax
jne    1288 <main+0x148>
mov    -0x10(%rbp),%rax
mov    0x2d5c(%rip),%rcx
movslq (%rcx),%rcx
mov    (%rax,%rcx,8),%rdi
call   1030 <_unassociate@plt>
mov    %eax,-0x14(%rbp)
jmp    12bc <main+0x17c>
mov    -0x10(%rbp),%rax
mov    0x2d3d(%rip),%rcx
movslq (%rcx),%rcx
mov    (%rax,%rcx,8),%rdi
mov    -0x10(%rbp),%rax
mov    0x2d2b(%rip),%rcx
mov    (%rcx),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rsi
mov    -0x18(%rbp),%edx
call   1050 <_associate@plt>
mov    %eax,-0x14(%rbp)
jmp    12c1 <main+0x181>
mov    -0x14(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r13
mov    %edi,%ebp
mov    0x2e69(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rsp)
mov    $0x2,%r14d
mov    $0x61,%bl
mov    0x2e74(%rip),%rax
lea    0xe8d(%rip),%r12
mov    (%rax),%r15d
mov    %ebx,%ecx
mov    %r14d,%edx
nopl   0x0(%rax,%rax,1)
mov    %edx,%r14d
mov    %ecx,%ebx
mov    %ebp,%edi
mov    %r13,%rsi
mov    %r12,%rdx
call   1060 <getopt@plt>
mov    $0x1,%edx
mov    $0x64,%cl
cmp    $0x64,%eax
je     1180 <main+0x40>
cmp    $0xffffffff,%eax
je     11b1 <main+0x71>
cmp    $0x72,%eax
jne    11df <main+0x9f>
mov    0x2e29(%rip),%rax
jmp    1173 <main+0x33>
mov    0x2e18(%rip),%rax
movslq (%rax),%rax
sub    %eax,%ebp
cmp    %r14d,%ebp
jne    11df <main+0x9f>
mov    0x0(%r13,%rax,8),%rdi
cmp    $0x64,%bl
jne    120c <main+0xcc>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <_unassociate@plt>
mov    0x2e0a(%rip),%rax
mov    (%rax),%edi
lea    0xe14(%rip),%rsi
mov    $0x23,%edx
call   1040 <write@plt>
mov    0x4(%rsp),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x8(%r13,%rax,8),%rsi
mov    %r15d,%edx
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <_associate@plt>

LLVM IR (-O0):
@EXIT_FAILURE = global i32 0
@O_RDWR = global i32 0
@.str = [3 x i8] c"dr\00"
@O_RDONLY = global i32 0
@STDOUT_FILENO = global i32 0
@.str.1 = [35 x i8] c"Usage: losetup [-d] DEVICE [FILE]\0A\00"
@optind = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* @EXIT_FAILURE
store i32 %11, i32* %6
%12 = load i32, i32* @O_RDWR
store i32 %12, i32* %7
store i32 0, i32* %8
store i8 97, i8* %9
store i32 2, i32* %10
br label %13
13: ; preds = %31, %2
%14 = load i32, i32* %4
%15 = load i8**, i8*** %5
%16 = call i32 @getopt(i32 %14, i8** %15, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32 %16, i32* %8
%17 = load i32, i32* %8
%18 = icmp eq i32 -1, %17
br i1 %18, label %19, label %20
19: ; preds = %13
br label %32
20: ; preds = %13
%21 = load i32, i32* %8
switch i32 %21, label %27 [
i32 100, label %22
i32 114, label %25
]
22: ; preds = %20
%23 = load i32, i32* %8
%24 = trunc i32 %23 to i8
store i8 %24, i8* %9
store i32 1, i32* %10
br label %30
25: ; preds = %20
%26 = load i32, i32* @O_RDONLY
store i32 %26, i32* %7
br label %30
27: ; preds = %20
%28 = load i32, i32* @STDOUT_FILENO
%29 = call i32 @write(i32 %28, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i32 35)
br label %67
30: ; preds = %25, %22
br label %31
31: ; preds = %30
br i1 true, label %13, label %32
32: ; preds = %31, %19
%33 = load i32, i32* %4
%34 = load i32, i32* @optind
%35 = sub nsw i32 %33, %34
%36 = load i32, i32* %10
%37 = icmp ne i32 %35, %36
br i1 %37, label %38, label %41
38: ; preds = %32
%39 = load i32, i32* @STDOUT_FILENO
%40 = call i32 @write(i32 %39, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i32 35)
br label %67
41: ; preds = %32
%42 = load i8, i8* %9
%43 = sext i8 %42 to i32
%44 = icmp eq i32 100, %43
br i1 %44, label %45, label %52
45: ; preds = %41
%46 = load i8**, i8*** %5
%47 = load i32, i32* @optind
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds i8*, i8** %46, i64 %48
%50 = load i8*, i8** %49
%51 = call i32 @_unassociate(i8* %50)
store i32 %51, i32* %6
br label %66
52: ; preds = %41
%53 = load i8**, i8*** %5
%54 = load i32, i32* @optind
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds i8*, i8** %53, i64 %55
%57 = load i8*, i8** %56
%58 = load i8**, i8*** %5
%59 = load i32, i32* @optind
%60 = add nsw i32 1, %59
%61 = sext i32 %60 to i64
%62 = getelementptr inbounds i8*, i8** %58, i64 %61
%63 = load i8*, i8** %62
%64 = load i32, i32* %7
%65 = call i32 @_associate(i8* %57, i8* %63, i32 %64)
store i32 %65, i32* %6
br label %66
66: ; preds = %52, %45
br label %67
67: ; preds = %66, %38, %27
%68 = load i32, i32* %6
ret i32 %68
}

LLVM IR (-O2):
@EXIT_FAILURE = global i32 0
@O_RDWR = global i32 0
@.str = [3 x i8] c"dr\00"
@O_RDONLY = global i32 0
@STDOUT_FILENO = global i32 0
@.str.1 = [35 x i8] c"Usage: losetup [-d] DEVICE [FILE]\0A\00"
@optind = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = load i32, i32* @EXIT_FAILURE
br label %4
4: ; preds = %9, %2
%5 = phi i32* [ @O_RDWR, %2 ], [ @O_RDONLY, %9 ]
%6 = phi i8 [ 97, %2 ], [ %10, %9 ]
%7 = phi i32 [ 2, %2 ], [ %11, %9 ]
%8 = load i32, i32* %5
br label %9
9: ; preds = %4, %9
%10 = phi i8 [ 100, %9 ], [ %6, %4 ]
%11 = phi i32 [ 1, %9 ], [ %7, %4 ]
%12 = tail call i32 @getopt(i32 %0, i8** %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
switch i32 %12, label %13 [
i32 -1, label %16
i32 100, label %9
i32 114, label %4
]
13: ; preds = %9
%14 = load i32, i32* @STDOUT_FILENO
%15 = tail call i32 @write(i32 %14, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i32 35)
br label %36
16: ; preds = %9
%17 = load i32, i32* @optind
%18 = sub nsw i32 %0, %17
%19 = icmp eq i32 %18, %11
br i1 %19, label %23, label %20
20: ; preds = %16
%21 = load i32, i32* @STDOUT_FILENO
%22 = tail call i32 @write(i32 %21, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i32 35)
br label %36
23: ; preds = %16
%24 = icmp eq i8 %10, 100
%25 = sext i32 %17 to i64
%26 = getelementptr inbounds i8*, i8** %1, i64 %25
%27 = load i8*, i8** %26
br i1 %24, label %28, label %30
28: ; preds = %23
%29 = tail call i32 @_unassociate(i8* %27)
br label %36
30: ; preds = %23
%31 = add nsw i32 %17, 1
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8*, i8** %1, i64 %32
%34 = load i8*, i8** %33
%35 = tail call i32 @_associate(i8* %27, i8* %34, i32 %8)
br label %36
36: ; preds = %28, %30, %20, %13
%37 = phi i32 [ %3, %20 ], [ %29, %28 ], [ %35, %30 ], [ %3, %13 ]
ret i32 %37
}