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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ strcmp(char *, char *);
char *strdup(char *);
char *strtok(char *, char *);
char *getdev(char *arg) {
  FILE *f;
  char tmp[160];
  char *dev;
  char *mntpt;
  f = fopen("/etc/mtab", "r");
  if (f) {
    while (fgets(tmp, sizeof(tmp), f)) {
      dev = strtok(tmp, " \t");
      if (*tmp == '#' || dev == NULL)
        continue;
      mntpt = strtok(NULL, " \t");
      if (mntpt == NULL)
        continue;
      if ((strcmp(dev, arg) == 0) || (strcmp(mntpt, arg) == 0)) {
        fclose(f);
        return strdup(dev);
      }
    }
    fclose(f);
  }
  return NULL;
}


x86 assembly (clang, -O0):
<getdev>:
push   %rbp
mov    %rsp,%rbp
sub    $0xd0,%rsp
mov    %rdi,-0x10(%rbp)
lea    0xe8a(%rip),%rdi
lea    0xe8d(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     127c <getdev+0x11c>
jmp    1196 <getdev+0x36>
lea    -0xc0(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0xa0,%esi
call   1040 <fgets@plt>
cmp    $0x0,%rax
je     1273 <getdev+0x113>
lea    -0xc0(%rbp),%rdi
lea    0xe49(%rip),%rsi
call   1070 <strtok@plt>
mov    %rax,-0xc8(%rbp)
movsbl -0xc0(%rbp),%eax
cmp    $0x23,%eax
je     11ed <getdev+0x8d>
cmpq   $0x0,-0xc8(%rbp)
jne    11f2 <getdev+0x92>
jmp    1196 <getdev+0x36>
xor    %eax,%eax
mov    %eax,%edi
lea    0xe0f(%rip),%rsi
call   1070 <strtok@plt>
mov    %rax,-0xd0(%rbp)
cmpq   $0x0,-0xd0(%rbp)
jne    121c <getdev+0xbc>
jmp    1196 <getdev+0x36>
mov    -0xc8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
je     1250 <getdev+0xf0>
mov    -0xd0(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
jne    126e <getdev+0x10e>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0xc8(%rbp),%rdi
call   1080 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    1284 <getdev+0x124>
jmp    1196 <getdev+0x36>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0xd0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getdev>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xa8,%rsp
mov    %rdi,%r14
lea    0xe85(%rip),%rdi
lea    0xe88(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     123a <getdev+0xda>
mov    %rax,%r13
mov    %rsp,%rdi
mov    $0xa0,%esi
mov    %rax,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
je     1232 <getdev+0xd2>
lea    0xe59(%rip),%r15
mov    %rsp,%r12
jmp    11d5 <getdev+0x75>
nopl   0x0(%rax,%rax,1)
mov    %r12,%rdi
mov    $0xa0,%esi
mov    %r13,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
je     1232 <getdev+0xd2>
mov    %r12,%rdi
mov    %r15,%rsi
call   1070 <strtok@plt>
cmpb   $0x23,(%rsp)
je     11c0 <getdev+0x60>
mov    %rax,%rbx
test   %rax,%rax
je     11c0 <getdev+0x60>
xor    %edi,%edi
mov    %r15,%rsi
call   1070 <strtok@plt>
test   %rax,%rax
je     11c0 <getdev+0x60>
mov    %rax,%rbp
mov    %rbx,%rdi
mov    %r14,%rsi
call   1050 <strcmp@plt>
test   %rax,%rax
je     1220 <getdev+0xc0>
mov    %rbp,%rdi
mov    %r14,%rsi
call   1050 <strcmp@plt>
test   %rax,%rax
jne    11c0 <getdev+0x60>
mov    %r13,%rdi
call   1030 <fclose@plt>
mov    %rbx,%rdi
call   1080 <strdup@plt>
jmp    123c <getdev+0xdc>
mov    %r13,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
add    $0xa8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [10 x i8] c"/etc/mtab\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [3 x i8] c" \09\00"
define i8* @getdev(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca [160 x i8]
%6 = alloca i8*
%7 = alloca i8*
store i8* %0, i8** %3
%8 = call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %8, i32** %4
%9 = load i32*, i32** %4
%10 = icmp ne i32* %9, null
br i1 %10, label %11, label %52
11: ; preds = %1
br label %12
12: ; preds = %48, %32, %27, %11
%13 = getelementptr inbounds [160 x i8], [160 x i8]* %5, i64 0, i64 0
%14 = load i32*, i32** %4
%15 = call i64 @fgets(i8* %13, i32 160, i32* %14)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %49
17: ; preds = %12
%18 = getelementptr inbounds [160 x i8], [160 x i8]* %5, i64 0, i64 0
%19 = call i8* @strtok(i8* %18, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i8* %19, i8** %6
%20 = getelementptr inbounds [160 x i8], [160 x i8]* %5, i64 0, i64 0
%21 = load i8, i8* %20
%22 = sext i8 %21 to i32
%23 = icmp eq i32 %22, 35
br i1 %23, label %27, label %24
24: ; preds = %17
%25 = load i8*, i8** %6
%26 = icmp eq i8* %25, null
br i1 %26, label %27, label %28
27: ; preds = %24, %17
br label %12
28: ; preds = %24
%29 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i8* %29, i8** %7
%30 = load i8*, i8** %7
%31 = icmp eq i8* %30, null
br i1 %31, label %32, label %33
32: ; preds = %28
br label %12
33: ; preds = %28
%34 = load i8*, i8** %6
%35 = load i8*, i8** %3
%36 = call i64 @strcmp(i8* %34, i8* %35)
%37 = icmp eq i64 %36, 0
br i1 %37, label %43, label %38
38: ; preds = %33
%39 = load i8*, i8** %7
%40 = load i8*, i8** %3
%41 = call i64 @strcmp(i8* %39, i8* %40)
%42 = icmp eq i64 %41, 0
br i1 %42, label %43, label %48
43: ; preds = %38, %33
%44 = load i32*, i32** %4
%45 = call i32 @fclose(i32* %44)
%46 = load i8*, i8** %6
%47 = call i8* @strdup(i8* %46)
store i8* %47, i8** %2
br label %53
48: ; preds = %38
br label %12
49: ; preds = %12
%50 = load i32*, i32** %4
%51 = call i32 @fclose(i32* %50)
br label %52
52: ; preds = %49, %1
store i8* null, i8** %2
br label %53
53: ; preds = %52, %43
%54 = load i8*, i8** %2
ret i8* %54
}

LLVM IR (-O2):
@.str = [10 x i8] c"/etc/mtab\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [3 x i8] c" \09\00"
define noalias i8* @getdev(i8* %0) {
%2 = alloca [160 x i8]
%3 = getelementptr inbounds [160 x i8], [160 x i8]* %2, i64 0, i64 0
%4 = tail call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 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 %32, label %6
6: ; preds = %1
%7 = call i64 @fgets(i8* %3, i32 160, i32* %4)
%8 = icmp eq i64 %7, 0
br i1 %8, label %30, label %9
9: ; preds = %6, %15
%10 = call i8* @strtok(i8* %3, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%11 = load i8, i8* %3
%12 = icmp eq i8 %11, 35
%13 = icmp eq i8* %10, null
%14 = select i1 %12, i1 true, i1 %13
br i1 %14, label %15, label %18
15: ; preds = %9, %18, %24
%16 = call i64 @fgets(i8* %3, i32 160, i32* %4)
%17 = icmp eq i64 %16, 0
br i1 %17, label %30, label %9
18: ; preds = %9
%19 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%20 = icmp eq i8* %19, null
br i1 %20, label %15, label %21
21: ; preds = %18
%22 = call i64 @strcmp(i8* %10, i8* %0)
%23 = icmp eq i64 %22, 0
br i1 %23, label %27, label %24
24: ; preds = %21
%25 = call i64 @strcmp(i8* %19, i8* %0)
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %15
27: ; preds = %24, %21
%28 = call i32 @fclose(i32* %4)
%29 = call i8* @strdup(i8* %10)
br label %32
30: ; preds = %15, %6
%31 = call i32 @fclose(i32* %4)
br label %32
32: ; preds = %1, %30, %27
%33 = phi i8* [ %29, %27 ], [ null, %30 ], [ null, %1 ]
ret i8* %33
}