#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*/ usleep(int);
int write(int, char *, int);
int slowWrite(int fd, char *buf, int size) {
  int res = 0;
  for (int i = 0; i < size; i++) {
    int t = write(fd, &buf[i], 1);
    if (t < 0) {
      return t;
    }
    res += t;
    usleep(10 * 1000);
  }
  return res;
}


x86 assembly (clang, -O0):
<slowWrite>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    119d <slowWrite+0x7d>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x1c(%rbp),%rax
add    %rax,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    117c <slowWrite+0x5c>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11a3 <slowWrite+0x83>
mov    -0x20(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    $0x2710,%edi
call   1040 <usleep@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1140 <slowWrite+0x20>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<slowWrite>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %edx,%edx
jle    116d <slowWrite+0x4d>
mov    %rsi,%rbx
mov    %edi,%r14d
mov    %edx,%ebp
xor    %r15d,%r15d
cs nopw 0x0(%rax,%rax,1)
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
test   %eax,%eax
js     1172 <slowWrite+0x52>
add    %eax,%r15d
mov    $0x2710,%edi
call   1040 <usleep@plt>
add    $0x1,%rbx
add    $0xffffffffffffffff,%rbp
jne    1140 <slowWrite+0x20>
jmp    1175 <slowWrite+0x55>
xor    %r15d,%r15d
jmp    1175 <slowWrite+0x55>
mov    %eax,%r15d
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @slowWrite(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 0, i32* %8
store i32 0, i32* %9
br label %11
11: ; preds = %31, %3
%12 = load i32, i32* %9
%13 = load i32, i32* %7
%14 = icmp slt i32 %12, %13
br i1 %14, label %15, label %34
15: ; preds = %11
%16 = load i32, i32* %5
%17 = load i8*, i8** %6
%18 = load i32, i32* %9
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %17, i64 %19
%21 = call i32 @write(i32 %16, i8* %20, i32 1)
store i32 %21, i32* %10
%22 = load i32, i32* %10
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %15
%25 = load i32, i32* %10
store i32 %25, i32* %4
br label %36
26: ; preds = %15
%27 = load i32, i32* %10
%28 = load i32, i32* %8
%29 = add nsw i32 %28, %27
store i32 %29, i32* %8
%30 = call i32 @usleep(i32 10000)
br label %31
31: ; preds = %26
%32 = load i32, i32* %9
%33 = add nsw i32 %32, 1
store i32 %33, i32* %9
br label %11
34: ; preds = %11
%35 = load i32, i32* %8
store i32 %35, i32* %4
br label %36
36: ; preds = %34, %24
%37 = load i32, i32* %4
ret i32 %37
}

LLVM IR (-O2):
define i32 @slowWrite(i32 %0, i8* nocapture %1, i32 %2) {
%4 = icmp sgt i32 %2, 0
br i1 %4, label %5, label %18
5: ; preds = %3
%6 = zext i32 %2 to i64
br label %7
7: ; preds = %5, %13
%8 = phi i64 [ 0, %5 ], [ %16, %13 ]
%9 = phi i32 [ 0, %5 ], [ %14, %13 ]
%10 = getelementptr inbounds i8, i8* %1, i64 %8
%11 = tail call i32 @write(i32 %0, i8* %10, i32 1)
%12 = icmp slt i32 %11, 0
br i1 %12, label %18, label %13
13: ; preds = %7
%14 = add nsw i32 %11, %9
%15 = tail call i32 @usleep(i32 10000)
%16 = add nuw nsw i64 %8, 1
%17 = icmp eq i64 %16, %6
br i1 %17, label %18, label %7
18: ; preds = %13, %7, %3
%19 = phi i32 [ 0, %3 ], [ %11, %7 ], [ %14, %13 ]
ret i32 %19
}