#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*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int sscanf(char *, char *, char *);
int guess_dns_domain(char *domain) {
  char line[256];
  FILE *file;
  file = fopen("/etc/resolv.conf", "r");
  if (!file)
    return 0;
  while (fgets(line, sizeof(line), file)) {
    if (sscanf(line, "search %[^ \t\n]", domain) == 1) {
      fclose(file);
      return 1;
    }
    if (sscanf(line, "domain %[^ \t\n]", domain) == 1) {
      fclose(file);
      return 1;
    }
  }
  fclose(file);
  return 0;
}


x86 assembly (clang, -O0):
<guess_dns_domain>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
mov    %rdi,-0x10(%rbp)
lea    0xeaa(%rip),%rdi
lea    0xeb4(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x118(%rbp)
cmpq   $0x0,-0x118(%rbp)
jne    1183 <guess_dns_domain+0x43>
movl   $0x0,-0x4(%rbp)
jmp    1232 <guess_dns_domain+0xf2>
jmp    1188 <guess_dns_domain+0x48>
lea    -0x110(%rbp),%rdi
mov    -0x118(%rbp),%rdx
mov    $0x100,%esi
call   1040 <fgets@plt>
cmp    $0x0,%rax
je     121f <guess_dns_domain+0xdf>
lea    -0x110(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xe57(%rip),%rsi
call   1050 <sscanf@plt>
cmp    $0x1,%eax
jne    11e2 <guess_dns_domain+0xa2>
mov    -0x118(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x1,-0x4(%rbp)
jmp    1232 <guess_dns_domain+0xf2>
lea    -0x110(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xe2e(%rip),%rsi
call   1050 <sscanf@plt>
cmp    $0x1,%eax
jne    121a <guess_dns_domain+0xda>
mov    -0x118(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x1,-0x4(%rbp)
jmp    1232 <guess_dns_domain+0xf2>
jmp    1188 <guess_dns_domain+0x48>
mov    -0x118(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<guess_dns_domain>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x108,%rsp
mov    %rdi,%rbx
lea    0xea5(%rip),%rdi
lea    0xeaf(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     11cd <guess_dns_domain+0x8d>
mov    %rax,%r14
mov    %rsp,%r15
lea    0xe9a(%rip),%r12
lea    0xea2(%rip),%r13
mov    $0x1,%ebp
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r15,%rdi
mov    $0x100,%esi
mov    %r14,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
je     11d1 <guess_dns_domain+0x91>
mov    %r15,%rdi
mov    %r12,%rsi
mov    %rbx,%rdx
call   1050 <sscanf@plt>
cmp    $0x1,%eax
je     11d3 <guess_dns_domain+0x93>
mov    %r15,%rdi
mov    %r13,%rsi
mov    %rbx,%rdx
call   1050 <sscanf@plt>
cmp    $0x1,%eax
jne    1190 <guess_dns_domain+0x50>
jmp    11d3 <guess_dns_domain+0x93>
xor    %ebp,%ebp
jmp    11db <guess_dns_domain+0x9b>
xor    %ebp,%ebp
mov    %r14,%rdi
call   1030 <fclose@plt>
mov    %ebp,%eax
add    $0x108,%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 = [15 x i8] c"search %[^ \09\0A]\00"
@.str.3 = [15 x i8] c"domain %[^ \09\0A]\00"
define i32 @guess_dns_domain(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca [256 x i8]
%5 = alloca i32*
store i8* %0, i8** %3
%6 = 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* %6, i32** %5
%7 = load i32*, i32** %5
%8 = icmp ne i32* %7, null
br i1 %8, label %10, label %9
9: ; preds = %1
store i32 0, i32* %2
br label %36
10: ; preds = %1
br label %11
11: ; preds = %32, %10
%12 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%13 = load i32*, i32** %5
%14 = call i64 @fgets(i8* %12, i32 256, i32* %13)
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %33
16: ; preds = %11
%17 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%18 = load i8*, i8** %3
%19 = call i32 @sscanf(i8* %17, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* %18)
%20 = icmp eq i32 %19, 1
br i1 %20, label %21, label %24
21: ; preds = %16
%22 = load i32*, i32** %5
%23 = call i32 @fclose(i32* %22)
store i32 1, i32* %2
br label %36
24: ; preds = %16
%25 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%26 = load i8*, i8** %3
%27 = call i32 @sscanf(i8* %25, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i8* %26)
%28 = icmp eq i32 %27, 1
br i1 %28, label %29, label %32
29: ; preds = %24
%30 = load i32*, i32** %5
%31 = call i32 @fclose(i32* %30)
store i32 1, i32* %2
br label %36
32: ; preds = %24
br label %11
33: ; preds = %11
%34 = load i32*, i32** %5
%35 = call i32 @fclose(i32* %34)
store i32 0, i32* %2
br label %36
36: ; preds = %33, %29, %21, %9
%37 = load i32, i32* %2
ret i32 %37
}

LLVM IR (-O2):
@.str = [17 x i8] c"/etc/resolv.conf\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [15 x i8] c"search %[^ \09\0A]\00"
@.str.3 = [15 x i8] c"domain %[^ \09\0A]\00"
define i32 @guess_dns_domain(i8* %0) {
%2 = alloca [256 x i8]
%3 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%4 = 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))
%5 = icmp eq i32* %4, null
br i1 %5, label %18, label %6
6: ; preds = %1, %12
%7 = call i64 @fgets(i8* %3, i32 256, i32* %4)
%8 = icmp eq i64 %7, 0
br i1 %8, label %15, label %9
9: ; preds = %6
%10 = call i32 @sscanf(i8* %3, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* %0)
%11 = icmp eq i32 %10, 1
br i1 %11, label %15, label %12
12: ; preds = %9
%13 = call i32 @sscanf(i8* %3, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i8* %0)
%14 = icmp eq i32 %13, 1
br i1 %14, label %15, label %6
15: ; preds = %6, %12, %9
%16 = phi i32 [ 1, %9 ], [ 1, %12 ], [ 0, %6 ]
%17 = call i32 @fclose(i32* %4)
br label %18
18: ; preds = %15, %1
%19 = phi i32 [ 0, %1 ], [ %16, %15 ]
ret i32 %19
}