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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int atoi(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char *);
int /*<<< orphan*/ get_first_file_in_a_directory(char *, char *, int *);
int open(char *, int /*<<< orphan*/);
unsigned int read(int, char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *, char const *,
                            int);
int /*<<< orphan*/ stderr;
scalar_t__ strcmp(char const *, char *);
char *strerror(int /*<<< orphan*/);
int open_i2c_sensor(const char *dev, const char *type, int n, int *div) {
  char path[256];
  char buf[64];
  int fd;
  int divfd;
  if (dev == NULL || strcmp(dev, "*") == 0) {
    static int rep;
    if (!get_first_file_in_a_directory("/sys/bus/i2c/devices/", buf, &rep))
      return -1;
    dev = buf;
  }
  if (strcmp(type, "vol") == 0)
    type = "in";
  snprintf(path, 255,
           "/sys/bus/i2c/devices/"
           "%s/%s%d_input",
           dev, type, n);
  fd = open(path, O_RDONLY);
  if (fd < 0)
    fprintf(stderr,
            "torsmo: "
            "can't open '%s': %s"
            "\n",
            path, strerror(errno));
  if (strcmp(type, "in") == 0 || strcmp(type, "temp") == 0)
    *div = 1;
  else
    *div = 0;
  snprintf(path, 255,
           "/sys/bus/i2c/devices/"
           "%s/%s%d_div",
           dev, type, n);
  divfd = open(path, O_RDONLY);
  if (divfd > 0) {
    char divbuf[64];
    unsigned int divn;
    divn = read(divfd, divbuf, 63);
    divbuf[divn] = '\0';
    *div = atoi(divbuf);
  }
  close(divfd);
  return fd;
}


