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

/* Variables and functions */
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ printf(char *, ...);
int send(int, char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, unsigned int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ usleep(int);
void *producer_loop(void *arg) {
  printf("Entering producer\n");
  int socket = *(int *)arg;
  int connected = 1;
  unsigned int i;
  for (i = 0; connected && i < 3; i++) {
    char buf[30];
    snprintf(buf, sizeof(buf), "This is message #%u", i);
    ssize_t sent = send(socket, buf, strlen(buf), 0);
    if (sent == -1) {
      fprintf(stderr, "Error sending data: %s\n", strerror(errno));
      connected = 0;
    } else {
      printf("Sent %ld bytes\n", sent);
      usleep(1000000);
    }
  }
  close(socket);
  printf("Exiting producer\n");
  return NULL;
}


x86 assembly (clang, -O0):
<producer_loop>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
lea    0xe6d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
movl   $0x1,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x10(%rbp)
mov    %al,-0x45(%rbp)
je     11ca <producer_loop+0x4a>
cmpl   $0x3,-0x14(%rbp)
setb   %al
mov    %al,-0x45(%rbp)
mov    -0x45(%rbp),%al
test   $0x1,%al
jne    11da <producer_loop+0x5a>
jmp    128c <producer_loop+0x10c>
lea    -0x40(%rbp),%rdi
mov    -0x14(%rbp),%ecx
mov    $0x1e,%esi
lea    0xe26(%rip),%rdx
call   1060 <snprintf@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x54(%rbp)
lea    -0x40(%rbp),%rax
mov    %rax,-0x50(%rbp)
lea    -0x40(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x54(%rbp),%edi
mov    -0x50(%rbp),%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1040 <send@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0xffffffff,-0x44(%rbp)
jne    125e <producer_loop+0xde>
mov    0x2db3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x58(%rbp)
mov    0x2d9f(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x58(%rbp),%edi
mov    %rax,%rdx
lea    0xdda(%rip),%rsi
call   1080 <fprintf@plt>
movl   $0x0,-0x10(%rbp)
jmp    1279 <producer_loop+0xf9>
mov    -0x44(%rbp),%esi
lea    0xdd7(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0xf4240,%edi
call   10a0 <usleep@plt>
jmp    127e <producer_loop+0xfe>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11b1 <producer_loop+0x31>
mov    -0xc(%rbp),%edi
call   1070 <close@plt>
lea    0xdb4(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<producer_loop>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdi,%rbx
lea    0xe94(%rip),%rdi
call   1030 <puts@plt>
mov    (%rbx),%r12d
lea    0xe49(%rip),%r14
mov    %rsp,%r13
lea    0xe6b(%rip),%r15
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r13,%rdi
mov    $0x1e,%esi
mov    %r14,%rdx
mov    %ebp,%ecx
call   1070 <snprintf@plt>
mov    %r13,%rdi
call   1040 <strlen@plt>
mov    %r12d,%edi
mov    %r13,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1050 <send@plt>
cmp    $0xffffffff,%eax
je     1227 <producer_loop+0x97>
mov    %eax,%ebx
mov    %r15,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0xf4240,%edi
call   10b0 <usleep@plt>
cmp    $0xffffffff,%ebx
je     124f <producer_loop+0xbf>
lea    0x1(%rbp),%eax
cmp    $0x2,%ebp
mov    %eax,%ebp
jb     11d0 <producer_loop+0x40>
jmp    124f <producer_loop+0xbf>
mov    0x2db2(%rip),%rax
mov    (%rax),%ebx
mov    0x2da1(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xdcf(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1090 <fprintf@plt>
mov    %r12d,%edi
call   1080 <close@plt>
lea    0xdf0(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [19 x i8] c"Entering producer\0A\00"
@.str.1 = [20 x i8] c"This is message #%u\00"
@stderr = global i32 0
@.str.2 = [24 x i8] c"Error sending data: %s\0A\00"
@errno = global i32 0
@.str.3 = [16 x i8] c"Sent %ld bytes\0A\00"
@.str.4 = [18 x i8] c"Exiting producer\0A\00"
define i8* @producer_loop(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [30 x i8]
%7 = alloca i32
store i8* %0, i8** %2
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%9 = load i8*, i8** %2
%10 = bitcast i8* %9 to i32*
%11 = load i32, i32* %10
store i32 %11, i32* %3
store i32 1, i32* %4
store i32 0, i32* %5
br label %12
12: ; preds = %41, %1
%13 = load i32, i32* %4
%14 = icmp ne i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %12
%16 = load i32, i32* %5
%17 = icmp ult i32 %16, 3
br label %18
18: ; preds = %15, %12
%19 = phi i1 [ false, %12 ], [ %17, %15 ]
br i1 %19, label %20, label %44
20: ; preds = %18
%21 = getelementptr inbounds [30 x i8], [30 x i8]* %6, i64 0, i64 0
%22 = load i32, i32* %5
%23 = call i32 @snprintf(i8* %21, i32 30, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i32 %22)
%24 = load i32, i32* %3
%25 = getelementptr inbounds [30 x i8], [30 x i8]* %6, i64 0, i64 0
%26 = getelementptr inbounds [30 x i8], [30 x i8]* %6, i64 0, i64 0
%27 = call i32 @strlen(i8* %26)
%28 = call i32 @send(i32 %24, i8* %25, i32 %27, i32 0)
store i32 %28, i32* %7
%29 = load i32, i32* %7
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %36
31: ; preds = %20
%32 = load i32, i32* @stderr
%33 = load i32, i32* @errno
%34 = call i8* @strerror(i32 %33)
%35 = call i32 @fprintf(i32 %32, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %34)
store i32 0, i32* %4
br label %40
36: ; preds = %20
%37 = load i32, i32* %7
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i32 %37)
%39 = call i32 @usleep(i32 1000000)
br label %40
40: ; preds = %36, %31
br label %41
41: ; preds = %40
%42 = load i32, i32* %5
%43 = add i32 %42, 1
store i32 %43, i32* %5
br label %12
44: ; preds = %18
%45 = load i32, i32* %3
%46 = call i32 @close(i32 %45)
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
ret i8* null
}

LLVM IR (-O2):
@.str.1 = [20 x i8] c"This is message #%u\00"
@stderr = global i32 0
@.str.2 = [24 x i8] c"Error sending data: %s\0A\00"
@errno = global i32 0
@.str.3 = [16 x i8] c"Sent %ld bytes\0A\00"
@str = [18 x i8] c"Entering producer\00"
@str.5 = [17 x i8] c"Exiting producer\00"
define noalias i8* @producer_loop(i8* nocapture %0) {
%2 = alloca [30 x i8]
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
%4 = bitcast i8* %0 to i32*
%5 = load i32, i32* %4
%6 = getelementptr inbounds [30 x i8], [30 x i8]* %2, i64 0, i64 0
br label %7
7: ; preds = %1, %18
%8 = phi i32 [ 0, %1 ], [ %21, %18 ]
%9 = call i32 @snprintf(i8* %6, i32 30, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i32 %8)
%10 = call i32 @strlen(i8* %6)
%11 = call i32 @send(i32 %5, i8* %6, i32 %10, i32 0)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %18
13: ; preds = %7
%14 = load i32, i32* @stderr
%15 = load i32, i32* @errno
%16 = call i8* @strerror(i32 %15)
%17 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %16)
br label %24
18: ; preds = %7
%19 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i32 %11)
%20 = call i32 @usleep(i32 1000000)
%21 = add nuw nsw i32 %8, 1
%22 = icmp ugt i32 %8, 1
%23 = or i1 %12, %22
br i1 %23, label %24, label %7
24: ; preds = %18, %13
%25 = call i32 @close(i32 %5)
%26 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str.5, i64 0, i64 0))
ret i8* null
}