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

/* Variables and functions */
int /*<<< orphan*/ free(char *);
char *read_flash(int, int, int);
char *save_malloc(int);
int /*<<< orphan*/ strncpy(char *, char *, int);
const char *get_device_id(int fd) {
  char *ptr = NULL;
  unsigned long size;
  char *id = NULL;
  ptr = read_flash(fd, 0x3f8000, 0x50);
  if (ptr == NULL)
    goto err_ret;
  if (0xffffffff != ((unsigned long)(unsigned char)(ptr[4 + 0]) +
                     (((unsigned long)(unsigned char)(ptr[4 + 1])) << 8) +
                     (((unsigned long)(unsigned char)(ptr[4 + 2])) << 16) +
                     (((unsigned long)(unsigned char)(ptr[4 + 3])) << 24)))
    goto err_ret;
  if (0x694c != ((unsigned short)(unsigned char)(ptr[14 + 0]) +
                 (((unsigned short)(unsigned char)(ptr[14 + 1])) << 8)))
    goto err_ret;
  size = ((unsigned long)(unsigned char)(ptr[8 + 0]) +
          (((unsigned long)(unsigned char)(ptr[8 + 1])) << 8) +
          (((unsigned long)(unsigned char)(ptr[8 + 2])) << 16) +
          (((unsigned long)(unsigned char)(ptr[8 + 3])) << 24));
  if (size < 32)
    goto err_ret;
  id = save_malloc(16);
  strncpy(id, &ptr[32], 16 - 1);
err_ret:
  free(ptr);
  return id;
}


