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

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
scalar_t__ lseek(int, unsigned short, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
scalar_t__ read(int, unsigned short *, int);
long seek_magic_and_reposition_for_hostname(int fd) {
  int i;
  unsigned short sa_magic;
  struct magic {
    unsigned short magic_offset;
    unsigned short numeric;
    unsigned short swabbed;
    unsigned long utsname_offset;
  } magic[] = {
      {0, 0xd596, 0xd596, 89},
      {40, 0x216e, 0x6e21, 113},
      {36, 0x2169, 0x6921, 109},
  };
  for (i = 0; i < sizeof(magic) / sizeof(struct magic); i++) {
    if ((lseek(fd, magic[i].magic_offset, SEEK_SET)) < 0) {
      perror("lseek magic");
      close(fd);
      continue;
    }
    if (read(fd, &sa_magic, sizeof(sa_magic)) < 0) {
      perror("read magic");
      close(fd);
      continue;
    }
    if (sa_magic == magic[i].numeric || sa_magic == magic[i].swabbed) {
      if ((lseek(fd, magic[i].utsname_offset, SEEK_SET)) < 0) {
        perror("hostname lseek");
        close(fd);
        return -1;
      }
      return magic[i].utsname_offset;
    }
  }
  printf("  non-sa data file - cannot find any sa magic - ignored\n");
  return -1;
}


x86 assembly (clang, -O0):
<seek_magic_and_reposition_for_hostname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0xc(%rbp)
lea    -0x50(%rbp),%rdi
lea    0xe8a(%rip),%rsi
mov    $0x30,%edx
call   1070 <memcpy@plt>
movl   $0x0,-0x10(%rbp)
movslq -0x10(%rbp),%rax
cmp    $0x3,%rax
jae    12d1 <seek_magic_and_reposition_for_hostname+0x171>
mov    -0xc(%rbp),%edi
movslq -0x10(%rbp),%rcx
lea    -0x50(%rbp),%rax
shl    $0x4,%rcx
add    %rcx,%rax
mov    (%rax),%ax
mov    0x2e2f(%rip),%rcx
mov    (%rcx),%edx
movzwl %ax,%esi
call   1040 <lseek@plt>
cmp    $0x0,%rax
jge    11de <seek_magic_and_reposition_for_hostname+0x7e>
lea    0xe64(%rip),%rdi
call   1080 <perror@plt>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
jmp    12c3 <seek_magic_and_reposition_for_hostname+0x163>
mov    -0xc(%rbp),%edi
lea    -0x12(%rbp),%rsi
mov    $0x2,%edx
call   1060 <read@plt>
cmp    $0x0,%rax
jge    1212 <seek_magic_and_reposition_for_hostname+0xb2>
lea    0xe3c(%rip),%rdi
call   1080 <perror@plt>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
jmp    12c3 <seek_magic_and_reposition_for_hostname+0x163>
movzwl -0x12(%rbp),%eax
movslq -0x10(%rbp),%rdx
lea    -0x50(%rbp),%rcx
shl    $0x4,%rdx
add    %rdx,%rcx
movzwl 0x2(%rcx),%ecx
cmp    %ecx,%eax
je     1250 <seek_magic_and_reposition_for_hostname+0xf0>
movzwl -0x12(%rbp),%eax
movslq -0x10(%rbp),%rdx
lea    -0x50(%rbp),%rcx
shl    $0x4,%rdx
add    %rdx,%rcx
movzwl 0x4(%rcx),%ecx
cmp    %ecx,%eax
jne    12be <seek_magic_and_reposition_for_hostname+0x15e>
mov    -0xc(%rbp),%edi
movslq -0x10(%rbp),%rcx
lea    -0x50(%rbp),%rax
shl    $0x4,%rcx
add    %rcx,%rax
mov    0x8(%rax),%rax
mov    0x2d73(%rip),%rcx
mov    (%rcx),%edx
movzwl %ax,%esi
call   1040 <lseek@plt>
cmp    $0x0,%rax
jge    12a2 <seek_magic_and_reposition_for_hostname+0x142>
lea    0xdbf(%rip),%rdi
call   1080 <perror@plt>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    12e5 <seek_magic_and_reposition_for_hostname+0x185>
movslq -0x10(%rbp),%rcx
lea    -0x50(%rbp),%rax
shl    $0x4,%rcx
add    %rcx,%rax
mov    0x8(%rax),%rax
mov    %rax,-0x8(%rbp)
jmp    12e5 <seek_magic_and_reposition_for_hostname+0x185>
jmp    12c3 <seek_magic_and_reposition_for_hostname+0x163>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1187 <seek_magic_and_reposition_for_hostname+0x27>
lea    0xd7e(%rip),%rdi
call   1030 <printf@plt>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<seek_magic_and_reposition_for_hostname>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebp
mov    0x2e7f(%rip),%r14
mov    (%r14),%edx
xor    %esi,%esi
call   1040 <lseek@plt>
test   %rax,%rax
js     1199 <seek_magic_and_reposition_for_hostname+0x49>
lea    0xe(%rsp),%rsi
mov    %ebp,%edi
mov    $0x2,%edx
call   1060 <read@plt>
test   %rax,%rax
js     11a2 <seek_magic_and_reposition_for_hostname+0x52>
movzwl 0xe(%rsp),%eax
cmp    $0xd596,%eax
jne    11b5 <seek_magic_and_reposition_for_hostname+0x65>
xor    %eax,%eax
jmp    1261 <seek_magic_and_reposition_for_hostname+0x111>
lea    0xe90(%rip),%rdi
jmp    11a9 <seek_magic_and_reposition_for_hostname+0x59>
lea    0xe93(%rip),%rdi
call   1070 <perror@plt>
mov    %ebp,%edi
call   1050 <close@plt>
mov    (%r14),%edx
mov    %ebp,%edi
mov    $0x28,%esi
call   1040 <lseek@plt>
test   %rax,%rax
js     11fe <seek_magic_and_reposition_for_hostname+0xae>
lea    0xe(%rsp),%rsi
mov    %ebp,%edi
mov    $0x2,%edx
call   1060 <read@plt>
test   %rax,%rax
js     1207 <seek_magic_and_reposition_for_hostname+0xb7>
movzwl 0xe(%rsp),%ecx
mov    $0x1,%eax
cmp    $0x216e,%ecx
je     1261 <seek_magic_and_reposition_for_hostname+0x111>
movzwl %cx,%ecx
cmp    $0x6e21,%ecx
je     1261 <seek_magic_and_reposition_for_hostname+0x111>
jmp    121a <seek_magic_and_reposition_for_hostname+0xca>
lea    0xe2b(%rip),%rdi
jmp    120e <seek_magic_and_reposition_for_hostname+0xbe>
lea    0xe2e(%rip),%rdi
call   1070 <perror@plt>
mov    %ebp,%edi
call   1050 <close@plt>
mov    (%r14),%edx
mov    %ebp,%edi
mov    $0x24,%esi
call   1040 <lseek@plt>
test   %rax,%rax
js     12a4 <seek_magic_and_reposition_for_hostname+0x154>
lea    0xe(%rsp),%rsi
mov    %ebp,%edi
mov    $0x2,%edx
call   1060 <read@plt>
test   %rax,%rax
js     12ad <seek_magic_and_reposition_for_hostname+0x15d>
movzwl 0xe(%rsp),%ecx
mov    $0x2,%eax
cmp    $0x2169,%ecx
je     1261 <seek_magic_and_reposition_for_hostname+0x111>
movzwl %cx,%ecx
cmp    $0x6921,%ecx
jne    12c0 <seek_magic_and_reposition_for_hostname+0x170>
shl    $0x4,%rax
lea    0xd94(%rip),%rcx
mov    0x8(%rax,%rcx,1),%rbx
mov    (%r14),%edx
movzwl %bx,%esi
mov    %ebp,%edi
call   1040 <lseek@plt>
test   %rax,%rax
js     128f <seek_magic_and_reposition_for_hostname+0x13f>
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdb1(%rip),%rdi
call   1070 <perror@plt>
mov    %ebp,%edi
call   1050 <close@plt>
jmp    12cc <seek_magic_and_reposition_for_hostname+0x17c>
lea    0xd85(%rip),%rdi
jmp    12b4 <seek_magic_and_reposition_for_hostname+0x164>
lea    0xd88(%rip),%rdi
call   1070 <perror@plt>
mov    %ebp,%edi
call   1050 <close@plt>
lea    0xd8f(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffffffffffff,%rbx
jmp    1283 <seek_magic_and_reposition_for_hostname+0x133>

LLVM IR (-O0):
%struct.magic = type { i16, i16, i16, i64 }
@__const.seek_magic_and_reposition_for_hostname.magic = [3 x %struct.magic] [%struct.magic { i16 0, i16 -10858, i16 -10858, i64 89 }, %struct.magic { i16 40, i16 8558, i16 28193, i64 113 }, %struct.magic { i16 36, i16 8553, i16 26913, i64 109 }]
@SEEK_SET = global i32 0
@.str = [12 x i8] c"lseek magic\00"
@.str.1 = [11 x i8] c"read magic\00"
@.str.2 = [15 x i8] c"hostname lseek\00"
@.str.3 = [57 x i8] c" non-sa data file - cannot find any sa magic - ignored\0A\00"
define i64 @seek_magic_and_reposition_for_hostname(i32 %0) {
%2 = alloca i64
%3 = alloca i32
%4 = alloca i32
%5 = alloca i16
%6 = alloca [3 x %struct.magic]
store i32 %0, i32* %3
%7 = bitcast [3 x %struct.magic]* %6 to i8*
store i32 0, i32* %4
br label %8
8: ; preds = %76, %1
%9 = load i32, i32* %4
%10 = sext i32 %9 to i64
%11 = icmp ult i64 %10, 3
br i1 %11, label %12, label %79
12: ; preds = %8
%13 = load i32, i32* %3
%14 = load i32, i32* %4
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds [3 x %struct.magic], [3 x %struct.magic]* %6, i64 0, i64 %15
%17 = getelementptr inbounds %struct.magic, %struct.magic* %16, i32 0, i32 0
%18 = load i16, i16* %17
%19 = load i32, i32* @SEEK_SET
%20 = call i64 @lseek(i32 %13, i16 zeroext %18, i32 %19)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %26
22: ; preds = %12
%23 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%24 = load i32, i32* %3
%25 = call i32 @close(i32 %24)
br label %76
26: ; preds = %12
%27 = load i32, i32* %3
%28 = call i64 @read(i32 %27, i16* %5, i32 2)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %34
30: ; preds = %26
%31 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%32 = load i32, i32* %3
%33 = call i32 @close(i32 %32)
br label %76
34: ; preds = %26
%35 = load i16, i16* %5
%36 = zext i16 %35 to i32
%37 = load i32, i32* %4
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds [3 x %struct.magic], [3 x %struct.magic]* %6, i64 0, i64 %38
%40 = getelementptr inbounds %struct.magic, %struct.magic* %39, i32 0, i32 1
%41 = load i16, i16* %40
%42 = zext i16 %41 to i32
%43 = icmp eq i32 %36, %42
br i1 %43, label %54, label %44
44: ; preds = %34
%45 = load i16, i16* %5
%46 = zext i16 %45 to i32
%47 = load i32, i32* %4
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds [3 x %struct.magic], [3 x %struct.magic]* %6, i64 0, i64 %48
%50 = getelementptr inbounds %struct.magic, %struct.magic* %49, i32 0, i32 2
%51 = load i16, i16* %50
%52 = zext i16 %51 to i32
%53 = icmp eq i32 %46, %52
br i1 %53, label %54, label %75
54: ; preds = %44, %34
%55 = load i32, i32* %3
%56 = load i32, i32* %4
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds [3 x %struct.magic], [3 x %struct.magic]* %6, i64 0, i64 %57
%59 = getelementptr inbounds %struct.magic, %struct.magic* %58, i32 0, i32 3
%60 = load i64, i64* %59
%61 = trunc i64 %60 to i16
%62 = load i32, i32* @SEEK_SET
%63 = call i64 @lseek(i32 %55, i16 zeroext %61, i32 %62)
%64 = icmp slt i64 %63, 0
br i1 %64, label %65, label %69
65: ; preds = %54
%66 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%67 = load i32, i32* %3
%68 = call i32 @close(i32 %67)
store i64 -1, i64* %2
br label %81
69: ; preds = %54
%70 = load i32, i32* %4
%71 = sext i32 %70 to i64
%72 = getelementptr inbounds [3 x %struct.magic], [3 x %struct.magic]* %6, i64 0, i64 %71
%73 = getelementptr inbounds %struct.magic, %struct.magic* %72, i32 0, i32 3
%74 = load i64, i64* %73
store i64 %74, i64* %2
br label %81
75: ; preds = %44
br label %76
76: ; preds = %75, %30, %22
%77 = load i32, i32* %4
%78 = add nsw i32 %77, 1
store i32 %78, i32* %4
br label %8
79: ; preds = %8
%80 = call i32 @printf(i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.3, i64 0, i64 0))
store i64 -1, i64* %2
br label %81
81: ; preds = %79, %69, %65
%82 = load i64, i64* %2
ret i64 %82
}

LLVM IR (-O2):
%struct.magic = type { i16, i16, i16, i64 }
@__const.seek_magic_and_reposition_for_hostname.magic = [3 x %struct.magic] [%struct.magic { i16 0, i16 -10858, i16 -10858, i64 89 }, %struct.magic { i16 40, i16 8558, i16 28193, i64 113 }, %struct.magic { i16 36, i16 8553, i16 26913, i64 109 }]
@SEEK_SET = global i32 0
@.str = [12 x i8] c"lseek magic\00"
@.str.1 = [11 x i8] c"read magic\00"
@.str.2 = [15 x i8] c"hostname lseek\00"
@str = [56 x i8] c" non-sa data file - cannot find any sa magic - ignored\00"
define i64 @seek_magic_and_reposition_for_hostname(i32 %0) {
%2 = alloca i16
%3 = bitcast i16* %2 to i8*
%4 = load i32, i32* @SEEK_SET
%5 = tail call i64 @lseek(i32 %0, i16 zeroext 0, i32 %4)
%6 = icmp slt i64 %5, 0
br i1 %6, label %24, label %7
7: ; preds = %1
%8 = call i64 @read(i32 %0, i16* %2, i32 2)
%9 = icmp slt i64 %8, 0
br i1 %9, label %24, label %10
10: ; preds = %7
%11 = load i16, i16* %2
%12 = icmp eq i16 %11, -10858
br i1 %12, label %13, label %28
13: ; preds = %48, %48, %35, %35, %10
%14 = phi i64 [ 0, %10 ], [ 1, %35 ], [ 2, %48 ], [ 1, %35 ], [ 2, %48 ]
%15 = getelementptr inbounds [3 x %struct.magic], [3 x %struct.magic]* @__const.seek_magic_and_reposition_for_hostname.magic, i64 0, i64 %14, i32 3
%16 = load i64, i64* %15
%17 = trunc i64 %16 to i16
%18 = load i32, i32* @SEEK_SET
%19 = tail call i64 @lseek(i32 %0, i16 zeroext %17, i32 %18)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %56
21: ; preds = %13
%22 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%23 = tail call i32 @close(i32 %0)
br label %56
24: ; preds = %7, %1
%25 = phi i8* [ getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), %1 ], [ getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), %7 ]
%26 = tail call i32 @perror(i8* %25)
%27 = tail call i32 @close(i32 %0)
br label %28
28: ; preds = %24, %10
%29 = load i32, i32* @SEEK_SET
%30 = tail call i64 @lseek(i32 %0, i16 zeroext 40, i32 %29)
%31 = icmp slt i64 %30, 0
br i1 %31, label %37, label %32
32: ; preds = %28
%33 = call i64 @read(i32 %0, i16* %2, i32 2)
%34 = icmp slt i64 %33, 0
br i1 %34, label %37, label %35
35: ; preds = %32
%36 = load i16, i16* %2
switch i16 %36, label %41 [
i16 8558, label %13
i16 28193, label %13
]
37: ; preds = %28, %32
%38 = phi i8* [ getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), %32 ], [ getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), %28 ]
%39 = tail call i32 @perror(i8* %38)
%40 = tail call i32 @close(i32 %0)
br label %41
41: ; preds = %37, %35
%42 = load i32, i32* @SEEK_SET
%43 = tail call i64 @lseek(i32 %0, i16 zeroext 36, i32 %42)
%44 = icmp slt i64 %43, 0
br i1 %44, label %50, label %45
45: ; preds = %41
%46 = call i64 @read(i32 %0, i16* %2, i32 2)
%47 = icmp slt i64 %46, 0
br i1 %47, label %50, label %48
48: ; preds = %45
%49 = load i16, i16* %2
switch i16 %49, label %54 [
i16 8553, label %13
i16 26913, label %13
]
50: ; preds = %41, %45
%51 = phi i8* [ getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), %45 ], [ getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), %41 ]
%52 = tail call i32 @perror(i8* %51)
%53 = tail call i32 @close(i32 %0)
br label %54
54: ; preds = %50, %48
%55 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([56 x i8], [56 x i8]* @str, i64 0, i64 0))
br label %56
56: ; preds = %13, %54, %21
%57 = phi i64 [ -1, %21 ], [ -1, %54 ], [ %16, %13 ]
ret i64 %57
}