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

/* Variables and functions */
int WEXITSTATUS(int);
int /*<<< orphan*/ WIFEXITED(int);
int execvp(char *, char **);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fork();
int /*<<< orphan*/ waitpid(int /*<<< orphan*/, int *, int /*<<< orphan*/);
int i2c_bus_detect(char *index) {
  pid_t pid;
  int status;
  int pipefd[2] = {-1, -1};
  char *envp[] = {NULL};
  char *argv[] = {"i2cdetect", "-y", "-r", index, NULL};
  switch ((pid = fork())) {
  case -1:
    return -1;
  case 0:
    status = execvp(argv[0], argv);
    exit(status);
  default:
    if (waitpid(pid, &status, 0) < 0) {
      return -1;
    }
    if (WIFEXITED(status)) {
      return WEXITSTATUS(status);
    }
    break;
  }
  return -1;
}


x86 assembly (clang, -O0):
<i2c_bus_detect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    0xe8d(%rip),%rax
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
lea    0xe82(%rip),%rax
mov    %rax,-0x50(%rbp)
lea    0xe81(%rip),%rax
mov    %rax,-0x48(%rbp)
lea    0xe79(%rip),%rax
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
movq   $0x0,-0x30(%rbp)
xor    %eax,%eax
call   1080 <fork@plt>
mov    %eax,-0x14(%rbp)
mov    %eax,%ecx
mov    %ecx,-0x54(%rbp)
sub    $0xffffffff,%eax
je     11dd <i2c_bus_detect+0x7d>
jmp    11cd <i2c_bus_detect+0x6d>
mov    -0x54(%rbp),%eax
test   %eax,%eax
je     11e9 <i2c_bus_detect+0x89>
jmp    1201 <i2c_bus_detect+0xa1>
movl   $0xffffffff,-0x4(%rbp)
jmp    1251 <i2c_bus_detect+0xf1>
mov    -0x50(%rbp),%rdi
lea    -0x50(%rbp),%rsi
call   1050 <execvp@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%edi
call   1070 <exit@plt>
mov    -0x14(%rbp),%edi
lea    -0x18(%rbp),%rsi
xor    %edx,%edx
call   1040 <waitpid@plt>
cmp    $0x0,%eax
jge    1224 <i2c_bus_detect+0xc4>
movl   $0xffffffff,-0x4(%rbp)
jmp    1251 <i2c_bus_detect+0xf1>
mov    -0x18(%rbp),%edi
call   1060 <WIFEXITED@plt>
cmp    $0x0,%eax
je     1245 <i2c_bus_detect+0xe5>
mov    -0x18(%rbp),%edi
call   1030 <WEXITSTATUS@plt>
mov    %eax,-0x4(%rbp)
jmp    1251 <i2c_bus_detect+0xf1>
jmp    124a <i2c_bus_detect+0xea>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<i2c_bus_detect>:
push   %rbx
sub    $0x40,%rsp
lea    0xe94(%rip),%rax
mov    %rax,0x10(%rsp)
lea    0xe92(%rip),%rax
mov    %rax,0x18(%rsp)
lea    0xe89(%rip),%rax
mov    %rax,0x20(%rsp)
mov    %rdi,0x28(%rsp)
movq   $0x0,0x30(%rsp)
xor    %eax,%eax
call   1080 <fork@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     11d8 <i2c_bus_detect+0x78>
test   %ebx,%ebx
je     11e0 <i2c_bus_detect+0x80>
lea    0xc(%rsp),%rsi
mov    %ebx,%edi
xor    %edx,%edx
call   1040 <waitpid@plt>
mov    $0xffffffff,%ebx
test   %eax,%eax
js     11d8 <i2c_bus_detect+0x78>
mov    0xc(%rsp),%edi
call   1060 <WIFEXITED@plt>
test   %eax,%eax
je     11d8 <i2c_bus_detect+0x78>
mov    0xc(%rsp),%edi
call   1030 <WEXITSTATUS@plt>
mov    %eax,%ebx
mov    %ebx,%eax
add    $0x40,%rsp
pop    %rbx
ret
lea    0xe19(%rip),%rdi
lea    0x10(%rsp),%rsi
call   1050 <execvp@plt>
mov    %eax,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@__const.i2c_bus_detect.pipefd = [2 x i32] [i32 -1, i32 -1]
@.str = [10 x i8] c"i2cdetect\00"
@.str.1 = [3 x i8] c"-y\00"
@.str.2 = [3 x i8] c"-r\00"
define i32 @i2c_bus_detect(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca [2 x i32]
%7 = alloca [1 x i8*]
%8 = alloca [5 x i8*]
store i8* %0, i8** %3
%9 = bitcast [2 x i32]* %6 to i8*
%10 = bitcast [1 x i8*]* %7 to i8*
%11 = getelementptr inbounds [5 x i8*], [5 x i8*]* %8, i64 0, i64 0
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8** %11
%12 = getelementptr inbounds i8*, i8** %11, i64 1
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8** %12
%13 = getelementptr inbounds i8*, i8** %12, i64 1
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8** %13
%14 = getelementptr inbounds i8*, i8** %13, i64 1
%15 = load i8*, i8** %3
store i8* %15, i8** %14
%16 = getelementptr inbounds i8*, i8** %14, i64 1
store i8* null, i8** %16
%17 = call i32 (...) @fork()
store i32 %17, i32* %4
switch i32 %17, label %26 [
i32 -1, label %18
i32 0, label %19
]
18: ; preds = %1
store i32 -1, i32* %2
br label %40
19: ; preds = %1
%20 = getelementptr inbounds [5 x i8*], [5 x i8*]* %8, i64 0, i64 0
%21 = load i8*, i8** %20
%22 = getelementptr inbounds [5 x i8*], [5 x i8*]* %8, i64 0, i64 0
%23 = call i32 @execvp(i8* %21, i8** %22)
store i32 %23, i32* %5
%24 = load i32, i32* %5
%25 = call i32 @exit(i32 %24)
unreachable
26: ; preds = %1
%27 = load i32, i32* %4
%28 = call i32 @waitpid(i32 %27, i32* %5, i32 0)
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %26
store i32 -1, i32* %2
br label %40
31: ; preds = %26
%32 = load i32, i32* %5
%33 = call i32 @WIFEXITED(i32 %32)
%34 = icmp ne i32 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %31
%36 = load i32, i32* %5
%37 = call i32 @WEXITSTATUS(i32 %36)
store i32 %37, i32* %2
br label %40
38: ; preds = %31
br label %39
39: ; preds = %38
store i32 -1, i32* %2
br label %40
40: ; preds = %39, %35, %30, %18
%41 = load i32, i32* %2
ret i32 %41
}

