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

/* Variables and functions */
int /*<<< orphan*/ AT_RANDOM;
int EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ getauxval(int /*<<< orphan*/);
int /*<<< orphan*/ memcpy(char *, void *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ umask(int);
int /*<<< orphan*/ writelv(int /*<<< orphan*/ *, int, char *);
int /*<<< orphan*/ writestrlv(int /*<<< orphan*/ *, char const *);
int /*<<< orphan*/ writeu16(int /*<<< orphan*/ *, int);
int main(int argc, char *argv[]) {
  static uint16_t family = 0x100;
  static const char address[] = "localhost";
  static const char number[] = "0";
  static const char name[] = "MIT-MAGIC-COOKIE-1";
  char cookie[16];
  umask(0022);
  void *at_random = (void *)(uintptr_t)getauxval(AT_RANDOM);
  if (!at_random) {
    uintptr_t addr = (uintptr_t)&main;
    memcpy(cookie, &addr, sizeof(addr));
    do {
      (void)(sizeof(char[1 - 2 * !(sizeof(addr) <= sizeof(cookie))]));
    } while (0);
  } else {
    memcpy(cookie, at_random, 16);
    do {
      (void)(sizeof(char[1 - 2 * !(16 <= sizeof(cookie))]));
    } while (0);
  }
  FILE *fp = fopen(argv[1], "wb");
  if (fp == NULL || !writeu16(fp, family) || !writestrlv(fp, address) ||
      !writestrlv(fp, number) || !writestrlv(fp, name) ||
      !writelv(fp, sizeof(cookie), cookie)) {
    perror(argv[0]);
    return EXIT_FAILURE;
  }
  return EXIT_SUCCESS;
}


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)
mov    $0x12,%edi
call   1040 <umask@plt>
mov    0x2e31(%rip),%rax
mov    (%rax),%edi
call   1080 <getauxval@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11e4 <main+0x64>
mov    0x2e24(%rip),%rax
mov    %rax,-0x30(%rbp)
lea    -0x20(%rbp),%rdi
lea    -0x30(%rbp),%rsi
mov    $0x8,%edx
call   1050 <memcpy@plt>
jmp    11df <main+0x5f>
jmp    1200 <main+0x80>
lea    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    $0x10,%edx
call   1050 <memcpy@plt>
jmp    11fb <main+0x7b>
jmp    1200 <main+0x80>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe14(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     12a1 <main+0x121>
mov    -0x38(%rbp),%rdi
mov    0x2e33(%rip),%esi
call   1060 <writeu16@plt>
cmp    $0x0,%eax
je     12a1 <main+0x121>
mov    -0x38(%rbp),%rdi
lea    0xdba(%rip),%rsi
call   1070 <writestrlv@plt>
cmp    $0x0,%eax
je     12a1 <main+0x121>
mov    -0x38(%rbp),%rdi
lea    0xdab(%rip),%rsi
call   1070 <writestrlv@plt>
cmp    $0x0,%eax
je     12a1 <main+0x121>
mov    -0x38(%rbp),%rdi
lea    0xd98(%rip),%rsi
call   1070 <writestrlv@plt>
cmp    $0x0,%eax
je     12a1 <main+0x121>
mov    -0x38(%rbp),%rdi
lea    -0x20(%rbp),%rdx
mov    $0x10,%esi
call   1030 <writelv@plt>
cmp    $0x0,%eax
jne    12be <main+0x13e>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   10a0 <perror@plt>
mov    0x2d1c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12ca <main+0x14a>
mov    0x2d03(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%r14
mov    $0x12,%edi
call   1040 <umask@plt>
mov    0x2e3d(%rip),%rax
mov    (%rax),%edi
call   1080 <getauxval@plt>
test   %rax,%rax
je     11b6 <main+0x36>
lea    0x10(%rsp),%rdi
mov    %rax,%rsi
mov    $0x10,%edx
jmp    11d1 <main+0x51>
mov    0x2e2b(%rip),%rax
mov    %rax,0x8(%rsp)
lea    0x10(%rsp),%rdi
lea    0x8(%rsp),%rsi
mov    $0x8,%edx
call   1050 <memcpy@plt>
mov    0x8(%r14),%rdi
lea    0xe42(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     1257 <main+0xd7>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0x100,%esi
call   1060 <writeu16@plt>
test   %eax,%eax
je     1257 <main+0xd7>
lea    0xdfa(%rip),%rsi
mov    %rbx,%rdi
call   1070 <writestrlv@plt>
test   %eax,%eax
je     1257 <main+0xd7>
lea    0xdf1(%rip),%rsi
mov    %rbx,%rdi
call   1070 <writestrlv@plt>
test   %eax,%eax
je     1257 <main+0xd7>
lea    0xde4(%rip),%rsi
mov    %rbx,%rdi
call   1070 <writestrlv@plt>
test   %eax,%eax
je     1257 <main+0xd7>
lea    0x10(%rsp),%rdx
mov    %rbx,%rdi
mov    $0x10,%esi
call   1030 <writelv@plt>
test   %eax,%eax
je     1257 <main+0xd7>
mov    0x2d73(%rip),%rax
jmp    1266 <main+0xe6>
mov    (%r14),%rdi
call   10a0 <perror@plt>
mov    0x2d6a(%rip),%rax
mov    (%rax),%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@main.family = internal global i32 256
@main.address = internal constant [10 x i8] c"localhost\00"
@main.number = internal constant [2 x i8] c"0\00"
@main.name = internal constant [19 x i8] c"MIT-MAGIC-COOKIE-1\00"
@AT_RANDOM = global i32 0
@.str = [3 x i8] c"wb\00"
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [16 x i8]
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = call i32 @umask(i32 18)
%11 = load i32, i32* @AT_RANDOM
%12 = call i64 @getauxval(i32 %11)
%13 = inttoptr i64 %12 to i8*
store i8* %13, i8** %7
%14 = load i8*, i8** %7
%15 = icmp ne i8* %14, null
br i1 %15, label %22, label %16
16: ; preds = %2
store i64 ptrtoint (i32 (i32, i8**)* @main to i64), i64* %8
%17 = getelementptr inbounds [16 x i8], [16 x i8]* %6, i64 0, i64 0
%18 = bitcast i64* %8 to i8*
%19 = call i32 @memcpy(i8* %17, i8* %18, i32 8)
br label %20
20: ; preds = %16
br label %21
21: ; preds = %20
br label %28
22: ; preds = %2
%23 = getelementptr inbounds [16 x i8], [16 x i8]* %6, i64 0, i64 0
%24 = load i8*, i8** %7
%25 = call i32 @memcpy(i8* %23, i8* %24, i32 16)
br label %26
26: ; preds = %22
br label %27
27: ; preds = %26
br label %28
28: ; preds = %27, %21
%29 = load i8**, i8*** %5
%30 = getelementptr inbounds i8*, i8** %29, i64 1
%31 = load i8*, i8** %30
%32 = call i32* @fopen(i8* %31, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %32, i32** %9
%33 = load i32*, i32** %9
%34 = icmp eq i32* %33, null
br i1 %34, label %57, label %35
35: ; preds = %28
%36 = load i32*, i32** %9
%37 = load i32, i32* @main.family
%38 = call i32 @writeu16(i32* %36, i32 %37)
%39 = icmp ne i32 %38, 0
br i1 %39, label %40, label %57
40: ; preds = %35
%41 = load i32*, i32** %9
%42 = call i32 @writestrlv(i32* %41, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @main.address, i64 0, i64 0))
%43 = icmp ne i32 %42, 0
br i1 %43, label %44, label %57
44: ; preds = %40
%45 = load i32*, i32** %9
%46 = call i32 @writestrlv(i32* %45, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @main.number, i64 0, i64 0))
%47 = icmp ne i32 %46, 0
br i1 %47, label %48, label %57
48: ; preds = %44
%49 = load i32*, i32** %9
%50 = call i32 @writestrlv(i32* %49, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @main.name, i64 0, i64 0))
%51 = icmp ne i32 %50, 0
br i1 %51, label %52, label %57
52: ; preds = %48
%53 = load i32*, i32** %9
%54 = getelementptr inbounds [16 x i8], [16 x i8]* %6, i64 0, i64 0
%55 = call i32 @writelv(i32* %53, i32 16, i8* %54)
%56 = icmp ne i32 %55, 0
br i1 %56, label %63, label %57
57: ; preds = %52, %48, %44, %40, %35, %28
%58 = load i8**, i8*** %5
%59 = getelementptr inbounds i8*, i8** %58, i64 0
%60 = load i8*, i8** %59
%61 = call i32 @perror(i8* %60)
%62 = load i32, i32* @EXIT_FAILURE
store i32 %62, i32* %3
br label %65
63: ; preds = %52
%64 = load i32, i32* @EXIT_SUCCESS
store i32 %64, i32* %3
br label %65
65: ; preds = %63, %57
%66 = load i32, i32* %3
ret i32 %66
}

