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

/* Variables and functions */
int readcount(int *);
int readdata(size_t, char *, int);
int scmerr(int, char *, int);
int readskip(void) {
  int x;
  int n;
  char buf[2048];
  x = readcount(&n);
  if (x != (1))
    return (x);
  if (n < 0)
    return (scmerr(-1, "Invalid message count %d", n));
  while (x == (1) && n > 0) {
    x = readdata((size_t)((n > 2048) ? 2048 : n), buf, false);
    n -= ((n > 2048) ? 2048 : n);
  }
  return (x);
}


x86 assembly (clang, -O0):
<readskip>:
push   %rbp
mov    %rsp,%rbp
sub    $0x820,%rsp
lea    -0xc(%rbp),%rdi
call   1050 <readcount@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x1,-0x8(%rbp)
je     115c <readskip+0x2c>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1238 <readskip+0x108>
cmpl   $0x0,-0xc(%rbp)
jge    1182 <readskip+0x52>
mov    -0xc(%rbp),%edx
mov    $0xffffffff,%edi
lea    0xe8b(%rip),%rsi
call   1040 <scmerr@plt>
mov    %eax,-0x4(%rbp)
jmp    1238 <readskip+0x108>
jmp    1187 <readskip+0x57>
xor    %eax,%eax
cmpl   $0x1,-0x8(%rbp)
mov    %al,-0x811(%rbp)
jne    11a6 <readskip+0x76>
cmpl   $0x0,-0xc(%rbp)
setg   %al
mov    %al,-0x811(%rbp)
mov    -0x811(%rbp),%al
test   $0x1,%al
jne    11b9 <readskip+0x89>
jmp    1232 <readskip+0x102>
cmpl   $0x800,-0xc(%rbp)
jle    11d6 <readskip+0xa6>
mov    $0x800,%eax
mov    %eax,-0x818(%rbp)
jmp    11df <readskip+0xaf>
mov    -0xc(%rbp),%eax
mov    %eax,-0x818(%rbp)
mov    -0x818(%rbp),%eax
movslq %eax,%rdi
lea    -0x810(%rbp),%rsi
xor    %edx,%edx
call   1030 <readdata@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x800,-0xc(%rbp)
jle    1216 <readskip+0xe6>
mov    $0x800,%eax
mov    %eax,-0x81c(%rbp)
jmp    121f <readskip+0xef>
mov    -0xc(%rbp),%eax
mov    %eax,-0x81c(%rbp)
mov    -0x81c(%rbp),%ecx
mov    -0xc(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0xc(%rbp)
jmp    1187 <readskip+0x57>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x820,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readskip>:
push   %rbp
push   %r14
push   %rbx
sub    $0x810,%rsp
lea    0xc(%rsp),%rdi
call   1050 <readcount@plt>
cmp    $0x1,%eax
jne    11c1 <readskip+0x91>
mov    0xc(%rsp),%edx
test   %edx,%edx
js     11a9 <readskip+0x79>
je     11bc <readskip+0x8c>
mov    $0x800,%r14d
lea    0x10(%rsp),%rbx
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
cmp    $0x800,%edx
cmovae %r14d,%edx
mov    %rdx,%rdi
mov    %rbx,%rsi
xor    %edx,%edx
call   1030 <readdata@plt>
mov    0xc(%rsp),%ecx
lea    -0x800(%rcx),%edx
cmp    $0x800,%ecx
cmovl  %ebp,%edx
mov    %edx,0xc(%rsp)
cmp    $0x1,%eax
jne    11c1 <readskip+0x91>
test   %edx,%edx
jne    1170 <readskip+0x40>
jmp    11c1 <readskip+0x91>
lea    0xe50(%rip),%rsi
mov    $0xffffffff,%edi
call   1040 <scmerr@plt>
jmp    11c1 <readskip+0x91>
mov    $0x1,%eax
add    $0x810,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [25 x i8] c"Invalid message count %d\00"
define i32 @readskip() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca [2048 x i8]
%5 = call i32 @readcount(i32* %3)
store i32 %5, i32* %2
%6 = load i32, i32* %2
%7 = icmp ne i32 %6, 1
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = load i32, i32* %2
store i32 %9, i32* %1
br label %47
10: ; preds = %0
%11 = load i32, i32* %3
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %10
%14 = load i32, i32* %3
%15 = call i32 @scmerr(i32 -1, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 %14)
store i32 %15, i32* %1
br label %47
16: ; preds = %10
br label %17
17: ; preds = %41, %16
%18 = load i32, i32* %2
%19 = icmp eq i32 %18, 1
br i1 %19, label %20, label %23
20: ; preds = %17
%21 = load i32, i32* %3
%22 = icmp sgt i32 %21, 0
br label %23
23: ; preds = %20, %17
%24 = phi i1 [ false, %17 ], [ %22, %20 ]
br i1 %24, label %25, label %45
25: ; preds = %23
%26 = load i32, i32* %3
%27 = icmp sgt i32 %26, 2048
br i1 %27, label %28, label %29
28: ; preds = %25
br label %31
29: ; preds = %25
%30 = load i32, i32* %3
br label %31
31: ; preds = %29, %28
%32 = phi i32 [ 2048, %28 ], [ %30, %29 ]
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds [2048 x i8], [2048 x i8]* %4, i64 0, i64 0
%35 = call i32 @readdata(i64 %33, i8* %34, i32 0)
store i32 %35, i32* %2
%36 = load i32, i32* %3
%37 = icmp sgt i32 %36, 2048
br i1 %37, label %38, label %39
38: ; preds = %31
br label %41
39: ; preds = %31
%40 = load i32, i32* %3
br label %41
41: ; preds = %39, %38
%42 = phi i32 [ 2048, %38 ], [ %40, %39 ]
%43 = load i32, i32* %3
%44 = sub nsw i32 %43, %42
store i32 %44, i32* %3
br label %17
45: ; preds = %23
%46 = load i32, i32* %2
store i32 %46, i32* %1
br label %47
47: ; preds = %45, %13, %8
%48 = load i32, i32* %1
ret i32 %48
}

