#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*/ exit(int);
int /*<<< orphan*/ printf(char *);
int strlen(char *);
int write(int, char *, int);
void sendTCP(char *msg, int socket) {
  int nwritten;
  int tries = 5;
  int nleft = strlen(msg);
  while (nleft > 0) {
    nwritten = write(socket, msg, strlen(msg));
    nleft -= nwritten;
    msg += nwritten;
    if (nwritten == -1) {
      if (tries > 0) {
        printf("error sending with sendTCP\n");
        tries--;
      } else {
        printf("too many failed attempts, exiting\n");
        exit(-1);
      }
    }
  }
}


x86 assembly (clang, -O0):
<sendTCP>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x5,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jle    11fe <sendTCP+0xbe>
mov    -0xc(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%ecx
mov    -0x18(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%ecx
mov    -0x8(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    11f9 <sendTCP+0xb9>
cmpl   $0x0,-0x14(%rbp)
jle    11de <sendTCP+0x9e>
lea    0xe35(%rip),%rdi
call   1050 <printf@plt>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    11f4 <sendTCP+0xb4>
lea    0xe37(%rip),%rdi
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   1060 <exit@plt>
jmp    11f9 <sendTCP+0xb9>
jmp    1162 <sendTCP+0x22>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sendTCP>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %rdi,%rbx
call   1050 <strlen@plt>
test   %eax,%eax
jle    11ad <sendTCP+0x6d>
mov    %eax,%ebp
mov    $0x5,%r13d
lea    0xeb9(%rip),%r15
jmp    117d <sendTCP+0x3d>
nopl   0x0(%rax,%rax,1)
sub    %r12d,%ebp
movslq %r12d,%rax
add    %rax,%rbx
test   %ebp,%ebp
jle    11ad <sendTCP+0x6d>
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    %eax,%r12d
cmp    $0xffffffff,%eax
jne    1170 <sendTCP+0x30>
test   %r13d,%r13d
jle    11bc <sendTCP+0x7c>
mov    %r15,%rdi
call   1030 <puts@plt>
add    $0xffffffff,%r13d
jmp    1170 <sendTCP+0x30>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe3d(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [28 x i8] c"error sending with sendTCP\0A\00"
@.str.1 = [35 x i8] c"too many failed attempts, exiting\0A\00"
define void @sendTCP(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
store i32 5, i32* %6
%8 = load i8*, i8** %3
%9 = call i32 @strlen(i8* %8)
store i32 %9, i32* %7
br label %10
10: ; preds = %39, %2
%11 = load i32, i32* %7
%12 = icmp sgt i32 %11, 0
br i1 %12, label %13, label %40
13: ; preds = %10
%14 = load i32, i32* %4
%15 = load i8*, i8** %3
%16 = load i8*, i8** %3
%17 = call i32 @strlen(i8* %16)
%18 = call i32 @write(i32 %14, i8* %15, i32 %17)
store i32 %18, i32* %5
%19 = load i32, i32* %5
%20 = load i32, i32* %7
%21 = sub nsw i32 %20, %19
store i32 %21, i32* %7
%22 = load i32, i32* %5
%23 = load i8*, i8** %3
%24 = sext i32 %22 to i64
%25 = getelementptr inbounds i8, i8* %23, i64 %24
store i8* %25, i8** %3
%26 = load i32, i32* %5
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %39
28: ; preds = %13
%29 = load i32, i32* %6
%30 = icmp sgt i32 %29, 0
br i1 %30, label %31, label %35
31: ; preds = %28
%32 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
%33 = load i32, i32* %6
%34 = add nsw i32 %33, -1
store i32 %34, i32* %6
br label %38
35: ; preds = %28
%36 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0))
%37 = call i32 @exit(i32 -1)
unreachable
38: ; preds = %31
br label %39
39: ; preds = %38, %13
br label %10
40: ; preds = %10
ret void
}

LLVM IR (-O2):
@str = [34 x i8] c"too many failed attempts, exiting\00"
@str.2 = [27 x i8] c"error sending with sendTCP\00"
define void @sendTCP(i8* %0, i32 %1) {
%3 = tail call i32 @strlen(i8* %0)
%4 = icmp sgt i32 %3, 0
br i1 %4, label %5, label %26
5: ; preds = %2, %23
%6 = phi i32 [ %11, %23 ], [ %3, %2 ]
%7 = phi i32 [ %24, %23 ], [ 5, %2 ]
%8 = phi i8* [ %13, %23 ], [ %0, %2 ]
%9 = tail call i32 @strlen(i8* %8)
%10 = tail call i32 @write(i32 %1, i8* %8, i32 %9)
%11 = sub nsw i32 %6, %10
%12 = sext i32 %10 to i64
%13 = getelementptr inbounds i8, i8* %8, i64 %12
%14 = icmp eq i32 %10, -1
br i1 %14, label %15, label %23
15: ; preds = %5
%16 = icmp sgt i32 %7, 0
br i1 %16, label %17, label %20
17: ; preds = %15
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.2, i64 0, i64 0))
%19 = add nsw i32 %7, -1
br label %23
20: ; preds = %15
%21 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str, i64 0, i64 0))
%22 = tail call i32 @exit(i32 -1)
unreachable
23: ; preds = %17, %5
%24 = phi i32 [ %19, %17 ], [ %7, %5 ]
%25 = icmp sgt i32 %11, 0
br i1 %25, label %5, label %26
26: ; preds = %23, %2
ret void
}