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

/* Variables and functions */
int /*<<< orphan*/ errno;
int read(int, char *, int);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int /*<<< orphan*/ test_msg(char *, char *, int, int /*<<< orphan*/,
                            int /*<<< orphan*/);
int clean_sk_buf(int fd, int limit) {
  int size, ret;
  char buf[4096];
  size = 0;
  while (1) {
    ret = read(fd, buf, sizeof(buf));
    if (ret == -1) {
      test_msg("ERR: %s:%d: "
               "read"
               " (errno = %d (%s))\n",
               "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/"
               "outputs/2022-01-22/02-19-57/repos/AuthenticEshkinKot/criu/refs/"
               "heads/master/test/zdtm/live/static/socket-tcpbuf.c",
               90, errno, strerror(errno));
      return -11;
    }
    if (ret == 0)
      break;
    size += ret;
    if (limit && size >= limit)
      break;
  }
  return size;
}


x86 assembly (clang, -O0):
<clean_sk_buf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    $0x1000,%edx
call   1040 <read@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11b3 <clean_sk_buf+0x83>
mov    0x2e70(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1024(%rbp)
mov    0x2e61(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
mov    -0x1024(%rbp),%ecx
mov    %eax,%r8d
lea    0xe6a(%rip),%rdi
lea    0xe87(%rip),%rsi
mov    $0x5a,%edx
call   1030 <test_msg@plt>
movl   $0xfffffff5,-0x4(%rbp)
jmp    11f1 <clean_sk_buf+0xc1>
cmpl   $0x0,-0x14(%rbp)
jne    11c2 <clean_sk_buf+0x92>
jmp    11eb <clean_sk_buf+0xbb>
mov    -0x14(%rbp),%eax
add    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     11e6 <clean_sk_buf+0xb6>
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jl     11e6 <clean_sk_buf+0xb6>
jmp    11eb <clean_sk_buf+0xbb>
jmp    1148 <clean_sk_buf+0x18>
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<clean_sk_buf>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x1008,%rsp
mov    %esi,%ebx
mov    %edi,%r15d
xor    %ebp,%ebp
mov    %rsp,%r14
nopw   0x0(%rax,%rax,1)
mov    %r15d,%edi
mov    %r14,%rsi
mov    $0x1000,%edx
call   1040 <read@plt>
test   %eax,%eax
je     1179 <clean_sk_buf+0x49>
cmp    $0xffffffff,%eax
je     117d <clean_sk_buf+0x4d>
add    %ebp,%eax
mov    %eax,%ebp
test   %ebx,%ebx
je     1150 <clean_sk_buf+0x20>
mov    %eax,%ebp
cmp    %ebx,%eax
jl     1150 <clean_sk_buf+0x20>
jmp    11af <clean_sk_buf+0x7f>
mov    %ebp,%eax
jmp    11af <clean_sk_buf+0x7f>
mov    0x2e5c(%rip),%rax
mov    (%rax),%ebx
mov    %ebx,%edi
call   1050 <strerror@plt>
lea    0xe6c(%rip),%rdi
lea    0xe89(%rip),%rsi
mov    $0x5a,%edx
mov    %ebx,%ecx
mov    %eax,%r8d
call   1030 <test_msg@plt>
mov    $0xfffffff5,%eax
add    $0x1008,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [36 x i8] c"ERR: %s:%d: read (errno = %d (%s))\0A\00"
@.str.1 = [170 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/AuthenticEshkinKot/criu/refs/heads/master/test/zdtm/live/static/socket-tcpbuf.c\00"
@errno = global i32 0
define i32 @clean_sk_buf(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca [4096 x i8]
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 0, i32* %6
br label %9
9: ; preds = %2, %35
%10 = load i32, i32* %4
%11 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%12 = call i32 @read(i32 %10, i8* %11, i32 4096)
store i32 %12, i32* %7
%13 = load i32, i32* %7
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %20
15: ; preds = %9
%16 = load i32, i32* @errno
%17 = load i32, i32* @errno
%18 = call i32 @strerror(i32 %17)
%19 = call i32 @test_msg(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([170 x i8], [170 x i8]* @.str.1, i64 0, i64 0), i32 90, i32 %16, i32 %18)
store i32 -11, i32* %3
br label %38
20: ; preds = %9
%21 = load i32, i32* %7
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %20
br label %36
24: ; preds = %20
%25 = load i32, i32* %7
%26 = load i32, i32* %6
%27 = add nsw i32 %26, %25
store i32 %27, i32* %6
%28 = load i32, i32* %5
%29 = icmp ne i32 %28, 0
br i1 %29, label %30, label %35
30: ; preds = %24
%31 = load i32, i32* %6
%32 = load i32, i32* %5
%33 = icmp sge i32 %31, %32
br i1 %33, label %34, label %35
34: ; preds = %30
br label %36
35: ; preds = %30, %24
br label %9
36: ; preds = %34, %23
%37 = load i32, i32* %6
store i32 %37, i32* %3
br label %38
38: ; preds = %36, %15
%39 = load i32, i32* %3
ret i32 %39
}

LLVM IR (-O2):
@.str = [36 x i8] c"ERR: %s:%d: read (errno = %d (%s))\0A\00"
@.str.1 = [170 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/AuthenticEshkinKot/criu/refs/heads/master/test/zdtm/live/static/socket-tcpbuf.c\00"
@errno = global i32 0
define i32 @clean_sk_buf(i32 %0, i32 %1) {
%3 = alloca [4096 x i8]
%4 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%5 = icmp eq i32 %1, 0
br label %6
6: ; preds = %13, %2
%7 = phi i32 [ 0, %2 ], [ %14, %13 ]
%8 = call i32 @read(i32 %0, i8* %4, i32 4096)
switch i32 %8, label %13 [
i32 -1, label %9
i32 0, label %17
]
9: ; preds = %6
%10 = load i32, i32* @errno
%11 = tail call i32 @strerror(i32 %10)
%12 = tail call i32 @test_msg(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([170 x i8], [170 x i8]* @.str.1, i64 0, i64 0), i32 90, i32 %10, i32 %11)
br label %17
13: ; preds = %6
%14 = add nsw i32 %8, %7
%15 = icmp slt i32 %14, %1
%16 = select i1 %5, i1 true, i1 %15
br i1 %16, label %6, label %17
17: ; preds = %6, %13, %9
%18 = phi i32 [ -11, %9 ], [ %14, %13 ], [ %7, %6 ]
ret i32 %18
}