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

/* Variables and functions */
scalar_t__ EAGAIN;
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int O_NONBLOCK;
scalar_t__ errno;
int fcntl(int, int /*<<< orphan*/, int);
int /*<<< orphan*/ test_msg(char *, char *, int, scalar_t__);
int write(int, char *, int);
int fill_sock_buf(int fd) {
  int flags;
  int size;
  int ret;
  flags = fcntl(fd, F_GETFL, 0);
  if (flags == -1) {
    test_msg("ERR: %s:%d: "
             "Can't get flags"
             " (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",
             52, errno);
    return -1;
  }
  if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1) {
    test_msg("ERR: %s:%d: "
             "Can't set flags"
             " (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",
             56, errno);
    return -1;
  }
  size = 0;
  while (1) {
    char zdtm[] = "zdtm test packet";
    ret = write(fd, zdtm, sizeof(zdtm));
    if (ret == -1) {
      if (errno == EAGAIN)
        break;
      test_msg("ERR: %s:%d: "
               "write"
               " (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",
               67, errno);
      return -1;
    }
    size += ret;
  }
  if (fcntl(fd, F_SETFL, flags) == -1) {
    test_msg("ERR: %s:%d: "
             "Can't set flags"
             " (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",
             74, errno);
    return -1;
  }
  return size;
}


