#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 */
typedef int /*<<< orphan*/ line;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ snprintf(char *, size_t, char *, char *);
int sscanf(char *, char *, char *);
int ns_get_ip_address_of_nameserver(char *name, size_t name_len) {
  int ret = 0;
  FILE *fp;
  char line[512];
  if ((fp = fopen("/etc/resolv.conf", "r")) == NULL) {
    ret--;
  } else {
    for (ret--; fgets(line, sizeof(line), fp) != NULL;) {
      char buf[256];
      if (sscanf(line, "nameserver %255[^\n]s", buf) == 1) {
        snprintf(name, name_len, "udp://%s:53", buf);
        ret++;
        break;
      }
    }
    (void)fclose(fp);
  }
  return ret;
}


x86 assembly (clang, -O0):
<ns_get_ip_address_of_nameserver>:
push   %rbp
mov    %rsp,%rbp
sub    $0x320,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
lea    0xe8f(%rip),%rdi
lea    0xe99(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1199 <ns_get_ip_address_of_nameserver+0x49>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    121b <ns_get_ip_address_of_nameserver+0xcb>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
lea    -0x220(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    $0x200,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     1212 <ns_get_ip_address_of_nameserver+0xc2>
lea    -0x220(%rbp),%rdi
lea    -0x320(%rbp),%rdx
lea    0xe3d(%rip),%rsi
call   1060 <sscanf@plt>
cmp    $0x1,%eax
jne    120d <ns_get_ip_address_of_nameserver+0xbd>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
lea    -0x320(%rbp),%rcx
lea    0xe2e(%rip),%rdx
call   1040 <snprintf@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1212 <ns_get_ip_address_of_nameserver+0xc2>
jmp    11a2 <ns_get_ip_address_of_nameserver+0x52>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x14(%rbp),%eax
add    $0x320,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ns_get_ip_address_of_nameserver>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x318,%rsp
mov    %rsi,%rbp
mov    %rdi,%r15
lea    0xe92(%rip),%rdi
lea    0xe9c(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11ea <ns_get_ip_address_of_nameserver+0x9a>
mov    %rax,%rbx
lea    0x110(%rsp),%rdi
mov    $0x200,%esi
mov    %rax,%rdx
call   1050 <fgets@plt>
mov    $0xffffffff,%r12d
test   %rax,%rax
je     120e <ns_get_ip_address_of_nameserver+0xbe>
mov    %rbp,0x8(%rsp)
lea    0xe65(%rip),%r13
lea    0x110(%rsp),%rbp
lea    0x10(%rsp),%r14
nopl   0x0(%rax,%rax,1)
mov    %rbp,%rdi
mov    %r13,%rsi
mov    %r14,%rdx
call   1060 <sscanf@plt>
cmp    $0x1,%eax
je     11f2 <ns_get_ip_address_of_nameserver+0xa2>
mov    %rbp,%rdi
mov    $0x200,%esi
mov    %rbx,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
jne    11c0 <ns_get_ip_address_of_nameserver+0x70>
jmp    120e <ns_get_ip_address_of_nameserver+0xbe>
mov    $0xffffffff,%r12d
jmp    1216 <ns_get_ip_address_of_nameserver+0xc6>
lea    0xe2f(%rip),%rdx
lea    0x10(%rsp),%rcx
mov    %r15,%rdi
mov    0x8(%rsp),%rsi
call   1040 <snprintf@plt>
xor    %r12d,%r12d
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    %r12d,%eax
add    $0x318,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [17 x i8] c"/etc/resolv.conf\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [21 x i8] c"nameserver %255[^\0A]s\00"
@.str.3 = [12 x i8] c"udp://%s:53\00"
define i32 @ns_get_ip_address_of_nameserver(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32
%6 = alloca i32*
%7 = alloca [512 x i8]
%8 = alloca [256 x i8]
store i8* %0, i8** %3
store i64 %1, i64* %4
store i32 0, i32* %5
%9 = call i32* @fopen(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %9, i32** %6
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = load i32, i32* %5
%13 = add nsw i32 %12, -1
store i32 %13, i32* %5
br label %38
14: ; preds = %2
%15 = load i32, i32* %5
%16 = add nsw i32 %15, -1
store i32 %16, i32* %5
br label %17
17: ; preds = %34, %14
%18 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%19 = load i32*, i32** %6
%20 = call i32* @fgets(i8* %18, i32 512, i32* %19)
%21 = icmp ne i32* %20, null
br i1 %21, label %22, label %35
22: ; preds = %17
%23 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%24 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%25 = call i32 @sscanf(i8* %23, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %24)
%26 = icmp eq i32 %25, 1
br i1 %26, label %27, label %34
27: ; preds = %22
%28 = load i8*, i8** %3
%29 = load i64, i64* %4
%30 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%31 = call i32 @snprintf(i8* %28, i64 %29, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i8* %30)
%32 = load i32, i32* %5
%33 = add nsw i32 %32, 1
store i32 %33, i32* %5
br label %35
34: ; preds = %22
br label %17
35: ; preds = %27, %17
%36 = load i32*, i32** %6
%37 = call i32 @fclose(i32* %36)
br label %38
38: ; preds = %35, %11
%39 = load i32, i32* %5
ret i32 %39
}

LLVM IR (-O2):
@.str = [17 x i8] c"/etc/resolv.conf\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [21 x i8] c"nameserver %255[^\0A]s\00"
@.str.3 = [12 x i8] c"udp://%s:53\00"
define i32 @ns_get_ip_address_of_nameserver(i8* %0, i64 %1) {
%3 = alloca [512 x i8]
%4 = alloca [256 x i8]
%5 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%6 = tail call i32* @fopen(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %24, label %8
8: ; preds = %2
%9 = call i32* @fgets(i8* %5, i32 512, i32* %6)
%10 = icmp eq i32* %9, null
br i1 %10, label %21, label %11
11: ; preds = %8
%12 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
br label %13
13: ; preds = %11, %18
%14 = call i32 @sscanf(i8* %5, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %12)
%15 = icmp eq i32 %14, 1
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = call i32 @snprintf(i8* %0, i64 %1, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i8* %12)
br label %21
18: ; preds = %13
%19 = call i32* @fgets(i8* %5, i32 512, i32* %6)
%20 = icmp eq i32* %19, null
br i1 %20, label %21, label %13
21: ; preds = %18, %8, %16
%22 = phi i32 [ 0, %16 ], [ -1, %8 ], [ -1, %18 ]
%23 = call i32 @fclose(i32* %6)
br label %24
24: ; preds = %2, %21
%25 = phi i32 [ %22, %21 ], [ -1, %2 ]
ret i32 %25
}