#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*/ test_msg(char *, char *, int, 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 (%m))\n",
               "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/"
               "outputs/2022-01-22/02-19-57/repos/kawamuray/criu/refs/heads/"
               "master/test/zdtm/live/static/socket-tcpbuf.c",
               90, 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    $0x1020,%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    1186 <clean_sk_buf+0x66>
mov    0x2e80(%rip),%rax
mov    (%rax),%ecx
lea    0xe97(%rip),%rdi
lea    0xeb4(%rip),%rsi
mov    $0x5a,%edx
call   1030 <test_msg@plt>
movl   $0xfffffff5,-0x4(%rbp)
jmp    11c4 <clean_sk_buf+0xa4>
cmpl   $0x0,-0x14(%rbp)
jne    1195 <clean_sk_buf+0x75>
jmp    11be <clean_sk_buf+0x9e>
mov    -0x14(%rbp),%eax
add    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     11b9 <clean_sk_buf+0x99>
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jl     11b9 <clean_sk_buf+0x99>
jmp    11be <clean_sk_buf+0x9e>
jmp    1138 <clean_sk_buf+0x18>
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1020,%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     1169 <clean_sk_buf+0x49>
cmp    $0xffffffff,%eax
je     116d <clean_sk_buf+0x4d>
add    %ebp,%eax
mov    %eax,%ebp
test   %ebx,%ebx
je     1140 <clean_sk_buf+0x20>
mov    %eax,%ebp
cmp    %ebx,%eax
jl     1140 <clean_sk_buf+0x20>
jmp    1193 <clean_sk_buf+0x73>
mov    %ebp,%eax
jmp    1193 <clean_sk_buf+0x73>
mov    0x2e6c(%rip),%rax
mov    (%rax),%ecx
lea    0xe83(%rip),%rdi
lea    0xea0(%rip),%rsi
mov    $0x5a,%edx
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 (%m))\0A\00"
@.str.1 = [161 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/kawamuray/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, %33
%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 %18
15: ; preds = %9
%16 = load i32, i32* @errno
%17 = call i32 @test_msg(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([161 x i8], [161 x i8]* @.str.1, i64 0, i64 0), i32 90, i32 %16)
store i32 -11, i32* %3
br label %36
18: ; preds = %9
%19 = load i32, i32* %7
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %18
br label %34
22: ; preds = %18
%23 = load i32, i32* %7
%24 = load i32, i32* %6
%25 = add nsw i32 %24, %23
store i32 %25, i32* %6
%26 = load i32, i32* %5
%27 = icmp ne i32 %26, 0
br i1 %27, label %28, label %33
28: ; preds = %22
%29 = load i32, i32* %6
%30 = load i32, i32* %5
%31 = icmp sge i32 %29, %30
br i1 %31, label %32, label %33
32: ; preds = %28
br label %34
33: ; preds = %28, %22
br label %9
34: ; preds = %32, %21
%35 = load i32, i32* %6
store i32 %35, i32* %3
br label %36
36: ; preds = %34, %15
%37 = load i32, i32* %3
ret i32 %37
}

LLVM IR (-O2):
@.str = [36 x i8] c"ERR: %s:%d: read (errno = %d (%m))\0A\00"
@.str.1 = [161 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/kawamuray/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 = %12, %2
%7 = phi i32 [ 0, %2 ], [ %13, %12 ]
%8 = call i32 @read(i32 %0, i8* %4, i32 4096)
switch i32 %8, label %12 [
i32 -1, label %9
i32 0, label %16
]
9: ; preds = %6
%10 = load i32, i32* @errno
%11 = tail call i32 @test_msg(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([161 x i8], [161 x i8]* @.str.1, i64 0, i64 0), i32 90, i32 %10)
br label %16
12: ; preds = %6
%13 = add nsw i32 %8, %7
%14 = icmp slt i32 %13, %1
%15 = select i1 %5, i1 true, i1 %14
br i1 %15, label %6, label %16
16: ; preds = %6, %12, %9
%17 = phi i32 [ -11, %9 ], [ %13, %12 ], [ %7, %6 ]
ret i32 %17
}