#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*/ STDOUT_FILENO;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ err_sys(char *);
int mydup2(int /*<<< orphan*/, int);
int strlen(char *);
int write(int, char *, int);
int main(int argc, char **argv) {
  int fd = 10;
  char buf[] = "Hello world\n";
  int n = strlen(buf);
  if ((fd = mydup2(STDOUT_FILENO, fd)) < 0) {
    err_sys("mydup2 error");
  }
  if (write(fd, buf, n) != n) {
    err_sys("write error");
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xa,-0x14(%rbp)
mov    0xe8c(%rip),%rax
mov    %rax,-0x21(%rbp)
mov    0xe8a(%rip),%eax
mov    %eax,-0x19(%rbp)
mov    0xe85(%rip),%al
mov    %al,-0x15(%rbp)
lea    -0x21(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x28(%rbp)
mov    0x2e53(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%esi
call   1070 <mydup2@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    11bf <main+0x6f>
lea    0xe53(%rip),%rdi
call   1030 <err_sys@plt>
mov    -0x14(%rbp),%edi
lea    -0x21(%rbp),%rsi
mov    -0x28(%rbp),%edx
call   1040 <write@plt>
cmp    -0x28(%rbp),%eax
je     11e3 <main+0x93>
lea    0xe3c(%rip),%rdi
call   1030 <err_sys@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
movabs $0xa646c726f7720,%rax
mov    %rax,0xd(%rsp)
movabs $0x6f77206f6c6c6548,%rax
20 77 6f
mov    %rax,0x8(%rsp)
lea    0x8(%rsp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ebp
mov    0x2e69(%rip),%rax
mov    (%rax),%edi
mov    $0xa,%esi
call   1070 <mydup2@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    11a5 <main+0x55>
lea    0xe6d(%rip),%rdi
call   1030 <err_sys@plt>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    %ebp,%edx
call   1040 <write@plt>
cmp    %ebp,%eax
je     11c3 <main+0x73>
lea    0xe5c(%rip),%rdi
call   1030 <err_sys@plt>
mov    %ebx,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@__const.main.buf = [13 x i8] c"Hello world\0A\00"
@STDOUT_FILENO = global i32 0
@.str = [13 x i8] c"mydup2 error\00"
@.str.1 = [12 x i8] c"write error\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca [13 x i8]
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 10, i32* %6
%9 = bitcast [13 x i8]* %7 to i8*
%10 = getelementptr inbounds [13 x i8], [13 x i8]* %7, i64 0, i64 0
%11 = call i32 @strlen(i8* %10)
store i32 %11, i32* %8
%12 = load i32, i32* @STDOUT_FILENO
%13 = load i32, i32* %6
%14 = call i32 @mydup2(i32 %12, i32 %13)
store i32 %14, i32* %6
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %2
%17 = call i32 @err_sys(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %18
18: ; preds = %16, %2
%19 = load i32, i32* %6
%20 = getelementptr inbounds [13 x i8], [13 x i8]* %7, i64 0, i64 0
%21 = load i32, i32* %8
%22 = call i32 @write(i32 %19, i8* %20, i32 %21)
%23 = load i32, i32* %8
%24 = icmp ne i32 %22, %23
br i1 %24, label %25, label %27
25: ; preds = %18
%26 = call i32 @err_sys(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %27
27: ; preds = %25, %18
%28 = load i32, i32* %6
%29 = call i32 @close(i32 %28)
ret i32 0
}

LLVM IR (-O2):
@__const.main.buf = [13 x i8] c"Hello world\0A\00"
@STDOUT_FILENO = global i32 0
@.str = [13 x i8] c"mydup2 error\00"
@.str.1 = [12 x i8] c"write error\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [13 x i8]
%4 = getelementptr inbounds [13 x i8], [13 x i8]* %3, i64 0, i64 0
%5 = call i32 @strlen(i8* %4)
%6 = load i32, i32* @STDOUT_FILENO
%7 = call i32 @mydup2(i32 %6, i32 10)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = call i32 @err_sys(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %9, %2
%12 = call i32 @write(i32 %7, i8* %4, i32 %5)
%13 = icmp eq i32 %12, %5
br i1 %13, label %16, label %14
14: ; preds = %11
%15 = call i32 @err_sys(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %16
16: ; preds = %14, %11
%17 = call i32 @close(i32 %7)
ret i32 0
}