#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 bind2dev(int, char const *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
scalar_t__ hookBuf(char *, int);
scalar_t__ malloc(int);
int openSocket();
int /*<<< orphan*/ parseCap();
int readCap(int, char *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ unHookBuf();
int main(int argc, char *argv[]) {
  int sockFd;
  int ret = 0;
  char *pBuf;
  const char *dev = "wlan0";
  sockFd = openSocket();
  if (sockFd < 0) {
    ret = -1;
    goto bad;
  }
  int n;
  if ((n = bind2dev(sockFd, dev)) != 0) {
    ret = n;
    goto bad;
  }
  pBuf = (char *)malloc(1 << 20);
  int cnt = 0;
  while (cnt < 10) {
    int len = readCap(sockFd, pBuf, 1 << 20);
    fprintf(stdout, "read len %d\r\n", len);
    if (hookBuf(pBuf, len) != 0) {
      fprintf(stderr, "hookBuf failed.\r\n");
      goto bad;
    }
    parseCap();
    fprintf(stdout, "\r\n");
    unHookBuf();
    cnt++;
  }
bad:
  return ret;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x18(%rbp)
lea    0xe5c(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    $0x0,%al
call   1040 <openSocket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11c8 <main+0x48>
movl   $0xffffffff,-0x18(%rbp)
jmp    12a2 <main+0x122>
mov    -0x14(%rbp),%edi
mov    -0x28(%rbp),%rsi
call   1090 <bind2dev@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
je     11eb <main+0x6b>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    12a2 <main+0x122>
mov    $0x100000,%edi
call   1080 <malloc@plt>
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x30(%rbp)
cmpl   $0xa,-0x30(%rbp)
jge    129d <main+0x11d>
mov    -0x14(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    $0x100000,%edx
call   10a0 <readCap@plt>
mov    %eax,-0x34(%rbp)
mov    0x2db3(%rip),%rax
mov    (%rax),%edi
mov    -0x34(%rbp),%edx
lea    0xdd5(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x20(%rbp),%rdi
mov    -0x34(%rbp),%esi
call   1070 <hookBuf@plt>
cmp    $0x0,%rax
je     126a <main+0xea>
mov    0x2d8b(%rip),%rax
mov    (%rax),%edi
lea    0xdb6(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
jmp    12a2 <main+0x122>
mov    $0x0,%al
call   1030 <parseCap@plt>
mov    0x2d60(%rip),%rax
mov    (%rax),%edi
lea    0xda2(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x0,%al
call   1050 <unHookBuf@plt>
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    1200 <main+0x80>
jmp    12a2 <main+0x122>
mov    -0x18(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
xor    %eax,%eax
call   1040 <openSocket@plt>
test   %eax,%eax
js     1238 <main+0xb8>
mov    %eax,%r12d
lea    0xe5c(%rip),%rsi
mov    %eax,%edi
call   1090 <bind2dev@plt>
mov    %eax,%ebp
test   %eax,%eax
jne    1258 <main+0xd8>
mov    $0x100000,%edi
call   1080 <malloc@plt>
mov    %rax,%r13
mov    $0xa,%ebx
mov    0x2e0a(%rip),%r15
lea    0xe31(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r12d,%edi
mov    %r13,%rsi
mov    $0x100000,%edx
call   10a0 <readCap@plt>
mov    %eax,%ebp
mov    (%r15),%edi
mov    %r14,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %r13,%rdi
mov    %ebp,%esi
call   1070 <hookBuf@plt>
test   %rax,%rax
jne    123f <main+0xbf>
xor    %ebp,%ebp
xor    %eax,%eax
call   1030 <parseCap@plt>
mov    (%r15),%edi
lea    0xe00(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
xor    %eax,%eax
call   1050 <unHookBuf@plt>
add    $0xffffffff,%ebx
jne    11e0 <main+0x60>
jmp    1258 <main+0xd8>
mov    $0xffffffff,%ebp
jmp    1258 <main+0xd8>
mov    0x2d9a(%rip),%rax
mov    (%rax),%edi
lea    0xdc5(%rip),%rsi
xor    %ebp,%ebp
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [6 x i8] c"wlan0\00"
@stdout = global i32 0
@.str.1 = [14 x i8] c"read len %d\0D\0A\00"
@stderr = global i32 0
@.str.2 = [18 x i8] c"hookBuf failed.\0D\0A\00"
@.str.3 = [3 x i8] c"\0D\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %7
store i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8** %9
%13 = call i32 (...) @openSocket()
store i32 %13, i32* %6
%14 = load i32, i32* %6
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %2
store i32 -1, i32* %7
br label %52
17: ; preds = %2
%18 = load i32, i32* %6
%19 = load i8*, i8** %9
%20 = call i32 @bind2dev(i32 %18, i8* %19)
store i32 %20, i32* %10
%21 = icmp ne i32 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %17
%23 = load i32, i32* %10
store i32 %23, i32* %7
br label %52
24: ; preds = %17
%25 = call i64 @malloc(i32 1048576)
%26 = inttoptr i64 %25 to i8*
store i8* %26, i8** %8
store i32 0, i32* %11
br label %27
27: ; preds = %44, %24
%28 = load i32, i32* %11
%29 = icmp slt i32 %28, 10
br i1 %29, label %30, label %51
30: ; preds = %27
%31 = load i32, i32* %6
%32 = load i8*, i8** %8
%33 = call i32 @readCap(i32 %31, i8* %32, i32 1048576)
store i32 %33, i32* %12
%34 = load i32, i32* @stdout
%35 = load i32, i32* %12
%36 = call i32 (i32, i8*, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %35)
%37 = load i8*, i8** %8
%38 = load i32, i32* %12
%39 = call i64 @hookBuf(i8* %37, i32 %38)
%40 = icmp ne i64 %39, 0
br i1 %40, label %41, label %44
41: ; preds = %30
%42 = load i32, i32* @stderr
%43 = call i32 (i32, i8*, ...) @fprintf(i32 %42, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
br label %52
44: ; preds = %30
%45 = call i32 (...) @parseCap()
%46 = load i32, i32* @stdout
%47 = call i32 (i32, i8*, ...) @fprintf(i32 %46, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%48 = call i32 (...) @unHookBuf()
%49 = load i32, i32* %11
%50 = add nsw i32 %49, 1
store i32 %50, i32* %11
br label %27
51: ; preds = %27
br label %52
52: ; preds = %51, %41, %22, %16
%53 = load i32, i32* %7
ret i32 %53
}

LLVM IR (-O2):
@.str = [6 x i8] c"wlan0\00"
@stdout = global i32 0
@.str.1 = [14 x i8] c"read len %d\0D\0A\00"
@stderr = global i32 0
@.str.2 = [18 x i8] c"hookBuf failed.\0D\0A\00"
@.str.3 = [3 x i8] c"\0D\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 (...) @openSocket()
%4 = icmp slt i32 %3, 0
br i1 %4, label %28, label %5
5: ; preds = %2
%6 = tail call i32 @bind2dev(i32 %3, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %28
8: ; preds = %5
%9 = tail call i64 @malloc(i32 1048576)
%10 = inttoptr i64 %9 to i8*
br label %11
11: ; preds = %8, %21
%12 = phi i32 [ 0, %8 ], [ %26, %21 ]
%13 = tail call i32 @readCap(i32 %3, i8* %10, i32 1048576)
%14 = load i32, i32* @stdout
%15 = tail call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %13)
%16 = tail call i64 @hookBuf(i8* %10, i32 %13)
%17 = icmp eq i64 %16, 0
br i1 %17, label %21, label %18
18: ; preds = %11
%19 = load i32, i32* @stderr
%20 = tail call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
br label %28
21: ; preds = %11
%22 = tail call i32 (...) @parseCap()
%23 = load i32, i32* @stdout
%24 = tail call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%25 = tail call i32 (...) @unHookBuf()
%26 = add nuw nsw i32 %12, 1
%27 = icmp eq i32 %26, 10
br i1 %27, label %28, label %11
28: ; preds = %21, %18, %2, %5
%29 = phi i32 [ -1, %2 ], [ %6, %5 ], [ 0, %18 ], [ 0, %21 ]
ret i32 %29
}