#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 const uint8_t;

/* Variables and functions */
int read(int const, int const *, int);
size_t strlen(char const *);
scalar_t__ strncmp(char const *, char *, size_t);
bool device_check(const int sockfd, const char *device_id,
                  const int protocol_major, const int protocol_minor) {
  uint8_t buffer[1024];
  int n;
  size_t id_length = strlen(device_id);
  n = read(sockfd, buffer, 1024);
  if (n <= id_length) {
    return false;
  }
  if (strncmp(device_id, (char *)buffer, id_length) != 0) {
    return false;
  }
  if (buffer[id_length + 1] != protocol_major) {
    return false;
  }
  if (buffer[id_length + 2] != protocol_minor) {
    return false;
  }
  return true;
}


x86 assembly (clang, -O0):
<device_check>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x1030(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    $0x400,%edx
call   1050 <read@plt>
mov    %eax,-0x1024(%rbp)
movslq -0x1024(%rbp),%rax
cmp    -0x1030(%rbp),%rax
ja     1192 <device_check+0x62>
movl   $0x0,-0x4(%rbp)
jmp    120c <device_check+0xdc>
mov    -0x10(%rbp),%rdi
lea    -0x1020(%rbp),%rsi
mov    -0x1030(%rbp),%rdx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
je     11bf <device_check+0x8f>
movl   $0x0,-0x4(%rbp)
jmp    120c <device_check+0xdc>
mov    -0x1030(%rbp),%rax
mov    -0x101c(%rbp,%rax,4),%eax
cmp    -0x14(%rbp),%eax
je     11e2 <device_check+0xb2>
movl   $0x0,-0x4(%rbp)
jmp    120c <device_check+0xdc>
mov    -0x1030(%rbp),%rax
mov    -0x1018(%rbp,%rax,4),%eax
cmp    -0x18(%rbp),%eax
je     1205 <device_check+0xd5>
movl   $0x0,-0x4(%rbp)
jmp    120c <device_check+0xdc>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<device_check>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1008,%rsp
mov    %ecx,%r14d
mov    %edx,%r15d
mov    %rsi,%rbx
mov    %edi,%ebp
mov    %rsi,%rdi
call   1040 <strlen@plt>
mov    %rax,%r13
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x400,%edx
call   1050 <read@plt>
cltq
xor    %r12d,%r12d
cmp    %rax,%r13
jae    1196 <device_check+0x66>
mov    %rsp,%rsi
mov    %rbx,%rdi
mov    %r13,%rdx
call   1030 <strncmp@plt>
test   %rax,%rax
jne    1196 <device_check+0x66>
cmp    %r15d,0x4(%rsp,%r13,4)
jne    1196 <device_check+0x66>
xor    %r12d,%r12d
cmp    %r14d,0x8(%rsp,%r13,4)
sete   %r12b
mov    %r12d,%eax
add    $0x1008,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @device_check(i32 %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca [1024 x i32]
%11 = alloca i32
%12 = alloca i64
store i32 %0, i32* %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%13 = load i8*, i8** %7
%14 = call i64 @strlen(i8* %13)
store i64 %14, i64* %12
%15 = load i32, i32* %6
%16 = getelementptr inbounds [1024 x i32], [1024 x i32]* %10, i64 0, i64 0
%17 = call i32 @read(i32 %15, i32* %16, i32 1024)
store i32 %17, i32* %11
%18 = load i32, i32* %11
%19 = sext i32 %18 to i64
%20 = load i64, i64* %12
%21 = icmp ule i64 %19, %20
br i1 %21, label %22, label %23
22: ; preds = %4
store i32 0, i32* %5
br label %48
23: ; preds = %4
%24 = load i8*, i8** %7
%25 = getelementptr inbounds [1024 x i32], [1024 x i32]* %10, i64 0, i64 0
%26 = bitcast i32* %25 to i8*
%27 = load i64, i64* %12
%28 = call i64 @strncmp(i8* %24, i8* %26, i64 %27)
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %23
store i32 0, i32* %5
br label %48
31: ; preds = %23
%32 = load i64, i64* %12
%33 = add i64 %32, 1
%34 = getelementptr inbounds [1024 x i32], [1024 x i32]* %10, i64 0, i64 %33
%35 = load i32, i32* %34
%36 = load i32, i32* %8
%37 = icmp ne i32 %35, %36
br i1 %37, label %38, label %39
38: ; preds = %31
store i32 0, i32* %5
br label %48
39: ; preds = %31
%40 = load i64, i64* %12
%41 = add i64 %40, 2
%42 = getelementptr inbounds [1024 x i32], [1024 x i32]* %10, i64 0, i64 %41
%43 = load i32, i32* %42
%44 = load i32, i32* %9
%45 = icmp ne i32 %43, %44
br i1 %45, label %46, label %47
46: ; preds = %39
store i32 0, i32* %5
br label %48
47: ; preds = %39
store i32 1, i32* %5
br label %48
48: ; preds = %47, %46, %38, %30, %22
%49 = load i32, i32* %5
ret i32 %49
}

LLVM IR (-O2):
define i32 @device_check(i32 %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca [1024 x i32]
%6 = bitcast [1024 x i32]* %5 to i8*
%7 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%8 = getelementptr inbounds [1024 x i32], [1024 x i32]* %5, i64 0, i64 0
%9 = call i32 @read(i32 %0, i32* %8, i32 1024)
%10 = sext i32 %9 to i64
%11 = icmp ult i64 %7, %10
br i1 %11, label %12, label %26
12: ; preds = %4
%13 = call i64 @strncmp(i8* %1, i8* %6, i64 %7)
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %26
15: ; preds = %12
%16 = add nuw i64 %7, 1
%17 = getelementptr inbounds [1024 x i32], [1024 x i32]* %5, i64 0, i64 %16
%18 = load i32, i32* %17
%19 = icmp eq i32 %18, %2
br i1 %19, label %20, label %26
20: ; preds = %15
%21 = add i64 %7, 2
%22 = getelementptr inbounds [1024 x i32], [1024 x i32]* %5, i64 0, i64 %21
%23 = load i32, i32* %22
%24 = icmp eq i32 %23, %3
%25 = zext i1 %24 to i32
br label %26
26: ; preds = %20, %15, %12, %4
%27 = phi i32 [ 0, %4 ], [ 0, %12 ], [ 0, %15 ], [ %25, %20 ]
ret i32 %27
}