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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
int atoi(char *);
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ error(char *);
int /*<<< orphan*/ htonl(int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int parseip(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
char *strchr(char *, char);
int makefd(char *spec) {
  int port = 53;
  int ip4 = 0x7f000001;
  struct sockaddr_in sa;
  int fd = -1;
  if (strchr(spec, '@')) {
    char *p = strchr(spec, '@');
    port = atoi(p + 1);
    *p = 0;
    ip4 = parseip(spec);
    *p = '@';
  } else if (strchr(spec, '.')) {
    ip4 = parseip(spec);
  } else {
    port = atoi(spec);
  }
  fd = socket(AF_INET, SOCK_DGRAM, 0);
  if (fd == -1)
    error("socket() failed");
  memset(&sa, 0, sizeof(sa));
  sa.sin_family = AF_INET;
  sa.sin_port = htons(port);
  sa.sin_addr.s_addr = htonl(ip4);
  if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1)
    error("connect() failed");
  return fd;
}


x86 assembly (clang, -O0):
<makefd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x35,-0xc(%rbp)
movl   $0x7f000001,-0x10(%rbp)
movl   $0xffffffff,-0x24(%rbp)
mov    -0x8(%rbp),%rdi
mov    $0x40,%esi
call   1040 <strchr@plt>
cmp    $0x0,%rax
je     120a <makefd+0x7a>
mov    -0x8(%rbp),%rdi
mov    $0x40,%esi
call   1040 <strchr@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
add    $0x1,%rdi
call   1080 <atoi@plt>
mov    %eax,-0xc(%rbp)
mov    -0x30(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x8(%rbp),%rdi
call   10a0 <parseip@plt>
mov    %eax,-0x10(%rbp)
mov    -0x30(%rbp),%rax
movb   $0x40,(%rax)
jmp    1244 <makefd+0xb4>
mov    -0x8(%rbp),%rdi
mov    $0x2e,%esi
call   1040 <strchr@plt>
cmp    $0x0,%rax
je     1233 <makefd+0xa3>
mov    -0x8(%rbp),%rdi
call   10a0 <parseip@plt>
mov    %eax,-0x10(%rbp)
jmp    123f <makefd+0xaf>
mov    -0x8(%rbp),%rdi
call   1080 <atoi@plt>
mov    %eax,-0xc(%rbp)
jmp    1244 <makefd+0xb4>
mov    0x2d9d(%rip),%rax
mov    (%rax),%edi
mov    0x2d8c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    1276 <makefd+0xe6>
lea    0xd8f(%rip),%rdi
call   1070 <error@plt>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2d5b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0xc(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%edi
call   1050 <htonl@plt>
mov    %eax,-0x20(%rbp)
mov    -0x24(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1090 <connect@plt>
cmp    $0xffffffff,%eax
jne    12ce <makefd+0x13e>
lea    0xd47(%rip),%rdi
call   1070 <error@plt>
mov    -0x24(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<makefd>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
mov    $0x40,%esi
call   1040 <strchr@plt>
test   %rax,%rax
je     11d5 <makefd+0x45>
mov    %rax,%rbp
mov    %rax,%rdi
add    $0x1,%rdi
call   1080 <atoi@plt>
mov    %eax,%r14d
movb   $0x0,0x0(%rbp)
mov    %rbx,%rdi
call   10a0 <parseip@plt>
mov    %eax,%r15d
movb   $0x40,0x0(%rbp)
jmp    1208 <makefd+0x78>
mov    %rbx,%rdi
mov    $0x2e,%esi
call   1040 <strchr@plt>
mov    %rbx,%rdi
test   %rax,%rax
je     11fa <makefd+0x6a>
call   10a0 <parseip@plt>
mov    %eax,%r15d
mov    $0x35,%r14d
jmp    1208 <makefd+0x78>
call   1080 <atoi@plt>
mov    %eax,%r14d
mov    $0x7f000001,%r15d
mov    0x2dd9(%rip),%r12
mov    (%r12),%edi
mov    0x2dc6(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    1236 <makefd+0xa6>
lea    0xdcf(%rip),%rdi
call   1070 <error@plt>
mov    %rsp,%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    (%r12),%eax
mov    %eax,0x8(%rsp)
mov    %r14d,%edi
call   1030 <htons@plt>
mov    %eax,0x4(%rsp)
mov    %r15d,%edi
call   1050 <htonl@plt>
mov    %eax,(%rsp)
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0xc,%edx
call   1090 <connect@plt>
cmp    $0xffffffff,%eax
jne    1287 <makefd+0xf7>
lea    0xd8e(%rip),%rdi
call   1070 <error@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [16 x i8] c"socket() failed\00"
@.str.1 = [17 x i8] c"connect() failed\00"
define i32 @makefd(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.sockaddr_in
%6 = alloca i32
%7 = alloca i8*
store i8* %0, i8** %2
store i32 53, i32* %3
store i32 2130706433, i32* %4
store i32 -1, i32* %6
%8 = load i8*, i8** %2
%9 = call i8* @strchr(i8* %8, i8 signext 64)
%10 = icmp ne i8* %9, null
br i1 %10, label %11, label %21
11: ; preds = %1
%12 = load i8*, i8** %2
%13 = call i8* @strchr(i8* %12, i8 signext 64)
store i8* %13, i8** %7
%14 = load i8*, i8** %7
%15 = getelementptr inbounds i8, i8* %14, i64 1
%16 = call i32 @atoi(i8* %15)
store i32 %16, i32* %3
%17 = load i8*, i8** %7
store i8 0, i8* %17
%18 = load i8*, i8** %2
%19 = call i32 @parseip(i8* %18)
store i32 %19, i32* %4
%20 = load i8*, i8** %7
store i8 64, i8* %20
br label %32
21: ; preds = %1
%22 = load i8*, i8** %2
%23 = call i8* @strchr(i8* %22, i8 signext 46)
%24 = icmp ne i8* %23, null
br i1 %24, label %25, label %28
25: ; preds = %21
%26 = load i8*, i8** %2
%27 = call i32 @parseip(i8* %26)
store i32 %27, i32* %4
br label %31
28: ; preds = %21
%29 = load i8*, i8** %2
%30 = call i32 @atoi(i8* %29)
store i32 %30, i32* %3
br label %31
31: ; preds = %28, %25
br label %32
32: ; preds = %31, %11
%33 = load i32, i32* @AF_INET
%34 = load i32, i32* @SOCK_DGRAM
%35 = call i32 @socket(i32 %33, i32 %34, i32 0)
store i32 %35, i32* %6
%36 = load i32, i32* %6
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %40
38: ; preds = %32
%39 = call i32 @error(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %40
40: ; preds = %38, %32
%41 = call i32 @memset(%struct.sockaddr_in* %5, i32 0, i32 12)
%42 = load i32, i32* @AF_INET
%43 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 2
store i32 %42, i32* %43
%44 = load i32, i32* %3
%45 = call i32 @htons(i32 %44)
%46 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 1
store i32 %45, i32* %46
%47 = load i32, i32* %4
%48 = call i32 @htonl(i32 %47)
%49 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 0
%50 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %49, i32 0, i32 0
store i32 %48, i32* %50
%51 = load i32, i32* %6
%52 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%53 = call i32 @connect(i32 %51, %struct.sockaddr* %52, i32 12)
%54 = icmp eq i32 %53, -1
br i1 %54, label %55, label %57
55: ; preds = %40
%56 = call i32 @error(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
br label %57
57: ; preds = %55, %40
%58 = load i32, i32* %6
ret i32 %58
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [16 x i8] c"socket() failed\00"
@.str.1 = [17 x i8] c"connect() failed\00"
define i32 @makefd(i8* %0) {
%2 = alloca %struct.sockaddr_in
%3 = bitcast %struct.sockaddr_in* %2 to i8*
%4 = tail call i8* @strchr(i8* dereferenceable(1) %0, i8 signext 64)
%5 = icmp eq i8* %4, null
br i1 %5, label %10, label %6
6: ; preds = %1
%7 = getelementptr inbounds i8, i8* %4, i64 1
%8 = tail call i32 @atoi(i8* %7)
store i8 0, i8* %4
%9 = tail call i32 @parseip(i8* %0)
store i8 64, i8* %4
br label %17
10: ; preds = %1
%11 = tail call i8* @strchr(i8* dereferenceable(1) %0, i8 signext 46)
%12 = icmp eq i8* %11, null
br i1 %12, label %15, label %13
13: ; preds = %10
%14 = tail call i32 @parseip(i8* %0)
br label %17
15: ; preds = %10
%16 = tail call i32 @atoi(i8* %0)
br label %17
17: ; preds = %13, %15, %6
%18 = phi i32 [ %9, %6 ], [ %14, %13 ], [ 2130706433, %15 ]
%19 = phi i32 [ %8, %6 ], [ 53, %13 ], [ %16, %15 ]
%20 = load i32, i32* @AF_INET
%21 = load i32, i32* @SOCK_DGRAM
%22 = tail call i32 @socket(i32 %20, i32 %21, i32 0)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %26
24: ; preds = %17
%25 = tail call i32 @error(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %26
26: ; preds = %24, %17
%27 = call i32 @memset(%struct.sockaddr_in* %2, i32 0, i32 12)
%28 = load i32, i32* @AF_INET
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %28, i32* %29
%30 = call i32 @htons(i32 %19)
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
store i32 %30, i32* %31
%32 = call i32 @htonl(i32 %18)
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0, i32 0
store i32 %32, i32* %33
%34 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%35 = call i32 @connect(i32 %22, %struct.sockaddr* %34, i32 12)
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %39
37: ; preds = %26
%38 = call i32 @error(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
br label %39
39: ; preds = %37, %26
ret i32 %22
}