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

/* Variables and functions */
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
int write(int, char *, int);
int test_write(int fd, char *buf, int size) {
  int ret = 0;
  while (1) {
    printf("--> write %s\n", buf);
    ret = write(fd, buf, size);
    if (ret < 0 || ret != size) {
      printf("write error: %d\n", ret);
      break;
    }
    sleep(1);
  }
  return ret;
}


x86 assembly (clang, -O0):
<test_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rsi
lea    0xeac(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jl     1183 <test_write+0x53>
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
je     1199 <test_write+0x69>
mov    -0x18(%rbp),%esi
lea    0xe81(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11a8 <test_write+0x78>
mov    $0x1,%edi
call   1050 <sleep@plt>
jmp    1149 <test_write+0x19>
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_write>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%r15d
mov    %rsi,%rbx
mov    %edi,%r14d
lea    0xeb8(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %r15d,%edx
call   1030 <write@plt>
mov    %eax,%ebp
test   %eax,%eax
js     11a0 <test_write+0x70>
cmp    %r15d,%ebp
jne    11a0 <test_write+0x70>
lea    0xe91(%rip),%r12
nop
mov    $0x1,%edi
call   1050 <sleep@plt>
mov    %r12,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %r15d,%edx
call   1030 <write@plt>
mov    %eax,%ebp
test   %eax,%eax
js     11a0 <test_write+0x70>
cmp    %r15d,%ebp
je     1170 <test_write+0x40>
lea    0xe67(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [14 x i8] c"--> write %s\0A\00"
@.str.1 = [17 x i8] c"write error: %d\0A\00"
define i32 @test_write(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
store i32 0, i32* %7
br label %8
8: ; preds = %3, %24
%9 = load i8*, i8** %5
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %9)
%11 = load i32, i32* %4
%12 = load i8*, i8** %5
%13 = load i32, i32* %6
%14 = call i32 @write(i32 %11, i8* %12, i32 %13)
store i32 %14, i32* %7
%15 = load i32, i32* %7
%16 = icmp slt i32 %15, 0
br i1 %16, label %21, label %17
17: ; preds = %8
%18 = load i32, i32* %7
%19 = load i32, i32* %6
%20 = icmp ne i32 %18, %19
br i1 %20, label %21, label %24
21: ; preds = %17, %8
%22 = load i32, i32* %7
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %22)
br label %26
24: ; preds = %17
%25 = call i32 @sleep(i32 1)
br label %8
26: ; preds = %21
%27 = load i32, i32* %7
ret i32 %27
}

LLVM IR (-O2):
@.str = [14 x i8] c"--> write %s\0A\00"
@.str.1 = [17 x i8] c"write error: %d\0A\00"
define i32 @test_write(i32 %0, i8* %1, i32 %2) {
%4 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %1)
%5 = tail call i32 @write(i32 %0, i8* %1, i32 %2)
%6 = icmp sgt i32 %5, -1
%7 = icmp eq i32 %5, %2
%8 = and i1 %6, %7
br i1 %8, label %12, label %9
9: ; preds = %12, %3
%10 = phi i32 [ %5, %3 ], [ %15, %12 ]
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %10)
ret i32 %10
12: ; preds = %3, %12
%13 = tail call i32 @sleep(i32 1)
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %1)
%15 = tail call i32 @write(i32 %0, i8* %1, i32 %2)
%16 = icmp sgt i32 %15, -1
%17 = icmp eq i32 %15, %2
%18 = and i1 %16, %17
br i1 %18, label %12, label %9
}