#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 /*<<< orphan*/ AF_INET;
int O_CREAT;
int O_RDONLY;
int O_RDWR;
int O_WRONLY;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int open(char *, int);
int /*<<< orphan*/ printf(char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int main(int argc, char **argv) {
  int fd = -1;
  fd = open("my.test", O_CREAT | O_RDWR);
  printf("fd = %d\n", fd);
  if (fd >= 0)
    close(fd);
  close(1);
  fd = open("my.stdin", O_CREAT | O_WRONLY);
  fprintf(stdout, "out: fd = %d\n", fd);
  fprintf(stderr, "err: fd = %d\n", fd);
  if (fd >= 0)
    close(fd);
  fd = open("my.stdout", O_CREAT | O_RDONLY);
  fprintf(stdout, "out: fd = %d\n", fd);
  fprintf(stderr, "err: fd = %d\n", fd);
  if (fd >= 0)
    close(fd);
  fd = socket(AF_INET, SOCK_DGRAM, 0);
  fprintf(stderr, "socket: fd = %d\n", fd);
  if (fd >= 0)
    close(fd);
  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)
movl   $0xffffffff,-0x14(%rbp)
mov    0x2e84(%rip),%rax
mov    (%rax),%esi
mov    0x2e63(%rip),%rax
or     (%rax),%esi
lea    0xe7a(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%esi
lea    0xe8c(%rip),%rdi
call   1030 <printf@plt>
cmpl   $0x0,-0x14(%rbp)
jl     11af <main+0x5f>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    $0x1,%edi
call   1040 <close@plt>
mov    0x2e38(%rip),%rax
mov    (%rax),%esi
mov    0x2dd7(%rip),%rax
or     (%rax),%esi
lea    0xe36(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
mov    0x2dcf(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%edx
lea    0xe24(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2dbf(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%edx
lea    0xe1a(%rip),%rsi
call   1050 <fprintf@plt>
cmpl   $0x0,-0x14(%rbp)
jl     121c <main+0xcc>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    0x2dd5(%rip),%rax
mov    (%rax),%esi
mov    0x2da4(%rip),%rax
or     (%rax),%esi
lea    0xdf8(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
mov    0x2d6c(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%edx
lea    0xdc1(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2d5c(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%edx
lea    0xdb7(%rip),%rsi
call   1050 <fprintf@plt>
cmpl   $0x0,-0x14(%rbp)
jl     127f <main+0x12f>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    0x2d52(%rip),%rax
mov    (%rax),%edi
mov    0x2d39(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x14(%rbp)
mov    0x2d16(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%edx
lea    0xd89(%rip),%rsi
call   1050 <fprintf@plt>
cmpl   $0x0,-0x14(%rbp)
jl     12c5 <main+0x175>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    0x2e99(%rip),%r14
mov    0x2e7a(%rip),%rax
mov    (%rax),%esi
or     (%r14),%esi
lea    0xe8e(%rip),%rdi
call   1060 <open@plt>
mov    %eax,%ebx
lea    0xea4(%rip),%rdi
mov    %eax,%esi
call   1030 <printf@plt>
test   %ebx,%ebx
js     1192 <main+0x42>
mov    %ebx,%edi
call   1040 <close@plt>
mov    $0x1,%edi
call   1040 <close@plt>
mov    0x2dfd(%rip),%rax
mov    (%rax),%esi
or     (%r14),%esi
lea    0xe59(%rip),%rdi
call   1060 <open@plt>
mov    %eax,%ebx
mov    0x2df3(%rip),%r12
mov    (%r12),%edi
lea    0xe49(%rip),%rsi
mov    %eax,%edx
call   1050 <fprintf@plt>
mov    0x2de2(%rip),%r15
mov    (%r15),%edi
lea    0xe3f(%rip),%rsi
mov    %ebx,%edx
call   1050 <fprintf@plt>
test   %ebx,%ebx
js     11f2 <main+0xa2>
mov    %ebx,%edi
call   1040 <close@plt>
mov    0x2dd7(%rip),%rax
mov    (%rax),%esi
or     (%r14),%esi
lea    0xe28(%rip),%rdi
call   1060 <open@plt>
mov    %eax,%ebx
mov    (%r12),%edi
lea    0xdfa(%rip),%rsi
mov    %eax,%edx
call   1050 <fprintf@plt>
mov    (%r15),%edi
lea    0xdf7(%rip),%rsi
mov    %ebx,%edx
call   1050 <fprintf@plt>
test   %ebx,%ebx
js     123a <main+0xea>
mov    %ebx,%edi
call   1040 <close@plt>
mov    0x2d97(%rip),%rax
mov    (%rax),%edi
mov    0x2d7e(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,%ebx
mov    (%r15),%edi
lea    0xdd8(%rip),%rsi
mov    %eax,%edx
call   1050 <fprintf@plt>
test   %ebx,%ebx
js     1271 <main+0x121>
mov    %ebx,%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [8 x i8] c"my.test\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@.str.1 = [9 x i8] c"fd = %d\0A\00"
@.str.2 = [9 x i8] c"my.stdin\00"
@O_WRONLY = global i32 0
@stdout = global i32 0
@.str.3 = [14 x i8] c"out: fd = %d\0A\00"
@stderr = global i32 0
@.str.4 = [14 x i8] c"err: fd = %d\0A\00"
@.str.5 = [10 x i8] c"my.stdout\00"
@O_RDONLY = global i32 0
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str.6 = [17 x i8] c"socket: fd = %d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 -1, i32* %6
%7 = load i32, i32* @O_CREAT
%8 = load i32, i32* @O_RDWR
%9 = or i32 %7, %8
%10 = call i32 @open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %9)
store i32 %10, i32* %6
%11 = load i32, i32* %6
%12 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %11)
%13 = load i32, i32* %6
%14 = icmp sge i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %2
%16 = load i32, i32* %6
%17 = call i32 @close(i32 %16)
br label %18
18: ; preds = %15, %2
%19 = call i32 @close(i32 1)
%20 = load i32, i32* @O_CREAT
%21 = load i32, i32* @O_WRONLY
%22 = or i32 %20, %21
%23 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 %22)
store i32 %23, i32* %6
%24 = load i32, i32* @stdout
%25 = load i32, i32* %6
%26 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i32 %25)
%27 = load i32, i32* @stderr
%28 = load i32, i32* %6
%29 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i32 %28)
%30 = load i32, i32* %6
%31 = icmp sge i32 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %18
%33 = load i32, i32* %6
%34 = call i32 @close(i32 %33)
br label %35
35: ; preds = %32, %18
%36 = load i32, i32* @O_CREAT
%37 = load i32, i32* @O_RDONLY
%38 = or i32 %36, %37
%39 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i32 %38)
store i32 %39, i32* %6
%40 = load i32, i32* @stdout
%41 = load i32, i32* %6
%42 = call i32 @fprintf(i32 %40, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i32 %41)
%43 = load i32, i32* @stderr
%44 = load i32, i32* %6
%45 = call i32 @fprintf(i32 %43, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i32 %44)
%46 = load i32, i32* %6
%47 = icmp sge i32 %46, 0
br i1 %47, label %48, label %51
48: ; preds = %35
%49 = load i32, i32* %6
%50 = call i32 @close(i32 %49)
br label %51
51: ; preds = %48, %35
%52 = load i32, i32* @AF_INET
%53 = load i32, i32* @SOCK_DGRAM
%54 = call i32 @socket(i32 %52, i32 %53, i32 0)
store i32 %54, i32* %6
%55 = load i32, i32* @stderr
%56 = load i32, i32* %6
%57 = call i32 @fprintf(i32 %55, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.6, i64 0, i64 0), i32 %56)
%58 = load i32, i32* %6
%59 = icmp sge i32 %58, 0
br i1 %59, label %60, label %63
60: ; preds = %51
%61 = load i32, i32* %6
%62 = call i32 @close(i32 %61)
br label %63
63: ; preds = %60, %51
ret i32 0
}

LLVM IR (-O2):
@.str = [8 x i8] c"my.test\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@.str.1 = [9 x i8] c"fd = %d\0A\00"
@.str.2 = [9 x i8] c"my.stdin\00"
@O_WRONLY = global i32 0
@stdout = global i32 0
@.str.3 = [14 x i8] c"out: fd = %d\0A\00"
@stderr = global i32 0
@.str.4 = [14 x i8] c"err: fd = %d\0A\00"
@.str.5 = [10 x i8] c"my.stdout\00"
@O_RDONLY = global i32 0
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str.6 = [17 x i8] c"socket: fd = %d\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = load i32, i32* @O_CREAT
%4 = load i32, i32* @O_RDWR
%5 = or i32 %4, %3
%6 = tail call i32 @open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = tail call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %6)
%8 = icmp sgt i32 %6, -1
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = tail call i32 @close(i32 %6)
br label %11
11: ; preds = %9, %2
%12 = tail call i32 @close(i32 1)
%13 = load i32, i32* @O_CREAT
%14 = load i32, i32* @O_WRONLY
%15 = or i32 %14, %13
%16 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 %15)
%17 = load i32, i32* @stdout
%18 = tail call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i32 %16)
%19 = load i32, i32* @stderr
%20 = tail call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i32 %16)
%21 = icmp sgt i32 %16, -1
br i1 %21, label %22, label %24
22: ; preds = %11
%23 = tail call i32 @close(i32 %16)
br label %24
24: ; preds = %22, %11
%25 = load i32, i32* @O_CREAT
%26 = load i32, i32* @O_RDONLY
%27 = or i32 %26, %25
%28 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i32 %27)
%29 = load i32, i32* @stdout
%30 = tail call i32 @fprintf(i32 %29, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i32 %28)
%31 = load i32, i32* @stderr
%32 = tail call i32 @fprintf(i32 %31, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i32 %28)
%33 = icmp sgt i32 %28, -1
br i1 %33, label %34, label %36
34: ; preds = %24
%35 = tail call i32 @close(i32 %28)
br label %36
36: ; preds = %34, %24
%37 = load i32, i32* @AF_INET
%38 = load i32, i32* @SOCK_DGRAM
%39 = tail call i32 @socket(i32 %37, i32 %38, i32 0)
%40 = load i32, i32* @stderr
%41 = tail call i32 @fprintf(i32 %40, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.6, i64 0, i64 0), i32 %39)
%42 = icmp sgt i32 %39, -1
br i1 %42, label %43, label %45
43: ; preds = %36
%44 = tail call i32 @close(i32 %39)
br label %45
45: ; preds = %43, %36
ret i32 0
}