#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 */
struct test {
  int dummy;
};
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int BUFSIZ;
scalar_t__ EINTR;
int /*<<< orphan*/ SEEK_SET;
scalar_t__ errno;
scalar_t__ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
size_t read(int, char *, int);
int /*<<< orphan*/ strerror(scalar_t__);
int /*<<< orphan*/ test_abort(struct test *, char *, int, ...);
size_t write(int, char *, size_t);
size_t write_file(struct test *test, int outfd, int infd, int *endnl) {
  char buf[BUFSIZ];
  size_t rcnt, wcnt;
  size_t total = 0;
  if (lseek(infd, 0, SEEK_SET) < 0) {
    test_abort(test, "write_file lseek on %d: %s\n", infd, strerror(errno));
  }
  do {
    do {
      rcnt = read(infd, buf, sizeof(buf));
    } while (rcnt < 0 && errno == EINTR);
    if (rcnt > 0) {
      if (endnl)
        *endnl = (buf[rcnt - 1] == '\n');
      do {
        wcnt = write(outfd, buf, rcnt);
      } while (wcnt < 0 && errno == EINTR);
      if (wcnt < 0) {
        test_abort(test, "write_file got %s while writing!", strerror(errno));
        break;
      }
      total += rcnt;
    } else if (rcnt < 0) {
      test_abort(test, "write_file got %s while reading!", strerror(errno));
      break;
    }
  } while (rcnt);
  return total;
}


