#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*/ error(char *);
int strlen(char const *);
int write(int, char const *, int);
void write_data(int sock, const char *buffer) {
  int num_bytes = strlen(buffer);
  while (num_bytes > 0) {
    int num_bytes_written = write(sock, buffer, num_bytes);
    if (num_bytes_written <= 0) {
      error("write_data failed");
    }
    num_bytes -= num_bytes_written;
    buffer += num_bytes_written;
  }
}


x86 assembly (clang, -O0):
<write_data>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    119e <write_data+0x6e>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jg     117d <write_data+0x4d>
lea    0xe88(%rip),%rdi
call   1050 <error@plt>
mov    -0x18(%rbp),%ecx
mov    -0x14(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
jmp    114b <write_data+0x1b>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_data>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%rbx
mov    %edi,%r14d
mov    %rsi,%rdi
call   1040 <strlen@plt>
test   %eax,%eax
jle    1189 <write_data+0x59>
mov    %eax,%ebp
lea    0xead(%rip),%r15
jmp    116b <write_data+0x3b>
cs nopw 0x0(%rax,%rax,1)
nop
movslq %r12d,%rax
add    %rax,%rbx
sub    %r12d,%ebp
jle    1189 <write_data+0x59>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
mov    %eax,%r12d
test   %eax,%eax
jg     1160 <write_data+0x30>
mov    %r15,%rdi
call   1050 <error@plt>
jmp    1160 <write_data+0x30>
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [18 x i8] c"write_data failed\00"
define void @write_data(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %3
store i8* %1, i8** %4
%7 = load i8*, i8** %4
%8 = call i32 @strlen(i8* %7)
store i32 %8, i32* %5
br label %9
9: ; preds = %21, %2
%10 = load i32, i32* %5
%11 = icmp sgt i32 %10, 0
br i1 %11, label %12, label %29
12: ; preds = %9
%13 = load i32, i32* %3
%14 = load i8*, i8** %4
%15 = load i32, i32* %5
%16 = call i32 @write(i32 %13, i8* %14, i32 %15)
store i32 %16, i32* %6
%17 = load i32, i32* %6
%18 = icmp sle i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %12
%20 = call i32 @error(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
br label %21
21: ; preds = %19, %12
%22 = load i32, i32* %6
%23 = load i32, i32* %5
%24 = sub nsw i32 %23, %22
store i32 %24, i32* %5
%25 = load i32, i32* %6
%26 = load i8*, i8** %4
%27 = sext i32 %25 to i64
%28 = getelementptr inbounds i8, i8* %26, i64 %27
store i8* %28, i8** %4
br label %9
29: ; preds = %9
ret void
}

LLVM IR (-O2):
@.str = [18 x i8] c"write_data failed\00"
define void @write_data(i32 %0, i8* %1) {
%3 = tail call i32 @strlen(i8* %1)
%4 = icmp sgt i32 %3, 0
br i1 %4, label %5, label %17
5: ; preds = %2, %12
%6 = phi i8* [ %15, %12 ], [ %1, %2 ]
%7 = phi i32 [ %13, %12 ], [ %3, %2 ]
%8 = tail call i32 @write(i32 %0, i8* %6, i32 %7)
%9 = icmp slt i32 %8, 1
br i1 %9, label %10, label %12
10: ; preds = %5
%11 = tail call i32 @error(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
br label %12
12: ; preds = %10, %5
%13 = sub nsw i32 %7, %8
%14 = sext i32 %8 to i64
%15 = getelementptr inbounds i8, i8* %6, i64 %14
%16 = icmp sgt i32 %13, 0
br i1 %16, label %5, label %17
17: ; preds = %12, %2
ret void
}