#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;
  void *sin_family;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  char *h_name;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
void *AF_INET;
int errno;
struct hostent *gethostbyaddr(void const *, int, int);
int h_errno;
char *hstrerror(int);
int /*<<< orphan*/ htons(int);
int inet_pton(void *, char *, struct sockaddr *);
int /*<<< orphan*/ printf(char *, char *, ...);
char *strerror(int);
int /*<<< orphan*/ strncpy(char *, char *, int);
int main(int argc, char **argv) {
  char ip_string[4 * 4] = "127.0.0.1";
  if (argc >= 0) {
    strncpy(ip_string, argv[1], sizeof ip_string);
  }
  struct sockaddr_in addr;
  addr.sin_family = AF_INET;
  addr.sin_port = htons(80);
  int ret =
      inet_pton(AF_INET, ip_string, (struct sockaddr *)&addr.sin_addr.s_addr);
  if (ret == 0) {
    printf("%s(%d)\n", strerror(errno), errno);
    return 1;
  }
  struct hostent *ht = NULL;
  socklen_t len = sizeof(addr);
  int type = AF_INET;
  ht = gethostbyaddr((const void *)&(addr.sin_addr.s_addr), len, type);
  if (ht == NULL)
    printf("%s(%d)\n", hstrerror(h_errno), h_errno);
  else
    printf("ip_string = %s, ht = %p, ht->h_name = %s\n", ip_string, ht,
           ht->h_name);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0xe73(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0xe70(%rip),%rax
mov    %rax,-0x18(%rbp)
cmpl   $0x0,-0x8(%rbp)
jl     11bc <main+0x4c>
lea    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
mov    $0x10,%edx
call   1030 <strncpy@plt>
mov    0x2e25(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    $0x50,%edi
call   1040 <htons@plt>
mov    %eax,-0x2c(%rbp)
mov    0x2e0a(%rip),%rax
mov    (%rax),%rdi
lea    -0x20(%rbp),%rsi
lea    -0x30(%rbp),%rdx
call   1060 <inet_pton@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jne    122f <main+0xbf>
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    %rax,%rsi
mov    0x2dbd(%rip),%rax
mov    (%rax),%edx
lea    0xdf4(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12ba <main+0x14a>
movq   $0x0,-0x40(%rbp)
movl   $0x10,-0x44(%rbp)
mov    0x2da3(%rip),%rax
mov    (%rax),%rax
mov    %eax,-0x48(%rbp)
lea    -0x30(%rbp),%rdi
mov    -0x44(%rbp),%esi
mov    -0x48(%rbp),%edx
call   1080 <gethostbyaddr@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    1296 <main+0x126>
mov    0x2d70(%rip),%rax
mov    (%rax),%edi
call   1070 <hstrerror@plt>
mov    %rax,%rsi
mov    0x2d5f(%rip),%rax
mov    (%rax),%edx
lea    0xd86(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    12b3 <main+0x143>
lea    -0x20(%rbp),%rsi
mov    -0x40(%rbp),%rdx
mov    -0x40(%rbp),%rax
mov    (%rax),%rcx
lea    0xd6c(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x20,%rsp
movaps 0xe84(%rip),%xmm0
movaps %xmm0,(%rsp)
test   %edi,%edi
js     1195 <main+0x25>
mov    0x8(%rsi),%rsi
mov    %rsp,%rdi
mov    $0x10,%edx
call   1030 <strncpy@plt>
mov    0x2e4c(%rip),%rbx
mov    (%rbx),%rax
mov    %rax,0x18(%rsp)
mov    $0x50,%edi
call   1040 <htons@plt>
mov    %eax,0x14(%rsp)
mov    (%rbx),%rdi
mov    %rsp,%rsi
lea    0x10(%rsp),%rdx
call   1060 <inet_pton@plt>
test   %eax,%eax
je     11f7 <main+0x87>
mov    (%rbx),%edx
lea    0x10(%rsp),%rdi
mov    $0x10,%esi
call   1080 <gethostbyaddr@plt>
test   %rax,%rax
je     121f <main+0xaf>
mov    (%rax),%rcx
lea    0xe32(%rip),%rdi
xor    %ebx,%ebx
mov    %rsp,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1242 <main+0xd2>
mov    0x2dd2(%rip),%rbx
mov    (%rbx),%edi
call   1090 <strerror@plt>
mov    (%rbx),%edx
lea    0xe02(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%ebx
jmp    1242 <main+0xd2>
mov    0x2dba(%rip),%rbx
mov    (%rbx),%edi
call   1070 <hstrerror@plt>
mov    (%rbx),%edx
lea    0xdda(%rip),%rdi
xor    %ebx,%ebx
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i8* }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i8* }
%struct.sockaddr = type { i32 }
@__const.main.ip_string = [16 x i8] c"127.0.0.1\00\00\00\00\00\00\00"
@AF_INET = global i8* null
@.str = [8 x i8] c"%s(%d)\0A\00"
@errno = global i32 0
@h_errno = global i32 0
@.str.1 = [42 x i8] c"ip_string = %s, ht = %p, ht->h_name = %s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [16 x i8]
%7 = alloca %struct.sockaddr_in
%8 = alloca i32
%9 = alloca %struct.hostent*
%10 = alloca i32
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = bitcast [16 x i8]* %6 to i8*
%13 = load i32, i32* %4
%14 = icmp sge i32 %13, 0
br i1 %14, label %15, label %21
15: ; preds = %2
%16 = getelementptr inbounds [16 x i8], [16 x i8]* %6, i64 0, i64 0
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
%20 = call i32 @strncpy(i8* %16, i8* %19, i32 16)
br label %21
21: ; preds = %15, %2
%22 = load i8*, i8** @AF_INET
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i8* %22, i8** %23
%24 = call i32 @htons(i32 80)
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
store i32 %24, i32* %25
%26 = load i8*, i8** @AF_INET
%27 = getelementptr inbounds [16 x i8], [16 x i8]* %6, i64 0, i64 0
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
%29 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %28, i32 0, i32 0
%30 = bitcast i32* %29 to %struct.sockaddr*
%31 = call i32 @inet_pton(i8* %26, i8* %27, %struct.sockaddr* %30)
store i32 %31, i32* %8
%32 = load i32, i32* %8
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %39
34: ; preds = %21
%35 = load i32, i32* @errno
%36 = call i8* @strerror(i32 %35)
%37 = load i32, i32* @errno
%38 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %36, i32 %37)
store i32 1, i32* %3
br label %63
39: ; preds = %21
store %struct.hostent* null, %struct.hostent** %9
store i32 16, i32* %10
%40 = load i8*, i8** @AF_INET
%41 = ptrtoint i8* %40 to i32
store i32 %41, i32* %11
%42 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
%43 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %42, i32 0, i32 0
%44 = bitcast i32* %43 to i8*
%45 = load i32, i32* %10
%46 = load i32, i32* %11
%47 = call %struct.hostent* @gethostbyaddr(i8* %44, i32 %45, i32 %46)
store %struct.hostent* %47, %struct.hostent** %9
%48 = load %struct.hostent*, %struct.hostent** %9
%49 = icmp eq %struct.hostent* %48, null
br i1 %49, label %50, label %55
50: ; preds = %39
%51 = load i32, i32* @h_errno
%52 = call i8* @hstrerror(i32 %51)
%53 = load i32, i32* @h_errno
%54 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %52, i32 %53)
br label %62
55: ; preds = %39
%56 = getelementptr inbounds [16 x i8], [16 x i8]* %6, i64 0, i64 0
%57 = load %struct.hostent*, %struct.hostent** %9
%58 = load %struct.hostent*, %struct.hostent** %9
%59 = getelementptr inbounds %struct.hostent, %struct.hostent* %58, i32 0, i32 0
%60 = load i8*, i8** %59
%61 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0), i8* %56, %struct.hostent* %57, i8* %60)
br label %62
62: ; preds = %55, %50
store i32 0, i32* %3
br label %63
63: ; preds = %62, %34
%64 = load i32, i32* %3
ret i32 %64
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i8* }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
%struct.hostent = type { i8* }
@__const.main.ip_string = [16 x i8] c"127.0.0.1\00\00\00\00\00\00\00"
@AF_INET = global i8* null
@.str = [8 x i8] c"%s(%d)\0A\00"
@errno = global i32 0
@h_errno = global i32 0
@.str.1 = [42 x i8] c"ip_string = %s, ht = %p, ht->h_name = %s\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [16 x i8]
%4 = alloca %struct.sockaddr_in
%5 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 0
%6 = icmp sgt i32 %0, -1
br i1 %6, label %7, label %11
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = call i32 @strncpy(i8* %5, i8* %9, i32 16)
br label %11
11: ; preds = %7, %2
%12 = bitcast %struct.sockaddr_in* %4 to i8*
%13 = load i8*, i8** @AF_INET
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i8* %13, i8** %14
%15 = call i32 @htons(i32 80)
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
store i32 %15, i32* %16
%17 = load i8*, i8** @AF_INET
%18 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%19 = call i32 @inet_pton(i8* %17, i8* %5, %struct.sockaddr* %18)
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %26
21: ; preds = %11
%22 = load i32, i32* @errno
%23 = call i8* @strerror(i32 %22)
%24 = load i32, i32* @errno
%25 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %23, i32 %24)
br label %41
26: ; preds = %11
%27 = load i8*, i8** @AF_INET
%28 = ptrtoint i8* %27 to i64
%29 = trunc i64 %28 to i32
%30 = call %struct.hostent* @gethostbyaddr(i8* %12, i32 16, i32 %29)
%31 = icmp eq %struct.hostent* %30, null
br i1 %31, label %32, label %37
32: ; preds = %26
%33 = load i32, i32* @h_errno
%34 = call i8* @hstrerror(i32 %33)
%35 = load i32, i32* @h_errno
%36 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %34, i32 %35)
br label %41
37: ; preds = %26
%38 = getelementptr inbounds %struct.hostent, %struct.hostent* %30, i64 0, i32 0
%39 = load i8*, i8** %38
%40 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0), i8* %5, %struct.hostent* %30, i8* %39)
br label %41
41: ; preds = %32, %37, %21
%42 = phi i32 [ 1, %21 ], [ 0, %37 ], [ 0, %32 ]
ret i32 %42
}