#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 espera_envio(int, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int write(int, char const *, int);
int escribe(int s, const char *buffer, int longitud, int segundos) {
  int escritos_total = 0;
  int escritos_actual = 0;
  int error = 0;
  while (escritos_total < longitud && !error) {
    if (espera_envio(s, segundos) == 1) {
      escritos_actual =
          write(s, buffer + escritos_total, longitud - escritos_total);
      if (escritos_actual == 0)
        error = 1;
      else if (escritos_actual < 0) {
        perror("write");
        error = 1;
      } else
        escritos_total += escritos_actual;
    } else {
      fprintf(stderr, "La escritura tarda demasiado\n");
      error = 1;
    }
  }
  return (escritos_total == longitud);
}


x86 assembly (clang, -O0):
<escribe>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x1c(%rbp),%ecx
xor    %eax,%eax
cmp    -0x14(%rbp),%ecx
mov    %al,-0x25(%rbp)
jge    1187 <escribe+0x47>
cmpl   $0x0,-0x24(%rbp)
setne  %al
xor    $0xff,%al
mov    %al,-0x25(%rbp)
mov    -0x25(%rbp),%al
test   $0x1,%al
jne    1197 <escribe+0x57>
jmp    1233 <escribe+0xf3>
mov    -0x4(%rbp),%edi
mov    -0x18(%rbp),%esi
call   1040 <espera_envio@plt>
cmp    $0x1,%eax
jne    1212 <escribe+0xd2>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x1c(%rbp),%rax
add    %rax,%rsi
mov    -0x14(%rbp),%edx
sub    -0x1c(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jne    11dd <escribe+0x9d>
movl   $0x1,-0x24(%rbp)
jmp    120d <escribe+0xcd>
cmpl   $0x0,-0x20(%rbp)
jge    11ff <escribe+0xbf>
lea    0xe12(%rip),%rdi
call   1060 <perror@plt>
movl   $0x1,-0x24(%rbp)
jmp    1208 <escribe+0xc8>
mov    -0x20(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    120d <escribe+0xcd>
jmp    122e <escribe+0xee>
mov    0x2dc7(%rip),%rax
mov    (%rax),%edi
lea    0xde4(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x1,-0x24(%rbp)
jmp    116a <escribe+0x2a>
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
sete   %al
and    $0x1,%al
movzbl %al,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<escribe>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%r12d
test   %edx,%edx
jle    1191 <escribe+0x51>
mov    %ecx,%r14d
mov    %rsi,%r15
mov    %edi,%ebp
xor    %ebx,%ebx
nopl   0x0(%rax)
mov    %ebp,%edi
mov    %r14d,%esi
call   1040 <espera_envio@plt>
cmp    $0x1,%eax
jne    1195 <escribe+0x55>
movslq %ebx,%rax
lea    (%r15,%rax,1),%rsi
mov    %r12d,%edx
sub    %eax,%edx
mov    %ebp,%edi
call   1030 <write@plt>
test   %eax,%eax
je     11aa <escribe+0x6a>
js     11bb <escribe+0x7b>
add    %eax,%ebx
cmp    %r12d,%ebx
jl     1160 <escribe+0x20>
jmp    11aa <escribe+0x6a>
xor    %ebx,%ebx
jmp    11aa <escribe+0x6a>
mov    0x2e44(%rip),%rax
mov    (%rax),%edi
lea    0xe61(%rip),%rsi
call   1050 <fprintf@plt>
xor    %eax,%eax
cmp    %r12d,%ebx
sete   %al
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe3e(%rip),%rdi
call   1060 <perror@plt>
jmp    11aa <escribe+0x6a>

LLVM IR (-O0):
@.str = [6 x i8] c"write\00"
@stderr = global i32 0
@.str.1 = [30 x i8] c"La escritura tarda demasiado\0A\00"
define i32 @escribe(i32 %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 %3, i32* %8
store i32 0, i32* %9
store i32 0, i32* %10
store i32 0, i32* %11
br label %12
12: ; preds = %54, %4
%13 = load i32, i32* %9
%14 = load i32, i32* %7
%15 = icmp slt i32 %13, %14
br i1 %15, label %16, label %20
16: ; preds = %12
%17 = load i32, i32* %11
%18 = icmp ne i32 %17, 0
%19 = xor i1 %18, true
br label %20
20: ; preds = %16, %12
%21 = phi i1 [ false, %12 ], [ %19, %16 ]
br i1 %21, label %22, label %55
22: ; preds = %20
%23 = load i32, i32* %5
%24 = load i32, i32* %8
%25 = call i32 @espera_envio(i32 %23, i32 %24)
%26 = icmp eq i32 %25, 1
br i1 %26, label %27, label %51
27: ; preds = %22
%28 = load i32, i32* %5
%29 = load i8*, i8** %6
%30 = load i32, i32* %9
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %29, i64 %31
%33 = load i32, i32* %7
%34 = load i32, i32* %9
%35 = sub nsw i32 %33, %34
%36 = call i32 @write(i32 %28, i8* %32, i32 %35)
store i32 %36, i32* %10
%37 = load i32, i32* %10
%38 = icmp eq i32 %37, 0
br i1 %38, label %39, label %40
39: ; preds = %27
store i32 1, i32* %11
br label %50
40: ; preds = %27
%41 = load i32, i32* %10
%42 = icmp slt i32 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %40
%44 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %11
br label %49
45: ; preds = %40
%46 = load i32, i32* %10
%47 = load i32, i32* %9
%48 = add nsw i32 %47, %46
store i32 %48, i32* %9
br label %49
49: ; preds = %45, %43
br label %50
50: ; preds = %49, %39
br label %54
51: ; preds = %22
%52 = load i32, i32* @stderr
%53 = call i32 @fprintf(i32 %52, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %11
br label %54
54: ; preds = %51, %50
br label %12
55: ; preds = %20
%56 = load i32, i32* %9
%57 = load i32, i32* %7
%58 = icmp eq i32 %56, %57
%59 = zext i1 %58 to i32
ret i32 %59
}

LLVM IR (-O2):
@.str = [6 x i8] c"write\00"
@stderr = global i32 0
@.str.1 = [30 x i8] c"La escritura tarda demasiado\0A\00"
define i32 @escribe(i32 %0, i8* nocapture %1, i32 %2, i32 %3) {
%5 = icmp sgt i32 %2, 0
br i1 %5, label %6, label %26
6: ; preds = %4, %23
%7 = phi i32 [ %24, %23 ], [ 0, %4 ]
%8 = tail call i32 @espera_envio(i32 %0, i32 %3)
%9 = icmp eq i32 %8, 1
br i1 %9, label %10, label %20
10: ; preds = %6
%11 = sext i32 %7 to i64
%12 = getelementptr inbounds i8, i8* %1, i64 %11
%13 = sub nsw i32 %2, %7
%14 = tail call i32 @write(i32 %0, i8* %12, i32 %13)
%15 = icmp eq i32 %14, 0
br i1 %15, label %26, label %16
16: ; preds = %10
%17 = icmp slt i32 %14, 0
br i1 %17, label %18, label %23
18: ; preds = %16
%19 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %26
20: ; preds = %6
%21 = load i32, i32* @stderr
%22 = tail call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
br label %26
23: ; preds = %16
%24 = add nsw i32 %14, %7
%25 = icmp slt i32 %24, %2
br i1 %25, label %6, label %26
26: ; preds = %10, %23, %20, %18, %4
%27 = phi i32 [ 0, %4 ], [ %7, %18 ], [ %7, %20 ], [ %7, %10 ], [ %24, %23 ]
%28 = icmp eq i32 %27, %2
%29 = zext i1 %28 to i32
ret i32 %29
}