#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;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ cli_addr;

/* Variables and functions */
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ error(char *);
int /*<<< orphan*/ printf(char *, char *);
scalar_t__ recvfrom(int, char *, int, int /*<<< orphan*/, struct sockaddr *,
                    int *);
scalar_t__ sendToLog(char *, char *, int);
scalar_t__ sendto(int, char *, int, int /*<<< orphan*/, struct sockaddr *, int);
void serverReadWriteUdp(int sock, char *ip, int port) {
  char buffer[256];
  bzero(buffer, 256);
  struct sockaddr_in cli_addr;
  socklen_t clilen = sizeof(cli_addr);
  if (recvfrom(sock, buffer, 256, 0, (struct sockaddr *)&cli_addr, &clilen) <
      0) {
    error("ERROR in UDP recvfrom");
  }
  printf("UDP message received: %s\n", buffer);
  if (sendToLog(buffer, ip, port) < 0)
    error("ERROR sending to log server");
  if (sendto(sock, "I got your message\n", 19, 0, (struct sockaddr *)&cli_addr,
             clilen) < 0) {
    error("ERROR in UDP sendto");
  }
}


x86 assembly (clang, -O0):
<serverReadWriteUdp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
lea    -0x120(%rbp),%rdi
mov    $0x100,%esi
call   1080 <bzero@plt>
movl   $0x4,-0x12c(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x120(%rbp),%rsi
lea    -0x128(%rbp),%r8
mov    $0x100,%edx
xor    %ecx,%ecx
lea    -0x12c(%rbp),%r9
call   1050 <recvfrom@plt>
cmp    $0x0,%rax
jge    11ca <serverReadWriteUdp+0x6a>
lea    0xe3b(%rip),%rdi
call   1060 <error@plt>
lea    -0x120(%rbp),%rsi
lea    0xe3e(%rip),%rdi
call   1030 <printf@plt>
lea    -0x120(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1070 <sendToLog@plt>
cmp    $0x0,%rax
jge    1206 <serverReadWriteUdp+0xa6>
lea    0xe2f(%rip),%rdi
call   1060 <error@plt>
mov    -0x4(%rbp),%edi
lea    -0x128(%rbp),%r8
mov    -0x12c(%rbp),%r9d
lea    0xe2e(%rip),%rsi
mov    $0x13,%edx
xor    %ecx,%ecx
call   1040 <sendto@plt>
cmp    $0x0,%rax
jge    1240 <serverReadWriteUdp+0xe0>
lea    0xe25(%rip),%rdi
call   1060 <error@plt>
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<serverReadWriteUdp>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x118,%rsp
mov    %edx,%r14d
mov    %rsi,%r15
mov    %edi,%ebx
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x100,%esi
call   1080 <bzero@plt>
movl   $0x4,0x4(%rsp)
lea    0x8(%rsp),%r8
lea    0x4(%rsp),%r9
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x100,%edx
xor    %ecx,%ecx
call   1050 <recvfrom@plt>
test   %rax,%rax
jns    11bb <serverReadWriteUdp+0x5b>
lea    0xe4a(%rip),%rdi
call   1060 <error@plt>
lea    0xe54(%rip),%rdi
lea    0x10(%rsp),%rbp
mov    %rbp,%rsi
call   1030 <printf@plt>
mov    %rbp,%rdi
mov    %r15,%rsi
mov    %r14d,%edx
call   1070 <sendToLog@plt>
test   %rax,%rax
jns    11ee <serverReadWriteUdp+0x8e>
lea    0xe47(%rip),%rdi
call   1060 <error@plt>
mov    0x4(%rsp),%r9d
lea    0xe52(%rip),%rsi
lea    0x8(%rsp),%r8
mov    %ebx,%edi
mov    $0x13,%edx
xor    %ecx,%ecx
call   1040 <sendto@plt>
test   %rax,%rax
jns    121e <serverReadWriteUdp+0xbe>
lea    0xe47(%rip),%rdi
call   1060 <error@plt>
add    $0x118,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [22 x i8] c"ERROR in UDP recvfrom\00"
@.str.1 = [26 x i8] c"UDP message received: %s\0A\00"
@.str.2 = [28 x i8] c"ERROR sending to log server\00"
@.str.3 = [20 x i8] c"I got your message\0A\00"
@.str.4 = [20 x i8] c"ERROR in UDP sendto\00"
define void @serverReadWriteUdp(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca [256 x i8]
%8 = alloca %struct.sockaddr_in
%9 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%10 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%11 = call i32 @bzero(i8* %10, i32 256)
store i32 4, i32* %9
%12 = load i32, i32* %4
%13 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%14 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%15 = call i64 @recvfrom(i32 %12, i8* %13, i32 256, i32 0, %struct.sockaddr* %14, i32* %9)
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %3
%18 = call i32 @error(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %19
19: ; preds = %17, %3
%20 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%21 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %20)
%22 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%23 = load i8*, i8** %5
%24 = load i32, i32* %6
%25 = call i64 @sendToLog(i8* %22, i8* %23, i32 %24)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %19
%28 = call i32 @error(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
br label %29
29: ; preds = %27, %19
%30 = load i32, i32* %4
%31 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%32 = load i32, i32* %9
%33 = call i64 @sendto(i32 %30, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i32 19, i32 0, %struct.sockaddr* %31, i32 %32)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %29
%36 = call i32 @error(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0))
br label %37
37: ; preds = %35, %29
ret void
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [22 x i8] c"ERROR in UDP recvfrom\00"
@.str.1 = [26 x i8] c"UDP message received: %s\0A\00"
@.str.2 = [28 x i8] c"ERROR sending to log server\00"
@.str.3 = [20 x i8] c"I got your message\0A\00"
@.str.4 = [20 x i8] c"ERROR in UDP sendto\00"
define void @serverReadWriteUdp(i32 %0, i8* %1, i32 %2) {
%4 = alloca [256 x i8]
%5 = alloca %struct.sockaddr_in
%6 = alloca i32
%7 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%8 = call i32 @bzero(i8* %7, i32 256)
%9 = bitcast %struct.sockaddr_in* %5 to i8*
%10 = bitcast i32* %6 to i8*
store i32 4, i32* %6
%11 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%12 = call i64 @recvfrom(i32 %0, i8* %7, i32 256, i32 0, %struct.sockaddr* %11, i32* %6)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %3
%15 = call i32 @error(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %16
16: ; preds = %14, %3
%17 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %7)
%18 = call i64 @sendToLog(i8* %7, i8* %1, i32 %2)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %16
%21 = call i32 @error(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
br label %22
22: ; preds = %20, %16
%23 = load i32, i32* %6
%24 = call i64 @sendto(i32 %0, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i32 19, i32 0, %struct.sockaddr* %11, i32 %23)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %22
%27 = call i32 @error(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0))
br label %28
28: ; preds = %26, %22
ret void
}