LLVM IR (-O2):
@main.address = internal constant [10 x i8] c"localhost\00"
@main.number = internal constant [2 x i8] c"0\00"
@main.name = internal constant [19 x i8] c"MIT-MAGIC-COOKIE-1\00"
@AT_RANDOM = global i32 0
@.str = [3 x i8] c"wb\00"
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [16 x i8]
%4 = alloca i64
%5 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 0
%6 = tail call i32 @umask(i32 18)
%7 = load i32, i32* @AT_RANDOM
%8 = tail call i64 @getauxval(i32 %7)
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = bitcast i64* %4 to i8*
store i64 ptrtoint (i32 (i32, i8**)* @main to i64), i64* %4
%12 = call i32 @memcpy(i8* %5, i8* %11, i32 8)
br label %16
13: ; preds = %2
%14 = inttoptr i64 %8 to i8*
%15 = call i32 @memcpy(i8* %5, i8* %14, i32 16)
br label %16
16: ; preds = %13, %10
%17 = getelementptr inbounds i8*, i8** %1, i64 1
%18 = load i8*, i8** %17
%19 = call i32* @fopen(i8* %18, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%20 = icmp eq i32* %19, null
br i1 %20, label %36, label %21
21: ; preds = %16
%22 = call i32 @writeu16(i32* %19, i32 256)
%23 = icmp eq i32 %22, 0
br i1 %23, label %36, label %24
24: ; preds = %21
%25 = call i32 @writestrlv(i32* %19, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @main.address, i64 0, i64 0))
%26 = icmp eq i32 %25, 0
br i1 %26, label %36, label %27
27: ; preds = %24
%28 = call i32 @writestrlv(i32* %19, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @main.number, i64 0, i64 0))
%29 = icmp eq i32 %28, 0
br i1 %29, label %36, label %30
30: ; preds = %27
%31 = call i32 @writestrlv(i32* %19, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @main.name, i64 0, i64 0))
%32 = icmp eq i32 %31, 0
br i1 %32, label %36, label %33
33: ; preds = %30
%34 = call i32 @writelv(i32* %19, i32 16, i8* %5)
%35 = icmp eq i32 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %33, %30, %27, %24, %21, %16
%37 = load i8*, i8** %1
%38 = call i32 @perror(i8* %37)
br label %39
39: ; preds = %33, %36
%40 = phi i32* [ @EXIT_FAILURE, %36 ], [ @EXIT_SUCCESS, %33 ]
%41 = load i32, i32* %40
ret i32 %41
}