#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 */
scalar_t__ EINTR;
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int strlen(char *);
int write(int, char *, int);
void writestring(int fd, char *string) {
  int result, bytes = strlen(string);
  while (bytes) {
    if ((result = write(fd, string, bytes)) < 0 && errno != EINTR) {
      perror("write");
      exit(1);
    } else if (result == 0) {
      fprintf(stderr, "write: %s\n", "socket closed unexpectedly");
      exit(1);
    }
    string += result;
    bytes -= result;
  }
}


x86 assembly (clang, -O0):
<writestring>:
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,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     1216 <writestring+0xc6>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    11c0 <writestring+0x70>
mov    0x2e41(%rip),%rax
mov    (%rax),%rax
mov    0x2e2f(%rip),%rcx
cmp    (%rcx),%rax
je     11c0 <writestring+0x70>
lea    0xe4f(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
cmpl   $0x0,-0x14(%rbp)
jne    11f0 <writestring+0xa0>
mov    0x2e0f(%rip),%rax
mov    (%rax),%edi
lea    0xe2c(%rip),%rsi
lea    0xe30(%rip),%rdx
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
jmp    11f5 <writestring+0xa5>
mov    -0x14(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x14(%rbp),%ecx
mov    -0x18(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x18(%rbp)
jmp    116b <writestring+0x1b>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writestring>:
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
je     11cc <writestring+0x7c>
mov    %eax,%ebp
mov    0x2e65(%rip),%r15
mov    0x2e56(%rip),%r12
jmp    1193 <writestring+0x43>
nopl   0x0(%rax)
mov    (%r15),%rcx
cmp    (%r12),%rcx
jne    11d5 <writestring+0x85>
movslq %eax,%rcx
add    %rcx,%rbx
sub    %eax,%ebp
je     11cc <writestring+0x7c>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
test   %eax,%eax
js     1180 <writestring+0x30>
jne    1189 <writestring+0x39>
mov    0x2e33(%rip),%rax
mov    (%rax),%edi
lea    0xe50(%rip),%rsi
lea    0xe54(%rip),%rdx
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe24(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@errno = global i64 0
@EINTR = global i64 0
@.str = [6 x i8] c"write\00"
@stderr = global i32 0
@.str.1 = [11 x i8] c"write: %s\0A\00"
@.str.2 = [27 x i8] c"socket closed unexpectedly\00"
define void @writestring(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* %6
br label %9
9: ; preds = %33, %2
%10 = load i32, i32* %6
%11 = icmp ne i32 %10, 0
br i1 %11, label %12, label %41
12: ; preds = %9
%13 = load i32, i32* %3
%14 = load i8*, i8** %4
%15 = load i32, i32* %6
%16 = call i32 @write(i32 %13, i8* %14, i32 %15)
store i32 %16, i32* %5
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %25
18: ; preds = %12
%19 = load i64, i64* @errno
%20 = load i64, i64* @EINTR
%21 = icmp ne i64 %19, %20
br i1 %21, label %22, label %25
22: ; preds = %18
%23 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %18, %12
%26 = load i32, i32* %5
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %32
28: ; preds = %25
%29 = load i32, i32* @stderr
%30 = call i32 @fprintf(i32 %29, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %25
br label %33
33: ; preds = %32
%34 = load i32, i32* %5
%35 = load i8*, i8** %4
%36 = sext i32 %34 to i64
%37 = getelementptr inbounds i8, i8* %35, i64 %36
store i8* %37, i8** %4
%38 = load i32, i32* %5
%39 = load i32, i32* %6
%40 = sub nsw i32 %39, %38
store i32 %40, i32* %6
br label %9
41: ; preds = %9
ret void
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
@.str = [6 x i8] c"write\00"
@stderr = global i32 0
@.str.1 = [11 x i8] c"write: %s\0A\00"
@.str.2 = [27 x i8] c"socket closed unexpectedly\00"
define void @writestring(i32 %0, i8* %1) {
%3 = tail call i32 @strlen(i8* %1)
%4 = icmp eq i32 %3, 0
br i1 %4, label %28, label %5
5: ; preds = %2, %23
%6 = phi i32 [ %26, %23 ], [ %3, %2 ]
%7 = phi i8* [ %25, %23 ], [ %1, %2 ]
%8 = tail call i32 @write(i32 %0, i8* %7, i32 %6)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %17
10: ; preds = %5
%11 = load i64, i64* @errno
%12 = load i64, i64* @EINTR
%13 = icmp eq i64 %11, %12
br i1 %13, label %23, label %14
14: ; preds = %10
%15 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%16 = tail call i32 @exit(i32 1)
unreachable
17: ; preds = %5
%18 = icmp eq i32 %8, 0
br i1 %18, label %19, label %23
19: ; preds = %17
%20 = load i32, i32* @stderr
%21 = tail call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
%22 = tail call i32 @exit(i32 1)
unreachable
23: ; preds = %10, %17
%24 = sext i32 %8 to i64
%25 = getelementptr inbounds i8, i8* %7, i64 %24
%26 = sub nsw i32 %6, %8
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %5
28: ; preds = %23, %2
ret void
}