x86 assembly (clang, -O0):
<get_device_id>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
movq   $0x0,-0x10(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x4(%rbp),%edi
mov    $0x3f8000,%esi
mov    $0x50,%edx
call   1060 <read_flash@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1181 <get_device_id+0x41>
jmp    1265 <get_device_id+0x125>
mov    -0x10(%rbp),%rax
movzbl 0x4(%rax),%eax
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
movzbl 0x5(%rax),%eax
shl    $0x8,%rax
add    %rax,%rcx
mov    -0x10(%rbp),%rax
movzbl 0x6(%rax),%eax
shl    $0x10,%rax
add    %rax,%rcx
mov    -0x10(%rbp),%rax
movzbl 0x7(%rax),%eax
shl    $0x18,%rax
add    %rax,%rcx
mov    $0xffffffff,%eax
cmp    %rcx,%rax
je     11cb <get_device_id+0x8b>
jmp    1265 <get_device_id+0x125>
mov    -0x10(%rbp),%rax
movzbl 0xe(%rax),%eax
movzwl %ax,%ecx
mov    -0x10(%rbp),%rax
movzbl 0xf(%rax),%eax
movzwl %ax,%eax
shl    $0x8,%eax
add    %eax,%ecx
mov    $0x694c,%eax
cmp    %ecx,%eax
je     11f8 <get_device_id+0xb8>
jmp    1265 <get_device_id+0x125>
mov    -0x10(%rbp),%rax
movzbl 0x8(%rax),%eax
mov    -0x10(%rbp),%rcx
movzbl 0x9(%rcx),%ecx
shl    $0x8,%rcx
add    %rcx,%rax
mov    -0x10(%rbp),%rcx
movzbl 0xa(%rcx),%ecx
shl    $0x10,%rcx
add    %rcx,%rax
mov    -0x10(%rbp),%rcx
movzbl 0xb(%rcx),%ecx
shl    $0x18,%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
cmpq   $0x20,-0x18(%rbp)
jae    1241 <get_device_id+0x101>
jmp    1265 <get_device_id+0x125>
mov    $0x10,%edi
call   1050 <save_malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
add    $0x20,%rsi
mov    $0xf,%edx
call   1040 <strncpy@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_device_id>:
push   %r14
push   %rbx
push   %rax
mov    $0x3f8000,%esi
mov    $0x50,%edx
call   1060 <read_flash@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1172 <get_device_id+0x32>
cmpl   $0xffffffff,0x4(%rbx)
jne    1172 <get_device_id+0x32>
movzwl 0xe(%rbx),%eax
cmp    $0x694c,%eax
jne    1172 <get_device_id+0x32>
cmpl   $0x20,0x8(%rbx)
jae    1188 <get_device_id+0x48>
xor    %r14d,%r14d
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    $0x10,%edi
call   1050 <save_malloc@plt>
mov    %rax,%r14
mov    %rbx,%rsi
add    $0x20,%rsi
mov    %rax,%rdi
mov    $0xf,%edx
call   1040 <strncpy@plt>
jmp    1175 <get_device_id+0x35>

LLVM IR (-O0):
define i8* @get_device_id(i32 %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i8*
store i32 %0, i32* %2
store i8* null, i8** %3
store i8* null, i8** %5
%6 = load i32, i32* %2
%7 = call i8* @read_flash(i32 %6, i32 4161536, i32 80)
store i8* %7, i8** %3
%8 = load i8*, i8** %3
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %11
10: ; preds = %1
br label %83
11: ; preds = %1
%12 = load i8*, i8** %3
%13 = getelementptr inbounds i8, i8* %12, i64 4
%14 = load i8, i8* %13
%15 = zext i8 %14 to i64
%16 = load i8*, i8** %3
%17 = getelementptr inbounds i8, i8* %16, i64 5
%18 = load i8, i8* %17
%19 = zext i8 %18 to i64
%20 = shl i64 %19, 8
%21 = add i64 %15, %20
%22 = load i8*, i8** %3
%23 = getelementptr inbounds i8, i8* %22, i64 6
%24 = load i8, i8* %23
%25 = zext i8 %24 to i64
%26 = shl i64 %25, 16
%27 = add i64 %21, %26
%28 = load i8*, i8** %3
%29 = getelementptr inbounds i8, i8* %28, i64 7
%30 = load i8, i8* %29
%31 = zext i8 %30 to i64
%32 = shl i64 %31, 24
%33 = add i64 %27, %32
%34 = icmp ne i64 4294967295, %33
br i1 %34, label %35, label %36
35: ; preds = %11
br label %83
36: ; preds = %11
%37 = load i8*, i8** %3
%38 = getelementptr inbounds i8, i8* %37, i64 14
%39 = load i8, i8* %38
%40 = zext i8 %39 to i16
%41 = zext i16 %40 to i32
%42 = load i8*, i8** %3
%43 = getelementptr inbounds i8, i8* %42, i64 15
%44 = load i8, i8* %43
%45 = zext i8 %44 to i16
%46 = zext i16 %45 to i32
%47 = shl i32 %46, 8
%48 = add nsw i32 %41, %47
%49 = icmp ne i32 26956, %48
br i1 %49, label %50, label %51
50: ; preds = %36
br label %83
51: ; preds = %36
%52 = load i8*, i8** %3
%53 = getelementptr inbounds i8, i8* %52, i64 8
%54 = load i8, i8* %53
%55 = zext i8 %54 to i64
%56 = load i8*, i8** %3
%57 = getelementptr inbounds i8, i8* %56, i64 9
%58 = load i8, i8* %57
%59 = zext i8 %58 to i64
%60 = shl i64 %59, 8
%61 = add i64 %55, %60
%62 = load i8*, i8** %3
%63 = getelementptr inbounds i8, i8* %62, i64 10
%64 = load i8, i8* %63
%65 = zext i8 %64 to i64
%66 = shl i64 %65, 16
%67 = add i64 %61, %66
%68 = load i8*, i8** %3
%69 = getelementptr inbounds i8, i8* %68, i64 11
%70 = load i8, i8* %69
%71 = zext i8 %70 to i64
%72 = shl i64 %71, 24
%73 = add i64 %67, %72
store i64 %73, i64* %4
%74 = load i64, i64* %4
%75 = icmp ult i64 %74, 32
br i1 %75, label %76, label %77
76: ; preds = %51
br label %83
77: ; preds = %51
%78 = call i8* @save_malloc(i32 16)
store i8* %78, i8** %5
%79 = load i8*, i8** %5
%80 = load i8*, i8** %3
%81 = getelementptr inbounds i8, i8* %80, i64 32
%82 = call i32 @strncpy(i8* %79, i8* %81, i32 15)
br label %83
83: ; preds = %77, %76, %50, %35, %10
%84 = load i8*, i8** %3
%85 = call i32 @free(i8* %84)
%86 = load i8*, i8** %5
ret i8* %86
}

LLVM IR (-O2):
define i8* @get_device_id(i32 %0) {
%2 = tail call i8* @read_flash(i32 %0, i32 4161536, i32 80)
%3 = icmp eq i8* %2, null
br i1 %3, label %58, label %4
4: ; preds = %1
%5 = getelementptr inbounds i8, i8* %2, i64 4
%6 = load i8, i8* %5
%7 = zext i8 %6 to i64
%8 = getelementptr inbounds i8, i8* %2, i64 5
%9 = load i8, i8* %8
%10 = zext i8 %9 to i64
%11 = shl nuw nsw i64 %10, 8
%12 = or i64 %11, %7
%13 = getelementptr inbounds i8, i8* %2, i64 6
%14 = load i8, i8* %13
%15 = zext i8 %14 to i64
%16 = shl nuw nsw i64 %15, 16
%17 = or i64 %12, %16
%18 = getelementptr inbounds i8, i8* %2, i64 7
%19 = load i8, i8* %18
%20 = zext i8 %19 to i64
%21 = shl nuw nsw i64 %20, 24
%22 = or i64 %17, %21
%23 = icmp eq i64 %22, 4294967295
br i1 %23, label %24, label %58
24: ; preds = %4
%25 = getelementptr inbounds i8, i8* %2, i64 14
%26 = load i8, i8* %25
%27 = zext i8 %26 to i32
%28 = getelementptr inbounds i8, i8* %2, i64 15
%29 = load i8, i8* %28
%30 = zext i8 %29 to i32
%31 = shl nuw nsw i32 %30, 8
%32 = or i32 %31, %27
%33 = icmp eq i32 %32, 26956
br i1 %33, label %34, label %58
34: ; preds = %24
%35 = getelementptr inbounds i8, i8* %2, i64 8
%36 = load i8, i8* %35
%37 = zext i8 %36 to i64
%38 = getelementptr inbounds i8, i8* %2, i64 9
%39 = load i8, i8* %38
%40 = zext i8 %39 to i64
%41 = shl nuw nsw i64 %40, 8
%42 = or i64 %41, %37
%43 = getelementptr inbounds i8, i8* %2, i64 10
%44 = load i8, i8* %43
%45 = zext i8 %44 to i64
%46 = shl nuw nsw i64 %45, 16
%47 = or i64 %42, %46
%48 = getelementptr inbounds i8, i8* %2, i64 11
%49 = load i8, i8* %48
%50 = zext i8 %49 to i64
%51 = shl nuw nsw i64 %50, 24
%52 = or i64 %47, %51
%53 = icmp ult i64 %52, 32
br i1 %53, label %58, label %54
54: ; preds = %34
%55 = tail call i8* @save_malloc(i32 16)
%56 = getelementptr inbounds i8, i8* %2, i64 32
%57 = tail call i32 @strncpy(i8* %55, i8* %56, i32 15)
br label %58
58: ; preds = %34, %24, %4, %1, %54
%59 = phi i8* [ null, %1 ], [ null, %4 ], [ null, %24 ], [ null, %34 ], [ %55, %54 ]
%60 = tail call i32 @free(i8* %2)
ret i8* %59
}