#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 O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_RDWR;
int O_TRUNC;
int /*<<< orphan*/ abort();
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ link(char *, char *);
int open(char *, int, ...);
int read(int, char *, int);
scalar_t__ strncmp(char *, char *, int);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char **argv) {
  int a, b;
  char buf[5] = "";
  a = open("a", O_CREAT | O_TRUNC | O_RDWR, 0644);
  write(a, "HUHU", 4);
  link("a", "b");
  b = open("b", O_RDONLY);
  if (read(b, buf, 4) < 4)
    abort();
  if (strncmp(buf, "HUHU", 4))
    abort();
  close(b);
  close(a);
  return 0;
}


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)
lea    -0x1d(%rbp),%rdi
xor    %esi,%esi
mov    $0x5,%edx
call   1060 <memset@plt>
mov    0x2e4b(%rip),%rax
mov    (%rax),%esi
mov    0x2e3a(%rip),%rax
or     (%rax),%esi
mov    0x2e19(%rip),%rax
or     (%rax),%esi
lea    0xe38(%rip),%rdi
mov    $0x1a4,%edx
mov    $0x0,%al
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
lea    0xe21(%rip),%rsi
mov    $0x4,%edx
call   1050 <write@plt>
lea    0xe0e(%rip),%rdi
lea    0xe0e(%rip),%rsi
call   10a0 <link@plt>
mov    0x2dcb(%rip),%rax
mov    (%rax),%esi
lea    0xdf9(%rip),%rdi
mov    $0x0,%al
call   1090 <open@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%edi
lea    -0x1d(%rbp),%rsi
mov    $0x4,%edx
call   1080 <read@plt>
cmp    $0x4,%eax
jge    1239 <main+0xb9>
mov    $0x0,%al
call   1030 <abort@plt>
lea    -0x1d(%rbp),%rdi
lea    0xdbe(%rip),%rsi
mov    $0x4,%edx
call   1040 <strncmp@plt>
cmp    $0x0,%rax
je     125f <main+0xdf>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x18(%rbp),%edi
call   1070 <close@plt>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
movb   $0x0,0xc(%rsp)
mov    0x2e74(%rip),%rax
mov    0x2e65(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
movl   $0x0,0x8(%rsp)
mov    0x2e3a(%rip),%rax
or     (%rax),%esi
lea    0xe59(%rip),%r14
mov    %r14,%rdi
mov    $0x1a4,%edx
xor    %eax,%eax
call   1080 <open@plt>
mov    %eax,%ebx
lea    0xe43(%rip),%rsi
mov    %eax,%edi
mov    $0x4,%edx
call   1050 <write@plt>
lea    0xe35(%rip),%rbp
mov    %r14,%rdi
mov    %rbp,%rsi
call   1090 <link@plt>
mov    0x2dec(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
xor    %eax,%eax
call   1080 <open@plt>
mov    %eax,%ebp
lea    0x8(%rsp),%rsi
mov    %eax,%edi
mov    $0x4,%edx
call   1070 <read@plt>
cmp    $0x3,%eax
jle    123c <main+0xcc>
lea    0xdf3(%rip),%rsi
lea    0x8(%rsp),%rdi
mov    $0x4,%edx
call   1040 <strncmp@plt>
test   %rax,%rax
jne    123c <main+0xcc>
mov    %ebp,%edi
call   1060 <close@plt>
mov    %ebx,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
xor    %eax,%eax
call   1030 <abort@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"a\00"
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@O_RDWR = global i32 0
@.str.1 = [5 x i8] c"HUHU\00"
@.str.2 = [2 x i8] c"b\00"
@O_RDONLY = 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 [5 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = bitcast [5 x i8]* %8 to i8*
%10 = load i32, i32* @O_CREAT
%11 = load i32, i32* @O_TRUNC
%12 = or i32 %10, %11
%13 = load i32, i32* @O_RDWR
%14 = or i32 %12, %13
%15 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 %14, i32 420)
store i32 %15, i32* %6
%16 = load i32, i32* %6
%17 = call i32 @write(i32 %16, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 4)
%18 = call i32 @link(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%19 = load i32, i32* @O_RDONLY
%20 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 %19)
store i32 %20, i32* %7
%21 = load i32, i32* %7
%22 = getelementptr inbounds [5 x i8], [5 x i8]* %8, i64 0, i64 0
%23 = call i32 @read(i32 %21, i8* %22, i32 4)
%24 = icmp slt i32 %23, 4
br i1 %24, label %25, label %27
25: ; preds = %2
%26 = call i32 (...) @abort()
unreachable
27: ; preds = %2
%28 = getelementptr inbounds [5 x i8], [5 x i8]* %8, i64 0, i64 0
%29 = call i64 @strncmp(i8* %28, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 4)
%30 = icmp ne i64 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %27
%32 = call i32 (...) @abort()
unreachable
33: ; preds = %27
%34 = load i32, i32* %7
%35 = call i32 @close(i32 %34)
%36 = load i32, i32* %6
%37 = call i32 @close(i32 %36)
ret i32 0
}

LLVM IR (-O2):
@.str = [2 x i8] c"a\00"
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@O_RDWR = global i32 0
@.str.1 = [5 x i8] c"HUHU\00"
@.str.2 = [2 x i8] c"b\00"
@O_RDONLY = global i32 0
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [5 x i8]
%4 = getelementptr inbounds [5 x i8], [5 x i8]* %3, i64 0, i64 0
%5 = load i32, i32* @O_CREAT
%6 = load i32, i32* @O_TRUNC
%7 = or i32 %6, %5
%8 = load i32, i32* @O_RDWR
%9 = or i32 %7, %8
%10 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 %9, i32 420)
%11 = tail call i32 @write(i32 %10, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 4)
%12 = tail call i32 @link(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%13 = load i32, i32* @O_RDONLY
%14 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 %13)
%15 = call i32 @read(i32 %14, i8* %4, i32 4)
%16 = icmp slt i32 %15, 4
br i1 %16, label %17, label %19
17: ; preds = %2
%18 = tail call i32 (...) @abort()
unreachable
19: ; preds = %2
%20 = call i64 @strncmp(i8* %4, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 4)
%21 = icmp eq i64 %20, 0
br i1 %21, label %24, label %22
22: ; preds = %19
%23 = call i32 (...) @abort()
unreachable
24: ; preds = %19
%25 = call i32 @close(i32 %14)
%26 = call i32 @close(i32 %10)
ret i32 0
}