x86 assembly (clang, -O0):
<fill_sock_buf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e83(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    1189 <fill_sock_buf+0x59>
mov    0x2e6e(%rip),%rax
mov    (%rax),%rcx
lea    0xe94(%rip),%rdi
lea    0xebc(%rip),%rsi
mov    $0x34,%edx
call   1050 <test_msg@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12cd <fill_sock_buf+0x19d>
mov    -0x8(%rbp),%edi
mov    0x2e45(%rip),%rax
mov    (%rax),%esi
mov    -0xc(%rbp),%edx
mov    0x2e49(%rip),%rax
or     (%rax),%edx
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    11dd <fill_sock_buf+0xad>
mov    0x2e1a(%rip),%rax
mov    (%rax),%rcx
lea    0xf10(%rip),%rdi
lea    0xe68(%rip),%rsi
mov    $0x38,%edx
call   1050 <test_msg@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12cd <fill_sock_buf+0x19d>
movl   $0x0,-0x10(%rbp)
mov    0xf45(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0xf42(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    0xf40(%rip),%al
mov    %al,-0x20(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x11,%edx
call   1040 <write@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    126e <fill_sock_buf+0x13e>
mov    0x2da8(%rip),%rax
mov    (%rax),%rax
mov    0x2d86(%rip),%rcx
cmp    (%rcx),%rax
jne    1240 <fill_sock_buf+0x110>
jmp    127c <fill_sock_buf+0x14c>
mov    0x2d89(%rip),%rax
mov    (%rax),%rcx
lea    0xeae(%rip),%rdi
lea    0xdd7(%rip),%rsi
mov    $0x43,%edx
call   1050 <test_msg@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12cd <fill_sock_buf+0x19d>
mov    -0x14(%rbp),%eax
add    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
jmp    11e4 <fill_sock_buf+0xb4>
mov    -0x8(%rbp),%edi
mov    0x2d52(%rip),%rax
mov    (%rax),%esi
mov    -0xc(%rbp),%edx
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    12c7 <fill_sock_buf+0x197>
mov    0x2d30(%rip),%rax
mov    (%rax),%rcx
lea    0xe26(%rip),%rdi
lea    0xd7e(%rip),%rsi
mov    $0x4a,%edx
call   1050 <test_msg@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12cd <fill_sock_buf+0x19d>
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fill_sock_buf>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %edi,%ebx
mov    0x2e83(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
je     1217 <fill_sock_buf+0xe7>
mov    %eax,%r14d
mov    0x2e77(%rip),%r12
mov    (%r12),%esi
mov    0x2e7c(%rip),%rax
mov    (%rax),%edx
or     %r14d,%edx
mov    %ebx,%edi
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
je     1236 <fill_sock_buf+0x106>
movaps 0xfa8(%rip),%xmm0
movaps %xmm0,(%rsp)
movb   $0x0,0x10(%rsp)
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x11,%edx
call   1040 <write@plt>
xor    %ebp,%ebp
cmp    $0xffffffff,%eax
je     11d6 <fill_sock_buf+0xa6>
mov    %rsp,%r15
nopw   0x0(%rax,%rax,1)
add    %eax,%ebp
movaps 0xf77(%rip),%xmm0
movaps %xmm0,(%rsp)
movb   $0x0,0x10(%rsp)
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x11,%edx
call   1040 <write@plt>
cmp    $0xffffffff,%eax
jne    11b0 <fill_sock_buf+0x80>
mov    0x2df3(%rip),%r15
mov    (%r15),%rcx
mov    0x2dd1(%rip),%rax
cmp    (%rax),%rcx
jne    1255 <fill_sock_buf+0x125>
mov    (%r12),%esi
mov    %ebx,%edi
mov    %r14d,%edx
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    1272 <fill_sock_buf+0x142>
mov    (%r15),%rcx
lea    0xec7(%rip),%rdi
lea    0xe1f(%rip),%rsi
mov    $0x4a,%edx
jmp    1268 <fill_sock_buf+0x138>
mov    0x2db2(%rip),%rax
mov    (%rax),%rcx
lea    0xdd8(%rip),%rdi
lea    0xe00(%rip),%rsi
mov    $0x34,%edx
jmp    1268 <fill_sock_buf+0x138>
mov    0x2d93(%rip),%rax
mov    (%rax),%rcx
lea    0xe89(%rip),%rdi
lea    0xde1(%rip),%rsi
mov    $0x38,%edx
jmp    1268 <fill_sock_buf+0x138>
lea    0xea3(%rip),%rdi
lea    0xdcc(%rip),%rsi
mov    $0x43,%edx
call   1050 <test_msg@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@F_GETFL = global i32 0
@.str = [47 x i8] c"ERR: %s:%d: Can't get flags (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 i64 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@.str.2 = [47 x i8] c"ERR: %s:%d: Can't set flags (errno = %d (%m))\0A\00"
@__const.fill_sock_buf.zdtm = [17 x i8] c"zdtm test packet\00"
@EAGAIN = global i64 0
@.str.3 = [37 x i8] c"ERR: %s:%d: write (errno = %d (%m))\0A\00"
define i32 @fill_sock_buf(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca [17 x i8]
store i32 %0, i32* %3
%8 = load i32, i32* %3
%9 = load i32, i32* @F_GETFL
%10 = call i32 @fcntl(i32 %8, i32 %9, i32 0)
store i32 %10, i32* %4
%11 = load i32, i32* %4
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %16
13: ; preds = %1
%14 = load i64, i64* @errno
%15 = call i32 @test_msg(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([161 x i8], [161 x i8]* @.str.1, i64 0, i64 0), i32 52, i64 %14)
store i32 -1, i32* %2
br label %58
16: ; preds = %1
%17 = load i32, i32* %3
%18 = load i32, i32* @F_SETFL
%19 = load i32, i32* %4
%20 = load i32, i32* @O_NONBLOCK
%21 = or i32 %19, %20
%22 = call i32 @fcntl(i32 %17, i32 %18, i32 %21)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %27
24: ; preds = %16
%25 = load i64, i64* @errno
%26 = call i32 @test_msg(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([161 x i8], [161 x i8]* @.str.1, i64 0, i64 0), i32 56, i64 %25)
store i32 -1, i32* %2
br label %58
27: ; preds = %16
store i32 0, i32* %5
br label %28
28: ; preds = %27, %43
%29 = bitcast [17 x i8]* %7 to i8*
%30 = load i32, i32* %3
%31 = getelementptr inbounds [17 x i8], [17 x i8]* %7, i64 0, i64 0
%32 = call i32 @write(i32 %30, i8* %31, i32 17)
store i32 %32, i32* %6
%33 = load i32, i32* %6
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %43
35: ; preds = %28
%36 = load i64, i64* @errno
%37 = load i64, i64* @EAGAIN
%38 = icmp eq i64 %36, %37
br i1 %38, label %39, label %40
39: ; preds = %35
br label %47
40: ; preds = %35
%41 = load i64, i64* @errno
%42 = call i32 @test_msg(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([161 x i8], [161 x i8]* @.str.1, i64 0, i64 0), i32 67, i64 %41)
store i32 -1, i32* %2
br label %58
43: ; preds = %28
%44 = load i32, i32* %6
%45 = load i32, i32* %5
%46 = add nsw i32 %45, %44
store i32 %46, i32* %5
br label %28
47: ; preds = %39
%48 = load i32, i32* %3
%49 = load i32, i32* @F_SETFL
%50 = load i32, i32* %4
%51 = call i32 @fcntl(i32 %48, i32 %49, i32 %50)
%52 = icmp eq i32 %51, -1
br i1 %52, label %53, label %56
53: ; preds = %47
%54 = load i64, i64* @errno
%55 = call i32 @test_msg(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([161 x i8], [161 x i8]* @.str.1, i64 0, i64 0), i32 74, i64 %54)
store i32 -1, i32* %2
br label %58
56: ; preds = %47
%57 = load i32, i32* %5
store i32 %57, i32* %2
br label %58
58: ; preds = %56, %53, %40, %24, %13
%59 = load i32, i32* %2
ret i32 %59
}

LLVM IR (-O2):
@F_GETFL = global i32 0
@.str = [47 x i8] c"ERR: %s:%d: Can't get flags (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 i64 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@.str.2 = [47 x i8] c"ERR: %s:%d: Can't set flags (errno = %d (%m))\0A\00"
@__const.fill_sock_buf.zdtm = [17 x i8] c"zdtm test packet\00"
@EAGAIN = global i64 0
@.str.3 = [37 x i8] c"ERR: %s:%d: write (errno = %d (%m))\0A\00"
define i32 @fill_sock_buf(i32 %0) {
%2 = alloca [17 x i8]
%3 = load i32, i32* @F_GETFL
%4 = tail call i32 @fcntl(i32 %0, i32 %3, i32 0)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %9
6: ; preds = %1
%7 = load i64, i64* @errno
%8 = tail call i32 @test_msg(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([161 x i8], [161 x i8]* @.str.1, i64 0, i64 0), i32 52, i64 %7)
br label %42
9: ; preds = %1
%10 = load i32, i32* @F_SETFL
%11 = load i32, i32* @O_NONBLOCK
%12 = or i32 %11, %4
%13 = tail call i32 @fcntl(i32 %0, i32 %10, i32 %12)
%14 = icmp eq i32 %13, -1
br i1 %14, label %19, label %15
15: ; preds = %9
%16 = getelementptr inbounds [17 x i8], [17 x i8]* %2, i64 0, i64 0
%17 = call i32 @write(i32 %0, i8* %16, i32 17)
%18 = icmp eq i32 %17, -1
br i1 %18, label %22, label %29
19: ; preds = %9
%20 = load i64, i64* @errno
%21 = tail call i32 @test_msg(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([161 x i8], [161 x i8]* @.str.1, i64 0, i64 0), i32 56, i64 %20)
br label %42
22: ; preds = %29, %15
%23 = phi i32 [ 0, %15 ], [ %32, %29 ]
%24 = load i64, i64* @errno
%25 = load i64, i64* @EAGAIN
%26 = icmp eq i64 %24, %25
br i1 %26, label %35, label %27
27: ; preds = %22
%28 = tail call i32 @test_msg(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([161 x i8], [161 x i8]* @.str.1, i64 0, i64 0), i32 67, i64 %24)
br label %42
29: ; preds = %15, %29
%30 = phi i32 [ %33, %29 ], [ %17, %15 ]
%31 = phi i32 [ %32, %29 ], [ 0, %15 ]
%32 = add nsw i32 %30, %31
%33 = call i32 @write(i32 %0, i8* %16, i32 17)
%34 = icmp eq i32 %33, -1
br i1 %34, label %22, label %29
35: ; preds = %22
%36 = load i32, i32* @F_SETFL
%37 = tail call i32 @fcntl(i32 %0, i32 %36, i32 %4)
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %42
39: ; preds = %35
%40 = load i64, i64* @errno
%41 = tail call i32 @test_msg(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([161 x i8], [161 x i8]* @.str.1, i64 0, i64 0), i32 74, i64 %40)
br label %42
42: ; preds = %27, %35, %39, %19, %6
%43 = phi i32 [ -1, %6 ], [ -1, %19 ], [ -1, %39 ], [ %23, %35 ], [ -1, %27 ]
ret i32 %43
}