#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 */

/* Variables and functions */
char *config_get_relayhost();
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ *gethostbyname(char *);
char *malloc(int);
int /*<<< orphan*/ memcpy(char *, char const *, size_t);
char *smtp_resolve_mx(char const *);
char *strchr(char *, char);
int strlen(char const *);
char *fwd_send_host(char *addr) {
  char *buf = NULL;
  const char *pos = NULL;
  size_t len = 0;
  pos = config_get_relayhost();
  if (NULL == pos) {
    pos = strchr(addr, '@') + 1;
  }
  len = strlen(pos) + 1;
  buf = malloc(sizeof(char) * len);
  memcpy(buf, pos, len);
  if (gethostbyname(buf) == NULL) {
    free(buf);
    buf = smtp_resolve_mx(pos);
  }
  return buf;
}


x86 assembly (clang, -O0):
<fwd_send_host>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    $0x0,%al
call   1070 <config_get_relayhost@plt>
mov    %rax,-0x18(%rbp)
xor    %eax,%eax
cmp    -0x18(%rbp),%rax
jne    11d1 <fwd_send_host+0x51>
mov    -0x8(%rbp),%rdi
mov    $0x40,%esi
call   1060 <strchr@plt>
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
add    $0x1,%eax
cltq
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   10a0 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1090 <memcpy@plt>
mov    -0x10(%rbp),%rdi
call   1080 <gethostbyname@plt>
cmp    $0x0,%rax
jne    1230 <fwd_send_host+0xb0>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rdi
call   1040 <smtp_resolve_mx@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fwd_send_host>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%rbx
xor    %eax,%eax
call   1070 <config_get_relayhost@plt>
mov    %rax,%r14
test   %rax,%rax
jne    11ab <fwd_send_host+0x2b>
mov    %rbx,%rdi
mov    $0x40,%esi
call   1060 <strchr@plt>
mov    %rax,%r14
add    $0x1,%r14
mov    %r14,%rdi
call   1050 <strlen@plt>
add    $0x1,%eax
movslq %eax,%r15
mov    %r15d,%edi
call   10a0 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1090 <memcpy@plt>
mov    %rbx,%rdi
call   1080 <gethostbyname@plt>
test   %rax,%rax
je     11e8 <fwd_send_host+0x68>
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r14,%rdi
pop    %rbx
pop    %r14
pop    %r15
jmp    1040 <smtp_resolve_mx@plt>

LLVM IR (-O0):
define i8* @fwd_send_host(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
store i8* %0, i8** %2
store i8* null, i8** %3
store i8* null, i8** %4
store i64 0, i64* %5
%6 = call i8* (...) @config_get_relayhost()
store i8* %6, i8** %4
%7 = load i8*, i8** %4
%8 = icmp eq i8* null, %7
br i1 %8, label %9, label %13
9: ; preds = %1
%10 = load i8*, i8** %2
%11 = call i8* @strchr(i8* %10, i8 signext 64)
%12 = getelementptr inbounds i8, i8* %11, i64 1
store i8* %12, i8** %4
br label %13
13: ; preds = %9, %1
%14 = load i8*, i8** %4
%15 = call i32 @strlen(i8* %14)
%16 = add nsw i32 %15, 1
%17 = sext i32 %16 to i64
store i64 %17, i64* %5
%18 = load i64, i64* %5
%19 = mul i64 1, %18
%20 = trunc i64 %19 to i32
%21 = call i8* @malloc(i32 %20)
store i8* %21, i8** %3
%22 = load i8*, i8** %3
%23 = load i8*, i8** %4
%24 = load i64, i64* %5
%25 = call i32 @memcpy(i8* %22, i8* %23, i64 %24)
%26 = load i8*, i8** %3
%27 = call i32* @gethostbyname(i8* %26)
%28 = icmp eq i32* %27, null
br i1 %28, label %29, label %34
29: ; preds = %13
%30 = load i8*, i8** %3
%31 = call i32 @free(i8* %30)
%32 = load i8*, i8** %4
%33 = call i8* @smtp_resolve_mx(i8* %32)
store i8* %33, i8** %3
br label %34
34: ; preds = %29, %13
%35 = load i8*, i8** %3
ret i8* %35
}

LLVM IR (-O2):
define i8* @fwd_send_host(i8* %0) {
%2 = tail call i8* (...) @config_get_relayhost()
%3 = icmp eq i8* %2, null
br i1 %3, label %4, label %7
4: ; preds = %1
%5 = tail call i8* @strchr(i8* dereferenceable(1) %0, i8 signext 64)
%6 = getelementptr inbounds i8, i8* %5, i64 1
br label %7
7: ; preds = %4, %1
%8 = phi i8* [ %6, %4 ], [ %2, %1 ]
%9 = tail call i32 @strlen(i8* %8)
%10 = add nsw i32 %9, 1
%11 = sext i32 %10 to i64
%12 = tail call i8* @malloc(i32 %10)
%13 = tail call i32 @memcpy(i8* %12, i8* %8, i64 %11)
%14 = tail call i32* @gethostbyname(i8* %12)
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %19
16: ; preds = %7
%17 = tail call i32 @free(i8* %12)
%18 = tail call i8* @smtp_resolve_mx(i8* %8)
br label %19
19: ; preds = %16, %7
%20 = phi i8* [ %18, %16 ], [ %12, %7 ]
ret i8* %20
}