#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 ENODEV;
int errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int sscanf(char *, char *, int *, char *);
scalar_t__ strcmp(char *, char *);
int lookup_dev(char *name) {
  FILE *f;
  int n;
  char s[32], t[32];
  f = fopen("/proc/misc", "r");
  if (f == NULL)
    return -errno;
  while (fgets(s, 32, f) != NULL) {
    if (sscanf(s, "%d %s", &n, t) == 2)
      if (strcmp(name, t) == 0)
        break;
  }
  fclose(f);
  if (strcmp(name, t) == 0)
    return n;
  else
    return -ENODEV;
}


x86 assembly (clang, -O0):
<lookup_dev>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
lea    0xe9d(%rip),%rdi
lea    0xea1(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1191 <lookup_dev+0x41>
xor    %eax,%eax
mov    0x2e59(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1232 <lookup_dev+0xe2>
jmp    1196 <lookup_dev+0x46>
lea    -0x40(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x20,%esi
call   1040 <fgets@plt>
cmp    $0x0,%rax
je     11f9 <lookup_dev+0xa9>
lea    -0x40(%rbp),%rdi
lea    -0x60(%rbp),%rcx
lea    0xe4c(%rip),%rsi
lea    -0x1c(%rbp),%rdx
call   1060 <sscanf@plt>
cmp    $0x2,%eax
jne    11f4 <lookup_dev+0xa4>
mov    -0x10(%rbp),%rdi
lea    -0x60(%rbp),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
jne    11ef <lookup_dev+0x9f>
jmp    11f9 <lookup_dev+0xa9>
jmp    11f4 <lookup_dev+0xa4>
jmp    1196 <lookup_dev+0x46>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x10(%rbp),%rdi
lea    -0x60(%rbp),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
jne    1224 <lookup_dev+0xd4>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1232 <lookup_dev+0xe2>
xor    %eax,%eax
mov    0x2dab(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lookup_dev>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x58,%rsp
mov    %rdi,%r14
lea    0xe98(%rip),%rdi
lea    0xe9c(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     120e <lookup_dev+0xbe>
mov    %rax,%rbx
lea    0x30(%rsp),%rdi
mov    $0x20,%esi
mov    %rax,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
je     11eb <lookup_dev+0x9b>
lea    0xe6f(%rip),%r15
lea    0x30(%rsp),%r12
lea    0xc(%rsp),%r13
lea    0x10(%rsp),%rbp
jmp    11c5 <lookup_dev+0x75>
nop
mov    %r12,%rdi
mov    $0x20,%esi
mov    %rbx,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
je     11eb <lookup_dev+0x9b>
mov    %r12,%rdi
mov    %r15,%rsi
mov    %r13,%rdx
mov    %rbp,%rcx
call   1060 <sscanf@plt>
cmp    $0x2,%eax
jne    11b0 <lookup_dev+0x60>
mov    %r14,%rdi
mov    %rbp,%rsi
call   1050 <strcmp@plt>
test   %rax,%rax
jne    11b0 <lookup_dev+0x60>
mov    %rbx,%rdi
call   1030 <fclose@plt>
lea    0x10(%rsp),%rsi
mov    %r14,%rdi
call   1050 <strcmp@plt>
test   %rax,%rax
je     121b <lookup_dev+0xcb>
mov    0x2dcc(%rip),%rcx
jmp    1215 <lookup_dev+0xc5>
mov    0x2dcb(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
jmp    121f <lookup_dev+0xcf>
mov    0xc(%rsp),%eax
add    $0x58,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [11 x i8] c"/proc/misc\00"
@.str.1 = [2 x i8] c"r\00"
@errno = global i32 0
@.str.2 = [6 x i8] c"%d %s\00"
@ENODEV = global i32 0
define i32 @lookup_dev(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32
%6 = alloca [32 x i8]
%7 = alloca [32 x i8]
store i8* %0, i8** %3
%8 = call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 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 eq i32* %9, null
br i1 %10, label %11, label %14
11: ; preds = %1
%12 = load i32, i32* @errno
%13 = sub nsw i32 0, %12
store i32 %13, i32* %2
br label %45
14: ; preds = %1
br label %15
15: ; preds = %32, %14
%16 = getelementptr inbounds [32 x i8], [32 x i8]* %6, i64 0, i64 0
%17 = load i32*, i32** %4
%18 = call i32* @fgets(i8* %16, i32 32, i32* %17)
%19 = icmp ne i32* %18, null
br i1 %19, label %20, label %33
20: ; preds = %15
%21 = getelementptr inbounds [32 x i8], [32 x i8]* %6, i64 0, i64 0
%22 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 0
%23 = call i32 @sscanf(i8* %21, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32* %5, i8* %22)
%24 = icmp eq i32 %23, 2
br i1 %24, label %25, label %32
25: ; preds = %20
%26 = load i8*, i8** %3
%27 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 0
%28 = call i64 @strcmp(i8* %26, i8* %27)
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %25
br label %33
31: ; preds = %25
br label %32
32: ; preds = %31, %20
br label %15
33: ; preds = %30, %15
%34 = load i32*, i32** %4
%35 = call i32 @fclose(i32* %34)
%36 = load i8*, i8** %3
%37 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 0
%38 = call i64 @strcmp(i8* %36, i8* %37)
%39 = icmp eq i64 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %33
%41 = load i32, i32* %5
store i32 %41, i32* %2
br label %45
42: ; preds = %33
%43 = load i32, i32* @ENODEV
%44 = sub nsw i32 0, %43
store i32 %44, i32* %2
br label %45
45: ; preds = %42, %40, %11
%46 = load i32, i32* %2
ret i32 %46
}

LLVM IR (-O2):
@.str = [11 x i8] c"/proc/misc\00"
@.str.1 = [2 x i8] c"r\00"
@errno = global i32 0
@.str.2 = [6 x i8] c"%d %s\00"
@ENODEV = global i32 0
define i32 @lookup_dev(i8* %0) {
%2 = alloca i32
%3 = alloca [32 x i8]
%4 = alloca [32 x i8]
%5 = bitcast i32* %2 to i8*
%6 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 0
%7 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 0
%8 = tail call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%9 = icmp eq i32* %8, null
br i1 %9, label %13, label %10
10: ; preds = %1
%11 = call i32* @fgets(i8* %6, i32 32, i32* %8)
%12 = icmp eq i32* %11, null
br i1 %12, label %25, label %16
13: ; preds = %1
%14 = load i32, i32* @errno
%15 = sub nsw i32 0, %14
br label %34
16: ; preds = %10, %22
%17 = call i32 @sscanf(i8* %6, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32* %2, i8* %7)
%18 = icmp eq i32 %17, 2
br i1 %18, label %19, label %22
19: ; preds = %16
%20 = call i64 @strcmp(i8* %0, i8* %7)
%21 = icmp eq i64 %20, 0
br i1 %21, label %25, label %22
22: ; preds = %19, %16
%23 = call i32* @fgets(i8* %6, i32 32, i32* %8)
%24 = icmp eq i32* %23, null
br i1 %24, label %25, label %16
25: ; preds = %22, %19, %10
%26 = call i32 @fclose(i32* %8)
%27 = call i64 @strcmp(i8* %0, i8* %7)
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %25
%30 = load i32, i32* %2
br label %34
31: ; preds = %25
%32 = load i32, i32* @ENODEV
%33 = sub nsw i32 0, %32
br label %34
34: ; preds = %31, %29, %13
%35 = phi i32 [ %15, %13 ], [ %30, %29 ], [ %33, %31 ]
ret i32 %35
}