#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
typedef int /*<<< orphan*/ szBuf;
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IPPROTO_UDP;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_addr(char *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
int recvfrom(int, char *, int, int /*<<< orphan*/, struct sockaddr *, int *);
int sendto(int, char *, scalar_t__, int /*<<< orphan*/, struct sockaddr *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stdin;
scalar_t__ strlen(char *);
void DatagramClient(char *serverIp) {
  char szBuf[256];
  int nRet;
  int theSocket;
  struct sockaddr_in saServer;
  int nFromLen;
  printf("\nUDP Client sending to server: %s on port: %d\n", serverIp, 10007);
  theSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  if (theSocket < 0) {
    printf("socket()");
    return;
  }
  saServer.sin_family = AF_INET;
  saServer.sin_addr.s_addr = inet_addr(serverIp);
  saServer.sin_port = htons(10007);
  printf("Enter message to send:\n");
  fgets(szBuf, 100, stdin);
  nRet = sendto(theSocket, szBuf, strlen(szBuf) + 1, 0,
                (struct sockaddr *)&saServer, sizeof(struct sockaddr));
  if (nRet < 0) {
    printf("sendto()");
    close(theSocket);
    return;
  }
  memset(szBuf, 0, sizeof(szBuf));
  nFromLen = sizeof(struct sockaddr);
  nRet = recvfrom(theSocket, szBuf, sizeof(szBuf), 0,
                  (struct sockaddr *)&saServer, &nFromLen);
  if (nRet < 0) {
    close(theSocket);
    return;
  }
  printf("Received following message:\n%s\n", szBuf);
  close(theSocket);
  return;
}


x86 assembly (clang, -O0):
<DatagramClient>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rsi
lea    0xe46(%rip),%rdi
mov    $0x2717,%edx
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2e1b(%rip),%rax
mov    (%rax),%edi
mov    0x2e02(%rip),%rax
mov    (%rax),%esi
mov    0x2de1(%rip),%rax
mov    (%rax),%edx
call   10c0 <socket@plt>
mov    %eax,-0x118(%rbp)
cmpl   $0x0,-0x118(%rbp)
jge    120c <DatagramClient+0x6c>
lea    0xe2f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1368 <DatagramClient+0x1c8>
mov    0x2dd5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x120(%rbp)
mov    -0x8(%rbp),%rdi
call   10a0 <inet_addr@plt>
mov    %eax,-0x124(%rbp)
mov    $0x2717,%edi
call   1040 <htons@plt>
mov    %eax,-0x128(%rbp)
lea    0xdf7(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x110(%rbp),%rdi
mov    0x2d8a(%rip),%rax
mov    (%rax),%edx
mov    $0x64,%esi
call   1090 <fgets@plt>
mov    -0x118(%rbp),%eax
mov    %eax,-0x13c(%rbp)
lea    -0x110(%rbp),%rax
mov    %rax,-0x138(%rbp)
lea    -0x110(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x13c(%rbp),%edi
mov    -0x138(%rbp),%rsi
mov    %rax,%rdx
add    $0x1,%rdx
lea    -0x128(%rbp),%r8
xor    %ecx,%ecx
mov    $0x4,%r9d
call   1070 <sendto@plt>
mov    %eax,-0x114(%rbp)
cmpl   $0x0,-0x114(%rbp)
jge    12e1 <DatagramClient+0x141>
lea    0xd86(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x118(%rbp),%edi
call   1080 <close@plt>
jmp    1368 <DatagramClient+0x1c8>
lea    -0x110(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1060 <memset@plt>
movl   $0x4,-0x12c(%rbp)
mov    -0x118(%rbp),%edi
lea    -0x110(%rbp),%rsi
lea    -0x128(%rbp),%r8
mov    $0x100,%edx
xor    %ecx,%ecx
lea    -0x12c(%rbp),%r9
call   10b0 <recvfrom@plt>
mov    %eax,-0x114(%rbp)
cmpl   $0x0,-0x114(%rbp)
jge    1348 <DatagramClient+0x1a8>
mov    -0x118(%rbp),%edi
call   1080 <close@plt>
jmp    1368 <DatagramClient+0x1c8>
lea    -0x110(%rbp),%rsi
lea    0xd03(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x118(%rbp),%edi
call   1080 <close@plt>
add    $0x140,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<DatagramClient>:
push   %rbp
push   %r14
push   %rbx
sub    $0x120,%rsp
mov    %rdi,%rbx
lea    0xe3b(%rip),%rdi
mov    %rbx,%rsi
mov    $0x2717,%edx
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2e0d(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2df3(%rip),%rax
mov    (%rax),%esi
mov    0x2dd2(%rip),%rax
mov    (%rax),%edx
call   10d0 <socket@plt>
test   %eax,%eax
js     12c1 <DatagramClient+0x111>
mov    %eax,%r14d
mov    0x0(%rbp),%eax
mov    %eax,0x18(%rsp)
mov    %rbx,%rdi
call   10b0 <inet_addr@plt>
mov    %eax,0x14(%rsp)
mov    $0x2717,%edi
call   1050 <htons@plt>
mov    %eax,0x10(%rsp)
lea    0xe39(%rip),%rdi
call   1030 <puts@plt>
mov    0x2dac(%rip),%rax
mov    (%rax),%edx
lea    0x20(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x64,%esi
call   10a0 <fgets@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
lea    0x1(%rax),%rdx
lea    0x10(%rsp),%r8
mov    %r14d,%edi
mov    %rbx,%rsi
xor    %ecx,%ecx
mov    $0x4,%r9d
call   1080 <sendto@plt>
test   %eax,%eax
js     12da <DatagramClient+0x12a>
lea    0x20(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1070 <memset@plt>
movl   $0x4,0xc(%rsp)
lea    0x10(%rsp),%r8
lea    0xc(%rsp),%r9
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x100,%edx
xor    %ecx,%ecx
call   10c0 <recvfrom@plt>
test   %eax,%eax
js     12e8 <DatagramClient+0x138>
lea    0xd8e(%rip),%rdi
lea    0x20(%rsp),%rsi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    12e8 <DatagramClient+0x138>
lea    0xd67(%rip),%rdi
xor    %eax,%eax
add    $0x120,%rsp
pop    %rbx
pop    %r14
pop    %rbp
jmp    1060 <printf@plt>
lea    0xd57(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r14d,%edi
call   1090 <close@plt>
add    $0x120,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [47 x i8] c"\0AUDP Client sending to server: %s on port: %d\0A\00"
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@.str.1 = [9 x i8] c"socket()\00"
@.str.2 = [24 x i8] c"Enter message to send:\0A\00"
@stdin = global i32 0
@.str.3 = [9 x i8] c"sendto()\00"
@.str.4 = [32 x i8] c"Received following message:\0A%s\0A\00"
define void @DatagramClient(i8* %0) {
%2 = alloca i8*
%3 = alloca [256 x i8]
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.sockaddr_in
%7 = alloca i32
store i8* %0, i8** %2
%8 = load i8*, i8** %2
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0), i8* %8, i32 10007)
%10 = load i32, i32* @AF_INET
%11 = load i32, i32* @SOCK_DGRAM
%12 = load i32, i32* @IPPROTO_UDP
%13 = call i32 @socket(i32 %10, i32 %11, i32 %12)
store i32 %13, i32* %5
%14 = load i32, i32* %5
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %1
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
br label %61
18: ; preds = %1
%19 = load i32, i32* @AF_INET
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 %19, i32* %20
%21 = load i8*, i8** %2
%22 = call i32 @inet_addr(i8* %21)
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
%24 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %23, i32 0, i32 0
store i32 %22, i32* %24
%25 = call i32 @htons(i32 10007)
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
store i32 %25, i32* %26
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
%28 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%29 = load i32, i32* @stdin
%30 = call i32 @fgets(i8* %28, i32 100, i32 %29)
%31 = load i32, i32* %5
%32 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%33 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%34 = call i64 @strlen(i8* %33)
%35 = add nsw i64 %34, 1
%36 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%37 = call i32 @sendto(i32 %31, i8* %32, i64 %35, i32 0, %struct.sockaddr* %36, i32 4)
store i32 %37, i32* %4
%38 = load i32, i32* %4
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %44
40: ; preds = %18
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%42 = load i32, i32* %5
%43 = call i32 @close(i32 %42)
br label %61
44: ; preds = %18
%45 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%46 = call i32 @memset(i8* %45, i32 0, i32 256)
store i32 4, i32* %7
%47 = load i32, i32* %5
%48 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%49 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%50 = call i32 @recvfrom(i32 %47, i8* %48, i32 256, i32 0, %struct.sockaddr* %49, i32* %7)
store i32 %50, i32* %4
%51 = load i32, i32* %4
%52 = icmp slt i32 %51, 0
br i1 %52, label %53, label %56
53: ; preds = %44
%54 = load i32, i32* %5
%55 = call i32 @close(i32 %54)
br label %61
56: ; preds = %44
%57 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0), i8* %57)
%59 = load i32, i32* %5
%60 = call i32 @close(i32 %59)
br label %61
61: ; preds = %56, %53, %40, %16
ret void
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [47 x i8] c"\0AUDP Client sending to server: %s on port: %d\0A\00"
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@.str.1 = [9 x i8] c"socket()\00"
@stdin = global i32 0
@.str.3 = [9 x i8] c"sendto()\00"
@.str.4 = [32 x i8] c"Received following message:\0A%s\0A\00"
@str = [23 x i8] c"Enter message to send:\00"
define void @DatagramClient(i8* %0) {
%2 = alloca [256 x i8]
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%6 = bitcast %struct.sockaddr_in* %3 to i8*
%7 = bitcast i32* %4 to i8*
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0), i8* %0, i32 10007)
%9 = load i32, i32* @AF_INET
%10 = load i32, i32* @SOCK_DGRAM
%11 = load i32, i32* @IPPROTO_UDP
%12 = tail call i32 @socket(i32 %9, i32 %10, i32 %11)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %1
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
br label %43
16: ; preds = %1
%17 = load i32, i32* @AF_INET
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %17, i32* %18
%19 = tail call i32 @inet_addr(i8* %0)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
store i32 %19, i32* %20
%21 = tail call i32 @htons(i32 10007)
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %21, i32* %22
%23 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str, i64 0, i64 0))
%24 = load i32, i32* @stdin
%25 = call i32 @fgets(i8* %5, i32 100, i32 %24)
%26 = call i64 @strlen(i8* %5)
%27 = add nsw i64 %26, 1
%28 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%29 = call i32 @sendto(i32 %12, i8* %5, i64 %27, i32 0, %struct.sockaddr* %28, i32 4)
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %16
%32 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%33 = call i32 @close(i32 %12)
br label %43
34: ; preds = %16
%35 = call i32 @memset(i8* %5, i32 0, i32 256)
store i32 4, i32* %4
%36 = call i32 @recvfrom(i32 %12, i8* %5, i32 256, i32 0, %struct.sockaddr* %28, i32* %4)
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %34
%39 = call i32 @close(i32 %12)
br label %43
40: ; preds = %34
%41 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0), i8* %5)
%42 = call i32 @close(i32 %12)
br label %43
43: ; preds = %40, %38, %31, %14
ret void
}