#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 */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(unsigned short *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ sprintf(char *, char *, unsigned short);
int get_mirror_tag(char *tag, char *device) {
  FILE *mirror;
  short unsigned int block;
  short unsigned int event;
  int i;
  mirror = fopen(device, "rb");
  if (!mirror) {
    printf("Error while opening %s Mir:ror device.\n", device);
    return -1;
  }
  while (event != 0x102) {
    fread(&event, 2, 1, mirror);
  }
  for (i = 0; i < 6; i++) {
    fread(&block, 2, 1, mirror);
    sprintf(&tag[i * 4], "%04X", block);
  }
  return 0;
}


x86 assembly (clang, -O0):
<get_mirror_tag>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xea5(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    118b <get_mirror_tag+0x4b>
mov    -0x18(%rbp),%rsi
lea    0xe89(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1217 <get_mirror_tag+0xd7>
jmp    1190 <get_mirror_tag+0x50>
movzwl -0x24(%rbp),%eax
cmp    $0x102,%eax
je     11bb <get_mirror_tag+0x7b>
mov    -0x20(%rbp),%rcx
lea    -0x24(%rbp),%rdi
mov    $0x2,%esi
mov    $0x1,%edx
call   1030 <fread@plt>
jmp    1190 <get_mirror_tag+0x50>
movl   $0x0,-0x28(%rbp)
cmpl   $0x6,-0x28(%rbp)
jge    1210 <get_mirror_tag+0xd0>
mov    -0x20(%rbp),%rcx
lea    -0x22(%rbp),%rdi
mov    $0x2,%esi
mov    $0x1,%edx
call   1030 <fread@plt>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%eax
shl    $0x2,%eax
cltq
add    %rax,%rdi
lea    0xe32(%rip),%rsi
movzwl -0x22(%rbp),%edx
call   1060 <sprintf@plt>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    11c2 <get_mirror_tag+0x82>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_mirror_tag>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
mov    %rdi,%r14
lea    0xea6(%rip),%rsi
mov    %rbx,%rdi
call   1050 <fopen@plt>
test   %rax,%rax
je     1297 <get_mirror_tag+0x157>
mov    %rax,%r13
lea    0xe(%rsp),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rbx,%rdi
mov    $0x2,%esi
mov    $0x1,%edx
mov    %r13,%rcx
call   1030 <fread@plt>
movzwl 0xe(%rsp),%eax
cmp    $0x102,%eax
jne    1180 <get_mirror_tag+0x40>
lea    0xc(%rsp),%r15
mov    %r15,%rdi
mov    $0x2,%esi
mov    $0x1,%edx
mov    %r13,%rcx
call   1030 <fread@plt>
movzwl 0xc(%rsp),%edx
lea    0xe64(%rip),%r12
mov    %r14,%rdi
mov    %r12,%rsi
call   1060 <sprintf@plt>
mov    %r15,%rdi
mov    $0x2,%esi
mov    $0x1,%edx
mov    %r13,%rcx
call   1030 <fread@plt>
lea    0x4(%r14),%rdi
movzwl 0xc(%rsp),%edx
mov    %r12,%rsi
call   1060 <sprintf@plt>
mov    %r15,%rdi
mov    $0x2,%esi
mov    $0x1,%edx
mov    %r13,%rcx
call   1030 <fread@plt>
lea    0x8(%r14),%rdi
movzwl 0xc(%rsp),%edx
mov    %r12,%rsi
call   1060 <sprintf@plt>
mov    %r15,%rdi
mov    $0x2,%esi
mov    $0x1,%edx
mov    %r13,%rcx
call   1030 <fread@plt>
lea    0xc(%r14),%rdi
movzwl 0xc(%rsp),%edx
mov    %r12,%rsi
call   1060 <sprintf@plt>
mov    %r15,%rdi
mov    $0x2,%esi
mov    $0x1,%edx
mov    %r13,%rcx
call   1030 <fread@plt>
lea    0x10(%r14),%rdi
movzwl 0xc(%rsp),%edx
mov    %r12,%rsi
call   1060 <sprintf@plt>
mov    %r15,%rdi
mov    $0x2,%esi
mov    $0x1,%edx
mov    %r13,%rcx
call   1030 <fread@plt>
add    $0x14,%r14
movzwl 0xc(%rsp),%edx
mov    %r14,%rdi
mov    %r12,%rsi
call   1060 <sprintf@plt>
xor    %eax,%eax
jmp    12ab <get_mirror_tag+0x16b>
lea    0xd65(%rip),%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@.str.1 = [40 x i8] c"Error while opening %s Mir:ror device.\0A\00"
@.str.2 = [5 x i8] c"%04X\00"
define i32 @get_mirror_tag(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i16
%8 = alloca i16
%9 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%10 = load i8*, i8** %5
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %6
%12 = load i32*, i32** %6
%13 = icmp ne i32* %12, null
br i1 %13, label %17, label %14
14: ; preds = %2
%15 = load i8*, i8** %5
%16 = call i32 @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i8* %15)
store i32 -1, i32* %3
br label %43
17: ; preds = %2
br label %18
18: ; preds = %22, %17
%19 = load i16, i16* %8
%20 = zext i16 %19 to i32
%21 = icmp ne i32 %20, 258
br i1 %21, label %22, label %25
22: ; preds = %18
%23 = load i32*, i32** %6
%24 = call i32 @fread(i16* %8, i32 2, i32 1, i32* %23)
br label %18
25: ; preds = %18
store i32 0, i32* %9
br label %26
26: ; preds = %39, %25
%27 = load i32, i32* %9
%28 = icmp slt i32 %27, 6
br i1 %28, label %29, label %42
29: ; preds = %26
%30 = load i32*, i32** %6
%31 = call i32 @fread(i16* %7, i32 2, i32 1, i32* %30)
%32 = load i8*, i8** %4
%33 = load i32, i32* %9
%34 = mul nsw i32 %33, 4
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %32, i64 %35
%37 = load i16, i16* %7
%38 = call i32 @sprintf(i8* %36, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i16 zeroext %37)
br label %39
39: ; preds = %29
%40 = load i32, i32* %9
%41 = add nsw i32 %40, 1
store i32 %41, i32* %9
br label %26
42: ; preds = %26
store i32 0, i32* %3
br label %43
43: ; preds = %42, %14
%44 = load i32, i32* %3
ret i32 %44
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@.str.1 = [40 x i8] c"Error while opening %s Mir:ror device.\0A\00"
@.str.2 = [5 x i8] c"%04X\00"
define i32 @get_mirror_tag(i8* nocapture writeonly %0, i8* %1) {
%3 = alloca i16
%4 = alloca i16
%5 = bitcast i16* %3 to i8*
%6 = bitcast i16* %4 to i8*
%7 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %35
9: ; preds = %2
%10 = tail call i32 @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i8* %1)
br label %39
11: ; preds = %35
%12 = call i32 @fread(i16* %3, i32 2, i32 1, i32* %7)
%13 = load i16, i16* %3
%14 = tail call i32 @sprintf(i8* dereferenceable(1) %0, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i16 zeroext %13)
%15 = call i32 @fread(i16* %3, i32 2, i32 1, i32* %7)
%16 = getelementptr inbounds i8, i8* %0, i64 4
%17 = load i16, i16* %3
%18 = tail call i32 @sprintf(i8* dereferenceable(1) %16, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i16 zeroext %17)
%19 = call i32 @fread(i16* %3, i32 2, i32 1, i32* %7)
%20 = getelementptr inbounds i8, i8* %0, i64 8
%21 = load i16, i16* %3
%22 = tail call i32 @sprintf(i8* dereferenceable(1) %20, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i16 zeroext %21)
%23 = call i32 @fread(i16* %3, i32 2, i32 1, i32* %7)
%24 = getelementptr inbounds i8, i8* %0, i64 12
%25 = load i16, i16* %3
%26 = tail call i32 @sprintf(i8* dereferenceable(1) %24, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i16 zeroext %25)
%27 = call i32 @fread(i16* %3, i32 2, i32 1, i32* %7)
%28 = getelementptr inbounds i8, i8* %0, i64 16
%29 = load i16, i16* %3
%30 = tail call i32 @sprintf(i8* dereferenceable(1) %28, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i16 zeroext %29)
%31 = call i32 @fread(i16* %3, i32 2, i32 1, i32* %7)
%32 = getelementptr inbounds i8, i8* %0, i64 20
%33 = load i16, i16* %3
%34 = tail call i32 @sprintf(i8* dereferenceable(1) %32, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i16 zeroext %33)
br label %39
35: ; preds = %2, %35
%36 = call i32 @fread(i16* %4, i32 2, i32 1, i32* %7)
%37 = load i16, i16* %4
%38 = icmp eq i16 %37, 258
br i1 %38, label %11, label %35
39: ; preds = %11, %9
%40 = phi i32 [ -1, %9 ], [ 0, %11 ]
ret i32 %40
}