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

/* Variables and functions */
int /*<<< orphan*/ inet_ntoa(TYPE_1__);
unsigned long ntohl(int /*<<< orphan*/);
unsigned short ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, unsigned short, ...);
int resolve(char *, char *, char *, struct sockaddr_in *);
int main(int argc, char *argv[]) {
  char *host = "localhost";
  char *service = "http";
  char *protocol = "tcp";
  struct sockaddr_in addr;
  unsigned short port;
  unsigned long address;
  int err;
  if (argc > 1)
    host = argv[1];
  if (argc > 2)
    service = argv[2];
  if (argc > 3)
    protocol = argv[3];
  err = resolve(host, service, protocol, &addr);
  if (!err) {
    port = ntohs(addr.sin_port);
    address = ntohl(addr.sin_addr.s_addr);
    printf("sin_family = %12d\n", addr.sin_family);
    printf("sin_addr   = %12lu (0x%08lX) (%s)\n", address, address,
           inet_ntoa(addr.sin_addr));
    printf("sin_port   = %12hu (0x%08hX)\n", port, port);
  }
  return err;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe93(%rip),%rax
mov    %rax,-0x18(%rbp)
lea    0xe92(%rip),%rax
mov    %rax,-0x20(%rbp)
lea    0xe8c(%rip),%rax
mov    %rax,-0x28(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    119d <main+0x4d>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
cmpl   $0x2,-0x8(%rbp)
jle    11b3 <main+0x63>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x20(%rbp)
cmpl   $0x3,-0x8(%rbp)
jle    11c9 <main+0x79>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
lea    -0x38(%rbp),%rcx
call   1050 <resolve@plt>
mov    %eax,-0x4c(%rbp)
cmpl   $0x0,-0x4c(%rbp)
jne    1263 <main+0x113>
mov    -0x30(%rbp),%edi
call   1060 <ntohs@plt>
mov    %ax,-0x3a(%rbp)
mov    -0x34(%rbp),%edi
call   1070 <ntohl@plt>
mov    %rax,-0x48(%rbp)
mov    -0x38(%rbp),%eax
lea    0xe06(%rip),%rdi
movzwl %ax,%esi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x48(%rbp),%rax
mov    %ax,-0x4e(%rbp)
mov    -0x48(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x34(%rbp),%edi
call   1030 <inet_ntoa@plt>
mov    -0x58(%rbp),%rdx
mov    %eax,%ecx
mov    -0x4e(%rbp),%ax
lea    0xde6(%rip),%rdi
movzwl %ax,%esi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x3a(%rbp),%ax
movzwl -0x3a(%rbp),%edx
lea    0xdf0(%rip),%rdi
movzwl %ax,%esi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x4c(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rax
lea    0xead(%rip),%rdx
lea    0xea1(%rip),%rsi
cmp    $0x2,%edi
jl     1185 <main+0x35>
mov    %edi,%ecx
mov    0x8(%rax),%rdi
je     118c <main+0x3c>
mov    0x10(%rax),%rsi
cmp    $0x4,%ecx
jb     118c <main+0x3c>
mov    0x18(%rax),%rdx
jmp    118c <main+0x3c>
lea    0xe74(%rip),%rdi
mov    %rsp,%rcx
call   1050 <resolve@plt>
mov    %eax,%ebx
test   %eax,%eax
jne    11f7 <main+0xa7>
mov    0x8(%rsp),%edi
call   1060 <ntohs@plt>
mov    %eax,%r14d
mov    0x4(%rsp),%edi
call   1070 <ntohl@plt>
mov    %rax,%rbp
movzwl (%rsp),%esi
lea    0xe56(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x4(%rsp),%edi
call   1030 <inet_ntoa@plt>
movzwl %bp,%esi
lea    0xe4f(%rip),%rdi
mov    %rbp,%rdx
mov    %eax,%ecx
xor    %eax,%eax
call   1040 <printf@plt>
movzwl %r14w,%edx
lea    0xe5b(%rip),%rdi
mov    %edx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebx,%eax
add    $0x10,%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 }
@.str = [10 x i8] c"localhost\00"
@.str.1 = [5 x i8] c"http\00"
@.str.2 = [4 x i8] c"tcp\00"
@.str.3 = [19 x i8] c"sin_family = %12d\0A\00"
@.str.4 = [35 x i8] c"sin_addr = %12lu (0x%08lX) (%s)\0A\00"
@.str.5 = [30 x i8] c"sin_port = %12hu (0x%08hX)\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca %struct.sockaddr_in
%10 = alloca i16
%11 = alloca i64
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8** %6
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8** %7
store i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8** %8
%13 = load i32, i32* %4
%14 = icmp sgt i32 %13, 1
br i1 %14, label %15, label %19
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 1
%18 = load i8*, i8** %17
store i8* %18, i8** %6
br label %19
19: ; preds = %15, %2
%20 = load i32, i32* %4
%21 = icmp sgt i32 %20, 2
br i1 %21, label %22, label %26
22: ; preds = %19
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 2
%25 = load i8*, i8** %24
store i8* %25, i8** %7
br label %26
26: ; preds = %22, %19
%27 = load i32, i32* %4
%28 = icmp sgt i32 %27, 3
br i1 %28, label %29, label %33
29: ; preds = %26
%30 = load i8**, i8*** %5
%31 = getelementptr inbounds i8*, i8** %30, i64 3
%32 = load i8*, i8** %31
store i8* %32, i8** %8
br label %33
33: ; preds = %29, %26
%34 = load i8*, i8** %6
%35 = load i8*, i8** %7
%36 = load i8*, i8** %8
%37 = call i32 @resolve(i8* %34, i8* %35, i8* %36, %struct.sockaddr_in* %9)
store i32 %37, i32* %12
%38 = load i32, i32* %12
%39 = icmp ne i32 %38, 0
br i1 %39, label %64, label %40
40: ; preds = %33
%41 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 2
%42 = load i32, i32* %41
%43 = call zeroext i16 @ntohs(i32 %42)
store i16 %43, i16* %10
%44 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 1
%45 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %44, i32 0, i32 0
%46 = load i32, i32* %45
%47 = call i64 @ntohl(i32 %46)
store i64 %47, i64* %11
%48 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
%49 = load i32, i32* %48
%50 = trunc i32 %49 to i16
%51 = call i32 (i8*, i16, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i16 zeroext %50)
%52 = load i64, i64* %11
%53 = trunc i64 %52 to i16
%54 = load i64, i64* %11
%55 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 1
%56 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %55, i32 0, i32 0
%57 = load i32, i32* %56
%58 = call i32 @inet_ntoa(i32 %57)
%59 = call i32 (i8*, i16, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.4, i64 0, i64 0), i16 zeroext %53, i64 %54, i32 %58)
%60 = load i16, i16* %10
%61 = load i16, i16* %10
%62 = zext i16 %61 to i32
%63 = call i32 (i8*, i16, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.5, i64 0, i64 0), i16 zeroext %60, i32 %62)
br label %64
64: ; preds = %40, %33
%65 = load i32, i32* %12
ret i32 %65
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
@.str = [10 x i8] c"localhost\00"
@.str.1 = [5 x i8] c"http\00"
@.str.2 = [4 x i8] c"tcp\00"
@.str.3 = [19 x i8] c"sin_family = %12d\0A\00"
@.str.4 = [35 x i8] c"sin_addr = %12lu (0x%08lX) (%s)\0A\00"
@.str.5 = [30 x i8] c"sin_port = %12hu (0x%08hX)\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = bitcast %struct.sockaddr_in* %3 to i8*
%5 = icmp sgt i32 %0, 1
br i1 %5, label %6, label %17
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = icmp eq i32 %0, 2
br i1 %9, label %17, label %10
10: ; preds = %6
%11 = getelementptr inbounds i8*, i8** %1, i64 2
%12 = load i8*, i8** %11
%13 = icmp ugt i32 %0, 3
br i1 %13, label %14, label %17
14: ; preds = %10
%15 = getelementptr inbounds i8*, i8** %1, i64 3
%16 = load i8*, i8** %15
br label %17
17: ; preds = %2, %6, %14, %10
%18 = phi i8* [ %12, %14 ], [ %12, %10 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), %6 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), %2 ]
%19 = phi i8* [ %8, %14 ], [ %8, %10 ], [ %8, %6 ], [ getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), %2 ]
%20 = phi i8* [ %16, %14 ], [ getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), %10 ], [ getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), %6 ], [ getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), %2 ]
%21 = call i32 @resolve(i8* %19, i8* %18, i8* %20, %struct.sockaddr_in* %3)
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %40
23: ; preds = %17
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
%25 = load i32, i32* %24
%26 = call zeroext i16 @ntohs(i32 %25)
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
%28 = load i32, i32* %27
%29 = call i64 @ntohl(i32 %28)
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
%31 = load i32, i32* %30
%32 = trunc i32 %31 to i16
%33 = call i32 (i8*, i16, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i16 zeroext %32)
%34 = trunc i64 %29 to i16
%35 = load i32, i32* %27
%36 = call i32 @inet_ntoa(i32 %35)
%37 = call i32 (i8*, i16, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.4, i64 0, i64 0), i16 zeroext %34, i64 %29, i32 %36)
%38 = zext i16 %26 to i32
%39 = call i32 (i8*, i16, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.5, i64 0, i64 0), i16 zeroext %26, i32 %38)
br label %40
40: ; preds = %23, %17
ret i32 %21
}