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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_aton(char const *, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(int /*<<< orphan*/, char, int);
struct sockaddr_in *sockaddr_in_set(struct sockaddr_in *sa, const char *ipaddr,
                                    int port) {
  if (!sa) {
    return NULL;
  }
  sa->sin_family = AF_INET;
  sa->sin_port = htons(port);
  if (!inet_aton(ipaddr, &(sa->sin_addr))) {
    return NULL;
  }
  memset(sa->sin_zero, '\0', 8);
  return sa;
}


x86 assembly (clang, -O0):
<sockaddr_in_set>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    115b <sockaddr_in_set+0x2b>
movq   $0x0,-0x8(%rbp)
jmp    11bd <sockaddr_in_set+0x8d>
mov    0x2e86(%rip),%rax
mov    (%rax),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0xc(%rax)
mov    -0x1c(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
add    $0x4,%rsi
call   1050 <inet_aton@plt>
cmp    $0x0,%eax
jne    11a3 <sockaddr_in_set+0x73>
movq   $0x0,-0x8(%rbp)
jmp    11bd <sockaddr_in_set+0x8d>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sockaddr_in_set>:
push   %r14
push   %rbx
push   %rax
test   %rdi,%rdi
je     1175 <sockaddr_in_set+0x45>
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2ea2(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rdi)
mov    %edx,%edi
call   1030 <htons@plt>
mov    %eax,0x8(%rbx)
lea    0x4(%rbx),%rsi
mov    %r14,%rdi
call   1050 <inet_aton@plt>
test   %eax,%eax
je     1175 <sockaddr_in_set+0x45>
mov    (%rbx),%edi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
jmp    1177 <sockaddr_in_set+0x47>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32, i32 }
@AF_INET = global i32 0
define %struct.sockaddr_in* @sockaddr_in_set(%struct.sockaddr_in* %0, i8* %1, i32 %2) {
%4 = icmp eq %struct.sockaddr_in* %0, null
br i1 %4, label %17, label %5
5: ; preds = %3
%6 = load i32, i32* @AF_INET
%7 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %0, i64 0, i32 3
store i32 %6, i32* %7
%8 = tail call i32 @htons(i32 %2)
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %0, i64 0, i32 2
store i32 %8, i32* %9
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %0, i64 0, i32 1
%11 = tail call i32 @inet_aton(i8* %1, i32* %10)
%12 = icmp eq i32 %11, 0
br i1 %12, label %17, label %13
13: ; preds = %5
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %0, i64 0, i32 0
%15 = load i32, i32* %14
%16 = tail call i32 @memset(i32 %15, i8 signext 0, i32 8)
br label %17
17: ; preds = %5, %3, %13
%18 = phi %struct.sockaddr_in* [ %0, %13 ], [ null, %3 ], [ null, %5 ]
ret %struct.sockaddr_in* %18
}