#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 */
struct TYPE_2__ {
  void *s_addr;
};
struct sockaddr_in {
  int sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ server;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IPPROTO_UDP;
int /*<<< orphan*/ SOCK_DGRAM;
int atoi(char *);
scalar_t__ connect(int, struct sockaddr *, int);
void *htons(int);
void *inet_addr(char *);
char *inet_ntoa(TYPE_1__);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ puts(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int createsSocket(int argc, char *argv[]) {
  int sock;
  struct sockaddr_in server;
  sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  if (sock == -1) {
    printf("Could not create socket");
    return -1;
  }
  puts("Socket created");
  if (argc >= 2)
    server.sin_addr.s_addr = inet_addr(argv[1]);
  else
    server.sin_addr.s_addr = inet_addr("127.0.0.1");
  server.sin_family = AF_INET;
  if (argc >= 3)
    server.sin_port = htons(atoi(argv[2]));
  else
    server.sin_port = htons(4546);
  printf("Target address: %s,  port %d : addr\n", inet_ntoa(server.sin_addr),
         server.sin_port);
  if (connect(sock, (struct sockaddr *)&server, sizeof(server)) < 0) {
    perror("connect failed. Error");
    return -2;
  }
  puts("Connected\n");
  return sock;
}


x86 assembly (clang, -O0):
<createsSocket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e42(%rip),%rax
mov    (%rax),%edi
mov    0x2e31(%rip),%rax
mov    (%rax),%esi
mov    0x2e10(%rip),%rax
mov    (%rax),%edx
call   10b0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11e6 <createsSocket+0x56>
lea    0xe2d(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c3 <createsSocket+0x133>
lea    0xe2b(%rip),%rdi
call   1030 <puts@plt>
cmpl   $0x2,-0x8(%rbp)
jl     1212 <createsSocket+0x82>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1070 <inet_addr@plt>
mov    %rax,-0x28(%rbp)
jmp    1222 <createsSocket+0x92>
lea    0xe0e(%rip),%rdi
call   1070 <inet_addr@plt>
mov    %rax,-0x28(%rbp)
mov    0x2dbf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
cmpl   $0x3,-0x8(%rbp)
jl     1254 <createsSocket+0xc4>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1090 <atoi@plt>
mov    %eax,%edi
call   1050 <htons@plt>
mov    %eax,-0x30(%rbp)
jmp    1261 <createsSocket+0xd1>
mov    $0x11c2,%edi
call   1050 <htons@plt>
mov    %eax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <inet_ntoa@plt>
mov    %rax,%rsi
mov    -0x30(%rbp),%edx
lea    0xdba(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x14(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x18,%edx
call   10a0 <connect@plt>
cmp    $0x0,%rax
jge    12b1 <createsSocket+0x121>
lea    0xdb6(%rip),%rdi
call   1080 <perror@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    12c3 <createsSocket+0x133>
lea    0xdb4(%rip),%rdi
call   1030 <puts@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<createsSocket>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %rsi,%r15
mov    %edi,%ebp
mov    0x2e40(%rip),%r12
mov    (%r12),%edi
mov    0x2e2d(%rip),%rax
mov    (%rax),%esi
mov    0x2e0c(%rip),%rax
mov    (%rax),%edx
call   10b0 <socket@plt>
cmp    $0xffffffff,%eax
je     1206 <createsSocket+0x76>
mov    %eax,%ebx
lea    0xe47(%rip),%rdi
call   1030 <puts@plt>
cmp    $0x1,%ebp
jle    121b <createsSocket+0x8b>
mov    0x8(%r15),%rdi
call   1070 <inet_addr@plt>
mov    %rax,%r14
mov    %rax,0x10(%rsp)
mov    (%r12),%eax
mov    %eax,0x18(%rsp)
cmp    $0x2,%ebp
je     1237 <createsSocket+0xa7>
mov    0x10(%r15),%rdi
call   1090 <atoi@plt>
mov    %eax,%edi
jmp    123c <createsSocket+0xac>
lea    0xdf3(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0xffffffff,%ebx
jmp    1285 <createsSocket+0xf5>
lea    0xe05(%rip),%rdi
call   1070 <inet_addr@plt>
mov    %rax,%r14
mov    %rax,0x10(%rsp)
mov    (%r12),%eax
mov    %eax,0x18(%rsp)
mov    $0x11c2,%edi
call   1050 <htons@plt>
mov    %rax,%rbp
mov    %ebp,0x8(%rsp)
mov    %r14,%rdi
call   1040 <inet_ntoa@plt>
lea    0xdda(%rip),%rdi
mov    %rax,%rsi
mov    %ebp,%edx
xor    %eax,%eax
call   1060 <printf@plt>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0x18,%edx
call   10a0 <connect@plt>
test   %rax,%rax
js     1294 <createsSocket+0x104>
lea    0xdec(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdbb(%rip),%rdi
call   1080 <perror@plt>
mov    $0xfffffffe,%ebx
jmp    1285 <createsSocket+0xf5>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i8* }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@.str = [24 x i8] c"Could not create socket\00"
@.str.1 = [15 x i8] c"Socket created\00"
@.str.2 = [10 x i8] c"127.0.0.1\00"
@.str.3 = [37 x i8] c"Target address: %s, port %d : addr\0A\00"
@.str.4 = [22 x i8] c"connect failed. Error\00"
@.str.5 = [11 x i8] c"Connected\0A\00"
define i32 @createsSocket(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca %struct.sockaddr_in
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* @AF_INET
%9 = load i32, i32* @SOCK_DGRAM
%10 = load i32, i32* @IPPROTO_UDP
%11 = call i32 @socket(i32 %8, i32 %9, i32 %10)
store i32 %11, i32* %6
%12 = load i32, i32* %6
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %65
16: ; preds = %2
%17 = call i32 @puts(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%18 = load i32, i32* %4
%19 = icmp sge i32 %18, 2
br i1 %19, label %20, label %27
20: ; preds = %16
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 1
%23 = load i8*, i8** %22
%24 = call i8* @inet_addr(i8* %23)
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
%26 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %25, i32 0, i32 0
store i8* %24, i8** %26
br label %31
27: ; preds = %16
%28 = call i8* @inet_addr(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
%30 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %29, i32 0, i32 0
store i8* %28, i8** %30
br label %31
31: ; preds = %27, %20
%32 = load i32, i32* @AF_INET
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i32 %32, i32* %33
%34 = load i32, i32* %4
%35 = icmp sge i32 %34, 3
br i1 %35, label %36, label %44
36: ; preds = %31
%37 = load i8**, i8*** %5
%38 = getelementptr inbounds i8*, i8** %37, i64 2
%39 = load i8*, i8** %38
%40 = call i32 @atoi(i8* %39)
%41 = call i8* @htons(i32 %40)
%42 = ptrtoint i8* %41 to i32
%43 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
store i32 %42, i32* %43
br label %48
44: ; preds = %31
%45 = call i8* @htons(i32 4546)
%46 = ptrtoint i8* %45 to i32
%47 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
store i32 %46, i32* %47
br label %48
48: ; preds = %44, %36
%49 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
%50 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %49, i32 0, i32 0
%51 = load i8*, i8** %50
%52 = call i8* @inet_ntoa(i8* %51)
%53 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
%54 = load i32, i32* %53
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0), i8* %52, i32 %54)
%56 = load i32, i32* %6
%57 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%58 = call i64 @connect(i32 %56, %struct.sockaddr* %57, i32 24)
%59 = icmp slt i64 %58, 0
br i1 %59, label %60, label %62
60: ; preds = %48
%61 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0))
store i32 -2, i32* %3
br label %65
62: ; preds = %48
%63 = call i32 @puts(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0))
%64 = load i32, i32* %6
store i32 %64, i32* %3
br label %65
65: ; preds = %62, %60, %14
%66 = load i32, i32* %3
ret i32 %66
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i8* }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@.str = [24 x i8] c"Could not create socket\00"
@.str.1 = [15 x i8] c"Socket created\00"
@.str.2 = [10 x i8] c"127.0.0.1\00"
@.str.3 = [37 x i8] c"Target address: %s, port %d : addr\0A\00"
@.str.4 = [22 x i8] c"connect failed. Error\00"
@.str.5 = [11 x i8] c"Connected\0A\00"
define i32 @createsSocket(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = bitcast %struct.sockaddr_in* %3 to i8*
%5 = load i32, i32* @AF_INET
%6 = load i32, i32* @SOCK_DGRAM
%7 = load i32, i32* @IPPROTO_UDP
%8 = tail call i32 @socket(i32 %5, i32 %6, i32 %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
br label %54
12: ; preds = %2
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%14 = icmp sgt i32 %0, 1
br i1 %14, label %20, label %15
15: ; preds = %12
%16 = tail call i8* @inet_addr(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
store i8* %16, i8** %17
%18 = load i32, i32* @AF_INET
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %18, i32* %19
br label %36
20: ; preds = %12
%21 = getelementptr inbounds i8*, i8** %1, i64 1
%22 = load i8*, i8** %21
%23 = tail call i8* @inet_addr(i8* %22)
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
store i8* %23, i8** %24
%25 = load i32, i32* @AF_INET
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %25, i32* %26
%27 = icmp eq i32 %0, 2
br i1 %27, label %36, label %28
28: ; preds = %20
%29 = getelementptr inbounds i8*, i8** %1, i64 2
%30 = load i8*, i8** %29
%31 = tail call i32 @atoi(i8* %30)
%32 = tail call i8* @htons(i32 %31)
%33 = ptrtoint i8* %32 to i64
%34 = trunc i64 %33 to i32
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %34, i32* %35
br label %42
36: ; preds = %15, %20
%37 = phi i8* [ %16, %15 ], [ %23, %20 ]
%38 = tail call i8* @htons(i32 4546)
%39 = ptrtoint i8* %38 to i64
%40 = trunc i64 %39 to i32
%41 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %40, i32* %41
br label %42
42: ; preds = %36, %28
%43 = phi i32 [ %40, %36 ], [ %34, %28 ]
%44 = phi i8* [ %37, %36 ], [ %23, %28 ]
%45 = tail call i8* @inet_ntoa(i8* %44)
%46 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0), i8* %45, i32 %43)
%47 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%48 = call i64 @connect(i32 %8, %struct.sockaddr* %47, i32 24)
%49 = icmp slt i64 %48, 0
br i1 %49, label %50, label %52
50: ; preds = %42
%51 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0))
br label %54
52: ; preds = %42
%53 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0))
br label %54
54: ; preds = %52, %50, %10
%55 = phi i32 [ -1, %10 ], [ -2, %50 ], [ %8, %52 ]
ret i32 %55
}