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

/* Variables and functions */
int EINVAL;
int ENOENT;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fwrite(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ whine(char *, char *);
int device_bind_irq_to_cpu(int irq, int cpu) {
  int ret;
  char buff[256];
  char file[256];
  if (irq < 0 || cpu < 0)
    return -EINVAL;
  memset(file, 0, sizeof(file));
  memset(buff, 0, sizeof(buff));
  cpu = cpu + 1;
  sprintf(file, "/proc/irq/%d/smp_affinity", irq);
  FILE *fp = fopen(file, "w");
  if (!fp) {
    whine("Cannot open file %s!\n", file);
    return -ENOENT;
  }
  sprintf(buff, "%d", cpu);
  ret = fwrite(buff, sizeof(buff), 1, fp);
  fclose(fp);
  return (ret > 0 ? 0 : ret);
}


x86 assembly (clang, -O0):
<device_bind_irq_to_cpu>:
push   %rbp
mov    %rsp,%rbp
sub    $0x220,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
cmpl   $0x0,-0x8(%rbp)
jl     1185 <device_bind_irq_to_cpu+0x25>
cmpl   $0x0,-0xc(%rbp)
jge    1198 <device_bind_irq_to_cpu+0x38>
xor    %eax,%eax
mov    0x2e52(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1296 <device_bind_irq_to_cpu+0x136>
lea    -0x210(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1040 <memset@plt>
lea    -0x110(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1040 <memset@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
lea    -0x210(%rbp),%rdi
mov    -0x8(%rbp),%edx
lea    0xe28(%rip),%rsi
call   1070 <sprintf@plt>
lea    -0x210(%rbp),%rdi
lea    0xe2f(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x218(%rbp)
cmpq   $0x0,-0x218(%rbp)
jne    122b <device_bind_irq_to_cpu+0xcb>
lea    -0x210(%rbp),%rsi
lea    0xe09(%rip),%rdi
call   1050 <whine@plt>
xor    %eax,%eax
mov    0x2daf(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1296 <device_bind_irq_to_cpu+0x136>
lea    -0x110(%rbp),%rdi
mov    -0xc(%rbp),%edx
lea    0xdf6(%rip),%rsi
call   1070 <sprintf@plt>
lea    -0x110(%rbp),%rdi
mov    -0x218(%rbp),%rcx
mov    $0x100,%esi
mov    $0x1,%edx
call   1080 <fwrite@plt>
mov    %eax,-0x10(%rbp)
mov    -0x218(%rbp),%rdi
call   1030 <fclose@plt>
cmpl   $0x0,-0x10(%rbp)
jle    1284 <device_bind_irq_to_cpu+0x124>
xor    %eax,%eax
mov    %eax,-0x21c(%rbp)
jmp    128d <device_bind_irq_to_cpu+0x12d>
mov    -0x10(%rbp),%eax
mov    %eax,-0x21c(%rbp)
mov    -0x21c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x220,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<device_bind_irq_to_cpu>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x208,%rsp
mov    %esi,%eax
or     %edi,%eax
js     1212 <device_bind_irq_to_cpu+0xb2>
mov    %esi,%r15d
mov    %edi,%ebp
mov    %rsp,%r14
xor    %ebx,%ebx
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1040 <memset@plt>
lea    0x100(%rsp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1040 <memset@plt>
lea    0xe55(%rip),%rsi
mov    %r14,%rdi
mov    %ebp,%edx
call   1070 <sprintf@plt>
lea    0xe5e(%rip),%rsi
mov    %r14,%rdi
call   1060 <fopen@plt>
test   %rax,%rax
je     121d <device_bind_irq_to_cpu+0xbd>
mov    %rax,%r14
add    $0x1,%r15d
lea    0xe5b(%rip),%rsi
lea    0x100(%rsp),%rbx
mov    %rbx,%rdi
mov    %r15d,%edx
call   1070 <sprintf@plt>
mov    %rbx,%rdi
mov    $0x100,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   1080 <fwrite@plt>
mov    %eax,%ebp
mov    %r14,%rdi
call   1030 <fclose@plt>
mov    %ebp,%ebx
sar    $0x1f,%ebx
and    %ebp,%ebx
jmp    1235 <device_bind_irq_to_cpu+0xd5>
mov    0x2dc7(%rip),%rax
xor    %ebx,%ebx
jmp    1233 <device_bind_irq_to_cpu+0xd3>
lea    0xdf8(%rip),%rdi
mov    %rsp,%rsi
call   1050 <whine@plt>
mov    0x2d9d(%rip),%rax
sub    (%rax),%ebx
mov    %ebx,%eax
add    $0x208,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@EINVAL = global i32 0
@.str = [26 x i8] c"/proc/irq/%d/smp_affinity\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [22 x i8] c"Cannot open file %s!\0A\00"
@ENOENT = global i32 0
@.str.3 = [3 x i8] c"%d\00"
define i32 @device_bind_irq_to_cpu(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca [256 x i8]
%8 = alloca [256 x i8]
%9 = alloca i32*
store i32 %0, i32* %4
store i32 %1, i32* %5
%10 = load i32, i32* %4
%11 = icmp slt i32 %10, 0
br i1 %11, label %15, label %12
12: ; preds = %2
%13 = load i32, i32* %5
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %12, %2
%16 = load i32, i32* @EINVAL
%17 = sub nsw i32 0, %16
store i32 %17, i32* %3
br label %53
18: ; preds = %12
%19 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%20 = call i32 @memset(i8* %19, i32 0, i32 256)
%21 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%22 = call i32 @memset(i8* %21, i32 0, i32 256)
%23 = load i32, i32* %5
%24 = add nsw i32 %23, 1
store i32 %24, i32* %5
%25 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%26 = load i32, i32* %4
%27 = call i32 @sprintf(i8* %25, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 %26)
%28 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%29 = call i32* @fopen(i8* %28, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %29, i32** %9
%30 = load i32*, i32** %9
%31 = icmp ne i32* %30, null
br i1 %31, label %37, label %32
32: ; preds = %18
%33 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%34 = call i32 @whine(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %33)
%35 = load i32, i32* @ENOENT
%36 = sub nsw i32 0, %35
store i32 %36, i32* %3
br label %53
37: ; preds = %18
%38 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%39 = load i32, i32* %5
%40 = call i32 @sprintf(i8* %38, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 %39)
%41 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%42 = load i32*, i32** %9
%43 = call i32 @fwrite(i8* %41, i32 256, i32 1, i32* %42)
store i32 %43, i32* %6
%44 = load i32*, i32** %9
%45 = call i32 @fclose(i32* %44)
%46 = load i32, i32* %6
%47 = icmp sgt i32 %46, 0
br i1 %47, label %48, label %49
48: ; preds = %37
br label %51
49: ; preds = %37
%50 = load i32, i32* %6
br label %51
51: ; preds = %49, %48
%52 = phi i32 [ 0, %48 ], [ %50, %49 ]
store i32 %52, i32* %3
br label %53
53: ; preds = %51, %32, %15
%54 = load i32, i32* %3
ret i32 %54
}

LLVM IR (-O2):
@EINVAL = global i32 0
@.str = [26 x i8] c"/proc/irq/%d/smp_affinity\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [22 x i8] c"Cannot open file %s!\0A\00"
@ENOENT = global i32 0
@.str.3 = [3 x i8] c"%d\00"
define i32 @device_bind_irq_to_cpu(i32 %0, i32 %1) {
%3 = alloca [256 x i8]
%4 = alloca [256 x i8]
%5 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%6 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%7 = or i32 %1, %0
%8 = icmp sgt i32 %7, -1
br i1 %8, label %12, label %9
9: ; preds = %2
%10 = load i32, i32* @EINVAL
%11 = sub nsw i32 0, %10
br label %29
12: ; preds = %2
%13 = call i32 @memset(i8* %6, i32 0, i32 256)
%14 = call i32 @memset(i8* %5, i32 0, i32 256)
%15 = call i32 @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 %0)
%16 = call i32* @fopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %22
18: ; preds = %12
%19 = call i32 @whine(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %6)
%20 = load i32, i32* @ENOENT
%21 = sub nsw i32 0, %20
br label %29
22: ; preds = %12
%23 = add nsw i32 %1, 1
%24 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 %23)
%25 = call i32 @fwrite(i8* %5, i32 256, i32 1, i32* %16)
%26 = call i32 @fclose(i32* %16)
%27 = icmp slt i32 %25, 0
%28 = select i1 %27, i32 %25, i32 0
br label %29
29: ; preds = %18, %22, %9
%30 = phi i32 [ %11, %9 ], [ %28, %22 ], [ %21, %18 ]
ret i32 %30
}