#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 */
struct sockaddr_in {
  int dummy;
};
struct sockaddr {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ DEBUGF(char *, int);
int sendto(int, char *, int, int /*<<< orphan*/, struct sockaddr *, int);
int udp_write(int fd, char *buf, int len, struct sockaddr_in *to) {
  int bytes_sent;
  DEBUGF("Entered udp_write; len = %d\n", len);
  bytes_sent = sendto(fd, buf, len, 0, (struct sockaddr *)to,
                      sizeof(struct sockaddr_in));
  if (bytes_sent < 0) {
    DEBUGF("sendto on socket %d failed\n", fd);
    return -1;
  }
  return bytes_sent;
}


x86 assembly (clang, -O0):
<udp_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x14(%rbp),%esi
lea    0xec0(%rip),%rdi
call   1030 <DEBUGF@plt>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
mov    -0x20(%rbp),%r8
xor    %ecx,%ecx
mov    $0x4,%r9d
call   1040 <sendto@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1188 <udp_write+0x68>
mov    -0x8(%rbp),%esi
lea    0xea6(%rip),%rdi
call   1030 <DEBUGF@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    118e <udp_write+0x6e>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<udp_write>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rcx,%r15
mov    %edx,%ebp
mov    %rsi,%rbx
mov    %edi,%r14d
lea    0xec7(%rip),%rdi
mov    %edx,%esi
call   1030 <DEBUGF@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
xor    %ecx,%ecx
mov    %r15,%r8
mov    $0x4,%r9d
call   1040 <sendto@plt>
test   %eax,%eax
jns    1170 <udp_write+0x50>
lea    0xeba(%rip),%rdi
mov    %r14d,%esi
call   1030 <DEBUGF@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [29 x i8] c"Entered udp_write; len = %d\0A\00"
@.str.1 = [28 x i8] c"sendto on socket %d failed\0A\00"
define i32 @udp_write(i32 %0, i8* %1, i32 %2, %struct.sockaddr_in* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca %struct.sockaddr_in*
%10 = alloca i32
store i32 %0, i32* %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store %struct.sockaddr_in* %3, %struct.sockaddr_in** %9
%11 = load i32, i32* %8
%12 = call i32 @DEBUGF(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %11)
%13 = load i32, i32* %6
%14 = load i8*, i8** %7
%15 = load i32, i32* %8
%16 = load %struct.sockaddr_in*, %struct.sockaddr_in** %9
%17 = bitcast %struct.sockaddr_in* %16 to %struct.sockaddr*
%18 = call i32 @sendto(i32 %13, i8* %14, i32 %15, i32 0, %struct.sockaddr* %17, i32 4)
store i32 %18, i32* %10
%19 = load i32, i32* %10
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %4
%22 = load i32, i32* %6
%23 = call i32 @DEBUGF(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 %22)
store i32 -1, i32* %5
br label %26
24: ; preds = %4
%25 = load i32, i32* %10
store i32 %25, i32* %5
br label %26
26: ; preds = %24, %21
%27 = load i32, i32* %5
ret i32 %27
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [29 x i8] c"Entered udp_write; len = %d\0A\00"
@.str.1 = [28 x i8] c"sendto on socket %d failed\0A\00"
define i32 @udp_write(i32 %0, i8* %1, i32 %2, %struct.sockaddr_in* %3) {
%5 = tail call i32 @DEBUGF(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %2)
%6 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%7 = tail call i32 @sendto(i32 %0, i8* %1, i32 %2, i32 0, %struct.sockaddr* %6, i32 4)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %4
%10 = tail call i32 @DEBUGF(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 %0)
br label %11
11: ; preds = %4, %9
%12 = phi i32 [ -1, %9 ], [ %7, %4 ]
ret i32 %12
}