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

/* Variables and functions */
int /*<<< orphan*/ memmove(char *, char *, int);
int recvn(int, char *, int, int /*<<< orphan*/);
int search_flag(char *, int, void const *, int const, int *);
int read_flag(int fd, const void *flag, const int flag_size,
              const int max_bytes) {
  char buff[flag_size];
  int left = 0;
  int bytes;
  int ret;
  int total_bytes = 0;
  while (1) {
    bytes = sizeof(buff) - left;
    if (left > 0)
      memmove(buff, &buff[bytes], left);
    if (recvn(fd, &buff[left], bytes, 0) != bytes)
      goto err;
    ret = search_flag(buff, sizeof(buff), flag, flag_size, &left);
    if (ret == 0)
      return 0;
    if (max_bytes == 0)
      continue;
    total_bytes += (bytes - left);
    if (total_bytes >= max_bytes)
      goto err;
  }
err:
  return -1;
}


x86 assembly (clang, -O0):
<read_flag>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    -0x14(%rbp),%eax
mov    %rax,-0x50(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
movl   $0x0,-0x38(%rbp)
jmp    1182 <read_flag+0x52>
mov    -0x50(%rbp),%rax
movslq -0x2c(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jle    11b0 <read_flag+0x80>
mov    -0x48(%rbp),%rdi
movslq -0x30(%rbp),%rax
mov    %rdi,%rsi
add    %rax,%rsi
mov    -0x2c(%rbp),%edx
call   1050 <memmove@plt>
mov    -0x48(%rbp),%rsi
mov    -0x8(%rbp),%edi
movslq -0x2c(%rbp),%rax
add    %rax,%rsi
mov    -0x30(%rbp),%edx
xor    %ecx,%ecx
call   1040 <recvn@plt>
cmp    -0x30(%rbp),%eax
je     11d6 <read_flag+0xa6>
jmp    1241 <read_flag+0x111>
mov    -0x48(%rbp),%rdi
mov    -0x50(%rbp),%rax
mov    %eax,%esi
mov    -0x10(%rbp),%rdx
mov    -0x14(%rbp),%ecx
lea    -0x2c(%rbp),%r8
call   1030 <search_flag@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jne    1210 <read_flag+0xe0>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    124f <read_flag+0x11f>
cmpl   $0x0,-0x18(%rbp)
jne    121f <read_flag+0xef>
jmp    117d <read_flag+0x4d>
mov    -0x30(%rbp),%eax
sub    -0x2c(%rbp),%eax
add    -0x38(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x38(%rbp),%eax
cmp    -0x18(%rbp),%eax
jl     123c <read_flag+0x10c>
jmp    1241 <read_flag+0x111>
jmp    117d <read_flag+0x4d>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_flag>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %ecx,-0x30(%rbp)
mov    %edx,%r14d
mov    %rsi,-0x40(%rbp)
mov    %edi,%r12d
mov    %edx,%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
movl   $0x0,-0x2c(%rbp)
xor    %edx,%edx
xor    %eax,%eax
mov    $0xffffffff,%r15d
mov    %eax,-0x34(%rbp)
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r14d,%r13d
sub    %edx,%r13d
test   %edx,%edx
jle    119b <read_flag+0x6b>
movslq %r13d,%rsi
add    %rbx,%rsi
mov    %rbx,%rdi
call   1050 <memmove@plt>
mov    -0x2c(%rbp),%edx
movslq %edx,%rsi
add    %rbx,%rsi
mov    %r12d,%edi
mov    %r13d,%edx
xor    %ecx,%ecx
call   1040 <recvn@plt>
cmp    %r13d,%eax
jne    11e9 <read_flag+0xb9>
mov    %rbx,%rdi
mov    %r14d,%esi
mov    -0x40(%rbp),%rdx
mov    %r14d,%ecx
lea    -0x2c(%rbp),%r8
call   1030 <search_flag@plt>
test   %eax,%eax
je     11e6 <read_flag+0xb6>
mov    -0x2c(%rbp),%edx
cmpl   $0x0,-0x30(%rbp)
je     1180 <read_flag+0x50>
sub    %edx,%r13d
mov    -0x34(%rbp),%eax
add    %r13d,%eax
cmp    -0x30(%rbp),%eax
jl     1172 <read_flag+0x42>
jmp    11e9 <read_flag+0xb9>
xor    %r15d,%r15d
mov    %r15d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @read_flag(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 i8*
%11 = alloca i64
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
store i32 %0, i32* %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%17 = load i32, i32* %8
%18 = zext i32 %17 to i64
%19 = call i8* @llvm.stacksave()
store i8* %19, i8** %10
%20 = alloca i8, i64 %18
store i64 %18, i64* %11
store i32 0, i32* %12
store i32 0, i32* %15
br label %21
21: ; preds = %67, %56, %4
br label %22
22: ; preds = %21
%23 = load i32, i32* %12
%24 = sext i32 %23 to i64
%25 = sub i64 %18, %24
%26 = trunc i64 %25 to i32
store i32 %26, i32* %13
%27 = load i32, i32* %12
%28 = icmp sgt i32 %27, 0
br i1 %28, label %29, label %35
29: ; preds = %22
%30 = load i32, i32* %13
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %20, i64 %31
%33 = load i32, i32* %12
%34 = call i32 @memmove(i8* %20, i8* %32, i32 %33)
br label %35
35: ; preds = %29, %22
%36 = load i32, i32* %6
%37 = load i32, i32* %12
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %20, i64 %38
%40 = load i32, i32* %13
%41 = call i32 @recvn(i32 %36, i8* %39, i32 %40, i32 0)
%42 = load i32, i32* %13
%43 = icmp ne i32 %41, %42
br i1 %43, label %44, label %45
44: ; preds = %35
br label %68
45: ; preds = %35
%46 = trunc i64 %18 to i32
%47 = load i8*, i8** %7
%48 = load i32, i32* %8
%49 = call i32 @search_flag(i8* %20, i32 %46, i8* %47, i32 %48, i32* %12)
store i32 %49, i32* %14
%50 = load i32, i32* %14
%51 = icmp eq i32 %50, 0
br i1 %51, label %52, label %53
52: ; preds = %45
store i32 0, i32* %5
store i32 1, i32* %16
br label %69
53: ; preds = %45
%54 = load i32, i32* %9
%55 = icmp eq i32 %54, 0
br i1 %55, label %56, label %57
56: ; preds = %53
br label %21
57: ; preds = %53
%58 = load i32, i32* %13
%59 = load i32, i32* %12
%60 = sub nsw i32 %58, %59
%61 = load i32, i32* %15
%62 = add nsw i32 %61, %60
store i32 %62, i32* %15
%63 = load i32, i32* %15
%64 = load i32, i32* %9
%65 = icmp sge i32 %63, %64
br i1 %65, label %66, label %67
66: ; preds = %57
br label %68
67: ; preds = %57
br label %21
68: ; preds = %66, %44
store i32 -1, i32* %5
store i32 1, i32* %16
br label %69
69: ; preds = %68, %52
%70 = load i8*, i8** %10
%71 = load i32, i32* %5
ret i32 %71
}

LLVM IR (-O2):
define i32 @read_flag(i32 %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = zext i32 %2 to i64
%7 = alloca i8, i64 %6
%8 = bitcast i32* %5 to i8*
store i32 0, i32* %5
%9 = icmp eq i32 %3, 0
br label %10
10: ; preds = %4, %33
%11 = phi i32 [ 0, %4 ], [ %32, %33 ]
%12 = phi i32 [ 0, %4 ], [ %35, %33 ]
br label %13
13: ; preds = %31, %10
%14 = phi i32 [ %11, %10 ], [ %32, %31 ]
%15 = sub i32 %2, %14
%16 = icmp sgt i32 %14, 0
br i1 %16, label %17, label %22
17: ; preds = %13
%18 = sext i32 %15 to i64
%19 = getelementptr inbounds i8, i8* %7, i64 %18
%20 = call i32 @memmove(i8* %7, i8* %19, i32 %14)
%21 = load i32, i32* %5
br label %22
22: ; preds = %17, %13
%23 = phi i32 [ %21, %17 ], [ %14, %13 ]
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %7, i64 %24
%26 = call i32 @recvn(i32 %0, i8* %25, i32 %15, i32 0)
%27 = icmp eq i32 %26, %15
br i1 %27, label %28, label %37
28: ; preds = %22
%29 = call i32 @search_flag(i8* %7, i32 %2, i8* %1, i32 %2, i32* %5)
%30 = icmp eq i32 %29, 0
br i1 %30, label %37, label %31
31: ; preds = %28
%32 = load i32, i32* %5
br i1 %9, label %13, label %33
33: ; preds = %31
%34 = sub nsw i32 %15, %32
%35 = add nsw i32 %34, %12
%36 = icmp slt i32 %35, %3
br i1 %36, label %10, label %37
37: ; preds = %33, %22, %28
%38 = phi i32 [ -1, %22 ], [ 0, %28 ], [ -1, %33 ]
ret i32 %38
}