#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;
};
struct sockaddr {
  scalar_t__ sa_family;
};

/* Variables and functions */
scalar_t__ AF_INET;
char *inet_ntoa(int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(char *, char const *);
int format_ip4(struct sockaddr *addr, char *out) {
  if (addr->sa_family == AF_INET) {
    struct sockaddr_in *i = (struct sockaddr_in *)addr;
    const char *ip = inet_ntoa(i->sin_addr);
    if (!ip) {
      return -2;
    } else {
      strcpy(out, ip);
      return 0;
    }
  } else {
    return -1;
  }
}


x86 assembly (clang, -O0):
<format_ip4>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    0x2eaa(%rip),%rcx
cmp    (%rcx),%rax
jne    118e <format_ip4+0x6e>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
call   1040 <inet_ntoa@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1175 <format_ip4+0x55>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1195 <format_ip4+0x75>
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1030 <strcpy@plt>
movl   $0x0,-0x4(%rbp)
jmp    1195 <format_ip4+0x75>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<format_ip4>:
push   %rbx
mov    (%rdi),%rdi
mov    0x2ebd(%rip),%rcx
mov    $0xffffffff,%eax
cmp    (%rcx),%rdi
jne    114f <format_ip4+0x2f>
mov    %rsi,%rbx
call   1040 <inet_ntoa@plt>
test   %rax,%rax
je     1151 <format_ip4+0x31>
mov    %rbx,%rdi
mov    %rax,%rsi
call   1030 <strcpy@plt>
xor    %eax,%eax
pop    %rbx
ret
mov    $0xfffffffe,%eax
pop    %rbx
ret

LLVM IR (-O0):
%struct.sockaddr = type { i64 }
%struct.sockaddr_in = type { i32 }
@AF_INET = global i64 0
define i32 @format_ip4(%struct.sockaddr* %0, i8* %1) {
%3 = alloca i32
%4 = alloca %struct.sockaddr*
%5 = alloca i8*
%6 = alloca %struct.sockaddr_in*
%7 = alloca i8*
store %struct.sockaddr* %0, %struct.sockaddr** %4
store i8* %1, i8** %5
%8 = load %struct.sockaddr*, %struct.sockaddr** %4
%9 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %8, i32 0, i32 0
%10 = load i64, i64* %9
%11 = load i64, i64* @AF_INET
%12 = icmp eq i64 %10, %11
br i1 %12, label %13, label %27
13: ; preds = %2
%14 = load %struct.sockaddr*, %struct.sockaddr** %4
%15 = bitcast %struct.sockaddr* %14 to %struct.sockaddr_in*
store %struct.sockaddr_in* %15, %struct.sockaddr_in** %6
%16 = load %struct.sockaddr_in*, %struct.sockaddr_in** %6
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %16, i32 0, i32 0
%18 = load i32, i32* %17
%19 = call i8* @inet_ntoa(i32 %18)
store i8* %19, i8** %7
%20 = load i8*, i8** %7
%21 = icmp ne i8* %20, null
br i1 %21, label %23, label %22
22: ; preds = %13
store i32 -2, i32* %3
br label %28
23: ; preds = %13
%24 = load i8*, i8** %5
%25 = load i8*, i8** %7
%26 = call i32 @strcpy(i8* %24, i8* %25)
store i32 0, i32* %3
br label %28
27: ; preds = %2
store i32 -1, i32* %3
br label %28
28: ; preds = %27, %23, %22
%29 = load i32, i32* %3
ret i32 %29
}

LLVM IR (-O2):
%struct.sockaddr = type { i64 }
@AF_INET = global i64 0
define i32 @format_ip4(%struct.sockaddr* nocapture %0, i8* %1) {
%3 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %0, i64 0, i32 0
%4 = load i64, i64* %3
%5 = load i64, i64* @AF_INET
%6 = icmp eq i64 %4, %5
br i1 %6, label %7, label %13
7: ; preds = %2
%8 = trunc i64 %4 to i32
%9 = tail call i8* @inet_ntoa(i32 %8)
%10 = icmp eq i8* %9, null
br i1 %10, label %13, label %11
11: ; preds = %7
%12 = tail call i32 @strcpy(i8* %1, i8* %9)
br label %13
13: ; preds = %2, %11, %7
%14 = phi i32 [ 0, %11 ], [ -2, %7 ], [ -1, %2 ]
ret i32 %14
}