#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 /*<<< orphan*/ FILE;

/* Variables and functions */
int errno;
int fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char *, char *);
int /*<<< orphan*/ sprintf(char *, char *, ...);
int strlen(char *);
int /*<<< orphan*/ write(int, char *, int);
int utp_partition_mmc(char *disk) {
  char fc[50];
  int i;
  char shell_cmd[256];
  int fdisk;
  FILE *fdisk_f;
  sprintf(shell_cmd, "fdisk %s", disk);
  fdisk_f = popen(shell_cmd, "w");
  if (fdisk_f < 0)
    return errno;
  fdisk = fileno(fdisk_f);
  for (i = 4; i >= 1; i--) {
    sprintf(fc, "d\n%d\n", i);
    write(fdisk, fc, strlen(fc));
  }
  sprintf(fc, "n\np\n1\n1\n+16M\n");
  write(fdisk, fc, strlen(fc));
  sprintf(fc, "n\np\n2\n\n\n");
  write(fdisk, fc, strlen(fc));
  sprintf(fc, "t\n1\n0x%X\n\n", 0x53);
  write(fdisk, fc, strlen(fc));
  write(fdisk, "w\nq\n", 2);
  pclose(fdisk_f);
  return 0;
}


x86 assembly (clang, -O0):
<utp_partition_mmc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1b0,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x160(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xe7f(%rip),%rsi
mov    $0x0,%al
call   1080 <sprintf@plt>
lea    -0x160(%rbp),%rdi
lea    0xe73(%rip),%rsi
call   1070 <popen@plt>
mov    %rax,-0x170(%rbp)
cmpq   $0x0,-0x170(%rbp)
jae    11c1 <utp_partition_mmc+0x61>
mov    0x2e29(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    133c <utp_partition_mmc+0x1dc>
mov    -0x170(%rbp),%rdi
call   1060 <fileno@plt>
mov    %eax,-0x164(%rbp)
movl   $0x4,-0x54(%rbp)
cmpl   $0x1,-0x54(%rbp)
jl     123b <utp_partition_mmc+0xdb>
lea    -0x50(%rbp),%rdi
mov    -0x54(%rbp),%edx
lea    0xe19(%rip),%rsi
mov    $0x0,%al
call   1080 <sprintf@plt>
mov    -0x164(%rbp),%eax
mov    %eax,-0x17c(%rbp)
lea    -0x50(%rbp),%rax
mov    %rax,-0x178(%rbp)
lea    -0x50(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x17c(%rbp),%edi
mov    -0x178(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    -0x54(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x54(%rbp)
jmp    11da <utp_partition_mmc+0x7a>
lea    -0x50(%rbp),%rdi
lea    0xdcb(%rip),%rsi
mov    $0x0,%al
call   1080 <sprintf@plt>
mov    -0x164(%rbp),%eax
mov    %eax,-0x1ac(%rbp)
lea    -0x50(%rbp),%rax
mov    %rax,-0x1a8(%rbp)
lea    -0x50(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x1ac(%rbp),%edi
mov    -0x1a8(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
lea    -0x50(%rbp),%rdi
lea    0xd93(%rip),%rsi
mov    $0x0,%al
call   1080 <sprintf@plt>
mov    -0x164(%rbp),%eax
mov    %eax,-0x19c(%rbp)
lea    -0x50(%rbp),%rax
mov    %rax,-0x198(%rbp)
lea    -0x50(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x19c(%rbp),%edi
mov    -0x198(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
lea    -0x50(%rbp),%rdi
lea    0xd56(%rip),%rsi
mov    $0x53,%edx
mov    $0x0,%al
call   1080 <sprintf@plt>
mov    -0x164(%rbp),%eax
mov    %eax,-0x18c(%rbp)
lea    -0x50(%rbp),%rax
mov    %rax,-0x188(%rbp)
lea    -0x50(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x18c(%rbp),%edi
mov    -0x188(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    -0x164(%rbp),%edi
lea    0xd14(%rip),%rsi
mov    $0x2,%edx
call   1030 <write@plt>
mov    -0x170(%rbp),%rdi
call   1050 <pclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1b0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<utp_partition_mmc>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x148,%rsp
mov    %rdi,%rdx
lea    0xe89(%rip),%rsi
lea    0x40(%rsp),%rbx
mov    %rbx,%rdi
xor    %eax,%eax
call   1080 <sprintf@plt>
lea    0xe7c(%rip),%rsi
mov    %rbx,%rdi
call   1070 <popen@plt>
mov    %rax,%r14
mov    %rax,%rdi
call   1060 <fileno@plt>
mov    %eax,%ebp
lea    0xe62(%rip),%r15
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    %r15,%rsi
mov    $0x4,%edx
xor    %eax,%eax
call   1080 <sprintf@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    $0x3,%edx
xor    %eax,%eax
call   1080 <sprintf@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    $0x2,%edx
xor    %eax,%eax
call   1080 <sprintf@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    $0x1,%edx
xor    %eax,%eax
call   1080 <sprintf@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
movabs $0xa310a310a700a6e,%rax
0a 31 0a
mov    %rax,(%rsp)
movabs $0xa4d36312b0a31,%rax
mov    %rax,0x6(%rsp)
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
movabs $0xa0a0a320a700a6e,%rax
0a 0a 0a
mov    %rax,(%rsp)
movb   $0x0,0x8(%rsp)
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
lea    0xd85(%rip),%rsi
mov    %rbx,%rdi
mov    $0x53,%edx
xor    %eax,%eax
call   1080 <sprintf@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
lea    0xd66(%rip),%rsi
mov    %ebp,%edi
mov    $0x2,%edx
call   1030 <write@plt>
mov    %r14,%rdi
call   1050 <pclose@plt>
xor    %eax,%eax
add    $0x148,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [9 x i8] c"fdisk %s\00"
@.str.1 = [2 x i8] c"w\00"
@errno = global i32 0
@.str.2 = [6 x i8] c"d\0A%d\0A\00"
@.str.3 = [14 x i8] c"n\0Ap\0A1\0A1\0A+16M\0A\00"
@.str.4 = [9 x i8] c"n\0Ap\0A2\0A\0A\0A\00"
@.str.5 = [11 x i8] c"t\0A1\0A0x%X\0A\0A\00"
@.str.6 = [5 x i8] c"w\0Aq\0A\00"
define i32 @utp_partition_mmc(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca [50 x i8]
%5 = alloca i32
%6 = alloca [256 x i8]
%7 = alloca i32
%8 = alloca i32*
store i8* %0, i8** %3
%9 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%10 = load i8*, i8** %3
%11 = call i32 (i8*, i8*, ...) @sprintf(i8* %9, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* %10)
%12 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%13 = call i32* @popen(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %13, i32** %8
%14 = load i32*, i32** %8
%15 = icmp ult i32* %14, null
br i1 %15, label %16, label %18
16: ; preds = %1
%17 = load i32, i32* @errno
store i32 %17, i32* %2
br label %62
18: ; preds = %1
%19 = load i32*, i32** %8
%20 = call i32 @fileno(i32* %19)
store i32 %20, i32* %7
store i32 4, i32* %5
br label %21
21: ; preds = %33, %18
%22 = load i32, i32* %5
%23 = icmp sge i32 %22, 1
br i1 %23, label %24, label %36
24: ; preds = %21
%25 = getelementptr inbounds [50 x i8], [50 x i8]* %4, i64 0, i64 0
%26 = load i32, i32* %5
%27 = call i32 (i8*, i8*, ...) @sprintf(i8* %25, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %26)
%28 = load i32, i32* %7
%29 = getelementptr inbounds [50 x i8], [50 x i8]* %4, i64 0, i64 0
%30 = getelementptr inbounds [50 x i8], [50 x i8]* %4, i64 0, i64 0
%31 = call i32 @strlen(i8* %30)
%32 = call i32 @write(i32 %28, i8* %29, i32 %31)
br label %33
33: ; preds = %24
%34 = load i32, i32* %5
%35 = add nsw i32 %34, -1
store i32 %35, i32* %5
br label %21
36: ; preds = %21
%37 = getelementptr inbounds [50 x i8], [50 x i8]* %4, i64 0, i64 0
%38 = call i32 (i8*, i8*, ...) @sprintf(i8* %37, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
%39 = load i32, i32* %7
%40 = getelementptr inbounds [50 x i8], [50 x i8]* %4, i64 0, i64 0
%41 = getelementptr inbounds [50 x i8], [50 x i8]* %4, i64 0, i64 0
%42 = call i32 @strlen(i8* %41)
%43 = call i32 @write(i32 %39, i8* %40, i32 %42)
%44 = getelementptr inbounds [50 x i8], [50 x i8]* %4, i64 0, i64 0
%45 = call i32 (i8*, i8*, ...) @sprintf(i8* %44, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0))
%46 = load i32, i32* %7
%47 = getelementptr inbounds [50 x i8], [50 x i8]* %4, i64 0, i64 0
%48 = getelementptr inbounds [50 x i8], [50 x i8]* %4, i64 0, i64 0
%49 = call i32 @strlen(i8* %48)
%50 = call i32 @write(i32 %46, i8* %47, i32 %49)
%51 = getelementptr inbounds [50 x i8], [50 x i8]* %4, i64 0, i64 0
%52 = call i32 (i8*, i8*, ...) @sprintf(i8* %51, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0), i32 83)
%53 = load i32, i32* %7
%54 = getelementptr inbounds [50 x i8], [50 x i8]* %4, i64 0, i64 0
%55 = getelementptr inbounds [50 x i8], [50 x i8]* %4, i64 0, i64 0
%56 = call i32 @strlen(i8* %55)
%57 = call i32 @write(i32 %53, i8* %54, i32 %56)
%58 = load i32, i32* %7
%59 = call i32 @write(i32 %58, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0), i32 2)
%60 = load i32*, i32** %8
%61 = call i32 @pclose(i32* %60)
store i32 0, i32* %2
br label %62
62: ; preds = %36, %16
%63 = load i32, i32* %2
ret i32 %63
}

LLVM IR (-O2):
@.str = [9 x i8] c"fdisk %s\00"
@.str.1 = [2 x i8] c"w\00"
@errno = global i32 0
@.str.2 = [6 x i8] c"d\0A%d\0A\00"
@.str.3 = [14 x i8] c"n\0Ap\0A1\0A1\0A+16M\0A\00"
@.str.4 = [9 x i8] c"n\0Ap\0A2\0A\0A\0A\00"
@.str.5 = [11 x i8] c"t\0A1\0A0x%X\0A\0A\00"
@.str.6 = [5 x i8] c"w\0Aq\0A\00"
define i32 @utp_partition_mmc(i8* %0) {
%2 = alloca [50 x i8]
%3 = alloca [256 x i8]
%4 = getelementptr inbounds [50 x i8], [50 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%6 = call i32 (i8*, i8*, ...) @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* %0)
%7 = call i32* @popen(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%8 = tail call i32 @fileno(i32* %7)
%9 = call i32 (i8*, i8*, ...) @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 4)
%10 = call i32 @strlen(i8* %4)
%11 = call i32 @write(i32 %8, i8* %4, i32 %10)
%12 = call i32 (i8*, i8*, ...) @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 3)
%13 = call i32 @strlen(i8* %4)
%14 = call i32 @write(i32 %8, i8* %4, i32 %13)
%15 = call i32 (i8*, i8*, ...) @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 2)
%16 = call i32 @strlen(i8* %4)
%17 = call i32 @write(i32 %8, i8* %4, i32 %16)
%18 = call i32 (i8*, i8*, ...) @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 1)
%19 = call i32 @strlen(i8* %4)
%20 = call i32 @write(i32 %8, i8* %4, i32 %19)
%21 = call i32 @strlen(i8* %4)
%22 = call i32 @write(i32 %8, i8* %4, i32 %21)
%23 = call i32 @strlen(i8* %4)
%24 = call i32 @write(i32 %8, i8* %4, i32 %23)
%25 = call i32 (i8*, i8*, ...) @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0), i32 83)
%26 = call i32 @strlen(i8* %4)
%27 = call i32 @write(i32 %8, i8* %4, i32 %26)
%28 = call i32 @write(i32 %8, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0), i32 2)
%29 = call i32 @pclose(i32* %7)
ret i32 0
}