#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 /*<<< orphan*/ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ htons(unsigned int);
scalar_t__ inet_aton(char const *, int /*<<< orphan*/ *);
int /*<<< orphan*/ puts(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int CreateUdpSocket(const char *ip, unsigned port,
                    struct sockaddr_in *srvaddr) {
  int fd;
  if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
    puts("Socket creation error");
    return -1;
  }
  bzero(srvaddr, sizeof(struct sockaddr_in));
  srvaddr->sin_family = AF_INET;
  srvaddr->sin_port = htons(port);
  if (inet_aton(ip, &srvaddr->sin_addr) == 0) {
    puts("Invalid IP address");
    close(fd);
    return -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<CreateUdpSocket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    0x2e6e(%rip),%rax
mov    (%rax),%edi
mov    0x2e5d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    11b0 <CreateUdpSocket+0x50>
lea    0xe61(%rip),%rdi
call   1030 <puts@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    121c <CreateUdpSocket+0xbc>
mov    -0x20(%rbp),%rdi
mov    $0xc,%esi
call   1070 <bzero@plt>
mov    0x2e23(%rip),%rax
mov    (%rax),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    -0x14(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1060 <inet_aton@plt>
cmp    $0x0,%rax
jne    1216 <CreateUdpSocket+0xb6>
lea    0xe19(%rip),%rdi
call   1030 <puts@plt>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    121c <CreateUdpSocket+0xbc>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<CreateUdpSocket>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%rbx
mov    %esi,%r15d
mov    %rdi,%r14
mov    0x2e70(%rip),%r12
mov    (%r12),%edi
mov    0x2e5d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
test   %eax,%eax
js     11d6 <CreateUdpSocket+0x76>
mov    %eax,%ebp
mov    %rbx,%rdi
mov    $0xc,%esi
call   1070 <bzero@plt>
mov    (%r12),%eax
mov    %eax,0x8(%rbx)
mov    %r15d,%edi
call   1040 <htons@plt>
mov    %eax,0x4(%rbx)
mov    %r14,%rdi
mov    %rbx,%rsi
call   1060 <inet_aton@plt>
test   %rax,%rax
jne    11e7 <CreateUdpSocket+0x87>
lea    0xe4e(%rip),%rdi
call   1030 <puts@plt>
mov    %ebp,%edi
call   1050 <close@plt>
jmp    11e2 <CreateUdpSocket+0x82>
lea    0xe23(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [22 x i8] c"Socket creation error\00"
@.str.1 = [19 x i8] c"Invalid IP address\00"
define i32 @CreateUdpSocket(i8* %0, i32 %1, %struct.sockaddr_in* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca %struct.sockaddr_in*
%8 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store %struct.sockaddr_in* %2, %struct.sockaddr_in** %7
%9 = load i32, i32* @AF_INET
%10 = load i32, i32* @SOCK_DGRAM
%11 = call i32 @socket(i32 %9, i32 %10, i32 0)
store i32 %11, i32* %8
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %3
%14 = call i32 @puts(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %36
15: ; preds = %3
%16 = load %struct.sockaddr_in*, %struct.sockaddr_in** %7
%17 = call i32 @bzero(%struct.sockaddr_in* %16, i32 12)
%18 = load i32, i32* @AF_INET
%19 = load %struct.sockaddr_in*, %struct.sockaddr_in** %7
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %19, i32 0, i32 2
store i32 %18, i32* %20
%21 = load i32, i32* %6
%22 = call i32 @htons(i32 %21)
%23 = load %struct.sockaddr_in*, %struct.sockaddr_in** %7
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %23, i32 0, i32 1
store i32 %22, i32* %24
%25 = load i8*, i8** %5
%26 = load %struct.sockaddr_in*, %struct.sockaddr_in** %7
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %26, i32 0, i32 0
%28 = call i64 @inet_aton(i8* %25, i32* %27)
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %34
30: ; preds = %15
%31 = call i32 @puts(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
%32 = load i32, i32* %8
%33 = call i32 @close(i32 %32)
store i32 -1, i32* %4
br label %36
34: ; preds = %15
%35 = load i32, i32* %8
store i32 %35, i32* %4
br label %36
36: ; preds = %34, %30, %13
%37 = load i32, i32* %4
ret i32 %37
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [22 x i8] c"Socket creation error\00"
@.str.1 = [19 x i8] c"Invalid IP address\00"
define i32 @CreateUdpSocket(i8* %0, i32 %1, %struct.sockaddr_in* %2) {
%4 = load i32, i32* @AF_INET
%5 = load i32, i32* @SOCK_DGRAM
%6 = tail call i32 @socket(i32 %4, i32 %5, i32 0)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %3
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %22
10: ; preds = %3
%11 = tail call i32 @bzero(%struct.sockaddr_in* %2, i32 12)
%12 = load i32, i32* @AF_INET
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %12, i32* %13
%14 = tail call i32 @htons(i32 %1)
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
store i32 %14, i32* %15
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
%17 = tail call i64 @inet_aton(i8* %0, i32* %16)
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %10
%20 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
%21 = tail call i32 @close(i32 %6)
br label %22
22: ; preds = %10, %19, %8
%23 = phi i32 [ -1, %8 ], [ -1, %19 ], [ %6, %10 ]
ret i32 %23
}