LLVM IR (-O2):
@.str = [10 x i8] c"i2cdetect\00"
@.str.1 = [3 x i8] c"-y\00"
@.str.2 = [3 x i8] c"-r\00"
define i32 @i2c_bus_detect(i8* %0) {
%2 = alloca i32
%3 = alloca [5 x i8*]
%4 = bitcast i32* %2 to i8*
%5 = bitcast [5 x i8*]* %3 to i8*
%6 = getelementptr inbounds [5 x i8*], [5 x i8*]* %3, i64 0, i64 0
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8** %6
%7 = getelementptr inbounds [5 x i8*], [5 x i8*]* %3, i64 0, i64 1
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8** %7
%8 = getelementptr inbounds [5 x i8*], [5 x i8*]* %3, i64 0, i64 2
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8** %8
%9 = getelementptr inbounds [5 x i8*], [5 x i8*]* %3, i64 0, i64 3
store i8* %0, i8** %9
%10 = getelementptr inbounds [5 x i8*], [5 x i8*]* %3, i64 0, i64 4
store i8* null, i8** %10
%11 = tail call i32 (...) @fork()
switch i32 %11, label %15 [
i32 -1, label %25
i32 0, label %12
]
12: ; preds = %1
%13 = call i32 @execvp(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8** %6)
%14 = call i32 @exit(i32 %13)
unreachable
15: ; preds = %1
%16 = call i32 @waitpid(i32 %11, i32* %2, i32 0)
%17 = icmp slt i32 %16, 0
br i1 %17, label %25, label %18
18: ; preds = %15
%19 = load i32, i32* %2
%20 = call i32 @WIFEXITED(i32 %19)
%21 = icmp eq i32 %20, 0
br i1 %21, label %25, label %22
22: ; preds = %18
%23 = load i32, i32* %2
%24 = call i32 @WEXITSTATUS(i32 %23)
br label %25
25: ; preds = %18, %15, %1, %22
%26 = phi i32 [ %24, %22 ], [ %11, %1 ], [ -1, %15 ], [ -1, %18 ]
ret i32 %26
}