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

/* Variables and functions */
int /*<<< orphan*/ __fclose_ca(int /*<<< orphan*/ *);
int /*<<< orphan*/ *__fopen_rb_ca(char *, int /*<<< orphan*/ *, unsigned char *,
                                  int);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ isspace(char);
int /*<<< orphan*/ memcpy(char *, char *, int);
char *strchr(char *, char);
scalar_t__ strncmp(char *, char *, int);
unsigned long strtoul(char *, char **, int);
void reverse_services(char *buf, int port, int dgram) {
  unsigned long svport;
  char line[128], *p, *z;
  unsigned char _buf[1032];
  FILE _f, *f = __fopen_rb_ca("/etc/services", &_f, _buf, sizeof _buf);
  if (!f)
    return;
  while (fgets(line, sizeof line, f)) {
    if ((p = strchr(line, '#')))
      *p++ = '\n', *p = 0;
    for (p = line; *p && !isspace(*p); p++)
      ;
    if (!*p)
      continue;
    *p++ = 0;
    svport = strtoul(p, &z, 10);
    if (svport != port || z == p)
      continue;
    if (dgram && strncmp(z, "/udp", 4))
      continue;
    if (!dgram && strncmp(z, "/tcp", 4))
      continue;
    if (p - line > 32)
      continue;
    memcpy(buf, line, p - line);
    break;
  }
  __fclose_ca(f);
}


