#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 off_t;

/* Variables and functions */
int /*<<< orphan*/ SEEK_CUR;
int check_offsets(int *, size_t, int);
int lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, int, size_t const, size_t const);
size_t const write(int, char *, size_t const);
int write_and_check(int fds[], size_t count, int selected, const size_t size) {
  char data[size];
  off_t new_offset;
  if (write(fds[selected], data, size) != size) {
    return -1;
  }
  if ((new_offset = lseek(fds[selected], 0, SEEK_CUR)) == -1) {
    return -1;
  }
  printf("write(fds[%d], data, %ld) = %ld\n", selected, size, size);
  return check_offsets(fds, count, new_offset);
}


x86 assembly (clang, -O0):
<write_and_check>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rsi
sub    %rcx,%rsi
mov    %rsi,%rsp
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
mov    -0x28(%rbp),%rdx
call   1030 <write@plt>
cmp    -0x28(%rbp),%rax
je     11ab <write_and_check+0x6b>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x40(%rbp)
jmp    1216 <write_and_check+0xd6>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
mov    0x2e2b(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1050 <lseek@plt>
mov    %eax,-0x3c(%rbp)
cmp    $0xffffffff,%eax
jne    11e5 <write_and_check+0xa5>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x40(%rbp)
jmp    1216 <write_and_check+0xd6>
mov    -0x1c(%rbp),%esi
mov    -0x28(%rbp),%rdx
mov    -0x28(%rbp),%rcx
lea    0xe09(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x3c(%rbp),%edx
call   1060 <check_offsets@plt>
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x40(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_and_check>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %edx,%r12d
mov    %rsi,-0x30(%rbp)
mov    %rdi,%r15
mov    %rsp,%rsi
lea    0xf(%rcx),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rsi
mov    %rsi,%rsp
movslq %edx,%rbx
mov    (%rdi,%rbx,4),%edi
mov    %rcx,%rdx
call   1030 <write@plt>
cmp    %r14,%rax
jne    11cd <write_and_check+0x8d>
mov    (%r15,%rbx,4),%edi
mov    0x2e5e(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1050 <lseek@plt>
cmp    $0xffffffff,%eax
je     11cd <write_and_check+0x8d>
mov    %eax,%r13d
lea    0xe5e(%rip),%rdi
mov    %r12d,%esi
mov    %r14,%rdx
mov    %r14,%rcx
call   1040 <printf@plt>
mov    %r15,%rdi
mov    -0x30(%rbp),%rsi
mov    %r13d,%edx
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <check_offsets@plt>
mov    $0xffffffff,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@SEEK_CUR = global i32 0
@.str = [33 x i8] c"write(fds[%d], data, %ld) = %ld\0A\00"
define i32 @write_and_check(i32* %0, i64 %1, i32 %2, i64 %3) {
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i64
%10 = alloca i8*
%11 = alloca i64
%12 = alloca i32
%13 = alloca i32
store i32* %0, i32** %6
store i64 %1, i64* %7
store i32 %2, i32* %8
store i64 %3, i64* %9
%14 = load i64, i64* %9
%15 = call i8* @llvm.stacksave()
store i8* %15, i8** %10
%16 = alloca i8, i64 %14
store i64 %14, i64* %11
%17 = load i32*, i32** %6
%18 = load i32, i32* %8
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i32, i32* %17, i64 %19
%21 = load i32, i32* %20
%22 = load i64, i64* %9
%23 = call i64 @write(i32 %21, i8* %16, i64 %22)
%24 = load i64, i64* %9
%25 = icmp ne i64 %23, %24
br i1 %25, label %26, label %27
26: ; preds = %4
store i32 -1, i32* %5
store i32 1, i32* %13
br label %46
27: ; preds = %4
%28 = load i32*, i32** %6
%29 = load i32, i32* %8
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i32, i32* %28, i64 %30
%32 = load i32, i32* %31
%33 = load i32, i32* @SEEK_CUR
%34 = call i32 @lseek(i32 %32, i32 0, i32 %33)
store i32 %34, i32* %12
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %37
36: ; preds = %27
store i32 -1, i32* %5
store i32 1, i32* %13
br label %46
37: ; preds = %27
%38 = load i32, i32* %8
%39 = load i64, i64* %9
%40 = load i64, i64* %9
%41 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i32 %38, i64 %39, i64 %40)
%42 = load i32*, i32** %6
%43 = load i64, i64* %7
%44 = load i32, i32* %12
%45 = call i32 @check_offsets(i32* %42, i64 %43, i32 %44)
store i32 %45, i32* %5
store i32 1, i32* %13
br label %46
46: ; preds = %37, %36, %26
%47 = load i8*, i8** %10
%48 = load i32, i32* %5
ret i32 %48
}

LLVM IR (-O2):
@SEEK_CUR = global i32 0
@.str = [33 x i8] c"write(fds[%d], data, %ld) = %ld\0A\00"
define i32 @write_and_check(i32* %0, i64 %1, i32 %2, i64 %3) {
%5 = alloca i8, i64 %3
%6 = sext i32 %2 to i64
%7 = getelementptr inbounds i32, i32* %0, i64 %6
%8 = load i32, i32* %7
%9 = call i64 @write(i32 %8, i8* %5, i64 %3)
%10 = icmp eq i64 %9, %3
br i1 %10, label %11, label %19
11: ; preds = %4
%12 = load i32, i32* %7
%13 = load i32, i32* @SEEK_CUR
%14 = tail call i32 @lseek(i32 %12, i32 0, i32 %13)
%15 = icmp eq i32 %14, -1
br i1 %15, label %19, label %16
16: ; preds = %11
%17 = tail call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i32 %2, i64 %3, i64 %3)
%18 = tail call i32 @check_offsets(i32* %0, i64 %1, i32 %14)
br label %19
19: ; preds = %11, %4, %16
%20 = phi i32 [ %18, %16 ], [ -1, %4 ], [ -1, %11 ]
ret i32 %20
}