#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 EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ LOOP_CHANGE_FD;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
int ioctl(int, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ usage(char *);
int main(int argc, char **argv) {
  int ret;
  int fd, filefd;
  if (argc != 3)
    usage(argv[0]);
  fd = open(argv[1], O_RDWR);
  if (fd == -1) {
    perror("open");
    return EXIT_FAILURE;
  }
  filefd = open(argv[2], O_RDWR);
  if (filefd == -1) {
    perror("open");
    return EXIT_FAILURE;
  }
  ret = ioctl(fd, LOOP_CHANGE_FD, filefd);
  if (ret == -1) {
    perror("ioctl");
    close(fd);
    close(filefd);
    return EXIT_FAILURE;
  }
  close(fd);
  close(filefd);
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     117c <main+0x2c>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1050 <usage@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e5d(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    11bc <main+0x6c>
lea    0xe5a(%rip),%rdi
call   1070 <perror@plt>
mov    0x2e26(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1266 <main+0x116>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    0x2e1d(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11fc <main+0xac>
lea    0xe1a(%rip),%rdi
call   1070 <perror@plt>
mov    0x2de6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1266 <main+0x116>
mov    -0x18(%rbp),%edi
mov    0x2dba(%rip),%rax
mov    (%rax),%esi
mov    -0x1c(%rbp),%edx
call   1030 <ioctl@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    124a <main+0xfa>
lea    0xde1(%rip),%rdi
call   1070 <perror@plt>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
mov    0x2d98(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1266 <main+0x116>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
mov    0x2d6f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%rbp
cmp    $0x3,%edi
je     1165 <main+0x15>
mov    0x0(%rbp),%rdi
call   1050 <usage@plt>
mov    0x8(%rbp),%rdi
mov    0x2e78(%rip),%r14
mov    (%r14),%esi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     11c5 <main+0x75>
mov    %eax,%ebx
mov    0x10(%rbp),%rdi
mov    (%r14),%esi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     11c5 <main+0x75>
mov    %eax,%ebp
mov    0x2e27(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %ebp,%edx
call   1030 <ioctl@plt>
cmp    $0xffffffff,%eax
je     11da <main+0x8a>
mov    %ebx,%edi
call   1040 <close@plt>
mov    %ebp,%edi
call   1040 <close@plt>
mov    0x2e12(%rip),%rax
mov    (%rax),%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe34(%rip),%rdi
call   1070 <perror@plt>
mov    0x2e00(%rip),%rax
jmp    11be <main+0x6e>
lea    0xe24(%rip),%rdi
call   1070 <perror@plt>
mov    %ebx,%edi
call   1040 <close@plt>
mov    %ebp,%edi
call   1040 <close@plt>
mov    0x2ddd(%rip),%rax
jmp    11be <main+0x6e>

LLVM IR (-O0):
@O_RDWR = global i32 0
@.str = [5 x i8] c"open\00"
@EXIT_FAILURE = global i32 0
@LOOP_CHANGE_FD = global i32 0
@.str.1 = [6 x i8] c"ioctl\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp ne i32 %9, 3
br i1 %10, label %11, label %16
11: ; preds = %2
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 0
%14 = load i8*, i8** %13
%15 = call i32 @usage(i8* %14)
br label %16
16: ; preds = %11, %2
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
%20 = load i32, i32* @O_RDWR
%21 = call i32 @open(i8* %19, i32 %20)
store i32 %21, i32* %7
%22 = load i32, i32* %7
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %27
24: ; preds = %16
%25 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%26 = load i32, i32* @EXIT_FAILURE
store i32 %26, i32* %3
br label %58
27: ; preds = %16
%28 = load i8**, i8*** %5
%29 = getelementptr inbounds i8*, i8** %28, i64 2
%30 = load i8*, i8** %29
%31 = load i32, i32* @O_RDWR
%32 = call i32 @open(i8* %30, i32 %31)
store i32 %32, i32* %8
%33 = load i32, i32* %8
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %38
35: ; preds = %27
%36 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%37 = load i32, i32* @EXIT_FAILURE
store i32 %37, i32* %3
br label %58
38: ; preds = %27
%39 = load i32, i32* %7
%40 = load i32, i32* @LOOP_CHANGE_FD
%41 = load i32, i32* %8
%42 = call i32 @ioctl(i32 %39, i32 %40, i32 %41)
store i32 %42, i32* %6
%43 = load i32, i32* %6
%44 = icmp eq i32 %43, -1
br i1 %44, label %45, label %52
45: ; preds = %38
%46 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%47 = load i32, i32* %7
%48 = call i32 @close(i32 %47)
%49 = load i32, i32* %8
%50 = call i32 @close(i32 %49)
%51 = load i32, i32* @EXIT_FAILURE
store i32 %51, i32* %3
br label %58
52: ; preds = %38
%53 = load i32, i32* %7
%54 = call i32 @close(i32 %53)
%55 = load i32, i32* %8
%56 = call i32 @close(i32 %55)
%57 = load i32, i32* @EXIT_SUCCESS
store i32 %57, i32* %3
br label %58
58: ; preds = %52, %45, %35, %24
%59 = load i32, i32* %3
ret i32 %59
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@.str = [5 x i8] c"open\00"
@EXIT_FAILURE = global i32 0
@LOOP_CHANGE_FD = global i32 0
@.str.1 = [6 x i8] c"ioctl\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 3
br i1 %3, label %7, label %4
4: ; preds = %2
%5 = load i8*, i8** %1
%6 = tail call i32 @usage(i8* %5)
br label %7
7: ; preds = %4, %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = load i32, i32* @O_RDWR
%11 = tail call i32 @open(i8* %9, i32 %10)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %7
%14 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %34
15: ; preds = %7
%16 = getelementptr inbounds i8*, i8** %1, i64 2
%17 = load i8*, i8** %16
%18 = load i32, i32* @O_RDWR
%19 = tail call i32 @open(i8* %17, i32 %18)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %23
21: ; preds = %15
%22 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %34
23: ; preds = %15
%24 = load i32, i32* @LOOP_CHANGE_FD
%25 = tail call i32 @ioctl(i32 %11, i32 %24, i32 %19)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %31
27: ; preds = %23
%28 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%29 = tail call i32 @close(i32 %11)
%30 = tail call i32 @close(i32 %19)
br label %34
31: ; preds = %23
%32 = tail call i32 @close(i32 %11)
%33 = tail call i32 @close(i32 %19)
br label %34
34: ; preds = %31, %27, %21, %13
%35 = phi i32* [ @EXIT_FAILURE, %13 ], [ @EXIT_FAILURE, %21 ], [ @EXIT_FAILURE, %27 ], [ @EXIT_SUCCESS, %31 ]
%36 = load i32, i32* %35
ret i32 %36
}