x86 assembly (clang, -O0):
<reverse_services>:
push   %rbp
mov    %rsp,%rbp
sub    $0x4e0,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
lea    -0x4c0(%rbp),%rdx
lea    0xe5d(%rip),%rdi
lea    -0x4c4(%rbp),%rsi
mov    $0x408,%ecx
call   1090 <__fopen_rb_ca@plt>
mov    %rax,-0x4d0(%rbp)
cmpq   $0x0,-0x4d0(%rbp)
jne    11ce <reverse_services+0x4e>
jmp    13d2 <reverse_services+0x252>
jmp    11d3 <reverse_services+0x53>
lea    -0xa0(%rbp),%rdi
mov    -0x4d0(%rbp),%rdx
mov    $0x80,%esi
call   1070 <fgets@plt>
cmp    $0x0,%rax
je     13c6 <reverse_services+0x246>
lea    -0xa0(%rbp),%rdi
mov    $0x23,%esi
call   1050 <strchr@plt>
mov    %rax,-0xa8(%rbp)
cmp    $0x0,%rax
je     1239 <reverse_services+0xb9>
mov    -0xa8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0xa8(%rbp)
movb   $0xa,(%rax)
mov    -0xa8(%rbp),%rax
movb   $0x0,(%rax)
lea    -0xa0(%rbp),%rax
mov    %rax,-0xa8(%rbp)
mov    -0xa8(%rbp),%rax
movsbl (%rax),%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x4d1(%rbp)
je     127f <reverse_services+0xff>
mov    -0xa8(%rbp),%rax
movsbl (%rax),%edi
call   1060 <isspace@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
mov    %al,-0x4d1(%rbp)
mov    -0x4d1(%rbp),%al
test   $0x1,%al
jne    1292 <reverse_services+0x112>
jmp    12ae <reverse_services+0x12e>
jmp    1297 <reverse_services+0x117>
mov    -0xa8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0xa8(%rbp)
jmp    1247 <reverse_services+0xc7>
mov    -0xa8(%rbp),%rax
cmpb   $0x0,(%rax)
jne    12c3 <reverse_services+0x143>
jmp    11d3 <reverse_services+0x53>
mov    -0xa8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0xa8(%rbp)
movb   $0x0,(%rax)
mov    -0xa8(%rbp),%rdi
lea    -0xb0(%rbp),%rsi
mov    $0xa,%edx
call   10a0 <strtoul@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movslq -0xc(%rbp),%rcx
cmp    %rcx,%rax
jne    131c <reverse_services+0x19c>
mov    -0xb0(%rbp),%rax
cmp    -0xa8(%rbp),%rax
jne    1321 <reverse_services+0x1a1>
jmp    11d3 <reverse_services+0x53>
cmpl   $0x0,-0x10(%rbp)
je     1352 <reverse_services+0x1d2>
mov    -0xb0(%rbp),%rdi
lea    0xcd5(%rip),%rsi
mov    $0x4,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
je     1352 <reverse_services+0x1d2>
jmp    11d3 <reverse_services+0x53>
cmpl   $0x0,-0x10(%rbp)
jne    1383 <reverse_services+0x203>
mov    -0xb0(%rbp),%rdi
lea    0xca9(%rip),%rsi
mov    $0x4,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
je     1383 <reverse_services+0x203>
jmp    11d3 <reverse_services+0x53>
mov    -0xa8(%rbp),%rax
lea    -0xa0(%rbp),%rcx
sub    %rcx,%rax
cmp    $0x20,%rax
jle    13a3 <reverse_services+0x223>
jmp    11d3 <reverse_services+0x53>
mov    -0x8(%rbp),%rdi
lea    -0xa0(%rbp),%rsi
mov    -0xa8(%rbp),%rax
lea    -0xa0(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
call   1080 <memcpy@plt>
mov    -0x4d0(%rbp),%rdi
call   1040 <__fclose_ca@plt>
add    $0x4e0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<reverse_services>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x4a8,%rsp
mov    %edx,0x4(%rsp)
mov    %esi,%r14d
mov    %rdi,%rbp
lea    0xe5e(%rip),%rdi
lea    0x1c(%rsp),%rsi
lea    0xa0(%rsp),%rdx
mov    $0x408,%ecx
call   1090 <__fopen_rb_ca@plt>
test   %rax,%rax
je     12eb <reverse_services+0x16b>
mov    %rax,%rbx
mov    %rbp,0x8(%rsp)
lea    0x20(%rsp),%rdi
mov    $0x80,%esi
mov    %rax,%rdx
call   1070 <fgets@plt>
test   %rax,%rax
je     12e3 <reverse_services+0x163>
movslq %r14d,%r15
lea    0x20(%rsp),%rbp
lea    0x10(%rsp),%r12
jmp    1219 <reverse_services+0x99>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rbp,%rdi
mov    $0x80,%esi
mov    %rbx,%rdx
call   1070 <fgets@plt>
test   %rax,%rax
je     12e3 <reverse_services+0x163>
mov    %rbp,%rdi
mov    $0x23,%esi
call   1050 <strchr@plt>
test   %rax,%rax
je     1230 <reverse_services+0xb0>
movw   $0xa,(%rax)
mov    0x20(%rsp),%al
test   %al,%al
je     1200 <reverse_services+0x80>
mov    $0x1,%r13d
xchg   %ax,%ax
movsbl %al,%edi
call   1060 <isspace@plt>
test   %eax,%eax
jne    1260 <reverse_services+0xe0>
movzbl 0x20(%rsp,%r13,1),%eax
add    $0x1,%r13
test   %al,%al
jne    1240 <reverse_services+0xc0>
jmp    1200 <reverse_services+0x80>
nopl   0x0(%rax)
cmpb   $0x0,0x1f(%rsp,%r13,1)
je     1200 <reverse_services+0x80>
lea    (%rsp,%r13,1),%r14
add    $0x20,%r14
movb   $0x0,0x1f(%rsp,%r13,1)
mov    %r14,%rdi
mov    %r12,%rsi
mov    $0xa,%edx
call   10a0 <strtoul@plt>
cmp    %r15,%rax
jne    1200 <reverse_services+0x80>
mov    0x10(%rsp),%rdi
cmp    %rdi,%r14
je     1200 <reverse_services+0x80>
cmpl   $0x0,0x4(%rsp)
je     12ad <reverse_services+0x12d>
lea    0xd63(%rip),%rsi
jmp    12b4 <reverse_services+0x134>
lea    0xd5f(%rip),%rsi
mov    $0x4,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
jne    1200 <reverse_services+0x80>
cmp    $0x20,%r13
jg     1200 <reverse_services+0x80>
lea    0x20(%rsp),%rsi
mov    0x8(%rsp),%rdi
mov    %r13d,%edx
call   1080 <memcpy@plt>
mov    %rbx,%rdi
call   1040 <__fclose_ca@plt>
add    $0x4a8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [14 x i8] c"/etc/services\00"
@.str.1 = [5 x i8] c"/udp\00"
@.str.2 = [5 x i8] c"/tcp\00"
define void @reverse_services(i8* %0, i32 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i64
%8 = alloca [128 x i8]
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca [1032 x i8]
%12 = alloca i32
%13 = alloca i32*
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%14 = getelementptr inbounds [1032 x i8], [1032 x i8]* %11, i64 0, i64 0
%15 = call i32* @__fopen_rb_ca(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32* %12, i8* %14, i32 1032)
store i32* %15, i32** %13
%16 = load i32*, i32** %13
%17 = icmp ne i32* %16, null
br i1 %17, label %19, label %18
18: ; preds = %3
br label %108
19: ; preds = %3
br label %20
20: ; preds = %94, %86, %78, %70, %56, %19
%21 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
%22 = load i32*, i32** %13
%23 = call i64 @fgets(i8* %21, i32 128, i32* %22)
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %105
25: ; preds = %20
%26 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
%27 = call i8* @strchr(i8* %26, i8 signext 35)
store i8* %27, i8** %9
%28 = icmp ne i8* %27, null
br i1 %28, label %29, label %33
29: ; preds = %25
%30 = load i8*, i8** %9
%31 = getelementptr inbounds i8, i8* %30, i32 1
store i8* %31, i8** %9
store i8 10, i8* %30
%32 = load i8*, i8** %9
store i8 0, i8* %32
br label %33
33: ; preds = %29, %25
%34 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
store i8* %34, i8** %9
br label %35
35: ; preds = %49, %33
%36 = load i8*, i8** %9
%37 = load i8, i8* %36
%38 = sext i8 %37 to i32
%39 = icmp ne i32 %38, 0
br i1 %39, label %40, label %46
40: ; preds = %35
%41 = load i8*, i8** %9
%42 = load i8, i8* %41
%43 = call i32 @isspace(i8 signext %42)
%44 = icmp ne i32 %43, 0
%45 = xor i1 %44, true
br label %46
46: ; preds = %40, %35
%47 = phi i1 [ false, %35 ], [ %45, %40 ]
br i1 %47, label %48, label %52
48: ; preds = %46
br label %49
49: ; preds = %48
%50 = load i8*, i8** %9
%51 = getelementptr inbounds i8, i8* %50, i32 1
store i8* %51, i8** %9
br label %35
52: ; preds = %46
%53 = load i8*, i8** %9
%54 = load i8, i8* %53
%55 = icmp ne i8 %54, 0
br i1 %55, label %57, label %56
56: ; preds = %52
br label %20
57: ; preds = %52
%58 = load i8*, i8** %9
%59 = getelementptr inbounds i8, i8* %58, i32 1
store i8* %59, i8** %9
store i8 0, i8* %58
%60 = load i8*, i8** %9
%61 = call i64 @strtoul(i8* %60, i8** %10, i32 10)
store i64 %61, i64* %7
%62 = load i64, i64* %7
%63 = load i32, i32* %5
%64 = sext i32 %63 to i64
%65 = icmp ne i64 %62, %64
br i1 %65, label %70, label %66
66: ; preds = %57
%67 = load i8*, i8** %10
%68 = load i8*, i8** %9
%69 = icmp eq i8* %67, %68
br i1 %69, label %70, label %71
70: ; preds = %66, %57
br label %20
71: ; preds = %66
%72 = load i32, i32* %6
%73 = icmp ne i32 %72, 0
br i1 %73, label %74, label %79
74: ; preds = %71
%75 = load i8*, i8** %10
%76 = call i64 @strncmp(i8* %75, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 4)
%77 = icmp ne i64 %76, 0
br i1 %77, label %78, label %79
78: ; preds = %74
br label %20
79: ; preds = %74, %71
%80 = load i32, i32* %6
%81 = icmp ne i32 %80, 0
br i1 %81, label %87, label %82
82: ; preds = %79
%83 = load i8*, i8** %10
%84 = call i64 @strncmp(i8* %83, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 4)
%85 = icmp ne i64 %84, 0
br i1 %85, label %86, label %87
86: ; preds = %82
br label %20
87: ; preds = %82, %79
%88 = load i8*, i8** %9
%89 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
%90 = ptrtoint i8* %88 to i64
%91 = ptrtoint i8* %89 to i64
%92 = sub i64 %90, %91
%93 = icmp sgt i64 %92, 32
br i1 %93, label %94, label %95
94: ; preds = %87
br label %20
95: ; preds = %87
%96 = load i8*, i8** %4
%97 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
%98 = load i8*, i8** %9
%99 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
%100 = ptrtoint i8* %98 to i64
%101 = ptrtoint i8* %99 to i64
%102 = sub i64 %100, %101
%103 = trunc i64 %102 to i32
%104 = call i32 @memcpy(i8* %96, i8* %97, i32 %103)
br label %105
105: ; preds = %95, %20
%106 = load i32*, i32** %13
%107 = call i32 @__fclose_ca(i32* %106)
br label %108
108: ; preds = %105, %18
ret void
}

LLVM IR (-O2):
@.str = [14 x i8] c"/etc/services\00"
@.str.1 = [5 x i8] c"/udp\00"
@.str.2 = [5 x i8] c"/tcp\00"
define void @reverse_services(i8* %0, i32 %1, i32 %2) {
%4 = alloca [128 x i8]
%5 = alloca i8*
%6 = alloca [1032 x i8]
%7 = alloca i32
%8 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%9 = bitcast i8** %5 to i8*
%10 = getelementptr inbounds [1032 x i8], [1032 x i8]* %6, i64 0, i64 0
%11 = bitcast i32* %7 to i8*
%12 = call i32* @__fopen_rb_ca(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32* %7, i8* %10, i32 1032)
%13 = icmp eq i32* %12, null
br i1 %13, label %67, label %14
14: ; preds = %3
%15 = call i64 @fgets(i8* %8, i32 128, i32* %12)
%16 = icmp eq i64 %15, 0
br i1 %16, label %65, label %17
17: ; preds = %14
%18 = sext i32 %1 to i64
%19 = icmp eq i32 %2, 0
%20 = ptrtoint [128 x i8]* %4 to i64
br label %21
21: ; preds = %17, %48
%22 = call i8* @strchr(i8* %8, i8 signext 35)
%23 = icmp eq i8* %22, null
br i1 %23, label %26, label %24
24: ; preds = %21
%25 = getelementptr inbounds i8, i8* %22, i64 1
store i8 10, i8* %22
store i8 0, i8* %25
br label %26
26: ; preds = %24, %21
%27 = load i8, i8* %8
%28 = icmp eq i8 %27, 0
br i1 %28, label %48, label %29
29: ; preds = %26, %34
%30 = phi i8 [ %36, %34 ], [ %27, %26 ]
%31 = phi i8* [ %35, %34 ], [ %8, %26 ]
%32 = call i32 @isspace(i8 signext %30)
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %38
34: ; preds = %29
%35 = getelementptr inbounds i8, i8* %31, i64 1
%36 = load i8, i8* %35
%37 = icmp eq i8 %36, 0
br i1 %37, label %48, label %29
38: ; preds = %29
%39 = load i8, i8* %31
%40 = icmp eq i8 %39, 0
br i1 %40, label %48, label %41
41: ; preds = %38
%42 = getelementptr inbounds i8, i8* %31, i64 1
store i8 0, i8* %31
%43 = call i64 @strtoul(i8* %42, i8** %5, i32 10)
%44 = icmp eq i64 %43, %18
br i1 %44, label %45, label %48
45: ; preds = %41
%46 = load i8*, i8** %5
%47 = icmp eq i8* %46, %42
br i1 %47, label %48, label %51
48: ; preds = %34, %26, %41, %45, %38, %52, %55, %58
%49 = call i64 @fgets(i8* %8, i32 128, i32* %12)
%50 = icmp eq i64 %49, 0
br i1 %50, label %65, label %21
51: ; preds = %45
br i1 %19, label %55, label %52
52: ; preds = %51
%53 = call i64 @strncmp(i8* %46, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 4)
%54 = icmp eq i64 %53, 0
br i1 %54, label %58, label %48
55: ; preds = %51
%56 = call i64 @strncmp(i8* %46, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 4)
%57 = icmp eq i64 %56, 0
br i1 %57, label %58, label %48
58: ; preds = %52, %55
%59 = ptrtoint i8* %42 to i64
%60 = sub i64 %59, %20
%61 = icmp sgt i64 %60, 32
br i1 %61, label %48, label %62
62: ; preds = %58
%63 = trunc i64 %60 to i32
%64 = call i32 @memcpy(i8* %0, i8* %8, i32 %63)
br label %65
65: ; preds = %48, %14, %62
%66 = call i32 @__fclose_ca(i32* %12)
br label %67
67: ; preds = %3, %65
ret void
}