LLVM IR (-O2):
@.str = [25 x i8] c"Invalid message count %d\00"
define i32 @readskip() {
%1 = alloca i32
%2 = alloca [2048 x i8]
%3 = bitcast i32* %1 to i8*
%4 = getelementptr inbounds [2048 x i8], [2048 x i8]* %2, i64 0, i64 0
%5 = call i32 @readcount(i32* %1)
%6 = icmp eq i32 %5, 1
br i1 %6, label %7, label %27
7: ; preds = %0
%8 = load i32, i32* %1
%9 = icmp slt i32 %8, 0
br i1 %9, label %12, label %10
10: ; preds = %7
%11 = icmp eq i32 %8, 0
br i1 %11, label %27, label %14
12: ; preds = %7
%13 = call i32 @scmerr(i32 -1, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 %8)
br label %27
14: ; preds = %10, %14
%15 = phi i32 [ %23, %14 ], [ %8, %10 ]
%16 = icmp ult i32 %15, 2048
%17 = select i1 %16, i32 %15, i32 2048
%18 = zext i32 %17 to i64
%19 = call i32 @readdata(i64 %18, i8* %4, i32 0)
%20 = load i32, i32* %1
%21 = icmp slt i32 %20, 2048
%22 = add i32 %20, -2048
%23 = select i1 %21, i32 0, i32 %22
store i32 %23, i32* %1
%24 = icmp eq i32 %19, 1
%25 = icmp ne i32 %23, 0
%26 = select i1 %24, i1 %25, i1 false
br i1 %26, label %14, label %27
27: ; preds = %14, %10, %0, %12
%28 = phi i32 [ %13, %12 ], [ %5, %0 ], [ 1, %10 ], [ %19, %14 ]
ret i32 %28
}