#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 */
typedef int /*<<< orphan*/ uint8_t;

/* Variables and functions */
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
void *malloc(int);
int /*<<< orphan*/ printf(char *, int, ...);
int send(int, int /*<<< orphan*/ *, int, int /*<<< orphan*/);
int sprintf(void *, char *, int);
char *strerror(int /*<<< orphan*/);
int /*<<< orphan*/ usleep(int);
void send_frames(int sock) {
  void *frame_buffer = malloc(2048);
  for (int j = 0; j < 100; j++) {
    uint8_t *pb = frame_buffer;
    int pos = 0;
    int size = sprintf(frame_buffer, "This is frame number %d\n", j);
    while (pos < size) {
      int now = send(sock, pb + pos, size - pos, 0);
      if (now > 0) {
        pos += now;
      } else {
        printf("Can't send frame %d: %s\n", j, strerror(errno));
        exit(1);
      }
    }
    printf("Sent frame %d\n", j);
    usleep(1000 * 100);
  }
}


x86 assembly (clang, -O0):
<send_frames>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    $0x800,%edi
call   1050 <malloc@plt>
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0x64,-0x14(%rbp)
jge    1265 <send_frames+0xf5>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%edx
lea    0xe49(%rip),%rsi
call   1060 <sprintf@plt>
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x28(%rbp),%eax
jge    123c <send_frames+0xcc>
mov    -0x4(%rbp),%edi
mov    -0x20(%rbp),%rsi
movslq -0x24(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
mov    -0x28(%rbp),%edx
sub    -0x24(%rbp),%edx
xor    %ecx,%ecx
call   1030 <send@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jle    1205 <send_frames+0x95>
mov    -0x2c(%rbp),%eax
add    -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
jmp    1237 <send_frames+0xc7>
mov    -0x14(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x30(%rbp),%esi
mov    %rax,%rdx
lea    0xdf3(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
jmp    11bf <send_frames+0x4f>
mov    -0x14(%rbp),%esi
lea    0xdec(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x186a0,%edi
call   1090 <usleep@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1190 <send_frames+0x20>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<send_frames>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edi,%r13d
mov    $0x800,%edi
call   1050 <malloc@plt>
mov    %rax,%rbx
lea    0xea0(%rip),%r12
xor    %r14d,%r14d
jmp    11c1 <send_frames+0x51>
nopw   0x0(%rax,%rax,1)
mov    %r12,%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x186a0,%edi
call   1090 <usleep@plt>
add    $0x1,%r14d
cmp    $0x64,%r14d
je     1203 <send_frames+0x93>
mov    %rbx,%rdi
lea    0xe35(%rip),%rsi
mov    %r14d,%edx
call   1060 <sprintf@plt>
test   %eax,%eax
jle    11a0 <send_frames+0x30>
mov    %eax,%ebp
xor    %r15d,%r15d
nopl   0x0(%rax)
movslq %r15d,%rax
lea    (%rbx,%rax,4),%rsi
mov    %ebp,%edx
sub    %eax,%edx
mov    %r13d,%edi
xor    %ecx,%ecx
call   1030 <send@plt>
test   %eax,%eax
jle    1212 <send_frames+0xa2>
add    %eax,%r15d
cmp    %r15d,%ebp
jg     11e0 <send_frames+0x70>
jmp    11a0 <send_frames+0x30>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2dc7(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xdf2(%rip),%rdi
mov    %r14d,%esi
mov    %rax,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [25 x i8] c"This is frame number %d\0A\00"
@.str.1 = [25 x i8] c"Can't send frame %d: %s\0A\00"
@errno = global i32 0
@.str.2 = [15 x i8] c"Sent frame %d\0A\00"
define void @send_frames(i32 %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %2
%9 = call i8* @malloc(i32 2048)
store i8* %9, i8** %3
store i32 0, i32* %4
br label %10
10: ; preds = %50, %1
%11 = load i32, i32* %4
%12 = icmp slt i32 %11, 100
br i1 %12, label %13, label %53
13: ; preds = %10
%14 = load i8*, i8** %3
%15 = bitcast i8* %14 to i32*
store i32* %15, i32** %5
store i32 0, i32* %6
%16 = load i8*, i8** %3
%17 = load i32, i32* %4
%18 = call i32 @sprintf(i8* %16, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 %17)
store i32 %18, i32* %7
br label %19
19: ; preds = %45, %13
%20 = load i32, i32* %6
%21 = load i32, i32* %7
%22 = icmp slt i32 %20, %21
br i1 %22, label %23, label %46
23: ; preds = %19
%24 = load i32, i32* %2
%25 = load i32*, i32** %5
%26 = load i32, i32* %6
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i32, i32* %25, i64 %27
%29 = load i32, i32* %7
%30 = load i32, i32* %6
%31 = sub nsw i32 %29, %30
%32 = call i32 @send(i32 %24, i32* %28, i32 %31, i32 0)
store i32 %32, i32* %8
%33 = load i32, i32* %8
%34 = icmp sgt i32 %33, 0
br i1 %34, label %35, label %39
35: ; preds = %23
%36 = load i32, i32* %8
%37 = load i32, i32* %6
%38 = add nsw i32 %37, %36
store i32 %38, i32* %6
br label %45
39: ; preds = %23
%40 = load i32, i32* %4
%41 = load i32, i32* @errno
%42 = call i8* @strerror(i32 %41)
%43 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i32 %40, i8* %42)
%44 = call i32 @exit(i32 1)
unreachable
45: ; preds = %35
br label %19
46: ; preds = %19
%47 = load i32, i32* %4
%48 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 %47)
%49 = call i32 @usleep(i32 100000)
br label %50
50: ; preds = %46
%51 = load i32, i32* %4
%52 = add nsw i32 %51, 1
store i32 %52, i32* %4
br label %10
53: ; preds = %10
ret void
}

LLVM IR (-O2):
@.str = [25 x i8] c"This is frame number %d\0A\00"
@.str.1 = [25 x i8] c"Can't send frame %d: %s\0A\00"
@errno = global i32 0
@.str.2 = [15 x i8] c"Sent frame %d\0A\00"
define void @send_frames(i32 %0) {
%2 = tail call dereferenceable_or_null(2048) i8* @malloc(i32 2048)
%3 = bitcast i8* %2 to i32*
br label %5
4: ; preds = %24
ret void
5: ; preds = %1, %24
%6 = phi i32 [ 0, %1 ], [ %27, %24 ]
%7 = tail call i32 @sprintf(i8* dereferenceable(1) %2, i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = icmp sgt i32 %7, 0
br i1 %8, label %11, label %24
9: ; preds = %11
%10 = icmp sgt i32 %7, %18
br i1 %10, label %11, label %24
11: ; preds = %5, %9
%12 = phi i32 [ %18, %9 ], [ 0, %5 ]
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds i32, i32* %3, i64 %13
%15 = sub nsw i32 %7, %12
%16 = tail call i32 @send(i32 %0, i32* %14, i32 %15, i32 0)
%17 = icmp sgt i32 %16, 0
%18 = add nsw i32 %16, %12
br i1 %17, label %9, label %19
19: ; preds = %11
%20 = load i32, i32* @errno
%21 = tail call i8* @strerror(i32 %20)
%22 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i32 %6, i8* %21)
%23 = tail call i32 @exit(i32 1)
unreachable
24: ; preds = %9, %5
%25 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 %6)
%26 = tail call i32 @usleep(i32 100000)
%27 = add nuw nsw i32 %6, 1
%28 = icmp eq i32 %27, 100
br i1 %28, label %4, label %5
}