#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 scalar_t__ pid_t;
typedef int /*<<< orphan*/ ifindex;

/* Variables and functions */
int IF_NAMESIZE;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
scalar_t__ if_indextoname(int, char *);
int peer_ifindex(char *);
int /*<<< orphan*/ pipe(int *);
int /*<<< orphan*/ printf(char *, int, char *);
scalar_t__ read(int, int *, int);
scalar_t__ set_network_ns(char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ write(int, int *, int);
int main(int argc, char **argv) {
  int fd[2], ifindex;
  pid_t child;
  pipe(fd);
  child = fork();
  if (child < 0) {
    fprintf(stderr, "error: cannot fork\n");
    exit(1);
  }
  if (child == 0) {
    close(fd[0]);
    if (set_network_ns(argv[1]) < 0) {
      fprintf(stderr, "error: cannot set network namespace\n");
      exit(1);
    }
    ifindex = peer_ifindex(argv[2]);
    if (ifindex < 0) {
      fprintf(stderr, "error: cannot find peer ifindex\n");
      exit(1);
    }
    write(fd[1], &ifindex, sizeof(ifindex));
    exit(0);
  } else {
    close(fd[1]);
    if (read(fd[0], &ifindex, sizeof(ifindex)) <= 0) {
      fprintf(stderr, "error: cannot read from child\n");
      exit(1);
    }
    char ifname[IF_NAMESIZE];
    if (if_indextoname(ifindex, ifname) < 0) {
      fprintf(stderr, "error: cannot find name of iface %d\n", ifindex);
      exit(1);
    }
    printf("%d %s", ifindex, ifname);
    exit(0);
  }
}


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)
lea    -0x18(%rbp),%rdi
call   1060 <pipe@plt>
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jge    1206 <main+0x56>
mov    0x2dec(%rip),%rax
mov    (%rax),%edi
lea    0xe0b(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
cmpq   $0x0,-0x28(%rbp)
jne    12a4 <main+0xf4>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10b0 <set_network_ns@plt>
cmp    $0x0,%rax
jge    1251 <main+0xa1>
mov    0x2da1(%rip),%rax
mov    (%rax),%edi
lea    0xdd4(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1090 <peer_ifindex@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    128c <main+0xdc>
mov    0x2d66(%rip),%rax
mov    (%rax),%edi
lea    0xdbe(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x14(%rbp),%edi
lea    -0x1c(%rbp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
xor    %edi,%edi
call   10c0 <exit@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%edi
lea    -0x1c(%rbp),%rsi
mov    $0x4,%edx
call   1070 <read@plt>
cmp    $0x0,%rax
jg     12e8 <main+0x138>
mov    0x2d0a(%rip),%rax
mov    (%rax),%edi
lea    0xd83(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    0x2cf9(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rsi
sub    %rcx,%rsi
mov    %rsi,-0x40(%rbp)
mov    %rsi,%rsp
mov    %rax,-0x38(%rbp)
mov    -0x1c(%rbp),%edi
call   10a0 <if_indextoname@plt>
cmp    $0x0,%rax
jge    134a <main+0x19a>
mov    0x2cab(%rip),%rax
mov    (%rax),%edi
mov    -0x1c(%rbp),%edx
lea    0xd40(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x40(%rbp),%rdx
mov    -0x1c(%rbp),%esi
lea    0xd46(%rip),%rdi
call   1040 <printf@plt>
xor    %edi,%edi
call   10c0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
lea    -0x1c(%rbp),%rdi
call   1060 <pipe@plt>
xor    %eax,%eax
call   10d0 <fork@plt>
test   %rax,%rax
jns    11e5 <main+0x35>
mov    0x2dfe(%rip),%rax
mov    (%rax),%edi
lea    0xe1d(%rip),%rsi
jmp    125f <main+0xaf>
jne    120f <main+0x5f>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
mov    0x8(%rbx),%rdi
call   10b0 <set_network_ns@plt>
test   %rax,%rax
jns    123f <main+0x8f>
mov    0x2dd4(%rip),%rax
mov    (%rax),%edi
lea    0xe07(%rip),%rsi
jmp    125f <main+0xaf>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
mov    -0x1c(%rbp),%edi
lea    -0x14(%rbp),%rsi
mov    $0x4,%edx
call   1070 <read@plt>
test   %rax,%rax
jg     1270 <main+0xc0>
mov    0x2da4(%rip),%rax
mov    (%rax),%edi
lea    0xe1d(%rip),%rsi
jmp    125f <main+0xaf>
mov    0x10(%rbx),%rdi
call   1090 <peer_ifindex@plt>
mov    %eax,-0x14(%rbp)
test   %eax,%eax
jns    12c2 <main+0x112>
mov    0x2d82(%rip),%rax
mov    (%rax),%edi
lea    0xdda(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    0x2d71(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    -0x14(%rbp),%r14d
mov    %r14d,%edi
mov    %rbx,%rsi
call   10a0 <if_indextoname@plt>
test   %rax,%rax
jns    12da <main+0x12a>
mov    0x2d33(%rip),%rax
mov    (%rax),%edi
lea    0xdcb(%rip),%rsi
mov    %r14d,%edx
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x18(%rbp),%edi
lea    -0x14(%rbp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
xor    %edi,%edi
call   10c0 <exit@plt>
lea    0xdbd(%rip),%rdi
mov    %r14d,%esi
mov    %rbx,%rdx
call   1040 <printf@plt>
xor    %edi,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [20 x i8] c"error: cannot fork\0A\00"
@.str.1 = [37 x i8] c"error: cannot set network namespace\0A\00"
@.str.2 = [33 x i8] c"error: cannot find peer ifindex\0A\00"
@.str.3 = [31 x i8] c"error: cannot read from child\0A\00"
@IF_NAMESIZE = global i32 0
@.str.4 = [37 x i8] c"error: cannot find name of iface %d\0A\00"
@.str.5 = [6 x i8] c"%d %s\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [2 x i32]
%7 = alloca i32
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%12 = call i32 @pipe(i32* %11)
%13 = call i64 (...) @fork()
store i64 %13, i64* %8
%14 = load i64, i64* %8
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %20
16: ; preds = %2
%17 = load i32, i32* @stderr
%18 = call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %2
%21 = load i64, i64* %8
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %52
23: ; preds = %20
%24 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%25 = load i32, i32* %24
%26 = call i32 @close(i32 %25)
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 1
%29 = load i8*, i8** %28
%30 = call i64 @set_network_ns(i8* %29)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %36
32: ; preds = %23
%33 = load i32, i32* @stderr
%34 = call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0))
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %23
%37 = load i8**, i8*** %5
%38 = getelementptr inbounds i8*, i8** %37, i64 2
%39 = load i8*, i8** %38
%40 = call i32 @peer_ifindex(i8* %39)
store i32 %40, i32* %7
%41 = load i32, i32* %7
%42 = icmp slt i32 %41, 0
br i1 %42, label %43, label %47
43: ; preds = %36
%44 = load i32, i32* @stderr
%45 = call i32 (i32, i8*, ...) @fprintf(i32 %44, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0))
%46 = call i32 @exit(i32 1)
unreachable
47: ; preds = %36
%48 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%49 = load i32, i32* %48
%50 = call i32 @write(i32 %49, i32* %7, i32 4)
%51 = call i32 @exit(i32 0)
unreachable
52: ; preds = %20
%53 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%54 = load i32, i32* %53
%55 = call i32 @close(i32 %54)
%56 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%57 = load i32, i32* %56
%58 = call i64 @read(i32 %57, i32* %7, i32 4)
%59 = icmp sle i64 %58, 0
br i1 %59, label %60, label %64
60: ; preds = %52
%61 = load i32, i32* @stderr
%62 = call i32 (i32, i8*, ...) @fprintf(i32 %61, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0))
%63 = call i32 @exit(i32 1)
unreachable
64: ; preds = %52
%65 = load i32, i32* @IF_NAMESIZE
%66 = zext i32 %65 to i64
%67 = call i8* @llvm.stacksave()
store i8* %67, i8** %9
%68 = alloca i8, i64 %66
store i64 %66, i64* %10
%69 = load i32, i32* %7
%70 = call i64 @if_indextoname(i32 %69, i8* %68)
%71 = icmp slt i64 %70, 0
br i1 %71, label %72, label %77
72: ; preds = %64
%73 = load i32, i32* @stderr
%74 = load i32, i32* %7
%75 = call i32 (i32, i8*, ...) @fprintf(i32 %73, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0), i32 %74)
%76 = call i32 @exit(i32 1)
unreachable
77: ; preds = %64
%78 = load i32, i32* %7
%79 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i32 %78, i8* %68)
%80 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [20 x i8] c"error: cannot fork\0A\00"
@.str.1 = [37 x i8] c"error: cannot set network namespace\0A\00"
@.str.2 = [33 x i8] c"error: cannot find peer ifindex\0A\00"
@.str.3 = [31 x i8] c"error: cannot read from child\0A\00"
@IF_NAMESIZE = global i32 0
@.str.4 = [37 x i8] c"error: cannot find name of iface %d\0A\00"
@.str.5 = [6 x i8] c"%d %s\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [2 x i32]
%4 = alloca i32
%5 = bitcast [2 x i32]* %3 to i8*
%6 = bitcast i32* %4 to i8*
%7 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%8 = call i32 @pipe(i32* %7)
%9 = call i64 (...) @fork()
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %15
11: ; preds = %2
%12 = load i32, i32* @stderr
%13 = call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %2
%16 = icmp eq i64 %9, 0
br i1 %16, label %17, label %42
17: ; preds = %15
%18 = load i32, i32* %7
%19 = call i32 @close(i32 %18)
%20 = getelementptr inbounds i8*, i8** %1, i64 1
%21 = load i8*, i8** %20
%22 = call i64 @set_network_ns(i8* %21)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %28
24: ; preds = %17
%25 = load i32, i32* @stderr
%26 = call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0))
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %17
%29 = getelementptr inbounds i8*, i8** %1, i64 2
%30 = load i8*, i8** %29
%31 = call i32 @peer_ifindex(i8* %30)
store i32 %31, i32* %4
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %37
33: ; preds = %28
%34 = load i32, i32* @stderr
%35 = call i32 (i32, i8*, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0))
%36 = call i32 @exit(i32 1)
unreachable
37: ; preds = %28
%38 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%39 = load i32, i32* %38
%40 = call i32 @write(i32 %39, i32* %4, i32 4)
%41 = call i32 @exit(i32 0)
unreachable
42: ; preds = %15
%43 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%44 = load i32, i32* %43
%45 = call i32 @close(i32 %44)
%46 = load i32, i32* %7
%47 = call i64 @read(i32 %46, i32* %4, i32 4)
%48 = icmp slt i64 %47, 1
br i1 %48, label %49, label %53
49: ; preds = %42
%50 = load i32, i32* @stderr
%51 = call i32 (i32, i8*, ...) @fprintf(i32 %50, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0))
%52 = call i32 @exit(i32 1)
unreachable
53: ; preds = %42
%54 = load i32, i32* @IF_NAMESIZE
%55 = zext i32 %54 to i64
%56 = alloca i8, i64 %55
%57 = load i32, i32* %4
%58 = call i64 @if_indextoname(i32 %57, i8* %56)
%59 = icmp slt i64 %58, 0
br i1 %59, label %60, label %64
60: ; preds = %53
%61 = load i32, i32* @stderr
%62 = call i32 (i32, i8*, ...) @fprintf(i32 %61, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0), i32 %57)
%63 = call i32 @exit(i32 1)
unreachable
64: ; preds = %53
%65 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i32 %57, i8* %56)
%66 = call i32 @exit(i32 0)
unreachable
}