x86 assembly (clang, -O0):
<open_i2c_sensor>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1e0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %rcx,-0x28(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11cf <open_i2c_sensor+0x3f>
mov    -0x10(%rbp),%rdi
lea    0xe40(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    1209 <open_i2c_sensor+0x79>
lea    -0x170(%rbp),%rsi
lea    0xe25(%rip),%rdi
lea    0x2e88(%rip),%rdx
call   1050 <get_first_file_in_a_directory@plt>
cmp    $0x0,%eax
jne    11fe <open_i2c_sensor+0x6e>
movl   $0xffffffff,-0x4(%rbp)
jmp    13ad <open_i2c_sensor+0x21d>
lea    -0x170(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe04(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    122e <open_i2c_sensor+0x9e>
lea    0xdf2(%rip),%rax
mov    %rax,-0x18(%rbp)
lea    -0x130(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%r8
mov    -0x1c(%rbp),%r9d
mov    $0xff,%esi
lea    0xdd2(%rip),%rdx
call   1030 <snprintf@plt>
lea    -0x130(%rbp),%rdi
mov    0x2d88(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x174(%rbp)
cmpl   $0x0,-0x174(%rbp)
jge    12c1 <open_i2c_sensor+0x131>
mov    0x2d57(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1d4(%rbp)
lea    -0x130(%rbp),%rax
mov    %rax,-0x1d0(%rbp)
mov    0x2d32(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x1d4(%rbp),%edi
mov    -0x1d0(%rbp),%rdx
mov    %rax,%rcx
lea    0xd86(%rip),%rsi
call   1080 <fprintf@plt>
mov    -0x18(%rbp),%rdi
lea    0xd50(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
je     12f5 <open_i2c_sensor+0x165>
mov    -0x18(%rbp),%rdi
lea    0xd79(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    1304 <open_i2c_sensor+0x174>
mov    -0x28(%rbp),%rax
movl   $0x1,(%rax)
jmp    130e <open_i2c_sensor+0x17e>
mov    -0x28(%rbp),%rax
movl   $0x0,(%rax)
lea    -0x130(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%r8
mov    -0x1c(%rbp),%r9d
mov    $0xff,%esi
lea    0xd37(%rip),%rdx
call   1030 <snprintf@plt>
lea    -0x130(%rbp),%rdi
mov    0x2ca8(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x178(%rbp)
cmpl   $0x0,-0x178(%rbp)
jle    1399 <open_i2c_sensor+0x209>
mov    -0x178(%rbp),%edi
lea    -0x1c0(%rbp),%rsi
mov    $0x3f,%edx
call   1060 <read@plt>
mov    %eax,-0x1c4(%rbp)
mov    -0x1c4(%rbp),%eax
movb   $0x0,-0x1c0(%rbp,%rax,1)
lea    -0x1c0(%rbp),%rdi
call   10a0 <atoi@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x178(%rbp),%edi
call   1040 <close@plt>
mov    -0x174(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1e0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_i2c_sensor>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x188,%rsp
mov    %rcx,%r14
mov    %edx,%r15d
mov    %rsi,%rbx
test   %rdi,%rdi
je     11c3 <open_i2c_sensor+0x33>
mov    %rdi,%r13
lea    0xe47(%rip),%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
jne    11e6 <open_i2c_sensor+0x56>
lea    0x40(%rsp),%r13
lea    0xe33(%rip),%rdi
lea    0x2e96(%rip),%rdx
mov    %r13,%rsi
call   1050 <get_first_file_in_a_directory@plt>
test   %eax,%eax
je     130d <open_i2c_sensor+0x17d>
lea    0xe2b(%rip),%rsi
mov    %rbx,%rdi
call   1070 <strcmp@plt>
test   %rax,%rax
lea    0xe1d(%rip),%rbp
cmovne %rbx,%rbp
lea    0xe15(%rip),%rdx
lea    0x80(%rsp),%rbx
mov    %rbx,%rdi
mov    $0xff,%esi
mov    %r13,%rcx
mov    %rbp,%r8
mov    %r15d,%r9d
call   1030 <snprintf@plt>
mov    0x2db9(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1090 <open@plt>
mov    %eax,%r12d
test   %eax,%eax
jns    1270 <open_i2c_sensor+0xe0>
mov    0x2d91(%rip),%rax
mov    (%rax),%ebx
mov    0x2d80(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xde4(%rip),%rsi
lea    0x80(%rsp),%rdx
mov    %ebx,%edi
mov    %rax,%rcx
call   1080 <fprintf@plt>
lea    0xda5(%rip),%rsi
mov    %rbp,%rdi
call   1070 <strcmp@plt>
test   %rax,%rax
je     129d <open_i2c_sensor+0x10d>
lea    0xdd4(%rip),%rsi
mov    %rbp,%rdi
call   1070 <strcmp@plt>
xor    %ecx,%ecx
test   %rax,%rax
sete   %cl
jmp    12a2 <open_i2c_sensor+0x112>
mov    $0x1,%ecx
mov    %ecx,(%r14)
lea    0xdb8(%rip),%rdx
lea    0x80(%rsp),%rbx
mov    %rbx,%rdi
mov    $0xff,%esi
mov    %r13,%rcx
mov    %rbp,%r8
mov    %r15d,%r9d
call   1030 <snprintf@plt>
mov    0x2d17(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1090 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
jle    1304 <open_i2c_sensor+0x174>
mov    %rsp,%rbp
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x3f,%edx
call   1060 <read@plt>
mov    %eax,%eax
movb   $0x0,(%rsp,%rax,1)
mov    %rbp,%rdi
call   10a0 <atoi@plt>
mov    %eax,(%r14)
mov    %ebx,%edi
call   1040 <close@plt>
jmp    1313 <open_i2c_sensor+0x183>
mov    $0xffffffff,%r12d
mov    %r12d,%eax
add    $0x188,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"*\00"
@open_i2c_sensor.rep = internal global i32 0
@.str.1 = [22 x i8] c"/sys/bus/i2c/devices/\00"
@.str.2 = [4 x i8] c"vol\00"
@.str.3 = [3 x i8] c"in\00"
@.str.4 = [35 x i8] c"/sys/bus/i2c/devices/%s/%s%d_input\00"
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str.5 = [29 x i8] c"torsmo: can't open '%s': %s\0A\00"
@errno = global i32 0
@.str.6 = [5 x i8] c"temp\00"
@.str.7 = [33 x i8] c"/sys/bus/i2c/devices/%s/%s%d_div\00"
define i32 @open_i2c_sensor(i8* %0, i8* %1, i32 %2, i32* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32*
%10 = alloca [256 x i8]
%11 = alloca [64 x i8]
%12 = alloca i32
%13 = alloca i32
%14 = alloca [64 x i8]
%15 = alloca i32
store i8* %0, i8** %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32* %3, i32** %9
%16 = load i8*, i8** %6
%17 = icmp eq i8* %16, null
br i1 %17, label %22, label %18
18: ; preds = %4
%19 = load i8*, i8** %6
%20 = call i64 @strcmp(i8* %19, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %29
22: ; preds = %18, %4
%23 = getelementptr inbounds [64 x i8], [64 x i8]* %11, i64 0, i64 0
%24 = call i32 @get_first_file_in_a_directory(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %23, i32* @open_i2c_sensor.rep)
%25 = icmp ne i32 %24, 0
br i1 %25, label %27, label %26
26: ; preds = %22
store i32 -1, i32* %5
br label %88
27: ; preds = %22
%28 = getelementptr inbounds [64 x i8], [64 x i8]* %11, i64 0, i64 0
store i8* %28, i8** %6
br label %29
29: ; preds = %27, %18
%30 = load i8*, i8** %7
%31 = call i64 @strcmp(i8* %30, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%32 = icmp eq i64 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %29
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8** %7
br label %34
34: ; preds = %33, %29
%35 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%36 = load i8*, i8** %6
%37 = load i8*, i8** %7
%38 = load i32, i32* %8
%39 = call i32 @snprintf(i8* %35, i32 255, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.4, i64 0, i64 0), i8* %36, i8* %37, i32 %38)
%40 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%41 = load i32, i32* @O_RDONLY
%42 = call i32 @open(i8* %40, i32 %41)
store i32 %42, i32* %12
%43 = load i32, i32* %12
%44 = icmp slt i32 %43, 0
br i1 %44, label %45, label %51
45: ; preds = %34
%46 = load i32, i32* @stderr
%47 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%48 = load i32, i32* @errno
%49 = call i8* @strerror(i32 %48)
%50 = call i32 @fprintf(i32 %46, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.5, i64 0, i64 0), i8* %47, i8* %49)
br label %51
51: ; preds = %45, %34
%52 = load i8*, i8** %7
%53 = call i64 @strcmp(i8* %52, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%54 = icmp eq i64 %53, 0
br i1 %54, label %59, label %55
55: ; preds = %51
%56 = load i8*, i8** %7
%57 = call i64 @strcmp(i8* %56, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0))
%58 = icmp eq i64 %57, 0
br i1 %58, label %59, label %61
59: ; preds = %55, %51
%60 = load i32*, i32** %9
store i32 1, i32* %60
br label %63
61: ; preds = %55
%62 = load i32*, i32** %9
store i32 0, i32* %62
br label %63
63: ; preds = %61, %59
%64 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%65 = load i8*, i8** %6
%66 = load i8*, i8** %7
%67 = load i32, i32* %8
%68 = call i32 @snprintf(i8* %64, i32 255, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.7, i64 0, i64 0), i8* %65, i8* %66, i32 %67)
%69 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%70 = load i32, i32* @O_RDONLY
%71 = call i32 @open(i8* %69, i32 %70)
store i32 %71, i32* %13
%72 = load i32, i32* %13
%73 = icmp sgt i32 %72, 0
br i1 %73, label %74, label %84
74: ; preds = %63
%75 = load i32, i32* %13
%76 = getelementptr inbounds [64 x i8], [64 x i8]* %14, i64 0, i64 0
%77 = call i32 @read(i32 %75, i8* %76, i32 63)
store i32 %77, i32* %15
%78 = load i32, i32* %15
%79 = zext i32 %78 to i64
%80 = getelementptr inbounds [64 x i8], [64 x i8]* %14, i64 0, i64 %79
store i8 0, i8* %80
%81 = getelementptr inbounds [64 x i8], [64 x i8]* %14, i64 0, i64 0
%82 = call i32 @atoi(i8* %81)
%83 = load i32*, i32** %9
store i32 %82, i32* %83
br label %84
84: ; preds = %74, %63
%85 = load i32, i32* %13
%86 = call i32 @close(i32 %85)
%87 = load i32, i32* %12
store i32 %87, i32* %5
br label %88
88: ; preds = %84, %26
%89 = load i32, i32* %5
ret i32 %89
}

LLVM IR (-O2):
@.str = [2 x i8] c"*\00"
@open_i2c_sensor.rep = internal global i32 0
@.str.1 = [22 x i8] c"/sys/bus/i2c/devices/\00"
@.str.2 = [4 x i8] c"vol\00"
@.str.3 = [3 x i8] c"in\00"
@.str.4 = [35 x i8] c"/sys/bus/i2c/devices/%s/%s%d_input\00"
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str.5 = [29 x i8] c"torsmo: can't open '%s': %s\0A\00"
@errno = global i32 0
@.str.6 = [5 x i8] c"temp\00"
@.str.7 = [33 x i8] c"/sys/bus/i2c/devices/%s/%s%d_div\00"
define i32 @open_i2c_sensor(i8* %0, i8* %1, i32 %2, i32* nocapture writeonly %3) {
%5 = alloca [256 x i8]
%6 = alloca [64 x i8]
%7 = alloca [64 x i8]
%8 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%9 = getelementptr inbounds [64 x i8], [64 x i8]* %6, i64 0, i64 0
%10 = icmp eq i8* %0, null
br i1 %10, label %14, label %11
11: ; preds = %4
%12 = tail call i64 @strcmp(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %11, %4
%15 = call i32 @get_first_file_in_a_directory(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %9, i32* @open_i2c_sensor.rep)
%16 = icmp eq i32 %15, 0
br i1 %16, label %52, label %17
17: ; preds = %14, %11
%18 = phi i8* [ %0, %11 ], [ %9, %14 ]
%19 = call i64 @strcmp(i8* %1, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%20 = icmp eq i64 %19, 0
%21 = select i1 %20, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %1
%22 = call i32 @snprintf(i8* %8, i32 255, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.4, i64 0, i64 0), i8* %18, i8* %21, i32 %2)
%23 = load i32, i32* @O_RDONLY
%24 = call i32 @open(i8* %8, i32 %23)
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %31
26: ; preds = %17
%27 = load i32, i32* @stderr
%28 = load i32, i32* @errno
%29 = call i8* @strerror(i32 %28)
%30 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.5, i64 0, i64 0), i8* %8, i8* %29)
br label %31
31: ; preds = %26, %17
%32 = call i64 @strcmp(i8* %21, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%33 = icmp eq i64 %32, 0
br i1 %33, label %38, label %34
34: ; preds = %31
%35 = call i64 @strcmp(i8* %21, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0))
%36 = icmp eq i64 %35, 0
%37 = zext i1 %36 to i32
br label %38
38: ; preds = %34, %31
%39 = phi i32 [ 1, %31 ], [ %37, %34 ]
store i32 %39, i32* %3
%40 = call i32 @snprintf(i8* %8, i32 255, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.7, i64 0, i64 0), i8* %18, i8* %21, i32 %2)
%41 = load i32, i32* @O_RDONLY
%42 = call i32 @open(i8* %8, i32 %41)
%43 = icmp sgt i32 %42, 0
br i1 %43, label %44, label %50
44: ; preds = %38
%45 = getelementptr inbounds [64 x i8], [64 x i8]* %7, i64 0, i64 0
%46 = call i32 @read(i32 %42, i8* %45, i32 63)
%47 = zext i32 %46 to i64
%48 = getelementptr inbounds [64 x i8], [64 x i8]* %7, i64 0, i64 %47
store i8 0, i8* %48
%49 = call i32 @atoi(i8* %45)
store i32 %49, i32* %3
br label %50
50: ; preds = %44, %38
%51 = call i32 @close(i32 %42)
br label %52
52: ; preds = %14, %50
%53 = phi i32 [ %24, %50 ], [ -1, %14 ]
ret i32 %53
}