#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;
};
struct sockaddr {
  int dummy;
};
struct in_addr {
  int dummy;
};
struct hostent {
  int /*<<< orphan*/ *h_addr_list;
};
typedef int /*<<< orphan*/ inaddr;
typedef int /*<<< orphan*/ address;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ copyData(int /*<<< orphan*/, int);
int /*<<< orphan*/ die(char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
struct hostent *gethostbyaddr(char *, int, int /*<<< orphan*/);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ herror(char *);
int /*<<< orphan*/ htons(int);
scalar_t__ inet_aton(char *, struct in_addr *);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int main(int argc, char **argv) {
  struct sockaddr_in address;
  struct in_addr inaddr;
  struct hostent *host;
  int sock;
  if (argc != 2) {
    fprintf(stderr, "only a single argument is supported\n");
    return 1;
  }
  if (inet_aton(argv[1], &inaddr))
    host = gethostbyaddr((char *)&inaddr, sizeof(inaddr), AF_INET);
  else
    host = gethostbyname(argv[1]);
  if (!host) {
    herror("error looking up host");
    exit(1);
  }
  if ((sock = socket(PF_INET, SOCK_STREAM, 0)) < 0)
    die("socket");
  address.sin_family = AF_INET;
  address.sin_port = htons(1234);
  memcpy(&address.sin_addr, host->h_addr_list[0], sizeof(address.sin_addr));
  if (connect(sock, (struct sockaddr *)&address, sizeof(address)))
    die("connect");
  copyData(0, sock);
  close(sock);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     1211 <main+0x41>
mov    0x2de1(%rip),%rax
mov    (%rax),%edi
lea    0xe00(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    131f <main+0x14f>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x28(%rbp),%rsi
call   1080 <inet_aton@plt>
cmp    $0x0,%rax
je     124c <main+0x7c>
lea    -0x28(%rbp),%rdi
mov    0x2db1(%rip),%rax
mov    (%rax),%edx
mov    $0x4,%esi
call   10c0 <gethostbyaddr@plt>
mov    %rax,-0x30(%rbp)
jmp    125d <main+0x8d>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1050 <gethostbyname@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    127e <main+0xae>
lea    0xdb6(%rip),%rdi
call   1090 <herror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    0x2d43(%rip),%rax
mov    (%rax),%edi
mov    0x2d42(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,-0x34(%rbp)
cmp    $0x0,%eax
jge    12af <main+0xdf>
lea    0xd91(%rip),%rdi
call   10a0 <die@plt>
mov    0x2d32(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    $0x4d2,%edi
call   1030 <htons@plt>
mov    %eax,-0x1c(%rbp)
lea    -0x20(%rbp),%rdi
mov    -0x30(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%esi
mov    $0x4,%edx
call   1070 <memcpy@plt>
mov    -0x34(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   10e0 <connect@plt>
cmp    $0x0,%rax
je     1306 <main+0x136>
lea    0xd41(%rip),%rdi
call   10a0 <die@plt>
mov    -0x34(%rbp),%esi
xor    %edi,%edi
call   10b0 <copyData@plt>
mov    -0x34(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
cmp    $0x2,%edi
jne    120d <main+0x3d>
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
lea    0x8(%rsp),%rsi
call   1080 <inet_aton@plt>
test   %rax,%rax
je     122c <main+0x5c>
mov    0x2dee(%rip),%rax
mov    (%rax),%edx
lea    0x8(%rsp),%rdi
mov    $0x4,%esi
call   10c0 <gethostbyaddr@plt>
jmp    1235 <main+0x65>
mov    0x2dc4(%rip),%rax
mov    (%rax),%edi
lea    0xde3(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%ebp
jmp    12d0 <main+0x100>
mov    0x8(%rbx),%rdi
call   1050 <gethostbyname@plt>
mov    %rax,%r14
test   %rax,%rax
je     12db <main+0x10b>
mov    0x2d80(%rip),%rax
mov    (%rax),%edi
mov    0x2d7f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    126c <main+0x9c>
lea    0xdd4(%rip),%rdi
call   10a0 <die@plt>
mov    0x2d75(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    $0x4d2,%edi
call   1030 <htons@plt>
mov    %eax,0x14(%rsp)
mov    (%r14),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x4,%edx
call   1070 <memcpy@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0xc,%edx
call   10e0 <connect@plt>
test   %rax,%rax
je     12be <main+0xee>
lea    0xd89(%rip),%rdi
call   10a0 <die@plt>
xor    %ebp,%ebp
xor    %edi,%edi
mov    %ebx,%esi
call   10b0 <copyData@plt>
mov    %ebx,%edi
call   1040 <close@plt>
mov    %ebp,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xd43(%rip),%rdi
call   1090 <herror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.in_addr = type { i32 }
%struct.hostent = type { i32* }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [37 x i8] c"only a single argument is supported\0A\00"
@AF_INET = global i32 0
@.str.1 = [22 x i8] c"error looking up host\00"
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.2 = [7 x i8] c"socket\00"
@.str.3 = [8 x i8] c"connect\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.sockaddr_in
%7 = alloca %struct.in_addr
%8 = alloca %struct.hostent*
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 2
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %66
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 1
%18 = load i8*, i8** %17
%19 = call i64 @inet_aton(i8* %18, %struct.in_addr* %7)
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %25
21: ; preds = %15
%22 = bitcast %struct.in_addr* %7 to i8*
%23 = load i32, i32* @AF_INET
%24 = call %struct.hostent* @gethostbyaddr(i8* %22, i32 4, i32 %23)
store %struct.hostent* %24, %struct.hostent** %8
br label %30
25: ; preds = %15
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 1
%28 = load i8*, i8** %27
%29 = call %struct.hostent* @gethostbyname(i8* %28)
store %struct.hostent* %29, %struct.hostent** %8
br label %30
30: ; preds = %25, %21
%31 = load %struct.hostent*, %struct.hostent** %8
%32 = icmp ne %struct.hostent* %31, null
br i1 %32, label %36, label %33
33: ; preds = %30
%34 = call i32 @herror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %30
%37 = load i32, i32* @PF_INET
%38 = load i32, i32* @SOCK_STREAM
%39 = call i32 @socket(i32 %37, i32 %38, i32 0)
store i32 %39, i32* %9
%40 = icmp slt i32 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %36
%42 = call i32 @die(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %43
43: ; preds = %41, %36
%44 = load i32, i32* @AF_INET
%45 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 %44, i32* %45
%46 = call i32 @htons(i32 1234)
%47 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
store i32 %46, i32* %47
%48 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%49 = load %struct.hostent*, %struct.hostent** %8
%50 = getelementptr inbounds %struct.hostent, %struct.hostent* %49, i32 0, i32 0
%51 = load i32*, i32** %50
%52 = getelementptr inbounds i32, i32* %51, i64 0
%53 = load i32, i32* %52
%54 = call i32 @memcpy(i32* %48, i32 %53, i32 4)
%55 = load i32, i32* %9
%56 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%57 = call i64 @connect(i32 %55, %struct.sockaddr* %56, i32 12)
%58 = icmp ne i64 %57, 0
br i1 %58, label %59, label %61
59: ; preds = %43
%60 = call i32 @die(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
br label %61
61: ; preds = %59, %43
%62 = load i32, i32* %9
%63 = call i32 @copyData(i32 0, i32 %62)
%64 = load i32, i32* %9
%65 = call i32 @close(i32 %64)
store i32 0, i32* %3
br label %66
66: ; preds = %61, %12
%67 = load i32, i32* %3
ret i32 %67
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.in_addr = type { i32 }
%struct.hostent = type { i32* }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [37 x i8] c"only a single argument is supported\0A\00"
@AF_INET = global i32 0
@.str.1 = [22 x i8] c"error looking up host\00"
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.2 = [7 x i8] c"socket\00"
@.str.3 = [8 x i8] c"connect\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca %struct.in_addr
%5 = bitcast %struct.sockaddr_in* %3 to i8*
%6 = bitcast %struct.in_addr* %4 to i8*
%7 = icmp eq i32 %0, 2
br i1 %7, label %11, label %8
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
br label %53
11: ; preds = %2
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%13 = load i8*, i8** %12
%14 = call i64 @inet_aton(i8* %13, %struct.in_addr* %4)
%15 = icmp eq i64 %14, 0
br i1 %15, label %19, label %16
16: ; preds = %11
%17 = load i32, i32* @AF_INET
%18 = call %struct.hostent* @gethostbyaddr(i8* %6, i32 4, i32 %17)
br label %22
19: ; preds = %11
%20 = load i8*, i8** %12
%21 = call %struct.hostent* @gethostbyname(i8* %20)
br label %22
22: ; preds = %19, %16
%23 = phi %struct.hostent* [ %18, %16 ], [ %21, %19 ]
%24 = icmp eq %struct.hostent* %23, null
br i1 %24, label %25, label %28
25: ; preds = %22
%26 = call i32 @herror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %22
%29 = load i32, i32* @PF_INET
%30 = load i32, i32* @SOCK_STREAM
%31 = call i32 @socket(i32 %29, i32 %30, i32 0)
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %28
%34 = call i32 @die(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %35
35: ; preds = %33, %28
%36 = load i32, i32* @AF_INET
%37 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %36, i32* %37
%38 = call i32 @htons(i32 1234)
%39 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %38, i32* %39
%40 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
%41 = getelementptr inbounds %struct.hostent, %struct.hostent* %23, i64 0, i32 0
%42 = load i32*, i32** %41
%43 = load i32, i32* %42
%44 = call i32 @memcpy(i32* %40, i32 %43, i32 4)
%45 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%46 = call i64 @connect(i32 %31, %struct.sockaddr* %45, i32 12)
%47 = icmp eq i64 %46, 0
br i1 %47, label %50, label %48
48: ; preds = %35
%49 = call i32 @die(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
br label %50
50: ; preds = %48, %35
%51 = call i32 @copyData(i32 0, i32 %31)
%52 = call i32 @close(i32 %31)
br label %53
53: ; preds = %50, %8
%54 = phi i32 [ 1, %8 ], [ 0, %50 ]
ret i32 %54
}