#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;
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");
      continue;
    }
    if (read(fd, &sa_magic, sizeof(sa_magic)) < 0) {
      perror("read magic");
      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");
        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    0xe9a(%rip),%rsi
mov    $0x30,%edx
call   1060 <memcpy@plt>
movl   $0x0,-0x10(%rbp)
movslq -0x10(%rbp),%rax
cmp    $0x3,%rax
jae    12a9 <seek_magic_and_reposition_for_hostname+0x159>
mov    -0xc(%rbp),%edi
movslq -0x10(%rbp),%rcx
lea    -0x50(%rbp),%rax
shl    $0x4,%rcx
add    %rcx,%rax
mov    (%rax),%ax
mov    0x2e3f(%rip),%rcx
mov    (%rcx),%edx
movzwl %ax,%esi
call   1040 <lseek@plt>
cmp    $0x0,%rax
jge    11c6 <seek_magic_and_reposition_for_hostname+0x76>
lea    0xe74(%rip),%rdi
call   1070 <perror@plt>
jmp    129b <seek_magic_and_reposition_for_hostname+0x14b>
mov    -0xc(%rbp),%edi
lea    -0x12(%rbp),%rsi
mov    $0x2,%edx
call   1050 <read@plt>
cmp    $0x0,%rax
jge    11f2 <seek_magic_and_reposition_for_hostname+0xa2>
lea    0xe54(%rip),%rdi
call   1070 <perror@plt>
jmp    129b <seek_magic_and_reposition_for_hostname+0x14b>
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     1230 <seek_magic_and_reposition_for_hostname+0xe0>
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    1296 <seek_magic_and_reposition_for_hostname+0x146>
mov    -0xc(%rbp),%edi
movslq -0x10(%rbp),%rcx
lea    -0x50(%rbp),%rax
shl    $0x4,%rcx
add    %rcx,%rax
mov    0x8(%rax),%rax
mov    0x2d93(%rip),%rcx
mov    (%rcx),%edx
movzwl %ax,%esi
call   1040 <lseek@plt>
cmp    $0x0,%rax
jge    127a <seek_magic_and_reposition_for_hostname+0x12a>
lea    0xddf(%rip),%rdi
call   1070 <perror@plt>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    12bd <seek_magic_and_reposition_for_hostname+0x16d>
movslq -0x10(%rbp),%rcx
lea    -0x50(%rbp),%rax
shl    $0x4,%rcx
add    %rcx,%rax
mov    0x8(%rax),%rax
mov    %rax,-0x8(%rbp)
jmp    12bd <seek_magic_and_reposition_for_hostname+0x16d>
jmp    129b <seek_magic_and_reposition_for_hostname+0x14b>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1177 <seek_magic_and_reposition_for_hostname+0x27>
lea    0xda6(%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    0x2e8f(%rip),%r14
mov    (%r14),%edx
xor    %esi,%esi
call   1040 <lseek@plt>
test   %rax,%rax
js     1189 <seek_magic_and_reposition_for_hostname+0x49>
lea    0xe(%rsp),%rsi
mov    %ebp,%edi
mov    $0x2,%edx
call   1050 <read@plt>
test   %rax,%rax
js     1192 <seek_magic_and_reposition_for_hostname+0x52>
movzwl 0xe(%rsp),%eax
cmp    $0xd596,%eax
jne    119e <seek_magic_and_reposition_for_hostname+0x5e>
xor    %eax,%eax
jmp    123c <seek_magic_and_reposition_for_hostname+0xfc>
lea    0xea0(%rip),%rdi
jmp    1199 <seek_magic_and_reposition_for_hostname+0x59>
lea    0xea3(%rip),%rdi
call   1060 <perror@plt>
mov    (%r14),%edx
mov    %ebp,%edi
mov    $0x28,%esi
call   1040 <lseek@plt>
test   %rax,%rax
js     1278 <seek_magic_and_reposition_for_hostname+0x138>
lea    0xe(%rsp),%rsi
mov    %ebp,%edi
mov    $0x2,%edx
call   1050 <read@plt>
test   %rax,%rax
js     1289 <seek_magic_and_reposition_for_hostname+0x149>
movzwl 0xe(%rsp),%ecx
mov    $0x1,%eax
cmp    $0x216e,%ecx
je     123c <seek_magic_and_reposition_for_hostname+0xfc>
movzwl %cx,%ecx
cmp    $0x6e21,%ecx
je     123c <seek_magic_and_reposition_for_hostname+0xfc>
mov    (%r14),%edx
mov    %ebp,%edi
mov    $0x24,%esi
call   1040 <lseek@plt>
test   %rax,%rax
js     129a <seek_magic_and_reposition_for_hostname+0x15a>
lea    0xe(%rsp),%rsi
mov    %ebp,%edi
mov    $0x2,%edx
call   1050 <read@plt>
test   %rax,%rax
js     12a3 <seek_magic_and_reposition_for_hostname+0x163>
movzwl 0xe(%rsp),%ecx
mov    $0x2,%eax
cmp    $0x2169,%ecx
je     123c <seek_magic_and_reposition_for_hostname+0xfc>
movzwl %cx,%ecx
cmp    $0x6921,%ecx
jne    12af <seek_magic_and_reposition_for_hostname+0x16f>
shl    $0x4,%rax
lea    0xdb9(%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     126a <seek_magic_and_reposition_for_hostname+0x12a>
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdd6(%rip),%rdi
call   1060 <perror@plt>
jmp    12bb <seek_magic_and_reposition_for_hostname+0x17b>
lea    0xdb1(%rip),%rdi
call   1060 <perror@plt>
jmp    11ed <seek_magic_and_reposition_for_hostname+0xad>
lea    0xdac(%rip),%rdi
call   1060 <perror@plt>
jmp    11ed <seek_magic_and_reposition_for_hostname+0xad>
lea    0xd8f(%rip),%rdi
jmp    12aa <seek_magic_and_reposition_for_hostname+0x16a>
lea    0xd92(%rip),%rdi
call   1060 <perror@plt>
lea    0xda0(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffffffffffff,%rbx
jmp    125e <seek_magic_and_reposition_for_hostname+0x11e>

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 = %70, %1
%9 = load i32, i32* %4
%10 = sext i32 %9 to i64
%11 = icmp ult i64 %10, 3
br i1 %11, label %12, label %73
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 %24
22: ; preds = %12
%23 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %70
24: ; preds = %12
%25 = load i32, i32* %3
%26 = call i64 @read(i32 %25, i16* %5, i32 2)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %24
%29 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %70
30: ; preds = %24
%31 = load i16, i16* %5
%32 = zext i16 %31 to i32
%33 = load i32, i32* %4
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds [3 x %struct.magic], [3 x %struct.magic]* %6, i64 0, i64 %34
%36 = getelementptr inbounds %struct.magic, %struct.magic* %35, i32 0, i32 1
%37 = load i16, i16* %36
%38 = zext i16 %37 to i32
%39 = icmp eq i32 %32, %38
br i1 %39, label %50, label %40
40: ; preds = %30
%41 = load i16, i16* %5
%42 = zext i16 %41 to i32
%43 = load i32, i32* %4
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds [3 x %struct.magic], [3 x %struct.magic]* %6, i64 0, i64 %44
%46 = getelementptr inbounds %struct.magic, %struct.magic* %45, i32 0, i32 2
%47 = load i16, i16* %46
%48 = zext i16 %47 to i32
%49 = icmp eq i32 %42, %48
br i1 %49, label %50, label %69
50: ; preds = %40, %30
%51 = load i32, i32* %3
%52 = load i32, i32* %4
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds [3 x %struct.magic], [3 x %struct.magic]* %6, i64 0, i64 %53
%55 = getelementptr inbounds %struct.magic, %struct.magic* %54, i32 0, i32 3
%56 = load i64, i64* %55
%57 = trunc i64 %56 to i16
%58 = load i32, i32* @SEEK_SET
%59 = call i64 @lseek(i32 %51, i16 zeroext %57, i32 %58)
%60 = icmp slt i64 %59, 0
br i1 %60, label %61, label %63
61: ; preds = %50
%62 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
store i64 -1, i64* %2
br label %75
63: ; preds = %50
%64 = load i32, i32* %4
%65 = sext i32 %64 to i64
%66 = getelementptr inbounds [3 x %struct.magic], [3 x %struct.magic]* %6, i64 0, i64 %65
%67 = getelementptr inbounds %struct.magic, %struct.magic* %66, i32 0, i32 3
%68 = load i64, i64* %67
store i64 %68, i64* %2
br label %75
69: ; preds = %40
br label %70
70: ; preds = %69, %28, %22
%71 = load i32, i32* %4
%72 = add nsw i32 %71, 1
store i32 %72, i32* %4
br label %8
73: ; preds = %8
%74 = 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 %75
75: ; preds = %73, %63, %61
%76 = load i64, i64* %2
ret i64 %76
}

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 %23, 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 %23, label %10
10: ; preds = %7
%11 = load i16, i16* %2
%12 = icmp eq i16 %11, -10858
br i1 %12, label %13, label %26
13: ; preds = %45, %45, %33, %33, %10
%14 = phi i64 [ 0, %10 ], [ 1, %33 ], [ 2, %45 ], [ 1, %33 ], [ 2, %45 ]
%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 %52
21: ; preds = %13
%22 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %52
23: ; preds = %7, %1
%24 = 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 ]
%25 = tail call i32 @perror(i8* %24)
br label %26
26: ; preds = %23, %10
%27 = load i32, i32* @SEEK_SET
%28 = tail call i64 @lseek(i32 %0, i16 zeroext 40, i32 %27)
%29 = icmp slt i64 %28, 0
br i1 %29, label %35, label %30
30: ; preds = %26
%31 = call i64 @read(i32 %0, i16* %2, i32 2)
%32 = icmp slt i64 %31, 0
br i1 %32, label %35, label %33
33: ; preds = %30
%34 = load i16, i16* %2
switch i16 %34, label %38 [
i16 8558, label %13
i16 28193, label %13
]
35: ; preds = %26, %30
%36 = phi i8* [ getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), %30 ], [ getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), %26 ]
%37 = tail call i32 @perror(i8* %36)
br label %38
38: ; preds = %35, %33
%39 = load i32, i32* @SEEK_SET
%40 = tail call i64 @lseek(i32 %0, i16 zeroext 36, i32 %39)
%41 = icmp slt i64 %40, 0
br i1 %41, label %47, label %42
42: ; preds = %38
%43 = call i64 @read(i32 %0, i16* %2, i32 2)
%44 = icmp slt i64 %43, 0
br i1 %44, label %47, label %45
45: ; preds = %42
%46 = load i16, i16* %2
switch i16 %46, label %50 [
i16 8553, label %13
i16 26913, label %13
]
47: ; preds = %38, %42
%48 = phi i8* [ getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), %42 ], [ getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), %38 ]
%49 = tail call i32 @perror(i8* %48)
br label %50
50: ; preds = %47, %45
%51 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([56 x i8], [56 x i8]* @str, i64 0, i64 0))
br label %52
52: ; preds = %13, %50, %21
%53 = phi i64 [ -1, %21 ], [ -1, %50 ], [ %16, %13 ]
ret i64 %53
}