#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 */
struct ifconf {
  void *ifc_buf;
  int /*<<< orphan*/ ifc_len;
};

/* Variables and functions */
int SIOCGIFCONF;
int /*<<< orphan*/ free(void *);
int ioctl(int, int, void *);
void *malloc(int /*<<< orphan*/);
int /*<<< orphan*/ um_mod_umoven(long, int /*<<< orphan*/, void *);
int /*<<< orphan*/ um_mod_ustoren(long, int /*<<< orphan*/, void *);
int myioctl(int d, int request, void *arg) {
  if (request == SIOCGIFCONF) {
    int rv;
    void *save;
    struct ifconf *ifc = (struct ifconf *)arg;
    save = ifc->ifc_buf;
    ioctl(d, request, arg);
    ifc->ifc_buf = malloc(ifc->ifc_len);
    um_mod_umoven((long)save, ifc->ifc_len, ifc->ifc_buf);
    rv = ioctl(d, request, arg);
    if (rv >= 0)
      um_mod_ustoren((long)save, ifc->ifc_len, ifc->ifc_buf);
    free(ifc->ifc_buf);
    ifc->ifc_buf = save;
    return rv;
  }
  return ioctl(d, request, arg);
}


x86 assembly (clang, -O0):
<myioctl>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0xc(%rbp),%eax
mov    0x2e74(%rip),%rcx
cmp    (%rcx),%eax
jne    1218 <myioctl+0xc8>
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
mov    -0x18(%rbp),%rdx
call   1050 <ioctl@plt>
mov    -0x30(%rbp),%rax
mov    0x8(%rax),%edi
call   1070 <malloc@plt>
mov    %rax,%rcx
mov    -0x30(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rax
mov    0x8(%rax),%esi
mov    -0x30(%rbp),%rax
mov    (%rax),%rdx
call   1040 <um_mod_umoven@plt>
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
mov    -0x18(%rbp),%rdx
call   1050 <ioctl@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jl     11f6 <myioctl+0xa6>
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rax
mov    0x8(%rax),%esi
mov    -0x30(%rbp),%rax
mov    (%rax),%rdx
call   1060 <um_mod_ustoren@plt>
mov    -0x30(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rcx
mov    -0x30(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    122a <myioctl+0xda>
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
mov    -0x18(%rbp),%rdx
call   1050 <ioctl@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<myioctl>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%rbx
mov    %esi,%ebp
mov    %edi,%r15d
mov    0x2e79(%rip),%rax
cmp    %esi,(%rax)
jne    11cf <myioctl+0x7f>
mov    (%rbx),%r14
mov    %r15d,%edi
mov    %ebp,%esi
mov    %rbx,%rdx
call   1050 <ioctl@plt>
mov    0x8(%rbx),%r12d
mov    %r12d,%edi
call   1070 <malloc@plt>
mov    %rax,(%rbx)
mov    %r14,%rdi
mov    %r12d,%esi
mov    %rax,%rdx
call   1040 <um_mod_umoven@plt>
mov    %r15d,%edi
mov    %ebp,%esi
mov    %rbx,%rdx
call   1050 <ioctl@plt>
mov    %eax,%ebp
test   %eax,%eax
js     11b9 <myioctl+0x69>
mov    0x8(%rbx),%esi
mov    (%rbx),%rdx
mov    %r14,%rdi
call   1060 <um_mod_ustoren@plt>
mov    (%rbx),%rdi
call   1030 <free@plt>
mov    %r14,(%rbx)
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r15d,%edi
mov    %ebp,%esi
mov    %rbx,%rdx
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <ioctl@plt>

LLVM IR (-O0):
%struct.ifconf = type { i8*, i32 }
@SIOCGIFCONF = global i32 0
define i32 @myioctl(i32 %0, i32 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i8*
%10 = alloca %struct.ifconf*
store i32 %0, i32* %5
store i32 %1, i32* %6
store i8* %2, i8** %7
%11 = load i32, i32* %6
%12 = load i32, i32* @SIOCGIFCONF
%13 = icmp eq i32 %11, %12
br i1 %13, label %14, label %64
14: ; preds = %3
%15 = load i8*, i8** %7
%16 = bitcast i8* %15 to %struct.ifconf*
store %struct.ifconf* %16, %struct.ifconf** %10
%17 = load %struct.ifconf*, %struct.ifconf** %10
%18 = getelementptr inbounds %struct.ifconf, %struct.ifconf* %17, i32 0, i32 0
%19 = load i8*, i8** %18
store i8* %19, i8** %9
%20 = load i32, i32* %5
%21 = load i32, i32* %6
%22 = load i8*, i8** %7
%23 = call i32 @ioctl(i32 %20, i32 %21, i8* %22)
%24 = load %struct.ifconf*, %struct.ifconf** %10
%25 = getelementptr inbounds %struct.ifconf, %struct.ifconf* %24, i32 0, i32 1
%26 = load i32, i32* %25
%27 = call i8* @malloc(i32 %26)
%28 = load %struct.ifconf*, %struct.ifconf** %10
%29 = getelementptr inbounds %struct.ifconf, %struct.ifconf* %28, i32 0, i32 0
store i8* %27, i8** %29
%30 = load i8*, i8** %9
%31 = ptrtoint i8* %30 to i64
%32 = load %struct.ifconf*, %struct.ifconf** %10
%33 = getelementptr inbounds %struct.ifconf, %struct.ifconf* %32, i32 0, i32 1
%34 = load i32, i32* %33
%35 = load %struct.ifconf*, %struct.ifconf** %10
%36 = getelementptr inbounds %struct.ifconf, %struct.ifconf* %35, i32 0, i32 0
%37 = load i8*, i8** %36
%38 = call i32 @um_mod_umoven(i64 %31, i32 %34, i8* %37)
%39 = load i32, i32* %5
%40 = load i32, i32* %6
%41 = load i8*, i8** %7
%42 = call i32 @ioctl(i32 %39, i32 %40, i8* %41)
store i32 %42, i32* %8
%43 = load i32, i32* %8
%44 = icmp sge i32 %43, 0
br i1 %44, label %45, label %55
45: ; preds = %14
%46 = load i8*, i8** %9
%47 = ptrtoint i8* %46 to i64
%48 = load %struct.ifconf*, %struct.ifconf** %10
%49 = getelementptr inbounds %struct.ifconf, %struct.ifconf* %48, i32 0, i32 1
%50 = load i32, i32* %49
%51 = load %struct.ifconf*, %struct.ifconf** %10
%52 = getelementptr inbounds %struct.ifconf, %struct.ifconf* %51, i32 0, i32 0
%53 = load i8*, i8** %52
%54 = call i32 @um_mod_ustoren(i64 %47, i32 %50, i8* %53)
br label %55
55: ; preds = %45, %14
%56 = load %struct.ifconf*, %struct.ifconf** %10
%57 = getelementptr inbounds %struct.ifconf, %struct.ifconf* %56, i32 0, i32 0
%58 = load i8*, i8** %57
%59 = call i32 @free(i8* %58)
%60 = load i8*, i8** %9
%61 = load %struct.ifconf*, %struct.ifconf** %10
%62 = getelementptr inbounds %struct.ifconf, %struct.ifconf* %61, i32 0, i32 0
store i8* %60, i8** %62
%63 = load i32, i32* %8
store i32 %63, i32* %4
br label %69
64: ; preds = %3
%65 = load i32, i32* %5
%66 = load i32, i32* %6
%67 = load i8*, i8** %7
%68 = call i32 @ioctl(i32 %65, i32 %66, i8* %67)
store i32 %68, i32* %4
br label %69
69: ; preds = %64, %55
%70 = load i32, i32* %4
ret i32 %70
}

LLVM IR (-O2):
@SIOCGIFCONF = global i32 0
define i32 @myioctl(i32 %0, i32 %1, i8* %2) {
%4 = load i32, i32* @SIOCGIFCONF
%5 = icmp eq i32 %4, %1
br i1 %5, label %6, label %25
6: ; preds = %3
%7 = bitcast i8* %2 to i8**
%8 = load i8*, i8** %7
%9 = tail call i32 @ioctl(i32 %0, i32 %1, i8* %2)
%10 = getelementptr inbounds i8, i8* %2, i64 8
%11 = bitcast i8* %10 to i32*
%12 = load i32, i32* %11
%13 = tail call i8* @malloc(i32 %12)
store i8* %13, i8** %7
%14 = ptrtoint i8* %8 to i64
%15 = tail call i32 @um_mod_umoven(i64 %14, i32 %12, i8* %13)
%16 = tail call i32 @ioctl(i32 %0, i32 %1, i8* %2)
%17 = icmp sgt i32 %16, -1
br i1 %17, label %18, label %22
18: ; preds = %6
%19 = load i32, i32* %11
%20 = load i8*, i8** %7
%21 = tail call i32 @um_mod_ustoren(i64 %14, i32 %19, i8* %20)
br label %22
22: ; preds = %18, %6
%23 = load i8*, i8** %7
%24 = tail call i32 @free(i8* %23)
store i8* %8, i8** %7
br label %27
25: ; preds = %3
%26 = tail call i32 @ioctl(i32 %0, i32 %1, i8* %2)
br label %27
27: ; preds = %25, %22
%28 = phi i32 [ %16, %22 ], [ %26, %25 ]
ret i32 %28
}