x86 assembly (clang, -O0):
<write_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
mov    0x2e70(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x50(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
movq   $0x0,-0x40(%rbp)
mov    -0x10(%rbp),%edi
mov    0x2e25(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1040 <lseek@plt>
cmp    $0x0,%rax
jge    11f2 <write_file+0xa2>
mov    -0x8(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x54(%rbp)
mov    0x2e05(%rip),%rax
mov    (%rax),%rdi
call   1070 <strerror@plt>
mov    -0x60(%rbp),%rdi
mov    -0x54(%rbp),%edx
mov    %eax,%ecx
lea    0xe15(%rip),%rsi
mov    $0x0,%al
call   1060 <test_abort@plt>
jmp    11f7 <write_file+0xa7>
jmp    11fc <write_file+0xac>
mov    -0x48(%rbp),%rsi
mov    -0x50(%rbp),%rax
mov    -0x10(%rbp),%edi
mov    %eax,%edx
call   1050 <read@plt>
mov    %rax,-0x30(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x30(%rbp)
mov    %al,-0x61(%rbp)
jae    123c <write_file+0xec>
mov    0x2daf(%rip),%rax
mov    (%rax),%rax
mov    0x2d95(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x61(%rbp)
mov    -0x61(%rbp),%al
test   $0x1,%al
jne    11fc <write_file+0xac>
cmpq   $0x0,-0x30(%rbp)
jbe    1318 <write_file+0x1c8>
cmpq   $0x0,-0x18(%rbp)
je     127e <write_file+0x12e>
mov    -0x48(%rbp),%rax
mov    -0x30(%rbp),%rcx
sub    $0x1,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
sete   %al
and    $0x1,%al
movzbl %al,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
jmp    1283 <write_file+0x133>
mov    -0x48(%rbp),%rsi
mov    -0xc(%rbp),%edi
mov    -0x30(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x38(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x38(%rbp)
mov    %al,-0x62(%rbp)
jae    12c1 <write_file+0x171>
mov    0x2d2a(%rip),%rax
mov    (%rax),%rax
mov    0x2d10(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x62(%rbp)
mov    -0x62(%rbp),%al
test   $0x1,%al
jne    1283 <write_file+0x133>
cmpq   $0x0,-0x38(%rbp)
jae    1307 <write_file+0x1b7>
mov    -0x8(%rbp),%rax
mov    %rax,-0x70(%rbp)
mov    0x2cf2(%rip),%rax
mov    (%rax),%rdi
call   1070 <strerror@plt>
mov    -0x70(%rbp),%rdi
mov    %eax,%edx
lea    0xd21(%rip),%rsi
mov    $0x0,%al
call   1060 <test_abort@plt>
jmp    1368 <write_file+0x218>
mov    -0x30(%rbp),%rax
add    -0x40(%rbp),%rax
mov    %rax,-0x40(%rbp)
jmp    1358 <write_file+0x208>
cmpq   $0x0,-0x30(%rbp)
jae    1353 <write_file+0x203>
mov    -0x8(%rbp),%rax
mov    %rax,-0x78(%rbp)
mov    0x2ca6(%rip),%rax
mov    (%rax),%rdi
call   1070 <strerror@plt>
mov    -0x78(%rbp),%rdi
mov    %eax,%edx
lea    0xcf6(%rip),%rsi
mov    $0x0,%al
call   1060 <test_abort@plt>
jmp    1368 <write_file+0x218>
jmp    1358 <write_file+0x208>
jmp    135d <write_file+0x20d>
cmpq   $0x0,-0x30(%rbp)
jne    11f7 <write_file+0xa7>
mov    -0x40(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_file>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rcx,-0x38(%rbp)
mov    %edx,%r14d
mov    %esi,-0x2c(%rbp)
mov    %rdi,%r12
mov    0x2e6b(%rip),%rax
mov    (%rax),%r15d
mov    %rsp,%rbx
mov    %r15,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    0x2e3d(%rip),%rax
mov    (%rax),%edx
mov    %r14d,%edi
xor    %esi,%esi
call   1040 <lseek@plt>
test   %rax,%rax
jns    11c9 <write_file+0x79>
mov    0x2e2d(%rip),%rax
mov    (%rax),%rdi
call   1070 <strerror@plt>
lea    0xe46(%rip),%rsi
mov    %r12,%rdi
mov    %r14d,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1060 <test_abort@plt>
mov    %r14d,%r12d
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %r15d,%edx
call   1050 <read@plt>
test   %rax,%rax
je     122f <write_file+0xdf>
mov    %rax,%r14
mov    %r15,%r13
xor    %r15d,%r15d
jmp    1217 <write_file+0xc7>
nopw   0x0(%rax,%rax,1)
mov    -0x2c(%rbp),%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1030 <write@plt>
add    %r14,%r15
mov    %r12d,%edi
mov    %rbx,%rsi
mov    %r13d,%edx
call   1050 <read@plt>
mov    %rax,%r14
test   %rax,%rax
je     1232 <write_file+0xe2>
mov    -0x38(%rbp),%rcx
test   %rcx,%rcx
je     11f0 <write_file+0xa0>
xor    %eax,%eax
cmpb   $0xa,-0x1(%r14,%rbx,1)
sete   %al
mov    %eax,(%rcx)
jmp    11f0 <write_file+0xa0>
xor    %r15d,%r15d
mov    %r15,%rax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.test = type { i32 }
@BUFSIZ = global i32 0
@SEEK_SET = global i32 0
@.str = [28 x i8] c"write_file lseek on %d: %s\0A\00"
@errno = global i64 0
@EINTR = global i64 0
@.str.1 = [33 x i8] c"write_file got %s while writing!\00"
@.str.2 = [33 x i8] c"write_file got %s while reading!\00"
define i64 @write_file(%struct.test* %0, i32 %1, i32 %2, i32* %3) {
%5 = alloca %struct.test*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i64
%12 = alloca i64
%13 = alloca i64
store %struct.test* %0, %struct.test** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32* %3, i32** %8
%14 = load i32, i32* @BUFSIZ
%15 = zext i32 %14 to i64
%16 = call i8* @llvm.stacksave()
store i8* %16, i8** %9
%17 = alloca i8, i64 %15
store i64 %15, i64* %10
store i64 0, i64* %13
%18 = load i32, i32* %7
%19 = load i32, i32* @SEEK_SET
%20 = call i64 @lseek(i32 %18, i32 0, i32 %19)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %28
22: ; preds = %4
%23 = load %struct.test*, %struct.test** %5
%24 = load i32, i32* %7
%25 = load i64, i64* @errno
%26 = call i32 @strerror(i64 %25)
%27 = call i32 (%struct.test*, i8*, i32, ...) @test_abort(%struct.test* %23, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 %24, i32 %26)
br label %28
28: ; preds = %22, %4
br label %29
29: ; preds = %94, %28
br label %30
30: ; preds = %41, %29
%31 = load i32, i32* %7
%32 = trunc i64 %15 to i32
%33 = call i64 @read(i32 %31, i8* %17, i32 %32)
store i64 %33, i64* %11
br label %34
34: ; preds = %30
%35 = load i64, i64* %11
%36 = icmp ult i64 %35, 0
br i1 %36, label %37, label %41
37: ; preds = %34
%38 = load i64, i64* @errno
%39 = load i64, i64* @EINTR
%40 = icmp eq i64 %38, %39
br label %41
41: ; preds = %37, %34
%42 = phi i1 [ false, %34 ], [ %40, %37 ]
br i1 %42, label %30, label %43
43: ; preds = %41
%44 = load i64, i64* %11
%45 = icmp ugt i64 %44, 0
br i1 %45, label %46, label %84
46: ; preds = %43
%47 = load i32*, i32** %8
%48 = icmp ne i32* %47, null
br i1 %48, label %49, label %58
49: ; preds = %46
%50 = load i64, i64* %11
%51 = sub i64 %50, 1
%52 = getelementptr inbounds i8, i8* %17, i64 %51
%53 = load i8, i8* %52
%54 = sext i8 %53 to i32
%55 = icmp eq i32 %54, 10
%56 = zext i1 %55 to i32
%57 = load i32*, i32** %8
store i32 %56, i32* %57
br label %58
58: ; preds = %49, %46
br label %59
59: ; preds = %70, %58
%60 = load i32, i32* %6
%61 = load i64, i64* %11
%62 = call i64 @write(i32 %60, i8* %17, i64 %61)
store i64 %62, i64* %12
br label %63
63: ; preds = %59
%64 = load i64, i64* %12
%65 = icmp ult i64 %64, 0
br i1 %65, label %66, label %70
66: ; preds = %63
%67 = load i64, i64* @errno
%68 = load i64, i64* @EINTR
%69 = icmp eq i64 %67, %68
br label %70
70: ; preds = %66, %63
%71 = phi i1 [ false, %63 ], [ %69, %66 ]
br i1 %71, label %59, label %72
72: ; preds = %70
%73 = load i64, i64* %12
%74 = icmp ult i64 %73, 0
br i1 %74, label %75, label %80
75: ; preds = %72
%76 = load %struct.test*, %struct.test** %5
%77 = load i64, i64* @errno
%78 = call i32 @strerror(i64 %77)
%79 = call i32 (%struct.test*, i8*, i32, ...) @test_abort(%struct.test* %76, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i32 %78)
br label %97
80: ; preds = %72
%81 = load i64, i64* %11
%82 = load i64, i64* %13
%83 = add i64 %82, %81
store i64 %83, i64* %13
br label %93
84: ; preds = %43
%85 = load i64, i64* %11
%86 = icmp ult i64 %85, 0
br i1 %86, label %87, label %92
87: ; preds = %84
%88 = load %struct.test*, %struct.test** %5
%89 = load i64, i64* @errno
%90 = call i32 @strerror(i64 %89)
%91 = call i32 (%struct.test*, i8*, i32, ...) @test_abort(%struct.test* %88, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i32 %90)
br label %97
92: ; preds = %84
br label %93
93: ; preds = %92, %80
br label %94
94: ; preds = %93
%95 = load i64, i64* %11
%96 = icmp ne i64 %95, 0
br i1 %96, label %29, label %97
97: ; preds = %94, %87, %75
%98 = load i64, i64* %13
%99 = load i8*, i8** %9
ret i64 %98
}

LLVM IR (-O2):
%struct.test = type { i32 }
@BUFSIZ = global i32 0
@SEEK_SET = global i32 0
@.str = [28 x i8] c"write_file lseek on %d: %s\0A\00"
@errno = global i64 0
@EINTR = global i64 0
define i64 @write_file(%struct.test* %0, i32 %1, i32 %2, i32* writeonly %3) {
%5 = load i32, i32* @BUFSIZ
%6 = zext i32 %5 to i64
%7 = alloca i8, i64 %6
%8 = load i32, i32* @SEEK_SET
%9 = tail call i64 @lseek(i32 %2, i32 0, i32 %8)
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %15
11: ; preds = %4
%12 = load i64, i64* @errno
%13 = tail call i32 @strerror(i64 %12)
%14 = tail call i32 (%struct.test*, i8*, i32, ...) @test_abort(%struct.test* %0, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 %2, i32 %13)
br label %15
15: ; preds = %11, %4
%16 = call i64 @read(i32 %2, i8* %7, i32 %5)
%17 = icmp eq i64 %16, 0
br i1 %17, label %34, label %18
18: ; preds = %15
%19 = icmp eq i32* %3, null
br label %20
20: ; preds = %18, %29
%21 = phi i64 [ %16, %18 ], [ %32, %29 ]
%22 = phi i64 [ 0, %18 ], [ %31, %29 ]
br i1 %19, label %29, label %23
23: ; preds = %20
%24 = add i64 %21, -1
%25 = getelementptr inbounds i8, i8* %7, i64 %24
%26 = load i8, i8* %25
%27 = icmp eq i8 %26, 10
%28 = zext i1 %27 to i32
store i32 %28, i32* %3
br label %29
29: ; preds = %23, %20
%30 = call i64 @write(i32 %1, i8* %7, i64 %21)
%31 = add i64 %21, %22
%32 = call i64 @read(i32 %2, i8* %7, i32 %5)
%33 = icmp eq i64 %32, 0
br i1 %33, label %34, label %20
34: ; preds = %29, %15
%35 = phi i64 [ 0, %15 ], [ %31, %29 ]
ret i64 %35
}