#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*/ perror(char *);
int strlen(char *);
int write(int, char *, int);
int write_to_descriptor(int desc, char *txt) {
  int sofar, thisround, total;
  total = strlen(txt);
  sofar = 0;
  do {
    thisround = write(desc, txt + sofar, total - sofar);
    if (thisround < 0) {
      perror("Write to socket");
      return (-1);
    }
    sofar += thisround;
  } while (sofar < total);
  return (0);
}


x86 assembly (clang, -O0):
<write_to_descriptor>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x1c(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x14(%rbp),%rax
add    %rax,%rsi
mov    -0x1c(%rbp),%edx
sub    -0x14(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1190 <write_to_descriptor+0x60>
lea    0xe81(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ac <write_to_descriptor+0x7c>
mov    -0x18(%rbp),%eax
add    -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jl     1152 <write_to_descriptor+0x22>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_to_descriptor>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %edi,%r14d
mov    %rsi,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebp
xor    %ebx,%ebx
nopl   0x0(%rax)
movslq %ebx,%rax
lea    (%r15,%rax,1),%rsi
mov    %ebp,%edx
sub    %eax,%edx
mov    %r14d,%edi
call   1030 <write@plt>
test   %eax,%eax
js     117a <write_to_descriptor+0x4a>
add    %eax,%ebx
cmp    %ebp,%ebx
jl     1150 <write_to_descriptor+0x20>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe7f(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%eax
jmp    116f <write_to_descriptor+0x3f>

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

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